How to take nth order discrete sum from numpy array (total numpy.diff equivalent)
I know it is possible to take the nth order discrete difference of a numpy array using the numpy function numpy.diff()
, but is there a way to do the same with nth order discrete sum?
Let's say we have an array of numpy, A = np.arange(10)
. Expected output for 1st order discrete sum:
array([ 1., 3., 5., 7., 9., 11., 13., 15., 17.])
which i can get:
N = A.shape[0] B = np.zeros(N-1) for i in range(N-1): B[i] = A[i+1] + A[i]
But is there a function to avoid using a for loop?
A[i+1]
for for i in range(N-1)
will be covered in a A[1:]
similar way A[i]
for the same iterative facility A[:-1]
. So, in principle, you can sum these two versions of the vectorized input array in B
, for example:
B = A[:-1] + A[1:]