Daniel Yarlett daniel.yarlett@gmail....
Wed Apr 29 08:03:00 CDT 2009

```Hi All,

I'm coding an iterative algorithm which needs to update a results
array on each iteration. I have several (one-dimensional) arrays, and
currently I have the following non-vectorized code which gets called
at the end of each iteration (Inds, Current, and Update are all one-
dimensional and the same size):

for i in xrange(Indices.size):
Current[Indices[i]] += Update[i]

This is running too slowly, and so I am going through the usual
process of attempting to vectorize this code (Indices.size > 5000). My
attempt at vectorizing this was:

Current[Indices] += Update

But this does not yield the same result. Here's a simple example.
First the iterative version:

>>> Current = zeros(2,'d')
>>> Update = ones_like(Current)
>>> Indices = zeros(2,'i')
>>> for i in xrange(Indices.size):
... 	Current[Indices[i]] += Update[i]
...
>>> Current
array([ 2.,  0.])

And then my vectorized attempt:

>>> Current = zeros(2,'d')
>>> Update = ones_like(Current)
>>> Indices = zeros(2,'i')
>>> Current[Indices] += Update
>>> Current
array([ 1.,  0.])

As you can see, Current is different in the two cases. Any ideas how I
can recreate the behavior of the iterative process in a more numpy-
friendly, vectorized (and hopefully quicker) way? And possibly also
about why my intuitions concerning the semantics of the vectorized
code are wrong?

Many thanks,

Dan.
```