Working with np.array arrays in python - how to multiply arrays of matrices of the same length element-by-element?

There is an np. dot method that allows you to multiply a pair of matrices. Is there any syntax or other method that allows you to build an array consisting of the products of matrices with the same indexes on two arrays of matrices of the same length? P.S. it is very simple to implement this in one cycle, but the running time is too long. I would like the multiplication to occur in parallel, preferably using numpy

Author: insolor, 2020-01-19

1 answers

Example:

x = np.array([[[1, 0],[0,1]], [[2,0],[0,2]]])
y = x
print("shape of 'x': {}".format(x.shape))

res = x * y
print(res)

Conclusion:

shape of 'x': (2, 2, 2)

[[[1 0]
  [0 1]]

 [[4 0]
  [0 4]]]

PS if x is a list of matrices of the same dimension, then it can be easily converted into a 3D matrix:

In [90]: x = [np.array([[1, 0],[0,1]]), np.array([[2,0],[0,2]])]

In [91]: x
Out[91]:
[array([[1, 0],
        [0, 1]]), array([[2, 0],
        [0, 2]])]

In [92]: x = np.asarray(x)

In [93]: x
Out[93]:
array([[[1, 0],
        [0, 1]],

       [[2, 0],
        [0, 2]]])

In [94]: x.shape
Out[94]: (2, 2, 2)
 1
Author: MaxU, 2020-01-19 18:45:10