<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style id="owaParaStyle" type="text/css">P {margin-top:0;margin-bottom:0;}</style>
</head>
<body ocsi="0" fpstyle="1">
<div style="direction: ltr;font-family: Tahoma;color: #000000;font-size: 10pt;">Hi,<br>
<br>
here is something I am thinking about for some time and I am wondering whether there is a better solution<br>
within numpy.<br>
<br>
The task is:<br>
I have an array (300000+ entries) with arrays each with length == 3, that is initially empty like this:<br>
<br>
n = 100 # for test otherwise ~300000<br>
a1 = reshape(zeros(3*n).astype(float), (n,3))<br>
<br>
(Speaking literally this is a field of displacements in a Finite-Element-Mesh)<br>
Now I have a lot of triangles where the corners are the nodes, each with an index between 0 and n-1<br>
and I like to add a unique displacement for all three nodes to a1 like this<br>
<br>
a2 = zeros(n).astype(int)<br>
<br>
for indices, data in [...]:<br>
    #data = array((1.,2.,3.))<br>
    #indices = (1,5,60)<br>
    for index in indices:<br>
        a1[index] += data<br>
        a2[index] += 1<br>
<br>
Now after filling a1 and a2 over and over (for a lot of triangles) I can finally calculate the<br>
averaged displacement on all points by this<br>
<br>
meand = a1/reshape(a2,(n,1))<br>
<br>
I am doing this in the old numeric package (and can do it in numpy).<br>
I wonder whether there is a better way to do that in numpy. Numpy is in this actual case 20times slower<br>
than Numeric and I know that is due to the fact that I need to change only 3 values of the big array every<br>
loop.<br>
<br>
The basic problem is:<br>
How can I add very few values to a big array with a good performance?<br>
I thought about this:<br>
<br>
m = zeros(n)<br>
put(m, indices,1.) # only 3 ones in a long list of zeros!<br>
a1 += outer(m, data)<br>
a2 += m<br>
<br>
which is in fact very slow due to the function outer.<br>
<br>
Any help appriciated<br>
<br>
Thomas
<div>
<div style="font-family: Tahoma; font-size: 13px;">
<div style="font-family: Tahoma; font-size: 13px;"><br>
</div>
</div>
</div>
</div>
</body>
</html>