fast sum for matrices too

This commit is contained in:
Adrian Mariano 2021-03-13 20:33:03 -05:00
parent 5faecd6e79
commit 9f249d9cf3

View file

@ -585,7 +585,7 @@ function lcm(a,b=[]) =
function sum(v, dflt=0) =
v==[]? dflt :
assert(is_consistent(v), "Input to sum is non-numeric or inconsistent")
is_vector(v) ? [for(i=[1:len(v)]) 1]*v :
is_vector(v) || is_matrix(v) ? [for(i=[1:len(v)]) 1]*v :
_sum(v,v[0]*0);
function _sum(v,_total,_i=0) = _i>=len(v) ? _total : _sum(v,_total+v[_i], _i+1);