[Python-talk] Generator Question

Bruce Labitt bruce.labitt at myfairpoint.net
Sat Aug 15 10:56:22 EDT 2009


Kent Johnson wrote:
> On Fri, Aug 14, 2009 at 5:19 PM, <bruce.labitt at autoliv.com> wrote:
>   
>> 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.
>>     
>
> OK, so you need a function that consumes a sequence of row arrays and
> computes the element-by-element maxima. If you have
> powers = ( <some generator for the powers arrays> )
>
> and maximum(a, b) will take the desired maximum of two elements of
> powers, then I think the result you want is
> max_power = reduce(maximum, powers)
>
> Kent
>
>   
Well that is more elegant than what I came up with last night about 30 
minutes after posting.

temparr = -300.0*ones(N) # N is the length of the row of powers
for junk in powers:
    max_power = maximum(junk, temparr)
    temparr = max_power


I'll have to look up reduce...

Thanks!
Bruce

for



More information about the Python-talk mailing list