[Numpy-discussion] Want cumsum-like function

Peter Shinners pete@shinners....
Thu Feb 25 02:16:21 CST 2010


On 02/24/2010 11:48 PM, Friedrich Romstedt wrote:
> 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
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>    

I noticed my version with subtract wasn't right. I see now why it works 
with yours. Excellent.


More information about the NumPy-Discussion mailing list