In [20]: x = np.array([1, -2, 0, -2, 5, 0, 0, 0, 2]).reshape(3,-1)
In [21]: x
Out[21]:
array([[ 1, -2, 0],
[-2, 5, 0],
[ 0, 0, 2]])
x is symmetric, but the cumsum is not.
In [22]: np.cumsum(x,axis=1)
Out[22]:
array([[ 1, -1, -1],
[-2, 3, 3],
[ 0, 0, 2]], dtype=int32)
The first column is the same as in x. The 2nd is x[:,0]+x[:,1], etc.
Change the axis, and the result is just the transpose.
In [23]: np.cumsum(x,axis=0)
Out[23]:
array([[ 1, -2, 0], # x[0,:]
[-1, 3, 0], # x[0,:]+x[1,:]
[-1, 3, 2]], dtype=int32)
We can iterate across the columns with:
In [25]: res = np.zeros((3,3),int)
...: res[:,0] = x[:,0]
...: for i in range(1,3):
...: res[:,i] = res[:,i-1] + x[:,i]
...:
In [26]: res
Out[26]:
array([[ 1, -1, -1],
[-2, 3, 3],
[ 0, 0, 2]])
Your iteration is has 0 in the first column, not x[:,0]:
In [27]: res = np.zeros((3,3),int)
...: for i in range(0,3):
...: res[:,i] = x[:,:i].sum(axis=1)
...:
In [28]: res
Out[28]:
array([[ 0, 1, -1],
[ 0, -2, 3],
[ 0, 0, 0]])
That's because the :i does not include i.
In [29]: res = np.zeros((3,3),int)
...: for i in range(0,3):
...: res[:,i] = x[:,:i+1].sum(axis=1)
...:
In [30]: res
Out[30]:
array([[ 1, -1, -1],
[-2, 3, 3],
[ 0, 0, 2]])