[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 =

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,xdoti)

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread josef . pktd
On Fri, Feb 3, 2012 at 8:44 AM, Alan G Isaac alan.is...@gmail.com 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))

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, josef.p...@gmail.com wrote: On Fri, Feb 3, 2012 at 8:44 AM, Alan G Isaac alan.is...@gmail.com wrote: On 2/3/2012 5:16 AM, santhu kumar wrote: x = nX3

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

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread santhu kumar
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/20120203/d1faa546/attachment-0001.html

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread josef . pktd
___ 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/20120203/d1faa546

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

[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 mailto:how...@renci.org Senior Research Software Developer Renaissance Computing Institute (RENCI)

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 how...@renci.org 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 --

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 how...@renci.org mailto:how...@renci.org a écrit : Is there a method that

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread josef . pktd
On Fri, Feb 3, 2012 at 1:58 PM, santhu kumar mesan...@gmail.com 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

Re: [Numpy-discussion] Trick for fast

2012-02-03 Thread josef . pktd
On Fri, Feb 3, 2012 at 2:33 PM, josef.p...@gmail.com wrote: On Fri, Feb 3, 2012 at 1:58 PM, santhu kumar mesan...@gmail.com 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.

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 4:49 PM, Alan G Isaac alan.is...@gmail.com 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)