[Numpy-discussion] Array addition inconsistency

Robert Kern robert.kern at gmail.com
Thu Aug 29 08:04:58 EDT 2013


On Thu, Aug 29, 2013 at 12:00 PM, Martin Luethi <luethi at vaw.baug.ethz.ch>
wrote:
>
> Dear all,
>
> After some surprise, I noticed an inconsistency while adding array
> slices:
>
> > a = np.arange(5)
> > a[1:] = a[1:] + a[:-1]
> > a
> array([0, 1, 3, 5, 7])
>
> versus inplace
>
> > a = np.arange(5)
> > a[1:] += a[:-1]
> > a
> array([ 0,  1,  3,  6, 10])
>
> My suspicition is that the second variant does not create intermediate
> storage, and thus works on the intermediate result, effectively
> performing a.cumsum().

Correct. Not creating intermediate storage is the point of using augmented
assignment.

--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130829/15ad10da/attachment.html>


More information about the NumPy-Discussion mailing list