Numeric: fromfunction() performance

jsaul jsaul at
Tue Oct 15 11:57:54 CEST 2002

Hi there,

I have a code in which an array is created 'on the fly' using
Numeric's fromfunction() command:

    import Numeric

    x1,x2,nf = 1.08E-8, 6.28E-3, 100000
    q = Numeric.fromfunction (lambda i,k: - x1*i*i - 1j*i*x2 , (nf,1))
    q.shape = (nf,) # make it a one-dimensional array (arhhh)
    q = Numeric.exp(q)

Surprisingly, the fromfunction() statement takes quite long to
execute, while exp() is very quick, because it's coded in C.
Is there any way to improve the speed of the fromfunction() call?

And, is there a way to avoid that 'shape' statement?
fromfunction() apparently refuses to produce a one-dimensional
array of length nf, so I currently achieve that by computing a
nfX1 matrix, which I then re-shape to a 1-D array. Not quite
an elegant way...

What I actually want is a (quicker) construct corresponding to

    for i in range(0, nf):
        q[i] = cmath.exp(-x1*i*i -1j*i*x2)

Which is much too slow.

Gruß, jsaul
Que le gusta rrrodarrr la errre.

More information about the Python-list mailing list