[Numpy-discussion] Array addition inconsistency

Martin Luethi luethi@vaw.baug.ethz...
Thu Aug 29 06:00:20 CDT 2013


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().

This behaviour is certainly surprising, and leads to unexpected errors
if used without testing.

Best, Martin


-- 
Dr. Martin Lüthi   luethi@vaw.baug.ethz.ch
VAW Glaciology     http://www.vaw.ethz.ch/gz
ETH Zürich         http://people.ee.ethz.ch/~luethim
8093 Zürich        Tel: +41 44 632 40 93




More information about the NumPy-Discussion mailing list