[Numpy-discussion] Want cumsum-like function
Friedrich Romstedt
friedrichromstedt@gmail....
Thu Feb 25 01:48:49 CST 2010
2010/2/25 Peter Shinners <pete@shinners.org>:
> I want a function that works like cumsum, but starts at zero, instead of
> starting with the first actual value.
>
> [...]
>
> tallies = np.cumsum(initial_array)
> np.subtract(tallies, tallies[0], tallies)
Also note that this wouln't work as the example result [0, 3, 6, 7] (=
[4, 7, 10, 11] - 4) with initial_array = [4, 3, 3, 1] is different
from [0, 4, 7, 10].
Note that you want always leave out the last term in the sum result[k]
= \sum_{i = 0}^{k - 1} initial[i], thus the following expression
should work:
tallies = np.cumsum(initial_array) - initial_array.
Indeed, for initial_array = [4, 3, 3, 1], np.cumsum() = [4, 7, 10,
11], np.cumsum() - initial_array = [4, 7, 10, 11] - [4, 3, 3, 1] = [0,
4, 7, 10] as intended.
Friedrich
More information about the NumPy-Discussion
mailing list