Numpy: Multiplying arrays of matrices
Carl Banks
pavlovevidence at gmail.com
Thu Sep 16 06:13:33 CEST 2010
On Sep 14, 4:54 pm, Gregory Ewing <greg.ew... at canterbury.ac.nz> wrote:
> Suppose I have two N+2 dimensional arrays, representing
> N-d arrays of 2-d matrices. I want to perform matrix
> multiplication between corresponding matrices in these
> arrays.
>
> I had thought that dot() might do this, but it appears
> not, because e.g. applying it to two 3-d arrays gives
> a 4-d array, not another 3-d array.
>
> I'd also like to be able to find the inverse of each
> matrix in one of these arrays, but again, inv() doesn't
> do what I want -- it only works on 2-d arrays.
>
> Any thoughts on how to achieve these things using numpy
> functions?
I find for situations like this the best thing I can do is hand code
the bounded operation and use the slicing to handle the arbitrarily
large stuff with slicing.
So,
r[:,:,1,1] = a[:,:,1,1]*b[:,:,1,1] + a[:,:,2,1]*b[:,:,1,2]
r[:,:,1,2] = a[:,:,1,2]*b[:,:,1,1] + a[:,:,2,2]*b[:,:,1,2]
etc.
Carl Banks
More information about the Python-list
mailing list