Javascript Program to check Involutory Matrix
Last Updated :
27 Mar, 2023
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
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>
|
Output :
Involutory Matrix
Time complexity: O(N3) as three nested loops are executing. Here N is size of rows and columns.
Auxiliary space: O(N2) as res 2d matrix has been created.
Please refer complete article on Program to check Involutory Matrix for more details!
Share your thoughts in the comments
Please Login to comment...