[Python-talk] reduction Q

bruce.labitt at autoliv.com bruce.labitt at autoliv.com
Thu Oct 22 14:11:00 EDT 2009


Well, it turns out there is a simpler way :)

I have tried this, and it even works :)  I can show it at PySIG tonight.

def compute_response( f, h ):
        # a few calculations go here...
        retval = [ Ph, Pv, Prec ]
        return retval

for hr in hrlist:
        arrays = (compute_response( freq, hr ) for freq in freqarray )
        (sumh, sumv, sumP) = reduce( add, arrays ) 
        dBh = 10.0*log10( sumh/ freqarray.size )
        dBv = 10.0*log10( sumv/freqarray.size )
        PdB = 10.0*log10( sumP/freqarray.size )
        # more stuff...

The key was returning retval, rather than [Ph, Pv, Prec].  In this type of 
generator expression, one cannot return the tuple directly. 

I cannot tell you how cool this is... 

Ph, Pv, and Prec are the arrays that result from the calculation 
compute_response for frequency f, and height h.
dBh is the array which is the average response over all frequencies for 
every range.

Oh yeah, it all runs in seconds!

Very happy!

Bruce Labitt
Autoliv Electronics
1011B Pawtucket Blvd, PO Box 1858
Lowell, MA  01853

Email: bruce.labitt at autoliv.com. 
Tel:  (978) 674-6526
Fax: (978) 674-6581 



kent3737 at gmail.com wrote on 10/22/2009 01:42:42 PM:

> On Thu, Oct 22, 2009 at 11:01 AM,  <bruce.labitt at autoliv.com> wrote:
> > There is a generator function that produces as its output 2 arrays, a 
and
> > b.  I'd like to get the average of the ith elements for both arrays. 
 The
> > a and b arrays are the same length, if it matters.
> >
> > (a,b) = ( myfunc( args ) for f in freqarray )
> 
> You have lost me already. What is args? Do you mean the generator
> yields a sequence whose elements are pairs of arrays? And you want to
> create two new arrays whose values are the average of the values in
> the sequence?
> 
> ( myfunc( args ) for f in freqarray ) is a generator expression, its
> value is a generator object, I don't understand how you can assign
> that to a tuple.
> 
> > can I do something like the following?
> >
> > (suma,sumb) = reduce(add, (a, b))   # this stops the generator 
chain...
> 
> Again I'm confused. The above is equivalent to
> (suma, sumb) = add(a, b)
> so it doesn't seem to do what you want.
> 
> Assuming that myfunc() takes an element from freqarray and returns two
> arrays, and that what you want to do is average the result arrays
> separately, and that you have enough memory to hold all the arrays,
> then I would do something like this to create two independent lists:
>   (alla, allb) = zip(* ( myfunc( f ) for f in freqarray ) )
> 
> Then you can sum and average alla and allb separately.
> 
> Kent


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