[Numpy-discussion] vectorizing loops

Robert Kern robert.kern@gmail....
Fri Oct 26 01:56:26 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])

No, that doesn't work. The way you have it, for each i>0,

  z[i] = func2(func1(0))

What Matthew wants is this

  z[0] = 1.0
  z[1] = func2(func1(1.0))
  z[2] = func2(func1(func2(func1(1.0))))

I'm afraid that there is no fast, elegant way to do this. If you could turn
func2(func1(x)) into a binary ufunc f(x, y) with an ignored y, then you could
call f.accumulate(z). However, if that ufunc is not implemented in C, there's
not much point.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the Numpy-discussion mailing list