C++ Program to check Involutory Matrix
Last Updated :
19 Dec, 2022
Given a matrix and the task is to check matrix is an involutory matrix or not.
Involutory Matrix: A matrix is said to be an involutory matrix if matrix multiply by itself returns the identity matrix. The involutory matrix is the matrix that is its own inverse. The matrix A is said to be an 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
C++
#include <bits/stdc++.h>
#define N 3
using namespace std;
void multiply( int mat[][N], int res[][N])
{
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];
}
}
}
bool InvolutoryMatrix( int mat[N][N])
{
int res[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 ;
}
int main()
{
int mat[N][N] = { { 1, 0, 0 },
{ 0, -1, 0 },
{ 0, 0, -1 } };
if (InvolutoryMatrix(mat))
cout << "Involutory Matrix" ;
else
cout << "Not Involutory Matrix" ;
return 0;
}
|
Output :
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...