[Numpy-discussion] loop vectorization

qubax at gmx.at qubax at gmx.at
Fri Mar 11 14:07:49 EST 2011


we have had that discussion about ... two days ago. please look up
'How to sum weighted matrices' with at least two efficient solutions.

On Fri, Mar 11, 2011 at 02:00:09PM -0500, Josh Hykes wrote:
>    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 <[1]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:[2]505-665-4323 f:[3]505-665-4267
> 
>    "There cannot be a crisis next week. My schedule is already full."
> 
>    Henry Kissinger
> 
>      _______________________________________________
>      NumPy-Discussion mailing list
>      [4]NumPy-Discussion at scipy.org
>      [5]http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 
>    --
>    Josh Hykes
>    NCSU grad student
>    (717) 742-0264
>    [6]jmhykes at ncsu.edu or [7]jhykes at gmail.com
> 
> References
> 
>    1. mailto:tkg at lanl.gov
>    2. tel:505-665-4323
>    3. tel:505-665-4267
>    4. mailto:NumPy-Discussion at scipy.org
>    5. http://mail.scipy.org/mailman/listinfo/numpy-discussion
>    6. mailto:jmhykes at ncsu.edu
>    7. mailto:jhykes at gmail.com

> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion


-- 
There are two things children should get
from their parents: roots and wings.

The king who needs to remind his people of his rank, is no king.

A beggar's mistake harms no one but the beggar. A king's mistake,
however, harms everyone but the king. Too often, the measure of
power lies not in the number who obey your will, but in the number
who suffer your stupidity.



More information about the NumPy-Discussion mailing list