使用多維陣列的 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

結論

我們已經成功編譯並執行了一個 Go 語言程式,使用多維陣列以及示例來乘以兩個矩陣。在第一個示例中,我們在 main() 函式中使用 for 迴圈來實現邏輯,而在第二個示例中,我們使用了外部使用者定義函式。

更新於: 2023年1月6日

1K+ 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告