Program to check Involutory Matrix
Last Updated :
19 Aug, 2022
Given a matrix and the task is to check matrix is involutory matrix or not.
Involutory Matrix: A matrix is said to be involutory matrix if matrix multiply by itself return 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
Implementation:
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;
}
|
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" );
}
}
|
Python3
N = 3 ;
def multiply(mat, res):
for i in range (N):
for j in range (N):
res[i][j] = 0 ;
for k in range (N):
res[i][j] + = mat[i][k] * mat[k][j];
return res;
def InvolutoryMatrix(mat):
res = [[ 0 for i in range (N)]
for j in range (N)];
res = multiply(mat, res);
for i in range (N):
for j in range (N):
if (i = = j and res[i][j] ! = 1 ):
return False ;
if (i ! = j and res[i][j] ! = 0 ):
return False ;
return True ;
mat = [[ 1 , 0 , 0 ], [ 0 , - 1 , 0 ], [ 0 , 0 , - 1 ]];
if (InvolutoryMatrix(mat)):
print ( "Involutory Matrix" );
else :
print ( "Not Involutory Matrix" );
|
C#
using System;
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 bool 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 ()
{
int [,]mat = { { 1, 0, 0 },
{ 0, -1, 0 },
{ 0, 0, -1 } };
if (InvolutoryMatrix(mat))
Console.WriteLine( "Involutory Matrix" );
else
Console.WriteLine( "Not Involutory Matrix" );
}
}
|
PHP
<?php
$N = 3;
function multiply( $mat , $res )
{
global $N ;
for ( $i = 0; $i < $N ; $i ++)
{
for ( $j = 0; $j < $N ; $j ++)
{
$res [ $i ][ $j ] = 0;
for ( $k = 0; $k < $N ; $k ++)
$res [ $i ][ $j ] += $mat [ $i ][ $k ] *
$mat [ $k ][ $j ];
}
}
return $res ;
}
function InvolutoryMatrix( $mat )
{
global $N ;
$res ;
for ( $i = 0; $i < $N ; $i ++)
for ( $j = 0; $j < $N ; $j ++)
$res [ $i ][ $j ] = 0;
$res = multiply( $mat , $res );
for ( $i = 0; $i < $N ; $i ++)
{
for ( $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;
}
$mat = array ( array (1, 0, 0),
array (0, -1, 0),
array (0, 0, -1));
if (InvolutoryMatrix( $mat ))
echo "Involutory Matrix" ;
else
echo "Not Involutory Matrix" ;
?>
|
Javascript
<script>
var N = 3;
function multiply(mat, res)
{
for ( var i = 0; i < N; i++)
{
for ( var j = 0; j < N; j++)
{
res[i][j] = 0;
for ( var k = 0; k < N; k++)
res[i][j] += mat[i][k] * mat[k][j];
}
}
}
function InvolutoryMatrix(mat)
{
var res = Array(N).fill(0).map(
x => Array(N).fill(0));
multiply(mat, res);
for ( var i = 0; i < N; i++)
{
for ( var 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 ;
}
var mat = [ [ 1, 0, 0 ],
[ 0, -1, 0 ],
[ 0, 0, -1 ] ];
if (InvolutoryMatrix(mat))
document.write( "Involutory Matrix" );
else
document.write( "Not Involutory Matrix" );
</script>
|
Time Complexity: O(n3)
Auxiliary Space: O(n2), since n2 extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...