使用多维数组的 Go 语言矩阵乘法程序
在本教程中,我们将编写一个 Go 语言程序来乘以两个矩阵。一维数组和多维数组的区别在于前者保存一个属性,而后者在索引处保存另一个数组。此外,多维数组的每个元素都将具有相同的数据类型。
方法 1:在主函数中使用多维数组乘以两个矩阵
在这种方法中,我们将编写一个 Go 语言程序,使用 main() 函数中的 for 循环来乘以两个多维矩阵。
算法
步骤 1 − 导入 fmt 包。
步骤 2 − 现在,开始 main() 函数。初始化两个整数类型矩阵并向其中存储值。此外,在屏幕上打印这些矩阵。
步骤 3 − 要乘以矩阵,请使用三个 for 循环。在矩阵的每次迭代中,通过将两个矩阵的行与列相乘并相加来更新 total 变量。
步骤 4 − 更新 total 变量后,将结果存储在 result 变量的相应位置,将 total 重置为零并重复此过程。
步骤 5 − 使用 fmt.Println() 函数在屏幕上打印获得的最终结果。
示例
使用多维数组将两个矩阵相乘的 Go 语言程序。
package main import "fmt" func main() { // initializing variables var result [3][2]int var i, j, k, total int total = 0 matrixA := [3][3]int{ {0, 1, 2}, {4, 5, 6}, {8, 9, 10}, } matrixB := [3][2]int{ {10, 11}, {13, 14}, {16, 17}, } // printing matrices on the screen 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() } // printing a new line 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() // multiplying matrices and storing result 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 } } // printing result on the screen fmt.Println("Results of matrix multiplication: ") for i = 0; i < 3; i++ { for j = 0; j < 2; j++ { fmt.Print(result[i][j], "\t") } fmt.Println() } fmt.Println() }
输出
The first matrix is: 0 1 2 4 5 6 8 9 10 The second matrix is: 10 11 13 14 16 17 Results of matrix multiplication: 45 48 201 216 357 384
方法 2:在外部函数中使用多维数组乘以两个矩阵
在这种方法中,我们将创建一个用户定义函数来执行两个矩阵的乘法过程。我们创建的函数将接收相应的矩阵作为参数,并在执行乘法后返回最终矩阵,我们可以接收并在屏幕上打印该矩阵。
算法
步骤 1 − 导入 fmt 包。
步骤 2 − 创建一个名为 MultiplyMatrix() 的函数来乘以给定的矩阵。此函数接受两个矩阵作为参数,并将最终矩阵作为结果返回。
步骤 3 − 此函数使用三个 for 循环来实现逻辑。在矩阵的每次迭代中,我们通过将两个矩阵的行与列相乘并相加来更新 total 变量。
步骤 4 − 更新 total 变量后,将结果存储在 result 变量的相应位置,将 total 重置为零并重复此过程。
步骤 5 − 所有迭代完成后,返回结果。
步骤 6 − 现在,开始 main() 函数。初始化两个整数类型矩阵并向其中存储值。此外,在屏幕上打印这些矩阵。
步骤 7 − 通过将两个矩阵作为参数传递给函数并存储结果来调用 MultiplyMatrix() 函数。
步骤 8 − 使用 fmt.Println() 函数在屏幕上打印获得的最终结果。
示例
使用外部函数通过多维数组将两个矩阵相乘的 Go 语言程序
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 // multiplying matrices and storing result 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() { // initializing variables var result [3][2]int var i, j int matrixA := [3][3]int{ {0, 1, 2}, {4, 5, 6}, {8, 9, 10}, } matrixB := [3][2]int{ {10, 11}, {13, 14}, {16, 17}, } // printing matrices on the screen 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() } // printing a new line 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: 0 1 2 4 5 6 8 9 10 The second matrix is: 10 11 13 14 16 17 The results of multiplication of matrix A & B: 45 48 201 216 357 384
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
结论
我们已经成功编译并执行了一个 Go 语言程序,使用多维数组以及示例来乘以两个矩阵。在第一个示例中,我们在 main() 函数中使用 for 循环来实现逻辑,而在第二个示例中,我们使用了外部用户定义函数。