can this be made faster?

Bill Baxter wbaxter at gmail.com
Sun Oct 8 19:07:02 EDT 2006


So what's the answer then?  Can it be made faster?
--bb

On 10/9/06, Robert Kern <robert.kern at gmail.com> wrote:
>
> 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
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20061009/3d592229/attachment-0001.html>
-------------- next part --------------
-------------------------------------------------------------------------
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
-------------- next part --------------
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion


More information about the NumPy-Discussion mailing list