[Numpy-discussion] Using sum() within a function

Jim Cser jimcser at pacifier.com
Mon Aug 16 09:59:07 CDT 2004


Perry Greenfield wrote:

> 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
>>
> 
> Perhaps I'm confused but it seems to me the for loop is wrong.
> 
> Don't you want something like:
> 
> 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
> 
> Instead?
> 
> Not that I've tried this, but so long as the function will
> work given 3-d arrays for x,y,z, it should. (though the dependence
> on numT as a global is a bit worrisome)
> 
> Perry GReenfield


The loop was indeed wrong, and "tempval = 0.*x" was the trick I needed, 
thanks.  Unfortunately, iterating over t is still extremely slow. 
Ideally, there would be a way to use fromfunction() with slices as 
arguments.

-Jim




More information about the Numpy-discussion mailing list