# [SciPy-User] Accumulation sum using indirect indexes

Dan Goodman dg.gmane@thesamovar....
Fri Feb 3 23:03:04 CST 2012

```For the project I'm working on we have quite a specific case of this to
handle, where we have (1) generally have few repeats of the ids, (2)
arbitrary operations to be applied, not just addition. I've just been
working on an optimised numpy-only solution to this and it might be of
interest to others. It works particularly well with few repeats, but I
think it's no slower than other solutions if there are many repeats, at
least until it gets to be mostly repeats at which points doing a simple
loop is faster. For the case of just addition (the case below), a method
using sorting and reduceat is probably quicker (I didn't do a
comparison), but I thought it might be useful for many people to have an
efficient solution for the general case. And if anyone knows a better
one, I'd be very interested! It's still far from close to ideal, for the
typical case it's about 10-20x slower than doing it with C++ (I used
weave to test it), but also about 10-20x faster than doing it with a loop.

I've attached the code (function apply_batch, the others are for
comparison). If anyone's interested I can comment on the code, but it's
basically the trick used by unique(), sorting the indices and comparing

Dan

On 31/01/2012 21:34, Alexander Kalinin wrote:
> Hello!
>
> I use SciPy in computer graphics applications. My task is to calculate
> vertex normals by averaging faces normals. In other words I want to
> accumulate vectors with the same ids. For example,
>
> ids = numpy.array([0, 1, 1, 2])
> n = numpy.array([ [0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.1, 0.1, 0.1],
> [0.1, 0.1 0.1] ])
>
> I need result:
> nv = ([ [0.1, 0.1, 0.1], [0.2, 0.2, 0.2], [0.1, 0.1, 0.1]])
>
> The most simple code:
> nv[ids] += n
> does not work, I know about this. For 1D arrays I use
> numpy.bincount(...) function. But this function does not work for 2D arrays.
>
> So, my question. What is the best way calculate accumulation sum for 2D
> arrays using indirect indexes?
>
> Sincerely,
> Alexander
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: batch_apply.py
Url: http://mail.scipy.org/pipermail/scipy-user/attachments/20120204/28bdebf1/attachment.pl
```