[Numpy-discussion] Need a good idea: calculate the mean of many vectors

EMMEL Thomas Thomas.EMMEL at 3ds.com
Tue Feb 8 10:24:10 EST 2011


Hi,

here is something I am thinking about for some time and I am wondering whether there is a better solution
within numpy.

The task is:
I have an array (300000+ entries) with arrays each with length == 3, that is initially empty like this:

n = 100 # for test otherwise ~300000
a1 = reshape(zeros(3*n).astype(float), (n,3))

(Speaking literally this is a field of displacements in a Finite-Element-Mesh)
Now I have a lot of triangles where the corners are the nodes, each with an index between 0 and n-1
and I like to add a unique displacement for all three nodes to a1 like this

a2 = zeros(n).astype(int)

for indices, data in [...]:
    #data = array((1.,2.,3.))
    #indices = (1,5,60)
    for index in indices:
        a1[index] += data
        a2[index] += 1

Now after filling a1 and a2 over and over (for a lot of triangles) I can finally calculate the
averaged displacement on all points by this

meand = a1/reshape(a2,(n,1))

I am doing this in the old numeric package (and can do it in numpy).
I wonder whether there is a better way to do that in numpy. Numpy is in this actual case 20times slower
than Numeric and I know that is due to the fact that I need to change only 3 values of the big array every
loop.

The basic problem is:
How can I add very few values to a big array with a good performance?
I thought about this:

m = zeros(n)
put(m, indices,1.) # only 3 ones in a long list of zeros!
a1 += outer(m, data)
a2 += m

which is in fact very slow due to the function outer.

Any help appriciated

Thomas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110208/0fe02059/attachment.html>


More information about the NumPy-Discussion mailing list