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