Php Program to check Involutory Matrix
Last Updated :
22 Dec, 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
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" ;
?>
|
Output :
Involutory Matrix
Time complexity: O(N3)
Auxiliary space: O(N2)
Please refer complete article on Program to check Involutory Matrix for more details!
Share your thoughts in the comments
Please Login to comment...