如何在 Java 中檢查矩陣是否為對合矩陣?


矩陣不過是按二維矩形佈局排列的資料元素的集合。在 Java 中,二維陣列可以被視為矩陣。

對合矩陣是一個方形矩陣,當它自身相乘時,會得到一個單位矩陣。單位矩陣是一個矩陣,其中所有元素都為零,除了對角線上的元素為一。

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

舉幾個例子

例項 1

Suppose we have a matrix
     | 1 0 0 |
A  = | 0 1 0 |
     | 0 0 1 |

A2 = A X A

     | 1 0 0 |   | 1 0 0 |
   = | 0 1 0 | X | 0 1 0 |
     | 0 0 1 |   | 0 0 1 |
 
     | 1 0 0 |
A2 = | 0 1 0 | | 0 0 1 |

這是一個對合矩陣。

例項 2

假設我們有一個矩陣

Suppose we have a matrix
     | 1 0 0  |
A  = | 0 -1 0 |
     | 0 0 -1 |
A2 = A X A

| 1 0 0 | | 1 0 0 | = | 0 -1 0 | X | 0 -1 0| | 0 0 -1 | | 0 0 -1| | 1 0 0 | A2 = | 0 1 0 | | 0 0 1 |

這是一個對合矩陣。

演算法

步驟 1 - 初始化並宣告矩陣

步驟 2 - 將矩陣與其自身相乘並存儲結果

步驟 3 - 將乘積矩陣與單位矩陣進行比較,並檢查這兩個矩陣是否相同

步驟 4 - 如果這兩個矩陣相同,則該矩陣是對合矩陣

多種方法

我們提供了多種方法的解決方案。

  • 使用矩陣的靜態初始化

  • 使用矩陣的動態初始化

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

方法 1:使用矩陣的靜態初始化

在這種方法中,矩陣元素將在程式中初始化。然後根據演算法檢查矩陣是否是對合矩陣。

示例

import java.util.Scanner;
public class Main {
   public static void main(String[] args) {
      // Matrix to be checked
      int mat[][] = {{ 1, 0, 0 },{ 0, -1, 0 },{ 0, 0, -1 },};
      // Print results
      if (invoCheck(mat))
         System.out.println("The matrix is an involutory matrix.");
      else
         System.out.println("The matrix is not an involutory matrix.");
   }
   // Matrix multiplication
   static void mul(int mat[][], int prod[][]) {
      for (int i = 0; i < 3; i++) {
         for (int j = 0; j < 3; j++) {
            prod[i][j] = 0;
            
           // Resultant product is stored in prod
            for (int k = 0; k < 3; k++) {
               prod[i][j] += mat[i][k] * mat[k][j];
            }
         }
      }
   }
   // Check if the matrix is involutory
   static boolean invoCheck(int mat[][]) {
      // 3X3 Identity Matrix
      int identityMat[][] = { { 1, 0, 0 },{ 0, 1, 0 },{ 0, 0, 1 } };
      int prod[][] = new int[3][3];
      
      // Calls the matrix multiplication
      mul(mat, prod);
      
      // Checks if the product matrix is an identity matrix
      for (int i = 0; i < 3; i++) {
         for (int j = 0; j < 3; j++) {
            if (identityMat[i][j] != prod[i][j])
            return false;
         }
      }
      return true;
   }
}

輸出

The matrix is an involutory matrix.

方法 2:使用矩陣的動態初始化

在這種方法中,矩陣元素將作為使用者輸入在程式中輸入。然後根據演算法檢查矩陣是否是對合矩陣。

示例

import java.util.Scanner;
public class Main {
   public static void main(String[] args) {
      //Matrix to be checked
      int mat[][] = new int[3][3];
      
      //Take matrix as user input
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter matrix elements:-");
      for (int i = 0; i < 3; i++) {
         for (int j = 0; j < 3; j++) {
            mat[i][j] = sc.nextInt();
      }}
      // Print results
      if (invoCheck(mat))
         System.out.println("The matrix is an involutory matrix.");
      else
        System.out.println("The matrix is not an involutory matrix.");
   }
   // Matrix multiplication
   static void mul(int mat[][], int prod[][]) {
      for (int i = 0; i < 3; i++) {
         for (int j = 0; j < 3; j++) {
            prod[i][j] = 0;
            
            // Resultant product is stored in prod
            for (int k = 0; k < 3; k++) {
               prod[i][j] += mat[i][k] * mat[k][j];
            }
         }
      }
   }
   // Check if the matrix is involutory
   static boolean invoCheck(int mat[][]) {
      // 3X3 Identity Matrix
      int identityMat[][] = { { 1, 0, 0 },{ 0, 1, 0 },{ 0, 0, 1 } };
      int prod[][] = new int[3][3];
      
      // Calls the matrix multiplication
      mul(mat, prod);
      
      // Checks if the product matrix is an identity matrix
      for (int i = 0; i < 3; i++) {
         for (int j = 0; j < 3; j++) {
            if (identityMat[i][j] != prod[i][j])
            return false;
         }
      }
      return true;
   }
}

輸出

Enter matrix elements:-
1 0 0
0 1 0
0 0 1
The matrix is an involutory matrix

在這篇文章中,我們探索了使用 Java 程式語言檢查矩陣是否是對合矩陣的不同方法。

更新於: 2023 年 3 月 6 日

191 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.