[Python-talk] Generator Question

bruce.labitt at autoliv.com bruce.labitt at autoliv.com
Fri Aug 14 17:19:16 EDT 2009


 kent3737 at gmail.com wrote on 08/13/2009 12:42:04 PM:

> On Thu, Aug 13, 2009 at 11:58 AM, <bruce.labitt at autoliv.com> wrote:
> > Will the group of generators cascade the
> > same?
> 
> Yes, all that is changing is the source of the first generator (from a
> generator function instead of a generator expression).
> 
> > Say we use the previous example.  Something like this, or would I need 
to
> > do something different?
> >
> > seeds = [ <some sequence that defines the arrays to be created> ]
> > score = 0
> > while score < threshold:
> >   #arrays = (make_array(seed) for seed in seeds)
> >    arrays = make_array(seed)    # instead of line above
> >    fft_results = (compute_fft(array) for array in arrays)
> >    powers = (compute_power(result) for result in fft_results)
> >    score, seeds = optimize(powers)
> 
> Right.
> 
> > This is really powerful stuff...
> 
> Yes :-)
> 
> Kent

Question about the last stage, what I would call the consumer of the 
generator data.

I have written make_array, compute_fft, and compute_power.  Instead of 
using the 
routine optimize (which I have not written yet), I want to test what I 
have.

I am trying to write a function called max_power.

The output of the function is the element by element column maximum of the 
arrays in powers.

Consider the following as a matrix

[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 0 1 9 7]

 10 6 7 9 9  <== column max

There is a numpy function called maximum
It returns an array c whose ith element is the maximum of (ai, bi)

c = maximum( a, b ), ci = max(ai, bi) for i in c

The output of max_power should be one array that is the column maximum of 
the matrix

[ powers row 0 ] first iteration of generator
[ powers row 1 ] 2nd
    .
    .
    .
[ powers row last ] last


So I want max_power to consume all of the data from the previous stages 
and only return
a single row which is the column by column maximum.

Now there really isn't a real matrix here, just a series of arrays (or 
rows) that are output
by the previous generator.

I feel like I am really close here, but this conceptual part is holding me 
back.

Any suggestions?

Thanks!

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