[Numpy-discussion] element wise help
Chris Colbert
sccolbert@gmail....
Thu May 7 12:08:43 CDT 2009
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 #####
On Thu, May 7, 2009 at 1:04 PM, Chris Colbert <sccolbert@gmail.com> wrote:
> unfortunately, the actual function being processes is not so simple, and
> involves evaluating user functions input from the prompt as strings. So i
> have no idea how to do it in Cython.
>
> Let me look into this broadcasting.
>
> Thanks Josef!
>
>
> On Thu, May 7, 2009 at 12:56 PM, <josef.pktd@gmail.com> wrote:
>
>> On Thu, May 7, 2009 at 12:39 PM, Chris Colbert <sccolbert@gmail.com>
>> wrote:
>> > suppose i have two arrays: n and t, both are 1-D arrays.
>> >
>> > for each value in t, I need to use it to perform an element wise scalar
>> > operation on every value in n and then sum the results into a single
>> scalar
>> > to be stored in the output array.
>> >
>> > Is there any way to do this without the for loop like below:
>> >
>> > for val in t_array:
>> >
>> > out = (n / val).sum() # not the actual function being done,
>> but
>> > you get the idea
>> >
>>
>>
>> broad casting should work, e.g.
>>
>> (n[:,np.newaxis] / val[np.newaxis,:]).sum()
>>
>> but it constructs the full product array, which is memory intensive
>> for a reduce operation, if the 1d arrays are large.
>>
>> another candidate for a cython loop if the arrays are large?
>>
>> 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/23590f48/attachment.html
More information about the Numpy-discussion
mailing list