Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread josef . pktd
On Fri, Feb 3, 2012 at 4:49 PM, Alan G Isaac wrote: > On 2/3/2012 3:37 PM, josef.p...@gmail.com wrote: >> res = - np.dot(x.T, mass*x) >> res[np.arange(3), np.arange(3)] -= np.trace(res) > > > Nice! > Get some speed gain with slicing: > > res = - np.dot(x.T, mass*x) > res.flat[slice(0,None,4)] -= n

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread Alan G Isaac
On 2/3/2012 3:37 PM, josef.p...@gmail.com wrote: > res = - np.dot(x.T, mass*x) > res[np.arange(3), np.arange(3)] -= np.trace(res) Nice! Get some speed gain with slicing: res = - np.dot(x.T, mass*x) res.flat[slice(0,None,4)] -= np.trace(res) Alan ___

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread josef . pktd
On Fri, Feb 3, 2012 at 2:33 PM, wrote: > On Fri, Feb 3, 2012 at 1:58 PM, santhu kumar wrote: >> Hi Josef, >> >> I am unclear on what you want to say, but all I am doing in the code is >> getting inertia tensor for a bunch of particle masses. >> (http://en.wikipedia.org/wiki/Moment_of_inertia#Mom

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread josef . pktd
On Fri, Feb 3, 2012 at 1:58 PM, santhu kumar wrote: > Hi Josef, > > I am unclear on what you want to say, but all I am doing in the code is > getting inertia tensor for a bunch of particle masses. > (http://en.wikipedia.org/wiki/Moment_of_inertia#Moment_of_inertia_tensor) > > So the diagonals are

Re: [Numpy-discussion] Masked array elements where mask = True?

2012-02-03 Thread Howard
Indeed it does! Thanks very much. I was not aware of the numpy where command. Howard On 2/3/12 2:17 PM, Olivier Delalleau wrote: numpy.where(x.mask) should do it. -=- Olivier Le 3 février 2012 14:02, Howard > a écrit : Is there a method that gives an array of

Re: [Numpy-discussion] Masked array elements where mask = True?

2012-02-03 Thread Olivier Delalleau
numpy.where(x.mask) should do it. -=- Olivier Le 3 février 2012 14:02, Howard a écrit : > Is there a method that gives an array of all the array indices of a > masked array where the mask is True? I've been looking through the docs and > don't see it yet... > > Thanks > Howard > -- > Howard La

[Numpy-discussion] Masked array elements where mask = True?

2012-02-03 Thread Howard
Is there a method that gives an array of all the array indices of a masked array where the mask is True? I've been looking through the docs and don't see it yet... Thanks Howard -- Howard Lander Senior Research Software Developer Renaissance Computing Institute (RENCI)

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread santhu kumar
Hi Josef, I am unclear on what you want to say, but all I am doing in the code is getting inertia tensor for a bunch of particle masses. (http://en.wikipedia.org/wiki/Moment_of_inertia#Moment_of_inertia_tensor) So the diagonals are not actually zeros but would have z^2 + y^2 .. The reason which I

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread josef . pktd
t += massi*temp >> > > >> > > Alan Isaac >> > >> > maybe something like this, (self contained example and name spaces to >> > make running it easier) >> > >> > import numpy as np >> > n = 15 >> > x = np.arange(n*3.).

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread santhu kumar
ros((3,3)) > > for i in range(n): > > ri = x[i,:].reshape(1,3) > > inert = inert + mass[i,]*(sum(ri*ri)*np.eye(3) - np.dot(ri.T,ri)) > > print inert > > > > print np.diag((mass * x**2).sum(0)) - np.dot(x.T, mass*x) > > > >

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread Sebastian Berg
I guess Einsum is much cleaner, but I already had started with this and maybe someone likes it, this is fully vectorized and uses a bit of funny stuff too: # The dot product(s), written using broadcasting rules: a = -(x.reshape(-1,1,3) * x[...,None]) # Magic, to avoid the eye thing, takes all dia

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread Søren Gammelmark
What about this? A = einsum("i,ij->", mass, x ** 2) B = einsum("i,ij,ik->jk", mass, x, x) I = A * eye(3) - B /Søren On 3 February 2012 15:10, wrote: > On Fri, Feb 3, 2012 at 8:44 AM, Alan G Isaac wrote: > > On 2/3/2012 5:16 AM, santhu kumar wrote: > >> x = nX3 vector. > >> mass = nX1 vector >

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread josef . pktd
On Fri, Feb 3, 2012 at 8:44 AM, Alan G Isaac wrote: > On 2/3/2012 5:16 AM, santhu kumar wrote: >> x = nX3 vector. >> mass = nX1 vector >> inert = zeros((3,3)) >> for i in range(n): >>        ri = x[i,:].reshape(1,3) >>        inert = inert + mass[i,]*(sum(ri*ri)*eye(3) - dot(ri.T,ri)) >> > > > Thi

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread Alan G Isaac
On 2/3/2012 5:16 AM, santhu kumar wrote: > x = nX3 vector. > mass = nX1 vector > inert = zeros((3,3)) > for i in range(n): >ri = x[i,:].reshape(1,3) >inert = inert + mass[i,]*(sum(ri*ri)*eye(3) - dot(ri.T,ri)) > This should buy you a bit. xdot = (x*x).sum(axis=1) for (massi,xi,xd

[Numpy-discussion] Trick for fast

2012-02-03 Thread santhu kumar
Hello all, I have tried to optimize most of my code but this ones seems to be the major bottleneck as it gets called many times. I have run out of ideas to make it more faster, can somebody please help me here. x = nX3 vector. mass = nX1 vector inert = zeros((3,3)) for i in range(n): ri = x