Numeric: fromfunction() performance

Travis Oliphant oliphant at ee.byu.edu
Wed Oct 16 02:07:09 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)
>

My experience is that fromfunction is rarely needed.  The usual way I
build up evaluations like this is to create an array of indexes like this

i = Numeric.arange(0,nf)

then use it to do the calculation

q = Numeric.exp(-x1*i*i - 1j*i*x2)

If I understand your code, correctly, then this should reproduce what
you want and be very fast.

> And, is there a way to avoid that 'shape' statement?
> fromfunction() apparently refuses to produce a one-dimensional
> array of length nf,

I was not aware of this fromfunction limitation.  I will look into it.

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

i = Numeric.arange(0,nf)
q = Numeric.exp(-x1*i*i-1j*i*x2)


> Which is much too slow.
>
> Gruß, jsaul
>

Best,

-Travis


-- 
Travis Oliphant
Assistant Professor
459 CB
Electrical and Computer Engineering
Brigham Young University
Provo, UT 84602
Tel: (801) 422-3108
oliphant.travis at ieee.org




More information about the Python-list mailing list