Java Program to check Involutory Matrix
Last Updated :
15 Dec, 2022
Given a matrix and the task is to check matrix is involutory matrix or not.
A matrix is said to be involutory matrix if matrix multiplied by itself returns the identity matrix. Involutory matrix is the matrix that is its own inverse. The matrix A is said to be involutory matrix if A * A = I. Where I is the identity matrix.
Examples:
Input : mat[N][N] = {{1, 0, 0},
{0, -1, 0},
{0, 0, -1}}
Output : Involutory Matrix
Input : mat[N][N] = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}}
Output : Involutory Matrix
Java
import java.io.*;
class GFG {
static int N = 3 ;
static void multiply( int mat[][], int res[][])
{
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < N; j++) {
res[i][j] = 0 ;
for ( int k = 0 ; k < N; k++)
res[i][j] += mat[i][k] * mat[k][j];
}
}
}
static boolean InvolutoryMatrix( int mat[][])
{
int res[][] = new int [N][N];
multiply(mat, res);
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < N; j++) {
if (i == j && res[i][j] != 1 )
return false ;
if (i != j && res[i][j] != 0 )
return false ;
}
}
return true ;
}
public static void main (String[] args)
{
int mat[][] = { { 1 , 0 , 0 },
{ 0 , - 1 , 0 },
{ 0 , 0 , - 1 } };
if (InvolutoryMatrix(mat))
System.out.println ( "Involutory Matrix" );
else
System.out.println ( "Not Involutory Matrix" );
}
}
|
Time complexity: O(N3)
Auxiliary space: O(N2)
Please refer complete article on Program to check Involutory Matrix for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...