1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) Samuel GOUGEON - 2015 : http://bugzilla.scilab.org/13829
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution. The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11 function a = %hm_sum(varargin)
16 nargs = size(varargin);
26 if or(varargin(2) == ["native", "double"]) then
41 error(msprintf(_("%s: Wrong number of input argument(s): %d to %d expected.\n"),"sum", 1, 3));
44 // Check second argument : d
47 if size(d,"*") <> 1 then
48 error(msprintf(_("%s: Wrong size for input argument #%d: A scalar expected.\n"),"sum", 2))
50 if int(d) <> d | d < 0 then
51 error(msprintf(_("%s: Wrong value for input argument #%d: Integer >= %d expected.\n"),"sum", 2, 1))
54 if size(d,"*") <> 1 then
55 error(msprintf(_("%s: Wrong size for input argument #%d: A string expected.\n"),"sum",2))
57 if and(d<>["r","c","*","m"]) then
58 error(msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
59 "sum",2,"""*"",""r"",""c"",""m"""))
61 pos = [1,2,0,find(dims>1,1)];
62 d = pos(find(d==["r","c","*","m"]))
64 error(msprintf(_("%s: Wrong type for input argument #%d: A string or scalar expected.\n"),"sum",2))
67 // Check third argument
69 error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"sum",3))
72 if size(typ,"*")<>1 then
73 error(msprintf(_("%s: Wrong size for input argument #%d: A string expected.\n"),"sum",3))
76 if and(typ <> ["native" "double"]) then
77 error(msprintf(_("%s: Wrong value for input argument #%d: ""%s"" or ""%s"" expected.\n"),"sum", 3, "native", "double"));
81 a = sum(a.entries, "*", typ), dims;
82 elseif d > size(dims,"*") then
83 //requested summation direction exceeds array dims, return the array, converted
84 //to double if necessary.
85 if typ == "double" & or(tm == [4 8]) then
86 a.entries=double(a.entries),
90 //permute the array dimension to put the selected dimension first
94 a = matrix(a,dims(d),-1)
97 // Reshaping according to sizes of other dimensions
99 dims([1,d]) = [1 dims(1)]
104 // back-permuting switched dimensions