[Numpy-discussion] loop vectorization

Josh Hykes jmhykes at ncsu.edu
Fri Mar 11 14:00:09 EST 2011


I think you can use tensordot here.  Maybe something like the following:

from numpy.random import random
import numpy as np

ni, nj, nk = 4, 5, 6
bipData = random((ni,nj,nk))

data1 = np.zeros((nk,nk))

# loop
for i in range(nj):
    data1 += np.dot(np.transpose(bipData[:,i,:]), bipData[:,i,:])

# tensordot
axes_list = [(1,2), (1,0)]
data2 = np.tensordot(bipData.T, bipData, axes=axes_list)

diff = np.max((data1 - data2)/data1)
print diff

I hope this helps.

-Josh


On Fri, Mar 11, 2011 at 1:13 PM, Thomas K Gamble <tkg at lanl.gov> wrote:

>  I have the followin loop in my code:
>
>  for i in range(0, nFrames):
>
> data += dot(transpose(bipData[:,i,:]), bipData[:,i,:])
>
> bipData is a 1024x258x256 double precision float array.
>
> The loop takes all of 15 seconds to run on my computer and, with several
> hundred files to process...
>
> Is there a way to do something like:
>
> data = sum(dot(transpose(bipData), bipData))
>
> with dot done on the desired axis of bipData?
>
> This might give a fair speed increase. Or perhaps a different approach I'm
> not seeing?
>
> --
>
> Thomas K. Gamble
>
> Research Technologist, System/Network Administrator
>
> Chemical Diagnostics and Engineering (C-CDE)
>
> Los Alamos National Laboratory
>
> MS-E543,p:505-665-4323 f:505-665-4267
>
> "There cannot be a crisis next week. My schedule is already full."
>
> Henry Kissinger
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>


-- 
Josh Hykes
NCSU grad student
(717) 742-0264
jmhykes at ncsu.edu or jhykes at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110311/7ecd39c3/attachment.html>


More information about the NumPy-Discussion mailing list