Related Tags

matrix
data structures
communitycreator

# How to check if a given matrix is an involutory matrix

abhilash

If a matrix multiplied by itself returns the identity matrix, it is said to be an involutory matrix. An involutory matrix is the inverse of its own matrix.

If ${B^2}$ = $I$, matrix B is an involutory matrix. The identity matrix is represented by the letter $I$.

### Examples

Dimension Matrix
2 x 2 [[4, -1], [15, -4]]
3 x 3 [[4, 3, 3], [-1, 0, -1], [-4, -4, -3]]

### Algorithm

The steps of the algorithm are as follows:

1. First, multiply the given matrix by itself. To multiply two matrices, we use the algorithm defined here.
2. Check whether the resultant matrix from Step 1 is an identity matrix. To check whether a given matrix is an identity matrix, use the algorithm defined here.

### Code

import java.util.Arrays;

public class Main {

private static int[][] multiplyMatrix(int numRows1, int numCols1, int[][] matrix1, int numRows2, int numCols2, int[][] matrix2) throws Exception {
if(numCols1 != numRows2){
throw new Exception("Invalid dimensions for matrix multiplication");
}
int[][] result = new int[numRows1][numCols2];
for (int i = 0; i < numRows1; i++) {
for (int j = 0; j < numCols2; j++) {
for (int k = 0; k < numRows2; k++)
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
return result;
}

private static void printMatrix(int[][] matrix){
for(int[] row: matrix){
System.out.println(Arrays.toString(row));
}
}

private static boolean checkIdentityMatrix(int[][] matrix){
for(int i = 0; i < matrix.length; i++)
for(int j = 0; j < matrix[i].length; j++){
if((i == j && matrix[i][j] != 1) || (i != j && matrix[i][j] != 0)) return false;
}
return true;
}

private static void checkInvolutoryMatrix(int[][] matrix) throws Exception {
int[][] result = multiplyMatrix(matrix.length, matrix[0].length, matrix, matrix.length, matrix[0].length, matrix);
printMatrix(matrix);
System.out.printf("The above matrix %s an Involutory Matrix", checkIdentityMatrix(result)?"is": "is not");
}

public static void main(String[] args) throws Exception {
int[][] matrix = {{4, -1},{15, -4}};
checkInvolutoryMatrix(matrix);
System.out.println();
System.out.println("----------------------");
int[][] matrix1 = {{3, -54},{245, 543}};
checkInvolutoryMatrix(matrix1);
}
}

### Expected output

[4, -1]
[15, -4]
The above matrix is an Involutory Matrix
----------------------
[3, -54]
[245, 543]
The above matrix is not an Involutory Matrix


RELATED TAGS

matrix
data structures
communitycreator

CONTRIBUTOR

abhilash
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time