[Numpy-discussion] fast way of doing "cross-multiplications" ?

Eric Emsellem emsellem at obs.univ-lyon1.fr
Tue Jul 18 10:23:53 EDT 2006


Hi,

I have a specific quantity to derive from an array, and I am at the
moment unable to do it for a too large array because it just takes too
long! So I am looking for an advice on how to efficiently compute such a
quantity:

I have 3 arrays of N floats (x[...], y[..], z[..]) and I wish to do:

result = 0.
for i in range(N) :
   for j in range(i+1,N,1) :
      result += 1. / sqrt((x[j] - x[i])**2 + (y[j] - y[i])**2 + (z[j] -
z[i])**2)


Of course the procedure written above is very inefficient and I thought
of doing:

result = 0.
for i in range(N) :
   result += 1. / sqrt((x[i+1:] - x[i])**2 + (y[i+1:] - y[i])**2 +
(z[i+1:] - z[i])**2)

Still, this is quite slow and not workable for very large arrays (> 10^6
floats per array).

Any hint on how to speed things up here?

Thanks in advance!

Eric




More information about the NumPy-Discussion mailing list