Wednesday, September 25, 2024
Google search engine
HomeLanguagesPhp Program to check Involutory Matrix

Php Program to check Involutory Matrix

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. 
 

Involutory-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
// Program to implement
// involutory matrix.
 
$N = 3;
 
// Function for matrix
// multiplication.
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 to check
// involutory matrix.
function InvolutoryMatrix($mat)
{
    global $N;
    $res;
    for ($i = 0; $i < $N; $i++)
        for ($j = 0; $j < $N; $j++)
            $res[$i][$j] = 0;
 
    // multiply function call.
    $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;
}
 
// Driver Code
$mat = array(array(1, 0, 0),
             array(0, -1, 0),
             array(0, 0, -1));
 
// Function call. If function
// return true then if part
// will execute otherwise
// else part will execute.
if (InvolutoryMatrix($mat))
    echo "Involutory Matrix";
else
    echo "Not Involutory Matrix";
 
// This code is contributed by mits
?>


Output : 

Involutory Matrix

Time complexity: O(N3)

Auxiliary space: O(N2)

Please refer complete article on Program to check Involutory Matrix for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments