[Numpy-discussion] element wise help
Chris Colbert
sccolbert@gmail....
Thu May 7 13:11:08 CDT 2009
alright I got it working. Thanks!
This version is an astonishingly 1900x faster than my original
implementation which had two for loops. Both versions are below:
thanks again!
### new fast code ####
b = 4.7
n = arange(1, N+1, 1.0).reshape(N, -1)
n1 = (-1)**n
prefix = exp(b) / timearray
arg1 = {'S': b / timearray}
exec('from numpy import *', arg1)
term1 = (0.5) * eval(transform, arg1)
temp1 = b + (1J * pi * n)
temp2 = temp1 / timearray
arg2 = {'S': temp2}
exec('from numpy import *', arg2)
term2 = (eval(transform, arg2) * n1).sum(axis=0).real
f = prefix * (term1 + term2)
return f
##### old slow code ######
b = 4.7
f = []
for t in timearray:
rsum = 0.0
for n in range(1, N+1):
arg1 = {'S': ((b/t) + (1J*n*pi/t))}
exec('from numpy import *', arg1)
tempval = eval(transform, arg1)*((-1)**n)
rsum = rsum + tempval.real
arg2 = {'S': b/t}
exec('from numpy import *', arg2)
tempval2 = eval(transform, arg2)*0.5
fval = (exp(b) / t) * (tempval2 + rsum)
f.append(fval)
return f
On Thu, May 7, 2009 at 1:41 PM, Chris Colbert <sccolbert@gmail.com> wrote:
> its part of a larger program for designing PID controllers. This particular
> function numerical calculates the inverse laplace transform using riemann
> sums.
>
> The exec statements, from what i gather, allow the follow eval statement to
> be executed in the scope of numpy and its functions. I don't get how it
> works either, but it doesnt work without it.
>
> I've just about got something working using broadcasting and will post it
> soon.
>
> chris
>
>
> On Thu, May 7, 2009 at 1:37 PM, <josef.pktd@gmail.com> wrote:
>
>> On Thu, May 7, 2009 at 1:08 PM, Chris Colbert <sccolbert@gmail.com>
>> wrote:
>> > let me just post my code:
>> >
>> > t is the time array and n is also an array.
>> >
>> > For every value of time t, these operations are performed on the entire
>> > array n. Then, n is summed to a scalar which represents the system
>> response
>> > at time t.
>> >
>> > I would like to eliminate this for loop if possible.
>> >
>> > Chris
>> >
>> > #### code ####
>> >
>> > b = 4.7
>> > f = []
>> > n = arange(1, N+1, 1)
>> >
>> > for t in timearray:
>> > arg1 = {'S': ((b/t) + (1J*n*pi/t))}
>> > exec('from numpy import *', arg1)
>> > tempval = eval(transform, arg1)*((-1)**n)
>> > rsum = tempval.real.sum()
>> > arg2 = {'S': b/t}
>> > exec('from numpy import *', arg2)
>> > tempval2 = eval(transform, arg2)*0.5
>> > fval = (exp(b) / t) * (tempval2 + rsum)
>> > f.append(fval)
>> >
>> >
>> > #### /code #####
>> >
>>
>> I don't understand what the exec statements are doing, I never use it.
>> what is transform?
>> Can you use regular functions instead or is there a special reason for
>> the exec and eval?
>>
>> In these expressions ((b/t) + (1J*n*pi/t)), (exp(b) / t)
>> broadcasting can be used.
>>
>> Whats the size of t and n?
>>
>> Josef
>> _______________________________________________
>> Numpy-discussion mailing list
>> Numpy-discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090507/3207c515/attachment-0001.html
More information about the Numpy-discussion
mailing list