[Numpy-discussion] Array of Callables

Anne Archibald peridot.faceted at gmail.com
Wed Mar 21 08:58:10 EDT 2007


On 21/03/07, Andrew Corrigan <acorriga at gmu.edu> 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 [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



More information about the NumPy-Discussion mailing list