[Numpy-discussion] Array of Callables

Andrew Corrigan acorriga@gmu....
Wed Mar 21 00:02:26 CDT 2007

Robert Kern <robert.kern <at> gmail.com> writes:

> Shane Holloway wrote:
> > To the vector-processing masters of numpy!
> > 
> > I'm wanting to optimize calling a list (or array) of callable  
> > objects.  Consider the following:
> > 
> > vCallables = numpy.array([<python callables: fns, methods, lambdas,  
> > classes, builtin functions>])
> > vParam1 = numpy.array([<parameters for vCallables>])
> > vParam2 = numpy.array([<parameters for vCallables>])
> > vParam3 = numpy.array([<parameters for vCallables>])
> > vResults = numpy.array([None for e in vCallables])
> > 
> > Is it possible to have numpy call them in a way something like::
> > 
> > 	for i in xrange(vCallables.shape[0]):
> > 		vResults[i] = vCallables[i] ( vParam1[i], vParam2[i], vParam3[i]  )
> > 
> > 
> > With the core of that loop implemented in C?
> There's nothing sitting around, no. It's easy enough to write with Pyrex (and
> easier without bothering with numpy); however, unless if all of your functions
> are builtin, you won't see much, if any, speed gains. Python's function call
> overhead will dominate the savings of a C for loop over a Python for loop.

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.

- Andrew

More information about the Numpy-discussion mailing list