[Numpy-discussion] vectorizing loops

David Cournapeau david@ar.media.kyoto-u.ac...
Fri Oct 26 01:44:39 CDT 2007

Gael Varoquaux wrote:
> On Thu, Oct 25, 2007 at 04:16:06PM -0700, Mathew Yeates wrote:
>> Anybody know of any tricks for handling something like
>> z[0]=1.0
>> for i in range(100):
>>     out[i]=func1(z[i])
>>     z[i+1]=func2(out[i])
> Something like:
> z[0] = 1.
> out = func1(z)
> z[1:] = func2(out[:-1])
This only works if func1 has no side effect on its argument. The problem 
boils down to whether the above algorithm is recursive or not (does 
z[i+1] needs z[i]). If func1 has no side effect, then your solution is 
fine (but then writing the thing as out[i] = func1(z[i]); z[i+1] = 
func2(z[i]) is not really intuitive). If func1 has side effect, then you 
cannot vectorize easily.



P.S: IMHO, this is one of the main limitation of numpy (or any language 
using arrays for speed; and this is really difficult to optimize: you 
need compilation, JIT or similar to solve those efficiently).

More information about the Numpy-discussion mailing list