[Numpy-discussion] Multiplying list of matrices with list of vectors

David Reichert d.p.reichert at sms.ed.ac.uk
Thu Sep 30 12:21:05 EDT 2010


Hi,

Thanks for the tip. For small matrices this method seems to be faster,
however, for matrices of
the size I'm using it actually seems to be slower than simply looping...

Regards,
David

On Wed, Sep 29, 2010 at 9:34 PM, Charles R Harris <charlesr.harris at gmail.com
> wrote:

>
>
> On Wed, Sep 29, 2010 at 1:03 PM, David Reichert <d.p.reichert at sms.ed.ac.uk
> > wrote:
>
>> Hi,
>>
>> I have a list of matrices W_k I'd like to multiply with a list of vectors
>> v_k,
>> or another way of looking at it, writing all W_k into a 3d array and all
>> v_k into a 2d matrix/array, I'd like to compute matrix R as
>>
>> R_ik = sum_j W_ijk h_jk.
>>
>>
>> Is there a fast way of doing that in numpy?
>>
>>
> There is a way to do it:
>
> > Some operations on stacks of small matrices are easy to get, for
> instance,
> > +,-,*,/, and matrix multiply. The last is the interesting one. If A and B
> > are stacks of matrices with the same number of dimensions with the
> matrices
> > stored in the last two indices, then
> >
> > sum(A[...,:,:,newaxis]*B[...,newaxis,:,:], axis=-2)
> >
> > is the matrix-wise multiplication of the two stacks. If B is replaced by
> a
> > stack of 1D vectors, x, it is even simpler:
> >
> > sum(A[...,:,:]*x[...,newaxis,:], axis=-1)
> >
> > This doesn't go through BLAS, but for large stacks of small matrices it
> > might be even faster than BLAS because BLAS is kinda slow for small
> > matrices.
>
> It is probably a bit faster but uses more memory than using, say, list
> comprehensions. There is infrastructure in Numpy to build ufuncs for this
> sort of thing but it hasn't been used yet.
>
> Chuck
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100930/1fd74c2b/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100930/1fd74c2b/attachment.ksh>


More information about the NumPy-Discussion mailing list