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

Charles R Harris charlesr.harris@gmail....
Wed Sep 29 20:34:36 CDT 2010

On Wed, Sep 29, 2010 at 1:03 PM, David Reichert

> 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
> 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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100929/424991a9/attachment.html 

More information about the NumPy-Discussion mailing list