summaryrefslogtreecommitdiff
path: root/modules/compatibility_functions/macros/%b_sum.sci
blob: 8fb93bf7c71143e26ea32b86effce201c32493c4 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) ???? - INRIA - Scilab
//
// 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 a=%b_sum(varargin)
    a = varargin(1)
    dims = size(a);

    nargs = size(varargin);
    select nargs
    case 1
        d = "*";
        typ = "double";
    case 2
        if or(varargin(2) == ["native", "double"]) then
            d = "*";
            typ = varargin(2);
        else
            d = varargin(2);
            typ = "double";
        end
    case 3
        d = varargin(2);
        typ = varargin(3);
    else
        error(msprintf(_("%s: Wrong number of input argument(s): %d to %d expected.\n"),"sum", 1, 3));
    end

    // Check second argument : d
    select type(d)
    case 1
        if size(d,'*') <> 1 then
            error(msprintf(_("%s: Wrong size for input argument #%d: A scalar expected.\n"),"sum", 2))
        end
        if int(d) <> d | d < 0 then
            error(msprintf(_("%s: Wrong value for input argument #%d: Integer >= %d expected.\n"),"sum", 2, 1))
        end
    case 10 
        if size(d,'*') <> 1 then
            error(msprintf(_("%s: Wrong size for input argument #%d: A string expected.\n"),"sum",2))
        end
        if and(d<>["r","c","*","m"]) then
            error(msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
            "sum",2,"""*"",""r"",""c"",""m"""))
        end
    else
        error(msprintf(_("%s: Wrong type for input argument #%d: A string or scalar expected.\n"),"sum",2))
    end

    // Check third argument
    if type(typ)<>10 then
        error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"sum",3))
    end

    if size(typ,"*")<>1 then
        error(msprintf(_("%s: Wrong size for input argument #%d: A string expected.\n"),"sum",3))
    end

    if and(typ <> ["native" "double"]) then
        error(msprintf(_("%s: Wrong value for input argument #%d: ""%s"" or ""%s"" expected.\n"),"sum", 3, "native", "double"));
    end

    if typ == "native" then
        if d <> "*" & type(d) == 10 then
            pos = [1,2,find(dims>1,1)];
            d=pos(find(d==["r","c","m"]))
        end
        // Case "m"
        if type(d) == 1 & d > 2 then
            a = a;
        else
            a = or(a, d)
        end
    else
        a = sum(double(a), d)
    end
    
//    if varargin($)=="native" then
//        if size(varargin)==3 & type(varargin(2))==1 & varargin(2)>2 then
//            r=varargin(1)
//        else
//            // call sum(a, d) with d = "native"
//            // or call(a, d, typ) with typ = "native"
//            if size(varargin) == 3 then
//                d = varargin(2);
//                // d must be a string or scalar -> check type
//                if and(type(d)<> [1, 10]) then
//                    error(msprintf(_("%s: Wrong type for input argument #%d: A string or scalar expected.\n"),"sum",2))
//                end
//
//                if size(d,"*")<>1 then
//                    if type(d)==10 then
//                        error(msprintf(_("%s: Wrong size for input argument #%d: A string expected.\n"),"sum",2))
//                    else
//                        error(msprintf(_("%s: Wrong size for input argument #%d: A scalar expected.\n"),"sum",2))
//                    end
//                end
//
//                // If d is a string, d = "m", "c", "r" or "*"
//                // Else d is an integer > 0
//                if type(d)==10 then
//                    d=find(d==["m","*","r","c"])
//                    if d==[] then
//                        error(msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
//                        "sum",2,"""*"",""r"",""c"",""m"",1:"+string(ndims(varargin(1)))))
//                    end
//                    d=d-2
//                else
//                    if d<0 then
//                        error(msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
//                        "cumprod",2,"""*"",""r"",""c"",""m"",1:"+string(ndims(varargin(1)))))
//                    end
//                end
//
//                dims=size(varargin(1));
//
//                if d==-1 then //'m'
//                    d=find(dims>1,1)
//                    if d==[] then d=0,end
//                end
//                
//                // If d = 0, error in or function
//                if d == 0 then
//                    d = "*"
//                end
//                
//                varargin(2) = d;
//            end
//            r=or(varargin(1:$-1))
//        end
//    else
//        // converts boolean to double
//        varargin(1)=bool2s(varargin(1))
//
//        // call sum(a, d, typ)
//        if argn(2) == 3 then
//            // d must be a string or a scalar -> check type and size
//            d = varargin(2);
//            if and(type(d)<> [1, 10]) then
//                error(msprintf(_("%s: Wrong type for input argument #%d: A string or scalar expected.\n"),"sum",2))
//            end
//
//            if size(d,"*")<>1 then
//                if type(d)==10 then
//                    error(msprintf(_("%s: Wrong size for input argument #%d: A string expected.\n"),"sum",2))
//                else
//                    error(msprintf(_("%s: Wrong size for input argument #%d: A scalar expected.\n"),"sum",2))
//                end
//            end
//
//            // If d is a string, d = "m", "*", "r", "c"
//            // Else  d is an integer > 0
//            if type(d)==10 then
//                if and(d <> ["m","*","r","c"]) then
//                    error(msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
//                    "sum",2,"""*"",""r"",""c"",""m"",1:"+string(ndims(varargin(1)))))
//                end
//            else
//                if d < 0 then
//                    error(msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
//                    "sum",2,"""*"",""r"",""c"",""m"",1:"+string(ndims(varargin(1)))))
//                end
//            end
//
//            // typ must be a string and typ="double"
//            typ = varargin($)
//            if type(typ)<>10 then
//                error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"sum",3))
//            end
//
//            if size(typ,"*")<>1 then
//                error(msprintf(_("%s: Wrong size for input argument #%d: A string expected.\n"),"sum",3))
//            end
//
//            if typ <> "double" then
//                error(msprintf(_("%s: Wrong value for input argument #%d: ""%s"" or ""%s"" expected.\n"),"sum", 3, "native", "double"));
//            end
//            varargin($) = null()
//        end
//        r = sum(varargin(:))
//    end
endfunction