📜  生成两个 NumPy 数组的矩阵乘积

📅  最后修改于: 2022-05-13 01:55:24.358000             🧑  作者: Mango

生成两个 NumPy 数组的矩阵乘积

我们可以将两个矩阵与函数np.matmul(a,b) 相乘。当我们将两个阶数 (m*n) 和 (p*q ) 相乘以获得矩阵乘积时,其输出包含 m 行和 q 列,其中 n 为 n==p 是必要条件。

要将两个矩阵相乘,取第一个数组中的行和第二个数组的列,然后乘以相应的元素。然后添加最终答案的值。假设有两个矩阵 A 和 B。

A = [[A00, A01],
     [A10, A11]]
     
B = [[B00, B01],
     [B10, B11]]

Then the product is calculated as shown below
A*B = [[(A00*B00 + A01*B10), (A00*B01 + A01*B11)],
       [(A10*B00 + A11+B10), (A10*B01 + A11*B11)]]

下面是实现。

Python3
# Importing Library
import numpy as np
  
# Finding the matrix product
arr1 = np.array([[1, 2, 3], [4, 5, 6],
                 [7, 8, 9]])
arr2 = np.array([[11, 12, 13], [14, 15, 16],
                 [17, 18, 19]])
  
matrix_product = np.matmul(arr1, arr2)
print("Matrix Product is ")
print(matrix_product)
print()
  
arr1 = np.array([[2,2],[3,3]])
arr2 = np.array([[1,2,3],[4,5,6]])
  
matrix_product = np.matmul(arr1, arr2)
print("Matrix Product is ")
print(matrix_product)
print()
  
arr1 = np.array([[100,200],[300,400]])
arr2 = np.array([[1,2],[4,6]])
  
matrix_product = np.matmul(arr1, arr2)
print("Matrix Product is ")
print(matrix_product)


输出:

Matrix Product is 
[[ 90  96 102]
 [216 231 246]
 [342 366 390]]

Matrix Product is 
[[10 14 18]
 [15 21 27]]

Matrix Product is 
[[ 900 1400]
 [1900 3000]]