Python程序:通过函数传递矩阵来实现两个矩阵的乘法
矩阵是由许多数字以行和列排列组成的二维数组。它被称为m X n矩阵,其中m和n是矩阵的维度。
通常,两个矩阵的乘法只有在第一个矩阵的列数等于第二个矩阵的行数时才可能。
输入输出场景
假设我们有两个输入矩阵A和B,它们都具有3X3行和列。那么结果矩阵也将具有3行和3列。
[a, b, c] [j, k, l] [(a*j+b*m+c*p), (a*k+b*n+c*q), (a*l+b*o+c*r)] [d, e, f] * [m, n, o] = [(d*j+e*m+f*p), (d*k+e*n+f*q), (d*l+e*o+f*r)] [g, h, i] [p, q, r] [(g*j+h*m+i*p), (g*k+h*n+i*q), (g*l+h*o+i*r)]
矩阵的逐元素乘法解释如下。在这种情况下,两个输入矩阵的行数和列数必须相同。
[a, b, c] [j, k, l] [(a*j), (b*k), (c*i)] [d, e, f] * [m, n, o] = [(d*e), (e*n), (f*o)] [g, h, i] [p, q, r] [(g*p), (h*q), (i*r)]
让我们通过将两个矩阵传递给函数来实现矩阵乘法
使用循环
我们将使用Python中的def关键字定义一个用户自定义函数来乘以两个矩阵。在函数中,我们声明了一个零矩阵,它将存储结果矩阵。然后,通过使用for循环迭代两个矩阵的行和列,我们将两个矩阵的元素相乘并将结果存储在结果矩阵中。
示例
在下面的示例中,我们将通过将两个矩阵传递给用户定义的函数来实现矩阵乘法。
def multiply(A,B): result=[[0,0,0],[0,0,0],[0,0,0]] #for rows for i in range(len(A)): #for columns for j in range(len(B[0])): #for rows of matrix B for k in range(len(B)): result[i][j] += A[i][k] * B[k][j] for p in result: print(p) return #function for displaying matrix def display(matrix): for row in matrix: print(row) print() matrix_1 = [[2, 1, 2],[3, 2, 2], [1, 1, 2]] matrix_2 = [[1, 5, 3],[4, 2, 1], [1, 2, 2]] # Display two input matrices print('The first matrix is defined as:') display(matrix_1) print('The second matrix is defined as:') display(matrix_2) print("Result: ") multiply(matrix_1,matrix_2)
输出
The first matrix is defined as: [2, 1, 2] [3, 2, 2] [1, 1, 2] The second matrix is defined as: [1, 5, 3] [4, 2, 1] [1, 2, 2] Result: [8, 16, 11] [13, 23, 15] [7, 11, 8]
在这里,我们将使用一些内置的NumPy函数来计算两个矩阵的乘积。
使用NumPy.matmul()函数
matmul()函数将执行传递给函数的两个输入的矩阵乘法。
示例
首先,我们导入NumPy模块,并应用np.matmul()函数来计算两个矩阵的乘积。
import numpy as np matrix_1 = np.array([[2, 1, 2],[3, 2, 2], [1, 1, 2]]) matrix_2 =np.array([[1, 5, 3],[4, 2, 1], [1, 2, 2]]) #function for displaying matrix def display(matrix): for row in matrix: print(row) print() # Display two input matrices print('The first matrix is defined as:') display(matrix_1) print('The second matrix is defined as:') display(matrix_2) print("Result: ") print(np.matmul(matrix_1 ,matrix_2))
输出
The first matrix is defined as: [2 1 2] [3 2 2] [1 1 2] The second matrix is defined as: [1 5 3] [4 2 1] [1 2 2] Result: [[ 8 16 11] [13 23 15] [ 7 11 8]]
使用NumPy.multiply()函数
它将执行两个输入矩阵的逐元素乘法运算。
示例
首先,我们将使用numpy.array()函数创建两个矩阵。然后,使用np.multiply()函数计算两个矩阵的逐元素乘法。
import numpy as np matrix_1 = np.array([[2, 1, 2],[3, 2, 2], [1, 1, 2]]) matrix_2 =np.array([[1, 5, 3],[4, 2, 1], [1, 2, 2]]) #function for displaying matrix def display(matrix): for row in matrix: print(row) print() # Display two input matrices print('The first matrix is defined as:') display(matrix_1) print('The second matrix is defined as:') display(matrix_2) print("Result: ") print(np.multiply(matrix_1 ,matrix_2))
输出
The first matrix is defined as: [2 1 2] [3 2 2] [1 1 2] The second matrix is defined as: [1 5 3] [4 2 1] [1 2 2] Result: [[ 2 5 6] [12 4 2] [ 1 2 4]]
使用NumPy.dot()函数
dot()函数将对两个输入矩阵执行点积运算。
示例
我们将向np.dot()函数提供两个NumPy数组,以执行两个输入的点积运算。
import numpy as np matrix_1 = np.array([[2, 1, 2],[3, 2, 2], [1, 1, 2]]) matrix_2 =np.array([[1, 5, 3],[4, 2, 1], [1, 2, 2]]) #function for displaying matrix def display(matrix): for row in matrix: print(row) print() # Display two input matrices print('The first matrix is defined as:') display(matrix_1) print('The second matrix is defined as:') display(matrix_2) print("Result: ") print(np.dot(matrix_1 ,matrix_2))
输出
The first matrix is defined as: [2 1 2] [3 2 2] [1 1 2] The second matrix is defined as: [1 5 3] [4 2 1] [1 2 2] Result: [[ 8 16 11] [13 23 15] [ 7 11 8]]
广告