python - Cumulative-Sum-to-the-End for each element in the array -


suppose have array a = array([a0, a1,..., an]). every element in array, need find cumulative sums in direction left right. thus, n=2 have find

a0, a0+a1, a0+a1+a2, a1, a1+a2, a2.

the obvious approach following:

list_of_sums = [np.cumsum(a[i:]) in xrange(n+1)] 

i wonder if there exists more efficient way this. issue n may quite large , in addition want broadcast many different arrays a of same length. unfortunately, did not succeed create vectorized function this.

another problem how store these sums. instead of using list, can represent data 2d array a, in first column a[:,0] have sums a0 first term, in second column a[:,1] have sums a1 first term, , on. gives (n+1)x(n+1) matrix, half elements of zeros (the right lower triangle). on other hand, requires in 2 times more memory list use, or force use sparse matrices may overkill.


Comments