How to use Minor Method In Javascript

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.

Example: The example below shows how to find the rank of a matrix using the Minor Method.

JavaScript
function det(matrix) {
    if (matrix.length === 1) {
        return matrix[0][0];
    }
    if (matrix.length === 2) {
        return matrix[0][0] * matrix[1][1] - 
               matrix[0][1] * matrix[1][0];
    }
    let determinant = 0; // Renamed to avoid conflict
    for (let i = 0; i < matrix.length; i++) {
        const sign = i % 2 === 0 ? 1 : -1;
        const minor = matrix.slice(1).map(row => 
                    row.filter((_, j) => j !== i));
        determinant += sign * matrix[0][i] * det(minor);
    }
    return determinant;
}

function rank(matrix) {
    const numRows = matrix.length;
    const numCols = matrix[0].length;
    const minDim = Math.min(numRows, numCols);
    let rank = 0;
    for (let i = 1; i <= minDim; i++) {
        const submatrices = genSubs(matrix, i);
        for (const submatrix of submatrices) {
            if (det(submatrix) !== 0) {
                rank = i;
                break;
            }
        }
        if (rank !== i) {
            break;
        }
    }
    return rank;
}

function genSubs(matrix, size) {
    const submatrices = [];
    for (let i = 0; i <= matrix.length - size; i++) {
        for (let j = 0; j <= matrix[0].length - size; j++) {
            const submatrix = [];
            for (let k = i; k < i + size; k++) {
                submatrix.push(matrix[k].slice(j, j + size));
            }
            submatrices.push(submatrix);
        }
    }
    return submatrices;
}

const matrix = [
    [10, 20, 10],
    [20, 40, 20],
    [30, 50, 0]
];
console.log("Rank is", rank(matrix));

Output
Rank is 2

Time Complexity: O(M3N3) where M is the number of rows and N is the number of columns in the matrix.

Space Complexity: O(M2N2)

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