summaryrefslogtreecommitdiff
path: root/modules/optimization/macros/unpack.sci
blob: 7e86e0bd23756f8b7128aaab85e1f3131ad4c4a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) INRIA
// Copyright (C) 2011 - DIGITEO - Michael Baudin
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
//
function MM=unpack(M,blocksizes)
    //
    // Check input arguments
    [lhs,rhs]=argn();
    if rhs<>2 then
        error(msprintf(gettext("%s: Wrong number of input argument: %d expected.\n"),"unpack",2));
    end
    //
    // Check type
    if typeof(M) <> "constant" then
        error(msprintf(gettext("%s: Wrong type for argument #%d: Real matrix expected.\n"),"unpack",1));
    end
    if typeof(blocksizes) <> "constant" then
        error(msprintf(gettext("%s: Wrong type for argument #%d: Real matrix expected.\n"),"unpack",2));
    end
    //
    // Check content
    if ~isreal(M) then
        error(msprintf(gettext("%s: Wrong type for argument #%d: Real matrix expected.\n"),"unpack",1));
    end
    if ~isreal(blocksizes) then
        error(msprintf(gettext("%s: Wrong type for argument #%d: Real matrix expected.\n"),"unpack",2));
    end
    if or(blocksizes<1) then
        error(msprintf(gettext("%s: Wrong value for input argument #%d: Non-negative integers expected.\n"),"unpack",2));
    end
    if or(blocksizes<>floor(blocksizes)) then
        error(msprintf(gettext("%s: Wrong value for input argument #%d: An integer value expected.\n"),"unpack",2));
    end
    //
    // Proceed...
    MM=[]
    [mM,nM]=size(M)
    n=sum(blocksizes)
    for j=1:nM
        ptr=1
        Mu=[]
        for ni=blocksizes
            Mui=[]
            for l=1:ni
                Mui(l:ni,l)=M(ptr:ptr+ni-l,j)
                Mui(l,l:ni)=M(ptr:ptr+ni-l,j)'
                ptr=ptr+ni-l+1
            end
            Mu=[Mu;matrix(Mui,ni*ni,1)]
        end
        MM=[MM,Mu]
    end
endfunction