[Numpy-discussion] Speed up function on cross product of two sets?
arnd.baecker at web.de
Mon Apr 3 02:18:08 CDT 2006
On Sun, 2 Apr 2006, Zachary Pincus wrote:
> 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.
If do_something eats arrays, you could try:
result = do_something(l1[:,NewAxis], l2)
from numpy import *
l1 = linspace(0.0, pi, 10)
l2 = linspace(0.0, pi, 3)
def f(y, x):
print f(l1[:,NewAxis], l2)
((Note that I just learned in some other thread that with numpy there is
an alternative to NewAxis, but I haven't figured out which that is ...))
More information about the Numpy-discussion