通过向函数传递矩阵,实现两个矩阵相乘的Golang程序

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

方法1:将两个相同阶数的矩阵传递给一个函数,使其相乘

在这个方法中,我们将看到将两个同阶的矩阵绕过矩阵到一个用户定义的函数,然后将其输出返回给main()函数,进行乘法。

算法

第1步 – 导入fmt包。

第2步 – 创建一个名为MultiplyMatrix()的函数,将给定的矩阵相乘。

第3步 – 这个函数使用了三个for循环。在矩阵的每一次迭代中,我们通过将两个矩阵的行与列相乘和相加来更新总变量。

第4步 – 在更新总数变量后,将结果存储在结果变量的相应位置,将总数重新初始化为零,并重复这一过程。

第5步 – 一旦所有的迭代完成,返回结果。

第6步 – 现在,启动main()函数。初始化两个整数类型的矩阵,并向它们存储数值。此外,在屏幕上打印这些矩阵。

第7步 – 调用MultiplyMatrix()函数,将两个矩阵作为参数传给该函数,并存储结果。

第8步 – 使用fmt.Println()函数将得到的最终结果打印在屏幕上。

示例

Golang程序将两个相同阶数的矩阵相乘。

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循环。在矩阵的每一次迭代中,我们通过将两个矩阵的行与列相乘和相加来更新总变量。

第4步 – 在更新总变量后,将结果存储在结果中的相应位置,将总变量重新初始化为零,并重复这一过程。

第5步 – 一旦所有的迭代完成,返回结果。

第6步 – 现在,启动main()函数。初始化两个整数类型的矩阵,并向它们存储数值。此外,在屏幕上打印这些矩阵。

第7步 – 调用MultiplyMatrix()函数,将两个矩阵作为参数传给该函数,并存储结果。

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

示例

Golang程序通过传递给一个函数将两个不同阶数的矩阵相乘。

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 结论

我们已经成功地编译并执行了一个go语言程序,通过将两个矩阵传递给一个函数来进行乘法运算,同时还有一些例子。在第一个例子中,我们使用了两个相同顺序的矩阵,而在第二个例子中,我们使用不同顺序的矩阵来实现这个结果。