can this be made faster?

Robert Kern robert.kern at gmail.com
Sun Oct 8 17:59:19 CDT 2006


Bill Baxter wrote:
> Yes, that'd be
>    a[b] += c

No, I'm afraid that fancy indexing does not do the loop that you are thinking it 
would (and for reasons that we've discussed previously on this list, *can't* do 
that loop). That statement reduces to something like the following:

   tmp = a[b]
   tmp = tmp.__iadd__(c)
   a[b] = tmp


In [1]: from numpy import *

In [2]: a = array([0, 0])

In [3]: b = array([0, 1, 0, 1, 0])

In [4]: c = array([1, 1, 1, 1, 1])

In [5]: a[b] += c

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

In [7]: a = array([0, 0])

In [8]: tmp = a[b]

In [9]: tmp
Out[9]: array([0, 0, 0, 0, 0])

In [10]: tmp = tmp.__iadd__(c)

In [11]: tmp
Out[11]: array([1, 1, 1, 1, 1])

In [12]: a[b] = tmp

In [13]: a
Out[13]: array([1, 1])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV




More information about the Numpy-discussion mailing list