- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# JavaScript Program to check idempotent matrix

An idempotent matrix is a square matrix that has the same number of rows and columns and when we multiply the matrix by itself the result will be equal to the same matrix. We will be given a matrix and we have to find that whether it is an idempotent matrix or not.

**Mathematically ***−*

If the given matrix ix M, then of M to be an idempotent matrix is should follow the property −

M*M = M

## Multiplication of Matrix

Multiplication of a matrix with another matrix produces another matrix and if the given matrix is the square matrix of N*N then the resultant matrix will also be of the same dimensions (N*N).

Each index (i,j) of the result matrix of the multiplication of two matrices A and B is the sum of the multiplicative addition of the jth column of matrix A with the ith column of matrix B.

### Input

Mat = [ [1, 0], [0, 1]]

### Output

Yes, the given matrix is an Idempotent matrix.

### Explanation

For the cell (0,0): We have to multiply [1,0] with [1,0] => 1* 1 + 0 * 0 = 1. For the cell (0,1): We have to multiply [0,1] with [1,0] => 0* 1 + 0 * 1 = 0. For the cell (1,0): We have to multiply [1,0] with [0,1] => 1* 0 + 0 * 1 = 0. For the cell (1,1): We have to multiply [0,1] with [0,1] => 0* 0 + 1 * 1 = 1. So, the final matrix we will get is: [ [1, 0], [0, 1]]

## Approach

We have seen the example and method to find the multiplication of the two matrices, now let us see steps to implement code to find given matrix is idempotent matrix or not.

First we will create a function that will take a single parameter that will be the matrix to be find whether it is an idempotent matrix or not.

We will get the length of the matrix and using it will traverse over each cell of the matrix by using for loop.

At each index or cell, we will get the value present in the current cell of the answer matrix by using the steps describes above.

We will use the for loop to traverse over the current column and row and get their multiplicative sum.

If the current sum is equal to the current index value, then we will move to the next value otherwise we will return false.

On the basis of return value, we will print the statement whether current matrix is idempotent or not.

### Example

// function to check if the current matrix is an Idempotent matrix or not function check(mat){ // getting the size of the given matrix. var n = mat.length; // traversing over the given matrix for(var i = 0;i < n; i++){ for(var j = 0; j<n; j++){ // for the current cell calculating the value present in the resultant matrix // variable to store the current cell value var cur = 0; for(var k = 0; k<n; k++){ cur += mat[k][j]*mat[i][k]; } // if the current value is not equal to value we got for this cell then return false if(cur != mat[i][j]){ return false; } } } // returing true as all the values matched return true; } // defining the matrix var mat = [[2, -2, -4], [-1, 3, 4 ], [1, -2, -3]] console.log("The given matrix is: ") console.log(mat); // calling the function to check if the current matrix is idempotent matrix or not if(check(mat)){ console.log("The given matrix is idempotent matrix") } else { console.log("The given matrix is not idempotent matrix") }

### Time and Space Complexity

The time complexity of the above code is O(N^3) where N is the number of rows of the given matrix. For each cell, we have to multiply the current column with the current row to produce the factor or N and there are a total of N^N cells.

The space complexity of the above code is O(1), as we are not using any extra space to store the matrix.

## Conclusion

In this tutorial, we have implemented a JavaScript program to check if the given matrix is an idempotent matrix or not. An idempotent matrix is a square matrix that has the same number of rows and columns and when we multiply the matrix by itself the result will be equal to the same matrix. We have implemented code in O(N^3) time complexity and work in the O(1) space complexity.