Open In App

Javascript Program to check Involutory Matrix

Last Updated : 12 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a matrix, the task is to check matrix is an involutory matrix or not. 

Involutory Matrix: A matrix is said to be an involutory matrix if the matrix multiplied by itself returns the identity matrix. The involutory matrix is the matrix that is its inverse. The matrix A is said to be an 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
JavaScript
// Javascript to implement involutory matrix. 
let N = 3;

// Function for matrix multiplication.
function multiply(mat, res) {
    for (let i = 0; i < N; i++) {
        for (let j = 0; j < N; j++) {
            res[i][j] = 0;
            for (let k = 0; k < N; k++)
                res[i][j] += mat[i][k] * mat[k][j];
        }
    }
}

// Function to check involutory matrix.
function InvolutoryMatrix(mat) {
    let res = Array(N).fill(0).map(
        x => Array(N).fill(0));

    // Multiply function call.
    multiply(mat, res);

    for (let i = 0; i < N; i++) {
        for (let 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
let mat = [[1, 0, 0],
[0, -1, 0],
[0, 0, -1]];

// Function call. If function return
// true then if part will execute 
// otherwise else part will execute.
if (InvolutoryMatrix(mat))
    console.log("Involutory Matrix");
else
    console.log("Not Involutory Matrix");

// This code is contributed by 29AjayKumar 

Output
Involutory Matrix

Complexity Analysis:

  • 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!



Next Article
Practice Tags :

Similar Reads