# Check if a Matrix is Involutory or not in Java?

Matrices are nothing but a collection of data elements arranged in a rectangular layout that is two-dimensional. In Java, an array with two dimensions can be considered as a matrix.

Involutory matrix is a square matrix which when multiplied by itself, gives back an identity matrix. Identity matrix is a matrix in which all elements are zero except the diagonals which are one.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

## To show you some instances

### Instance-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 |


It is an involutory matrix.

### Instance-2

Suppose we have a matrix

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 |


It is an involutory matrix.

### Algorithm

Step 1 − Initialise and declare the matrix

Step 2 − Multiply the matrix with itself and store the resultant

Step 3 − Compare the product matrix with an identity matrix and check if both matrices are the same or not

Step 4 − If both the matrices are the same then the matrix is an involutory matrix

### Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Initialization of Matrix

• By Using Dynamic Initialization of Matrix

Let’s see the program along with its output one by one.

## Approach-1: By Using Static Initialization of Matrix

In this approach, matrix elements will be initialized in the program. Then as per the algorithm check if the matrix is an involutory matrix or not.

### Example

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;
}
}



### Output

The matrix is an involutory matrix.


## Approach-2: By Using Dynamic Initialization of Matrix

In this approach, matrix elements will be taken as user input in the program. Then as per the algorithm check if the matrix is an involutory matrix or not.

### Example

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;
}
}


### Output

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


In this article, we explored different approaches to check if the matrix is an involutory matrix or not by using Java programming language.