[Numpy-discussion] Fwd: Advanced selection, duplicate indices, and augmented assignment
oliphant at ee.byu.edu
Sat Jan 6 21:35:16 CST 2007
Timothy Hochberg wrote:
> On 1/6/07, *Robert Kern* < robert.kern at gmail.com
> <mailto:robert.kern at gmail.com>> wrote:
> Sean R. Lynch wrote:
> >>>> x = zeros((3,))
> >>>> x[array([0, 1, 1])] += array([1, 1, 1])
> >>>> x
> > array([ 1., 1., 0.])
> > If this worked the way I was hoping, the output would be [1 2 0]
> > it would add to element 1 twice due to its duplication in the
> > selection array.
> > Is the current behavior intentional or is it an accident of
> > implementation?
> It is an unavoidable consequence of the way Python interprets that
> code and the
> way numpy arrays are fundamentally implemented. See Travis
> Oliphant's, Tim
> Hochberg's and my posts in the thread "Histograms via indirect
> index arrays" for
> more details.
> 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
> "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 suspect that in theory we could make the += form behave as you
> expect, but that would break the identity above, which would confuse a
> bunch of people]
I don't think we could make it work as he expects (and not radically
change NumPy itself so that other operations are very different) because
of the limitations of Python.
The point was that it is too complicated to do any differently (and is
More information about the Numpy-discussion