[Numpy-discussion] Speed up function on cross product of two sets?

Zachary Pincus zpincus at stanford.edu
Sun Apr 2 16:07:07 CDT 2006

Hi folks,

I have a inner loop that looks like this:
out = []
for elem1 in l1:
   for elem2 in l2:
     out.append(do_something(l1, l2))
result = do_something_else(out)

where do_something and do_something_else are implemented with only  
numpy ufuncs, and l1 and l2 are numpy arrays.

As an example, I need to compute the median distance from any element  
in one set to any element in another set.

What's the best way to speed this sort of thing up with numpy (e.g.  
push as much down into the underlying C as possible)? I could re- 
write do_something with the numexpr tools (which are very cool), but  
that doesn't address the fact that I've still got nested loops living  
in Python.

Perhaps there's some way in numpy to make one big honking array that  
contains all the pairs from the two lists, and then just run my  
do_something on that huge array, but that of course scales poorly.

Any thoughts?


More information about the Numpy-discussion mailing list