[Numpy-discussion] help using np.einsum for stacked matrix multiplication

Alexander Belopolsky ndarray at mac.com
Wed Oct 29 16:38:20 EDT 2014


On Wed, Oct 29, 2014 at 5:39 AM, Andrew Nelson <andyfaff at gmail.com> wrote:

> I have a 4D array, A, that has the shape (NX, NY, 2, 2).  I wish to
> perform matrix multiplication of the 'NY' 2x2 matrices, resulting in the
> matrix B.  B would have shape (NX, 2, 2).


What you are looking for is dot.reduce and NumPy does not implement that.
You can save an explicit loop by doing reduce(np.dot, matrices).  For
example


In [6] A
Out[6]
array([[[ 1.,  0.],
        [ 0.,  1.]],

       [[ 2.,  0.],
        [ 0.,  2.]],

       [[ 3.,  0.],
        [ 0.,  3.]]])

In [7] reduce(np.dot, A)
Out[7]
array([[ 6.,  0.],
       [ 0.,  6.]])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141029/6d19a597/attachment.html>


More information about the NumPy-Discussion mailing list