Applying a function to a 2-D numarray

Steven Bethard steven.bethard at gmail.com
Mon May 16 14:03:24 EDT 2005


Matt Feinstein wrote:
> On Mon, 16 May 2005 11:07:06 -0600, Steven Bethard
> <steven.bethard at gmail.com> wrote:
> 
> 
> 
>>I must be missing something, because the simplest possible thing seems 
>>to work for me:
>>
>>py> import numarray as na
>>py> def plus1(arr):
>>...     return arr + 1
>>...
>>py> def apply_func(arr, f):
>>...     return f(arr)
>>...
>>py> a = na.arange(20, shape=(4, 5))
>>py> a
>>array([[ 0,  1,  2,  3,  4],
>>       [ 5,  6,  7,  8,  9],
>>       [10, 11, 12, 13, 14],
>>       [15, 16, 17, 18, 19]])
>>py> apply_func(a, plus1)
>>array([[ 1,  2,  3,  4,  5],
>>       [ 6,  7,  8,  9, 10],
>>       [11, 12, 13, 14, 15],
>>       [16, 17, 18, 19, 20]])
>>
>>Is this not what you wanted?
> 
> 
> The problem is that I chose an example function that's too simple.
> Non-trivial functions aren't so polymorphic, unfortunately.

Can you give an example of what you really want to do?  Probably there 
are numarray functions that you can use.  In general, you'll do better 
applying a sequence of numarray functions than operating element-wise on 
an array and converting it from a list back to an array...

STeVe



More information about the Python-list mailing list