[Python-talk] Generator Question

bruce.labitt at autoliv.com bruce.labitt at autoliv.com
Thu Aug 13 11:18:29 EDT 2009


python-talk-bounces at dlslug.org wrote on 08/12/2009 10:36:04 PM:


> >
> > 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].
> >
<snip>

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

It should be more like:  t = dt*array(range(n)), but n is the wrong 
size...

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

A slightly better version, I hope...

def make_array( freqsequence, firstarray, dt ):
    n = freqsequence.size
    idx = 0
    quartarray = ones(firstarray.size/4)        # create array that is 1/4 
the size of firstarray
    t = dt*array(range(firstarray.size))        # create array with time
    while idx<n/4:
        freqarray = freqsequence[4*idx]*quartarray
          for jj in range(3):
            freqarray = concatenate([freqarray, 
freqsequence[4*idx+jj+1]*quartarray],1)
        yield firstarray*exp(2.0*pi*freqarray*t*1j)
        t = t + n*dt                            # increment time array for 
next iteration of generator
        idx = idx + 1                           # increment generator 
iterator?

freqsequence = float(random.randint(0,2048,2048))*1e6 # create an array of 
2048 random integers
firstarray = ones(10000)        #  there will be something more 
interesting here...
dt = 1e-10

x = make_array( freqsequence, firstarray, dt )  # creates generator object

This should generate n/4 = 512 arrays that are 10000 elements in length? 
(Assuming I use the generator properly...)  And each array of 10000 
elements is multiplied by a complex exponential such that 2500 elements 
are multiplied by freqsequence[0], 2500 elements by freqsequence[1],...  ? 



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

Can you explain this further? 

"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."

I don't quite grasp what you are saying.


This is really neat stuff!!!

-Bruce


******************************
Neither the footer nor anything else in this E-mail is intended to or constitutes an <br>electronic signature and/or legally binding agreement in the absence of an <br>express statement or Autoliv policy and/or procedure to the contrary.<br>This E-mail and any attachments hereto are Autoliv property and may contain legally <br>privileged, confidential and/or proprietary information.<br>The recipient of this E-mail is prohibited from distributing, copying, forwarding or in any way <br>disseminating any material contained within this E-mail without prior written <br>permission from the author. If you receive this E-mail in error, please <br>immediately notify the author and delete this E-mail.  Autoliv disclaims all <br>responsibility and liability for the consequences of any person who fails to <br>abide by the terms herein. <br>
******************************


More information about the Python-talk mailing list