calca

Open full view…

Cumulative Sums

jare
Sat, 06 May 2017 18:23:17 GMT

I would like to do a cumulative sum of a vector, eg, `cumsum([1,2,3]) => [1,3,6]`. Is there a way to do this with the predefined functions?

davip
Sat, 05 Aug 2017 19:31:38 GMT

v=[1, 2, 3] f(n)=sum(x,0..n) cumsum(x)=map(f,x) cumsum(v)=> [1, 3, 6]

davip
Tue, 15 Aug 2017 11:22:29 GMT

Sorry, that answer wasn't correct. Here's a better solution: v=[3, 5, 5, 0, 1] count(v)=reduce(x+1, v, 0) g(y,w)= if y==0 then w[0] else sum(x,w[0, 0:y]) cumsum= map( g(w=v), 0..(count(v)-1) ) => [3, 8, 13, 13, 14]