简体   繁体   中英

Elementwise multiplication of NumPy arrays of matrices

I have two NumPy arrays (of equal length), each with (equally-sized, square) NumPy matrices as elements. I want to do elementwise matrix multiplication of these two arrays, ie get back a single array where the i-th element is the matrix product of the i-th elements of my two arrays.

When I simply try to multiply the arrays together, it seems that the program tries to calculate the matrix product of the arrays , and then fails because their dimensionality is too high (1 for the array + 2 for the matrices which are its elements).

The problem could of course be solved with a for-loop, but I was hoping that there was some way in which it could be done that keeps everything internal to NumPy, in order to take full advantage of its increased efficiency.f

EDIT:

To clarify, say I have two arrays np.array([A, B, C]) and np.array([X, Y, Z]) where A , B , C , X , Y and Z are all 3x3 square matrices, what I need is a function that will return np.array([A*X, B*Y, C*Z]) , where * is matrix multiplication.

Operators are "element-wise" by default for numpy arrays. Just use the @ operator (matrix multiplication) instead of * :

In [24]: A = np.arange(9).reshape(3,3)

In [25]: X = np.array([A[:], A[:]*2, A[:]*3])

In [26]: Y = X[:]

In [27]: X @ Y
Out[27]:
array([[[ 15,  18,  21],
        [ 42,  54,  66],
        [ 69,  90, 111]],

       [[ 60,  72,  84],
        [168, 216, 264],
        [276, 360, 444]],

       [[135, 162, 189],
        [378, 486, 594],
        [621, 810, 999]]])

In [28]: X[0] @ Y[0]
Out[28]:
array([[ 15,  18,  21],
       [ 42,  54,  66],
       [ 69,  90, 111]])

In [29]: X[1] @ Y[1]
Out[29]:
array([[ 60,  72,  84],
       [168, 216, 264],
       [276, 360, 444]])

In [30]: X[2] @ Y[2]
Out[30]:
array([[135, 162, 189],
       [378, 486, 594],
       [621, 810, 999]])

HTH.

* in numpy will do elementwise operations, ie:

>>> a
array([[[0.86812606, 0.16249293, 0.61555956],
        [0.12381998, 0.84800823, 0.80731896],
        [0.56910074, 0.4071833 , 0.069167  ]],

       [[0.69742877, 0.45354268, 0.7220556 ],
        [0.86638233, 0.97552151, 0.85580334],
        [0.01171408, 0.35997806, 0.72999056]]])

>>> b
array([[[0.17162968, 0.52103661, 0.05433799],
        [0.19999652, 0.01852179, 0.7936977 ],
        [0.22392469, 0.34535168, 0.92808129]],

       [[0.7044144 , 0.03183893, 0.16469416],
        [0.6214784 , 0.57722859, 0.23789282],
        [0.934214  , 0.61396596, 0.5356328 ]]])

>>> a * b
array([[[0.1489962 , 0.08466477, 0.03344827],
        [0.02476357, 0.01570663, 0.6407672 ],
        [0.12743571, 0.14062144, 0.06419259]],

       [[0.49127887, 0.01444031, 0.11891834],
        [0.5384379 , 0.5630989 , 0.20358947],
        [0.01094346, 0.22101428, 0.39100689]]])

Isn't this what you looking for?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM