[Numpy-discussion] array of matrices

Alexandre Fayolle alexandre.fayolle at logilab.fr
Tue Mar 31 09:50:03 EDT 2009


Le Friday 27 March 2009 23:38:25 Bryan Cole, vous avez écrit :
> I have a number of arrays of shape (N,4,4). I need to perform a
> vectorised matrix-multiplication between pairs of them I.e.
> matrix-multiplication rules for the last two dimensions, usual
> element-wise rule for the 1st dimension (of length N).
>
> (How) is this possible with numpy?

I think dot will work, though you'll need to work a little bit to get the 
answer:

>>> import numpy as np
>>> a = np.array([[1,2], [3,4]], np.float)
>>> aa = np.array([a,a+1,a+2])
>>> bb = np.array((a*5, a*6, a*7, a*8))
>>> np.dot(aa, bb).shape
(3, 2, 4, 2)
>>> for i, a_ in enumerate(aa): 
...     for j, b_ in enumerate(bb):
...         print (np.dot(a_, b_) == np.dot(aa, bb)[i,:,j,:]).all()
... 
True
True
True
True
True
True
True
True
True
True
True
True



-- 
Alexandre Fayolle                              LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian:  http://www.logilab.fr/formations
Développement logiciel sur mesure:       http://www.logilab.fr/services
Informatique scientifique:               http://www.logilab.fr/science



More information about the NumPy-Discussion mailing list