# Numarray: Using sum() within functions

Jim Cser jimcser at pacifier.com
Tue Aug 17 21:52:29 CEST 2004

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

[wrong identity on last post, sorry]

```

More information about the Python-list mailing list