Java 检查矩阵是否是自合矩阵
矩阵是一种二维的数据元素按照矩形布局排列的集合。在Java中,具有两个维度的数组可以被视为矩阵。
自合矩阵是一个乘以自己后会得到一个单位矩阵的方阵。单位矩阵是一个除了对角线元素为1外,所有元素都为零的矩阵。
让我们深入探讨这篇文章,了解如何使用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编程语言探讨了不同的方法来检查矩阵是否是一个自反矩阵。