Say I have an array
data = np.arange(6)
I want to find the sum of the entire array and the second half using np.add.reduceat
.1
If I do it like this:
np.add.reduceat(data, [0, 6, 3])[::2]
I immediately get an error
IndexError: index 6 out-of-bounds in add.reduceat [0, 6)
If I do it like this
np.add.reduceat(data, [0, 5, 3])[::2]
I get the wrong answer (10 should be 15):
array([10, 12])
The only solution I have been able to come up with is to mask the locations where the last index is necessary, subtract 1 from them, and then add the last element back in there:
index = np.array([0, 6, 3])
mask = (index == data.size)
index[mask] -= 1
result = np.add.reduceat(data, index)
# Mask is shifted back by one because it's the previous element that needs to be updated
result[:-1][mask[1:]] += data[-1]
Then result[::2]
gives the desired answer. This looks like a giant kludge for something that I would expect to be an elegant one-liner (and faster than this).
1 I am fully aware that there are better ways to do this. This is just a contrived example for purposes of illustration. The real problem for this question originated with an attempt to solve numpy: fast regularly-spaced average for large numbers of line segments / points.