Applying a function to a 2-D numarray

Matt Feinstein nospam at here.com
Mon May 16 15:28:17 EDT 2005


On Mon, 16 May 2005 12:03:24 -0600, Steven Bethard
<steven.bethard at gmail.com> wrote:

>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...

Well, for example, suppose I want to modify the elements of the matrix
in some fashion. However, I'm not entirely sure how I want to do it.
As a strawman, I generate a function with a Boolean test in it that
multiplies by one factor if the matrix element is in an interval and
by a different factor if it is outside the interval

def genfunc(xmin, xmax, f_in, f_out):
     def booltest(x):
         in_interval = x > xmin and x < xmax
         if in_interval:
              return x*f_in
        else:
             return x*f_out
    return booltest
           
Generating the function in this way gives me great flexibility in
deciding exactly what function I apply to the matrix. It's why I want
to use Python for this analysis. The part of the function  I vary and
play around with is localized in one place in the 'genfunc' function--
I can change that and everything else stays the same. However, I
realize that the gain in flexibility means a loss in efficiency. I'm
limited to not-so-efficient ways of. For this work, it's OK-- I just
want to know the best not-so-efficient way of doing the calculation.

Matt Feinstein

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



More information about the Python-list mailing list