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 = , matrix B is an involutory matrix. The identity matrix is represented by the letter .
Dimension | Matrix |
---|---|
2 x 2 | [[4, -1], [15, -4]] |
3 x 3 | [[4, 3, 3], [-1, 0, -1], [-4, -4, -3]] |
The steps of the algorithm are as follows:
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);}}
[4, -1]
[15, -4]
The above matrix is an Involutory Matrix
----------------------
[3, -54]
[245, 543]
The above matrix is not an Involutory Matrix