1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution. The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10 function a = %hm_sum(varargin)
15 nargs = size(varargin);
25 if or(varargin(2) == ["native", "double"]) then
40 error(msprintf(_("%s: Wrong number of input argument(s): %d to %d expected.\n"),"sum", 1, 3));
43 // Check second argument : d
46 if size(d,"*") <> 1 then
47 error(msprintf(_("%s: Wrong size for input argument #%d: A scalar expected.\n"),"sum", 2))
49 if int(d) <> d | d < 0 then
50 error(msprintf(_("%s: Wrong value for input argument #%d: Integer >= %d expected.\n"),"sum", 2, 1))
53 if size(d,"*") <> 1 then
54 error(msprintf(_("%s: Wrong size for input argument #%d: A string expected.\n"),"sum",2))
56 if and(d<>["r","c","*","m"]) then
57 error(msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
58 "sum",2,"""*"",""r"",""c"",""m"""))
60 pos=[1,2,0,find(dims>1,1)];
61 d=pos(find(d==["r","c","*","m"]))
63 error(msprintf(_("%s: Wrong type for input argument #%d: A string or scalar expected.\n"),"sum",2))
66 // Check third argument
68 error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"sum",3))
71 if size(typ,"*")<>1 then
72 error(msprintf(_("%s: Wrong size for input argument #%d: A string expected.\n"),"sum",3))
75 if and(typ <> ["native" "double"]) then
76 error(msprintf(_("%s: Wrong value for input argument #%d: ""%s"" or ""%s"" expected.\n"),"sum", 3, "native", "double"));
80 a = sum(a.entries, "*", typ), dims;
81 elseif d > size(dims,"*") then
82 //requested summation direction exceeds array dims, return the array, converted
83 //to double if necessary.
84 if typ == "double" & or(tm == [4 8]) then
85 a.entries = double(a.entries),
89 //permute the array dimension to put the selected dimension first
92 a = matrix(permute(a,p),dims(d),-1)
95 if d==size(dims, "*") then
102 a = permute(matrix(a,dims(p)),p)