Numarray: Using sum() within functions

Jim Cser jimcser at pacifier.com
Mon Aug 16 13:01:55 EDT 2004


Jeff Epler wrote:

> On Sat, Aug 14, 2004 at 02:07:30PM -0700, Jim Cser wrote:
> 
>>Instead, I'm trying to do something like--
>>def f3(x,y,z):
>>   for t in range(numT):
>>     tempval = f1(x,y,z,t) * f2(y,z,t)
>>
>>   outval = sum(tempval,axis = 3)
>>   return outval
> 
> 
> Here, "tempval" takes on a series of values during the for loop, then
> "sum" is used on value from the final iteration of the loop.
> 
> Perhaps you want something like
>     def f3(x, y, z):
>         temps = []
>         for t in range(numT):
>             temps.append(f1(...) * f(...))
>         return sum(temps, axis=3)
> 
> Jeff

Thanks, that works, although someone gave me one that is faster:

  def f3(x,y,z):
     tempval = 0.*x
     for t in range(numT):
       tempval += f1(x,y,z,t) * f2(y,z,t)
     return tempval

In either case, unfortunately, looping over t is extremely slow.
Ideally, there would be a way to use fromfunction() with slices as
arguments.

-Jim




More information about the Python-list mailing list