Golang 通过将矩阵传递给函数来计算两个矩阵的乘法
在本教程中,我们将编写一个Go语言程序,通过将两个矩阵传递给函数来计算它们的乘积。为了实现这个结果,我们将使用单维和多维矩阵。单维数组和多维矩阵的区别在于前者具有相同的行和列的顺序,而后者具有不同的行和列的顺序。
方法1:通过将两个具有相同顺序的矩阵传递给函数来计算它们的乘积
在该方法中,我们将看到如何通过将矩阵传递给用户定义的函数来计算两个具有相同顺序的矩阵的乘积,然后将其输出返回给main()函数。
步骤
步骤1 - 导入fmt包。
步骤2 - 创建一个名为MultiplyMatrix()的函数来计算给定的矩阵的乘积。
步骤3 - 该函数使用三个for循环。在矩阵的每次迭代中,我们通过将两个矩阵的行与列相乘并相加来更新total变量。
步骤4 - 在更新total变量后,将结果存储在result变量的相应位置上,将total重新初始化为零,并重复该过程。
步骤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 - 此函数使用三个循环。在矩阵的每次迭代中,我们通过将两个矩阵的行与列相乘并相加,更新total变量。
步骤4 - 在更新total变量后,将结果存储在相应的位置,将total重新初始化为零,并重复该过程。
步骤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语言程序,用于计算矩阵相乘。在第一个示例中,我们使用了两个相同阶数的矩阵,而在第二个示例中,我们使用了阶数不同的矩阵来实现结果。