Numpy: Multiplying arrays of matrices

G

Gregory Ewing

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?
 
C

Colin J. Williams

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?


There is a Matrix sub-class which permit you to do that sort of thimg.

Colin W.
 
R

Robert Kern

There is a Matrix sub-class which permit you to do that sort of thimg.

No, it doesn't.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
C

Carl Banks

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
 
A

Andre Alexander Bell

Hi,

I assume you have arrays like these:

So that m1[0] is a 3x3 Matrix and m1[1] is another one, i.e. you have
four matrices.

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.

You now want to compute the matrixproducts like this

and most likely you want to do this for all of the pairs

or you could write the loop
>>> m1m2 = np.empty_like(m1)
>>> for i in range(m1m2.shape[0]):
.... m1m2 = np.dot(m1, m2)

which might scale better
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.

Same as before

or writing the loop
>>> m1inv = np.empty_like(m1)
>>> for i in range(m1inv.shape[0]):
.... m1inv = np.linalg.inv(m1)

Once again, I'm not sure whether or not it is acceptable to have the
overhead of treating the array as a list.


Andre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top