[Python-talk] Generator Question
Kent Johnson
kent37 at tds.net
Wed Aug 12 22:36:04 EDT 2009
On Wed, Aug 12, 2009 at 9:14 PM, Bruce
Labitt<bruce.labitt at myfairpoint.net> wrote:
> Right now I'm trying to wrap my mind around how to just code the make_array
> part. For some reason, I'm having trouble with the concept of some of the
> values inside the function being persistent.
>
> Say I have an array which is 100 elements. I need to generate a second
> array which is also 100 elements, but it is composed of 25 repetitions of 4
> elements of a different array. ( f[0], f[1], f[2], f[3] ) I then multiply
> the two arrays. The result is returned by the generator. The next time the
> generator is called, I use the first array again, but have to create a 'new'
> second array which is 25 repetitions of f[4], 25 repetitions of f[5],..., 25
> repetitions of f[7].
>
> I understand the repetitions and concatenation, which is very easy in
> scipy/numpy. It is the persistence which I don't yet understand.
>
> In the above example, it is clear that the first array doesn't need to be
> inside the generator, although it may be convenient.
> Here is a snippet off the top of my head - no real thought into this yet,
> but...
>
> from numpy import ones, concatenate # don't know which module size is in...
>
> def make_array( freqsequence, firstarray, dt, otherparams ):
> n = freqsequence.size
> idx = 0
> quartarray = ones(firstarray.size/4) # makes an array of ones
> t = dt*range(n)
That won't work, at least not if dt is numeric and range() is the
builtin function.
> while idx<n/4:
> freqarray = freqsequence[idx]*quartarray
> freqarray = concatenate([freqarray, freqsequence[idx+1]*quartarray],1)
> freqarray = concatenate([freqarray, freqsequence[idx+2]*quartarray],1)
> freqarray = concatenate([freqarray, freqsequence[idx+3]*quartarray],1)
> yield firstarray*exp(2.0*pi*freqarray*t*1j)
> t = t + n*dt
> idx = idx+1
>
> Forgive the concatenations, there is a better way of doing this... Which I
> will figure out about 50 microseconds after I hit send.
>
> Would this work? Assuming of course that freqsequence.size is really
> divisible by 4.
The concept is fine, I can't vouch for the details! If you create the
arrays this way, then the first step in the generator chain is just
arrays - make_array(freqsequence, firstarray, dt, otherparams)
because that is already a generator.
> Will the values for t keep on increasing continuously? At least until we
> run out of elements in freqsequence?
Yes, assuming n*dt is positive...
Kent
More information about the Python-talk
mailing list