[Numpy-discussion] Array of Callables
Shane Holloway
shane.holloway at ieee.org
Wed Mar 21 12:01:03 EDT 2007
On Mar 21, 2007, at 6:58 AM, Anne Archibald wrote:
> Vectorizing apply is what you're looking for, by the sound of it:
> In [13]: a = array([lambda x: x**2, lambda x: x**3])
>
> In [14]: b = arange(5)
>
> In [15]: va = vectorize(lambda f, x: f(x))
>
> In [16]: va(a[:,newaxis],b[newaxis,:])
> Out[16]:
> array([[ 0, 1, 4, 9, 16],
> [ 0, 1, 8, 27, 64]])
>
> Once in a while it would also be nice to vectorize methods, either
> over self or not over self, but the same trick (vectorize an anonymous
> function wrapper) should work fine. Varadic functions do give you
> headaches; I don't think even frompyfunc will allow you to vectorize
> only some of the arguments of a function and leave the others
> unchanged.
>
> Anne
Thanks for the info. I tried this, and found that it benchmarks
about half of just iterating over the methods in the list and calling
them. I think the reason is that two python frames are actually
being run -- one for the vectorized apply, and one for whatever I'm
calling. So what I'm thinking is that I could accomplish this if I
implemented the apply call as a C-level ufunc.
I'll try hacking about with this idea.
Thanks Anne!
More information about the NumPy-Discussion
mailing list