[Numpy-discussion] partial_sum/adj_difference?

Charles R Harris charlesr.harris at gmail.com
Tue Feb 19 14:40:43 EST 2008


On Feb 19, 2008 11:38 AM, Neal Becker <ndbecker2 at gmail.com> wrote:

> Does numpy/scipy have a partial_sum and adj_difference function?
>
> partial_sum[i] = \sum_{j=0}^{i} x[j]


Make add.accumulate will do the trick:

In [1]: add.accumulate(arange(10))
Out[1]: array([ 0,  1,  3,  6, 10, 15, 21, 28, 36, 45])

In [3]: arange(10).cumsum()
Out[3]: array([ 0,  1,  3,  6, 10, 15, 21, 28, 36, 45])



> adj_diff[i] = x[i] - x[i-1] : i > 1, x[i] otherwise
>
>
Well, x[1:] - x[:-1] will give the usual differences. If you need the
leading x[0] prefix the x vector with a 0.

In [4]: a = arange(10)

In [5]: b = a[1:] - a[:-1]

In [6]: b
Out[6]: array([1, 1, 1, 1, 1, 1, 1, 1, 1])

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080219/94022f5e/attachment.html>


More information about the NumPy-Discussion mailing list