[Numpy-discussion] Fwd: Advanced selection, duplicate indices, and augmented assignment

A. M. Archibald peridot.faceted at gmail.com
Sun Jan 7 01:08:27 EST 2007

On 06/01/07, Timothy Hochberg <tim.hochberg at ieee.org> wrote:

> Do we have to revisit that thread? I seem to recall it getting kind of
> cranky. To avoid reliving that, I will attempt dredge up the relevant issue:
> "a[indx]+=b" should be the same as "a[indx]=a[indx]+b". All else follow from
> that. If staring at that for a while doesn't enlighten you, then you will
> have to read that thread.

I'm afraid I dredged this issue up previously, and I have no desire to
do so again, but this is not an identity:

In [3]: a = arange(5)

In [4]: a[1:] = a[1:] + a[:-1]

In [5]: a
Out[5]: array([0, 1, 3, 5, 7])

In [6]: a = arange(5)

In [7]: a[1:] += a[:-1]

In [8]: a
Out[8]: array([ 0,  1,  3,  6, 10])

In fact the python documentation simply states that the two should be
"nearly identical". However, a subtler restriction, explained to me by
Robert Kern (http://aspn.activestate.com/ASPN/Mail/Message/scipy-user/3257570),
means that it is in basically infeasible and inappropriate to make
this do what I would have wanted it to.

Nothing, of course, prevents anyone from implementing this in a
function; in fact I think some of the histogram functions are capable
of efficiently doing the most common things one might want this for.

A. M. Archibald

A. M. Archibald

More information about the NumPy-Discussion mailing list