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