通过函数传递矩阵来实现 Go 语言矩阵乘法


在本教程中,我们将编写一个 Go 语言程序,通过将两个矩阵传递给函数来实现矩阵乘法。为了实现此结果,我们将使用一维和多维矩阵。一维数组和多维矩阵的区别在于前者具有相同的顺序,而后者具有不同的行和列顺序。

方法 1:通过函数传递相同阶矩阵进行乘法

在此方法中,我们将看到如何通过将矩阵传递给用户定义的函数,然后将其输出返回给 main() 函数来实现两个相同阶矩阵的乘法。

算法

步骤 1 − 导入 fmt 包。

步骤 2 − 创建一个名为 MultiplyMatrix() 的函数来乘以给定的矩阵。

步骤 3 − 此函数使用三个 for 循环。在矩阵的每次迭代中,我们通过将两个矩阵的行与列相乘并相加来更新 total 变量。

步骤 4 − 更新 total 变量后,将结果存储在 result 变量的相应位置,并将 total 重置为零,然后重复此过程。

步骤 5 − 所有迭代完成后,返回结果。

步骤 6 − 现在,开始 main() 函数。初始化两个整数类型的矩阵,并将值存储到其中。此外,在屏幕上打印这些矩阵。

步骤 7 − 通过将两个矩阵作为参数传递给 MultiplyMatrix() 函数并存储结果来调用该函数。

步骤 8 − 使用 fmt.Println() 函数在屏幕上打印获得的最终结果。

示例

Go 语言程序,用于乘以两个相同阶矩阵。

Open Compiler
package main import ( "fmt" ) // creating a function to multiply matrices func MultiplyMatrix(matrixA [3][3]int, matrixB [3][3]int) [3][3]int { var total int = 0 var result [3][3]int // multiplying matrices and storing result for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { for k := 0; k < 3; k++ { total = total + matrixA[i][k]*matrixB[k][j] } result[i][j] = total total = 0 } } return result } func main() { // initializing variables var result [3][3]int var i, j int matrixA := [3][3]int{ {0, 1, 2}, {4, 5, 6}, {8, 9, 10}, } matrixB := [3][3]int{ {10, 11, 12}, {13, 14, 15}, {16, 17, 18}, } fmt.Println("The first matrix is:") for i = 0; i < 3; i++ { for j = 0; j < 3; j++ { fmt.Print(matrixA[i][j], "\t") } fmt.Println() } fmt.Println() fmt.Println("The second matrix is:") for i = 0; i < 3; i++ { for j = 0; j < 3; j++ { fmt.Print(matrixB[i][j], "\t") } fmt.Println() } fmt.Println() result = MultiplyMatrix(matrixA, matrixB) // printing final result fmt.Println("The results of multiplication of matrix A & B: ") for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { fmt.Print(result[i][j], "\t") } fmt.Println() } }

输出

The first matrix is:
0  1  2
4  5  6
8  9 10

The second matrix is:
10  11  12
13  14  15
16  17  18

The results of multiplication of matrix A & B:
45    48   51
201   216  231
357   384  411

方法 2:通过函数传递不同阶矩阵进行乘法

在此方法中,我们将编写一个程序,通过将给定的矩阵传递给函数来实现两个不同阶矩阵的乘法。

算法

步骤 1 − 导入 fmt 包。

步骤 2 − 创建一个名为 MultiplyMatrix() 的函数来乘以给定的矩阵。

步骤 3 − 此函数使用三个 for 循环。在矩阵的每次迭代中,我们通过将两个矩阵的行与列相乘并相加来更新 total 变量。

步骤 4 − 更新 total 变量后,将结果存储在 result 的相应位置,并将 total 重置为零,然后重复此过程。

步骤 5 − 所有迭代完成后,返回结果。

步骤 6 − 现在,开始 main() 函数。初始化两个整数类型的矩阵,并将值存储到其中。此外,在屏幕上打印这些矩阵。

步骤 7 − 通过将两个矩阵作为参数传递给 MultiplyMatrix() 函数并存储结果来调用该函数。

步骤 8 − 使用 fmt.Println() 函数打印获得的最终结果。

示例

Go 语言程序,用于通过函数传递来实现两个不同阶矩阵的乘法。

Open Compiler
package main import ( "fmt" ) // creating a function to multiply matrices func MultiplyMatrix(matrixA [3][3]int, matrixB [3][2]int) [3][2]int { var total int = 0 var result [3][2]int for i := 0; i < 3; i++ { for j := 0; j < 2; j++ { for k := 0; k < 3; k++ { total = total + matrixA[i][k]*matrixB[k][j] } result[i][j] = total total = 0 } } return result } func main() { var result [3][2]int var i, j int matrixA := [3][3]int{ {11, 12, 13}, {4, 5, 6}, {15, 16, 17}, } matrixB := [3][2]int{ {0, 4}, {3, 6}, {8, 9}, } fmt.Println("The first matrix is:") for i = 0; i < 3; i++ { for j = 0; j < 3; j++ { fmt.Print(matrixA[i][j], "\t") } fmt.Println() } fmt.Println() fmt.Println("The second matrix is:") for i = 0; i < 3; i++ { for j = 0; j < 2; j++ { fmt.Print(matrixB[i][j], "\t") } fmt.Println() } fmt.Println() result = MultiplyMatrix(matrixA, matrixB) fmt.Println("The results of multiplication of matrix A & B: ") for i := 0; i < 3; i++ { for j := 0; j < 2; j++ { fmt.Print(result[i][j], "\t") } fmt.Println() } }

输出

The first matrix is:
11  12  13
4   5   6
15  16  17

The second matrix is:
0  4
3  6
8  9

The results of multiplication of matrix A & B:
140   233
63    100
184   309

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

结论

我们已经成功编译并执行了一个 Go 语言程序,通过将两个矩阵传递给函数来实现矩阵乘法,并附带示例。在第一个示例中,我们使用了两个相同阶矩阵,而在第二个示例中,我们使用了不同阶矩阵来实现结果。

更新于: 2023年1月6日

424 次浏览

开启您的 职业生涯

通过完成课程获得认证

立即开始
广告