Java 中查詢下三角矩陣和上三角矩陣的乘積


在 Java 中,陣列是一個物件。它是一種非原始資料型別,用於儲存相同資料型別的值。Java 中的矩陣只不過是一個多維陣列,它表示多行和多列。

  • 三角矩陣 - 如果方陣中對角線以上或以下的所有元素都為零,則該方陣稱為三角矩陣。

  • 下三角矩陣 - 如果方陣中對角線以上的所有元素都為零,則該方陣稱為下三角矩陣。

  • 上三角矩陣 - 如果方陣中對角線以下的所有元素都為零,則該方陣稱為上三角矩陣。

根據問題陳述,我們必須找到給定的下三角矩陣和上三角矩陣之間的乘積。

公式

矩陣 1

A	B
C	D

矩陣 2

	
E	F
G	H

乘積

Matrix-1 * Matrix-2: 	
AE+BG	      AF+BH
CE+DG	      CF+DH

讓我們深入研究本文,瞭解如何使用 Java 程式語言來實現它。

向您展示一些示例

示例 1

假設下三角矩陣 =

{{12, 0, 0},
{43, 3, 0},
{3, 8, 9}}

假設上三角矩陣 =

{{5, 2, 1},
{0, 7, 10},
{0, 0, 4}}

這兩個矩陣的乘積為

60    24    12    
215   107   73    
15    62    119

示例 2

假設下三角矩陣 =

{{1, 0, 0},
{2, 3, 0},
{4, 5, 6}}

假設上三角矩陣 =

{{7, 8, 9},
{0, 10, 11},
{0, 0, 12}}

這兩個矩陣的乘積為

7    8    9    
14    46    51    
28    82    163

演算法

  • 步驟 1 - 宣告並初始化兩個整數型別多維陣列。

  • 步驟 2 - 使用巢狀 for 迴圈根據公式執行乘積過程。

  • 步驟 3 - 在每次計算後,將元素儲存在乘積矩陣陣列的相應行和列中。

  • 步驟 4 - 列印計算出的乘積矩陣作為輸出。

語法

要獲取陣列的長度(陣列中的元素數量),陣列有一個內建屬性,即length

下面是它的語法:

array.length

其中,“陣列”指的是陣列引用。

多種方法

我們提供了不同方法的解決方案。

  • 使用陣列元素的靜態初始化

  • 使用使用者定義的方法

讓我們逐一檢視程式及其輸出。

方法 1:使用陣列元素的靜態初始化

在這種方法中,多維陣列元素將在程式中初始化。然後根據演算法,下三角矩陣和上三角矩陣的乘積將作為輸出列印。

示例

public class Main {
   public static void main(String[] args) {
      int r, c;
      
      // Declare and initializing two matrices
      int[][] LowerTriMat = {{12, 0, 0},
         {43, 3, 0},
         {3, 8, 9}};
      int[][] upperTriMat = {{5, 2, 1},
         {0, 7, 10},
         {0, 0, 4}};
      
      // Calculating the number of rows and columns present in matrices
      r = LowerTriMat.length;
      c = LowerTriMat[0].length;
      
      // Declare the product matrix
      int[][] prodMatrix = new int[r][c];
      
      //initiating the loop to find the product of two matrices
      for(int i = 0; i < r; i++) {
         for (int j = 0; j < c; j++) {
            for (int k = 0; k < c; k++) {
               prodMatrix[i][j] += LowerTriMat[i][k] * upperTriMat[k][j];
            }
         }
      }
      
      // output result
      System.out.println("The Product matrix of two matrices is: ");
      for(int[] row : prodMatrix) {
         for (int column : row) {
            System.out.print(column + "    ");
         }
         System.out.println();
      }
   }
}

輸出

The Product matrix of two matrices is: 
60     24     12    
215    107    73    
15     62    119    

方法 2:使用使用者定義的方法

在這種方法中,陣列元素將在程式中初始化。然後透過將陣列作為引數呼叫使用者定義的方法,並在方法內部根據演算法,下三角矩陣和上三角矩陣的乘積將作為輸出列印。

程式

public class Main {
   public static void main(String[] args){
      
      // Declare and initializing two matrices
      int[][] LowerTriangularMatrix = { {1, 0, 0},
         {2, 3, 0},
         {4, 5, 6} };
      int[][] upperTriangularMatrix = { {7, 8, 9},
         {0, 10, 11},
         {0, 0, 12} };
      productMat(LowerTriangularMatrix, upperTriangularMatrix);
   }  
   public static void productMat(int[][] LowerTriMat,int[][] upperTriMat) {
      
      // declare the variables 
      //and store the number of rows and columns present in matrices
      int r = LowerTriMat.length;
      int c = LowerTriMat[0].length;
      
      // Declare and initialize the product matrix
      int[][] prodMatrix = new int[r][c];
      
      //initiating the loop to find the product of two matrices
      for(int i = 0; i < r; i++) {
         for (int j = 0; j < c; j++) {
            for (int k = 0; k < c; k++) {
               prodMatrix[i][j] += LowerTriMat[i][k] * upperTriMat[k][j];
            }
         }
      }
      
      // output result
      System.out.println("The Product matrix of two matrices is: ");
      for(int[] row : prodMatrix){
         for (int column : row) {
            System.out.print(column + "    ");
         }
         System.out.println();
      }
   }
}

輸出

The Product matrix of two matrices is: 
7      8      9    
14    46     51    
28    82    163    

在本文中,我們探討了使用 Java 程式語言查詢下三角矩陣和上三角矩陣之間乘積的不同方法。

更新於: 2023 年 5 月 4 日

127 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告