Say I have such an array
>>> a = np.arange(1,8).reshape((1,-1)) >>> a array([[1, 2, 3, 4, 5, 6, 7]])
and I want to create for each of the elements in a "cumsum of next 4 items". That is my expected result
1, 2, 3, 4, 5, 6, 7, 8 1+2, 2+3, ... 1+2+3 2+3+4 ... 1+2+3+4 2+3+4+5 ...
i.e. matrix containing
1, 2, 3, 4, 5, 0, 0, 0 3, 5, 7, 9, 11,0, 0, 0 6, 9, 12,15,18,0, 0, 0 10,14,18,21,26,0, 0, 0
Since the cumsum operation cannot be performed correctly for the last 3 elements, I expect there to be 0 . I know how to make one kumma. In fact, arrays
a[:4].cumsum().reshape((-1,1)); a[1:5].cumsum().reshape((-1,1))...
arranged horizontally. However, I do not know how to do this in an effective way. What would be a beautiful vectorized numpy way of doing this? I am also open to scipy packages if they dominate numpy in terms of efficiency or readability.