I have a fairly large rectangular numpy array, of shape (m, n), for example:
>>> a.shape
(27584, 34092)
I have to compute the sum of each anti-diagonal of the array. This new array will have a shape of (m + n - 1,).
The simplistic approach is to do:
m, n = a.shape
r = np.zeros(m + n - 1)
for i in range(m):
for j in range(n):
r[i + j] += a[i][j]
# r is the sum of all anti-diagonals of a
This is obviously very slow, is there any way to perform the calculation using a clever numpy primitive? My only other option would be to code this in C++, which is also doable too - but requires more work.