How to use Row echelon form In Javascript

In this approach, we follow the row echelon form technique to reduce the matrix to its row echelon 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 row echelon form.

Example: The example below shows how to find the rank of a matrix using the Row echelon form.

JavaScript
function findRank(mat) {
    const rows = mat.length;
    const cols = mat[0].length;
    let rank = cols;

    // Iterate over each row
    for (let row = 0; row < rank; row++) {
        if (mat[row][row] !== 0) {
            for (let i = 0; i < rows; i++) {
                if (i !== row) {
                    const multiplier = mat[i][row] / mat[row][row];
                    for (let j = row; j < cols; j++) {
                        mat[i][j] -= multiplier * mat[row][j];
                    }
                }
            }
        } else {
            let reduce = true;
            for (let i = row + 1; i < rows; i++) {
                if (mat[i][row] !== 0) {
                    [mat[row], mat[i]] = [mat[i], mat[row]];
                    reduce = false;
                    break;
                }
            }

            if (reduce) {
                rank--;
                for (let i = 0; i < rows; i++) {
                    mat[i][row] = mat[i][rank];
                }
            }
            row--;
        }
    }

    return rank;
}
const matrix1 = [
    [10, 20, 10],
    [20, 40, 20],
    [30, 50, 0]
];
console.log("Rank of matrix 1:", findRank(matrix1));

const matrix2 = [
    [10, 20, 10],
    [-20, -30, 10],
    [30, 50, 0]
];
console.log("Rank of matrix 2:", findRank(matrix2)); 

Output
Rank of matrix 1: 2
Rank of matrix 2: 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.

Auxiliary Space: 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

Similar Reads

Using Minor Method

In this approach, we determine the rank of the matrix by analyzing the linear independence of its rows or columns. We start by converting the matrix to reduced row echelon form using elementary row operations and then count the number of non-zero rows or columns. This count gives us the rank of the matrix....

Using Row echelon form

In this approach, we follow the row echelon form technique to reduce the matrix to its row echelon 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 row echelon form....

Using the Normal Form Method

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....

Contact Us