Multiplying list of matrices with list of vectors
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? Regards, David The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
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 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
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 wrote: On Wed, Sep 29, 2010 at 1:03 PM, David Reichert 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 The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.
participants (2)
-
Charles R Harris
-
David Reichert