Home >Web Front-end >JS Tutorial >JavaScript program to check idempotent matrix
Impotent matrix is a square matrix with the same number of rows and columns, when we multiply the matrix with itself, the result will be equal to the same matrix. We are going to get a matrix and we have to determine if it is idempotent.
Mathematically−
If a matrix ix M is given, then M is an idempotent matrix and should follow the following properties -
M*M = M
Multiplying one matrix by another matrix produces another matrix, and if the given matrix is a square matrix of N*N, the resulting matrix will also have 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 multiplications of the j-th column of matrix A and the i-th column of matrix B.
Mat = [ [1, 0], [0, 1]]
Yes, the given matrix is an Idempotent matrix.
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]]
We have seen the examples and methods of multiplying two matrices, now let us look at the steps to implement the code to find whether a given matrix is idempotent.
First, we will create a function that will take a single parameter, which will be the matrix to be found, whether it is idempotent or not.
We will get the length of the matrix and use it to iterate through each cell of the matrix via a for loop.
In each index or cell we will get the value present in the current cell of the answer matrix using the above steps.
We will use a for loop to iterate through the current column and row and get their multiplicative sum.
If the current sum is equal to the current index value, we will move to the next value, otherwise false will be returned.
Based on the return value, print the statement whether the current matrix is idempotent.
// 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") }
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 must multiply the current column by the current row to produce a factor, or N, for a total of N^N cells.
The space complexity of the above code is O(1) because we are not using any extra space to store the matrix.
In this tutorial, we implemented a JavaScript program to check whether a given matrix is idempotent. An idempotent matrix is a square matrix with the same number of rows and columns, and when we multiply the matrix with itself, the result will be equal to the same matrix. We implemented the code in O(N^3) time complexity and worked in O(1) space complexity.
The above is the detailed content of JavaScript program to check idempotent matrix. For more information, please follow other related articles on the PHP Chinese website!