[Numpy-discussion] Multiplying list of matrices with list of vectors
David Reichert
d.p.reichert@sms.ed.ac...
Thu Sep 30 11:21:05 CDT 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@gmail.com
> wrote:
>
>
> On Wed, Sep 29, 2010 at 1:03 PM, David Reichert <d.p.reichert@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@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/20100930/1fd74c2b/attachment-0001.html
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: not available
Url: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100930/1fd74c2b/attachment-0001.pl
More information about the NumPy-Discussion
mailing list