[Numpy-discussion] Help in speeding up accumulation in a matrix

Charles R Harris charlesr.harris at gmail.com
Sat Jan 29 17:30:01 EST 2011


On Sat, Jan 29, 2011 at 2:01 PM, Nicolas SCHEFFER <
scheffer.nicolas at gmail.com> wrote:

> Hi all,
>
> First email to the list for me, I just want to say how grateful I am
> to have python+numpy+ipython etc... for my day to day needs. Great
> combination of software.
>
> Anyway, I've been having this bottleneck in one my algorithms that has
> been bugging me for quite a while.
> The objective is to speed this part up. I've been doing tons of
> optimization and parallel processing around that piece of code to get
> a decent run time.
>
> The problem is easy. You want to accumulate in a matrix, a weighted
> sum of other matrices. Let's call this function scale_and_add:
> def scale_and_add_re(R,w,Ms):
>    (nb_add,mdim,j)=np.shape(Ms)
>    for i in range(nb_add):
>        R+=w[i]*Ms[i]
>    return R
> This 'for' loop bugs me since I know this will slow things down.
>
>
I'd put the flattened matrices in a stack, weight in place, sum on the first
index, and reshape. Something like

In [1]: m = array([eye(3)]*4).reshape(4,-1)

In [2]: m
Out[2]:
array([[ 1.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.]])

In [3]: w = array([1.,2.,3.,4.])

In [4]: m *= w[:,None]

In [5]: r = m.sum(0).reshape(3,3)

In [6]: r
Out[6]:
array([[ 10.,   0.,   0.],
       [  0.,  10.,   0.],
       [  0.,   0.,  10.]])

This should fit in memory I think, depending of course on how much memory
you have.

<snip>

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110129/80614332/attachment.html>


More information about the NumPy-Discussion mailing list