[Numpy-discussion] Array of Callables
Wed Mar 21 07:58:10 CDT 2007
On 21/03/07, Andrew Corrigan <firstname.lastname@example.org> wrote:
> This is a feature I've been wanting for a long time, so I'm really glad that
> Shane brought this up.
> While I was hoping for a gain in speed, that isn't the only reason that I would
> like to see this added. In fact, the most compelling reason for me is that it's
> a very natural way to express certain operations.
> I really hope that support for this feature can be added to NumPy.
Vectorizing apply is what you're looking for, by the sound of it:
In : a = array([lambda x: x**2, lambda x: x**3])
In : b = arange(5)
In : va = vectorize(lambda f, x: f(x))
In : va(a[:,newaxis],b[newaxis,:])
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
More information about the Numpy-discussion