Re: [Numpydiscussion] reverse cumsum?
On 7/6/10 10:42 , numpydiscussionrequest@scipy.org wrote:
Date: Tue, 06 Jul 2010 10:02:57 0400 From: Alan G Isaac
Subject: Re: [Numpydiscussion] reverse cumsum? To: Discussion of Numerical Python MessageID:<4C333791.5010505@american.edu> ContentType: text/plain; charset=ISO88591; format=flowed On 7/6/2010 9:56 AM, Ken Basye wrote:
Is there a simple way to get a cumsum in reverse order?
> x = np.arange(10) > x[::1].cumsum()[::1] array([45, 45, 44, 42, 39, 35, 30, 24, 17, 9])
Is that what you want?
Alan Isaac
Or, you can do: In [1]: a = np.arange(10) In [5]: np.sum(a)  np.cumsum(a) Out[5]: array([45, 44, 42, 39, 35, 30, 24, 17, 9, 0]) Jonathan
On Tue, Jul 6, 2010 at 11:25 AM, Jonathan Stickel
On 7/6/10 10:42 , numpydiscussionrequest@scipy.org wrote:
Date: Tue, 06 Jul 2010 10:02:57 0400 From: Alan G Isaac
Subject: Re: [Numpydiscussion] reverse cumsum? To: Discussion of Numerical Python MessageID:<4C333791.5010505@american.edu> ContentType: text/plain; charset=ISO88591; format=flowed On 7/6/2010 9:56 AM, Ken Basye wrote:
Is there a simple way to get a cumsum in reverse order?
>> x = np.arange(10) >> x[::1].cumsum()[::1] array([45, 45, 44, 42, 39, 35, 30, 24, 17, 9])
Is that what you want?
Alan Isaac
Or, you can do:
In [1]: a = np.arange(10) In [5]: np.sum(a)  np.cumsum(a) Out[5]: array([45, 44, 42, 39, 35, 30, 24, 17, 9, 0])
Jonathan _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
Alternately:
In [11]: reversed(np.arange(10).cumsum())
Out[11]:
On 7/6/2010 3:37 PM, Joshua Holbrook wrote:
In [10]: np.array(list(reversed(np.arange(10).cumsum()))) Out[10]: array([45, 36, 28, 21, 15, 10, 6, 3, 1, 0])
That might appear to match the subject line but does not match the OP's example output, which was [45, 45, 44, 42, 39, 35, 30, 24, 17, 9]. You are giving the equivalent of x.cumsum()[::1], while the OP asked for the equivalent of x[::1].cumsum()[::1]. fwiw, Alan Isaac
On Tue, Jul 6, 2010 at 2:23 PM, Alan G Isaac
On 7/6/2010 3:37 PM, Joshua Holbrook wrote:
In [10]: np.array(list(reversed(np.arange(10).cumsum()))) Out[10]: array([45, 36, 28, 21, 15, 10, 6, 3, 1, 0])
That might appear to match the subject line but does not match the OP's example output, which was [45, 45, 44, 42, 39, 35, 30, 24, 17, 9].
You are giving the equivalent of x.cumsum()[::1], while the OP asked for the equivalent of x[::1].cumsum()[::1].
fwiw, Alan Isaac _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
Oh snap. Good callidk what I was thinking. Tired, I guess. :) In that case, if you were going to use reversed() things would get a bit nastier: In [13]: np.array(list(reversed(np.array([9i for i in xrange(10)]).cumsum()))) Out[13]: array([45, 45, 44, 42, 39, 35, 30, 24, 17, 9]) ...which is gross enough that this approach is probably worth abandoning.
I think Ken's suggestion may be the best so far...
I meant to say Alan's suggestion, i.e. x[::1].cumsum()[::1].
participants (3)

Alan G Isaac

Jonathan Stickel

Joshua Holbrook