Numarray: Using sum() within functions

duke0uke luser at aol.com
Tue Aug 17 21:41:11 CEST 2004


Christopher T King wrote:
> On Sat, 14 Aug 2004, Jim Cser wrote:
> 
> 
>>I have a function to generate a multi-dimensional array, which then
>>gets summed over one axis.  The problem is that the dimensions
>>are large, and I run out of memory when I create the entire array,
>>so I'm trying to do the sum *within* the function.
>>
>>Example-- variables x,y,z,t; dimensions numX, numY, numZ, numT;
>>functions f1(x,y,z,t), f2(y,z,t);  want to calculate f1*f2 and
>>sum over t to get out[x,y,z].
> 

Cobbling together a number of suggestions, what finally worked was--

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

out = fromfunction(f3,(numX,numY,numZ,1))


I couldn't quite get sum() to work inside the function, but this
is definitely good enough for now.  Thanks to all for your help.
-Jim Cser







More information about the Python-list mailing list