How to use the Normal Form Method In Javascript
The Normal Form Method involves reducing the matrix to its normal form, where each leading entry of a row is 1, and zeros appear below the leading entry. The rank of the matrix is then determined by the number of non-zero rows in the normal form.
Example: The example below shows how to find the rank of a matrix using the Normal Form Method.
function rank(matrix) {
// Get the number of rows and columns in the matrix
const num_rows = matrix.length;
const num_cols = matrix.length > 0 ? matrix[0].length : 0;
let rank = 0;
for (let i = 0; i < num_rows; i++) {
let pivot_found = false;
// Iterate over each column of the matrix
for (let j = 0; j < num_cols; j++) {
if (matrix[i][j] !== 0) {
pivot_found = true;
rank++; // Increment rank
for (let k = 0; k < num_rows; k++) {
if (k !== i) {
const ratio =
matrix[k][j] / matrix[i][j];
for (let l = 0; l < num_cols; l++) {
matrix[k][l] -= ratio * matrix[i][l];
}
}
}
break;
}
}
if (!pivot_found) {
break;
}
}
return rank;
}
const matrix = [[10, 20, 10],
[-20, -30, 10],
[30, 50, 0]];
console.log("Rank is", rank(matrix));
Output
Rank is 2
Time Complexity: O(M×N×min(M, N)), where M is the number of rows and N is the number of columns in the matrix.
Space Complexity: O(1)
JavaScript Program to Find the Rank of a Matrix
Given a matrix of size M x N, your task is to find the rank of a matrix using JavaScript.
Example:
Input: mat[][] = [[10, 20, 10],
[20, 40, 20],
[30, 50, 0]]
Output: Rank is 2
Explanation: Ist and 2nd rows are linearly dependent.
But Ist and 3rd or 2nd and 3rd are independent.
Input: mat[][] = [[10, 20, 10],
[-20, -30, 10],
[ 30, 50, 0]]
Output: Rank is 2
Explanation: Ist and 2nd rows are linearly independent.
So rank must be atleast 2. But all three rows are linearly dependent
(the first is equal to the sum of the second and third)
so the rank must be less than 3.
Table of Content
- Using Minor Method
- Using Row echelon form
- Using the Normal Form Method
Contact Us