Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Mathematical Definition
If the given matrix is M, then for M to be an idempotent matrix it should follow the property:
M * M = M
Matrix Multiplication Overview
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 row of matrix B.
Example: Identity Matrix
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 [1,0] with [0,1] => 1*0 + 0*1 = 0. For the cell (1,0): We have to multiply [0,1] with [1,0] => 0*1 + 1*0 = 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]]
Algorithm Approach
We will implement the following steps to check if a matrix is idempotent:
Create a function that takes the matrix as a parameter to check if it is idempotent
Get the length of the matrix and traverse each cell using nested for loops
At each index (i,j), calculate the value that would be present in the result matrix after multiplication
Use a loop to compute the dot product of row i and column j
Compare the computed value with the original matrix value at that position
Return false if any values don't match, otherwise return true
Implementation
// 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[i][k] * mat[k][j];
}
// if the current value is not equal to value we got for this cell then return false
if(cur != mat[i][j]){
return false;
}
}
}
// returning 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");
}
The given matrix is: [ [ 2, -2, -4 ], [ -1, 3, 4 ], [ 1, -2, -3 ] ] The given matrix is idempotent matrix
Testing with Identity Matrix
// Testing with identity matrix (always idempotent)
var identityMatrix = [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]];
console.log("Testing identity matrix:");
console.log(identityMatrix);
if(check(identityMatrix)){
console.log("The identity matrix is idempotent matrix");
}
else {
console.log("The identity matrix is not idempotent matrix");
}
Testing identity matrix: [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ] The identity matrix is idempotent matrix
Time and Space Complexity
The time complexity of the above code is O(N³) where N is the number of rows of the given matrix. For each cell, we have to multiply the current row with the current column producing a factor of N, and there are a total of 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 implemented a JavaScript program to check if a given matrix is idempotent. An idempotent matrix satisfies the property M × M = M, and our solution efficiently verifies this by computing matrix multiplication and comparing results with the original matrix.
