summaryrefslogtreecommitdiff
path: root/modules/optimization/macros/pack.sci
blob: 3e17d02a60b78a41af90abf25bca6829b2dc2046 (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
// 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 [M, sel] = pack(M, blocksizes)
    //
    // Check input arguments
    [lhs, rhs] = argn();
    if rhs <> 2 then
        error(msprintf(gettext("%s: Wrong number of input argument: %d expected.\n"),"pack",2));
    end
    //
    // Check type
    if and(typeof(M) <> ["constant" "sparse"]) then
        error(msprintf(gettext("%s: Wrong type for argument #%d: Real matrix or sparse matrix expected.\n"),"pack",1));
    end
    if typeof(blocksizes) <> "constant" then
        error(msprintf(gettext("%s: Wrong type for argument #%d: Real matrix expected.\n"),"pack",2));
    end
    //
    // Check content
    if ~isreal(M) then
        error(msprintf(gettext("%s: Wrong type for argument #%d: Real matrix expected.\n"),"pack",1));
    end
    if ~isreal(blocksizes) then
        error(msprintf(gettext("%s: Wrong type for argument #%d: Real matrix expected.\n"),"pack",2));
    end
    if or(blocksizes<1) then
        error(msprintf(gettext("%s: Wrong value for input argument #%d: Non-negative integers expected.\n"),"pack",2));
    end
    if or(blocksizes<>floor(blocksizes)) then
        error(msprintf(gettext("%s: Wrong value for input argument #%d: An integer value expected.\n"),"pack",2));
    end
    //
    // Proceed...
    sel = []
    kk = 0
    blocksizes = matrix(blocksizes, 1, size(blocksizes,"*"))
    for ni=blocksizes
        k = kk
        for j=1:ni
            sel = [sel, k+(j:ni)]
            k = k+ni
        end
        kk = kk+ni*ni
    end
    M = M(sel, :)

endfunction