Applying a function to a 2-D numarray

Matt Feinstein nospam at here.com
Mon May 16 09:29:19 EDT 2005


Is there an optimal way to apply a function to the elements of a two-d
array?

What I'd like to do is define some function:

def plone(x):
     return x+1

and then apply it elementwise to a 2-D numarray. I intend to treat the
function as a variable, so ufuncs are probably not appropriate-- I
realize that  what I'm looking for won't be terrifically efficient,
but I'd like to avoid doing it in the -worst- possible way.

Some things I've looked at include things like

def applyfun(m,f):
     elist = [f(e) for e in m]
     return reshape(elist,m.shape)

however, I can see that this looks neat but probably generates several
copies of the array, which is not so neat.

Is there a better way?

Matt Feinstein

--
There is no virtue in believing something that can be proved to be true.



More information about the Python-list mailing list