Skip to content

feat: add row echelon matrix algorithm #1454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions Maths/RowEchelon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* Given a two dimensional matrix, find its row echelon form.
*
* For more info: https://en.wikipedia.org/wiki/Row_echelon_form
*
* @param {number[[]]} matrix - Two dimensional array of rational numbers.
* @returns {number[[]]} - Two dimensional array of rational numbers (row echelon form).
*
* @example
* const matrix = [
* [2,3,4,5,7],
* [9,8,4,0,9],
* [5,7,4,3,9],
* [3,4,0,2,1]
* ]
*
* const result = rowEchelon(matrix)
*
* // The function returns the corresponding row echelon form:
* // result:
* // [
* // [1, 1.5, 2, 2.5, 3.5],
* // [0, 1, 2.54545, 4.09091, 4.09091],
* // [0, 0, 1, 1.57692, 1.36539],
* // [0, 0, 0, 1, -0.25]
* // ]
*/

const isMatrixValid = (matrix) => {
let numRows = matrix.length
let numCols = matrix[0].length
for (let i = 0; i < numRows; i++) {
if (numCols !== matrix[i].length) {
return false
}
}
if (
!Array.isArray(matrix) ||
matrix.length === 0 ||
!Array.isArray(matrix[0])
) {
return false
}
return true
}

const checkNonZero = (currentRow, currentCol, matrix) => {
let numRows = matrix.length
for (let i = currentRow; i < numRows; i++) {
if (matrix[i][currentCol] !== 0) {
return true
}
}
return false
}

const swapRows = (currentRow, withRow, matrix) => {
let numCols = matrix[0].length
let tempValue = 0
for (let j = 0; j < numCols; j++) {
tempValue = matrix[currentRow][j]
matrix[currentRow][j] = matrix[withRow][j]
matrix[withRow][j] = tempValue
}
}

const selectPivot = (currentRow, currentCol, matrix) => {
let numRows = matrix.length
for (let i = currentRow; i < numRows; i++) {
if (matrix[i][currentCol] !== 0) {
swapRows(currentRow, i, matrix)
return
}
}
}

const scalarMultiplication = (currentRow, factor, matrix) => {
let numCols = matrix[0].length
for (let j = 0; j < numCols; j++) {
matrix[currentRow][j] *= factor
}
}

const subtractRow = (currentRow, fromRow, matrix) => {
let numCols = matrix[0].length
for (let j = 0; j < numCols; j++) {
matrix[fromRow][j] -= matrix[currentRow][j]
}
}

const formatResult = (matrix) => {
let precision = 5
let numRows = matrix.length
let numCols = matrix[0].length
for (let i = 0; i < numRows; i++) {
for (let j = 0; j < numCols; j++) {
matrix[i][j] = parseFloat(matrix[i][j].toFixed(precision))
}
}
}

const rowEchelon = (matrix) => {
if (isMatrixValid(matrix) === false) {
return 'Input is not a valid 2D matrix.'
}

let numRows = matrix.length
let numCols = matrix[0].length
let result = matrix

for (let i = 0, j = 0; i < numRows && j < numCols; ) {
if (checkNonZero(i, j, result) === false) {
j++
continue
}

selectPivot(i, j, result)
let factor = 1 / result[i][j]
scalarMultiplication(i, factor, result)

//..............make bottom elements zero...............
for (let x = i + 1; x < numRows; x++) {
factor = result[x][j]
if (factor === 0) {
continue
}
scalarMultiplication(i, factor, result)
subtractRow(i, x, result)
factor = 1 / factor
scalarMultiplication(i, factor, result)
}
formatResult(result)
i++
}
return result
}

export { rowEchelon }
84 changes: 84 additions & 0 deletions Maths/test/RowEchelon.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { rowEchelon } from '../RowEchelon'
describe('Determinant', () => {
const testCases = [
[
[
[8, 1, 3, 5],
[4, 6, 8, 2],
[3, 5, 6, 8]
],
[
[1, 0.125, 0.375, 0.625],
[0, 1, 1.18182, -0.09091],
[0, 0, 1, -11.0769]
]
],
[
[
[6, 8, 1, 3, 5],
[1, 4, 6, 8, 2],
[0, 3, 5, 6, 8],
[2, 5, 9, 7, 8],
[5, 5, 7, 0, 1]
],
[
[1, 1.33333, 0.16667, 0.5, 0.83333],
[0, 1, 2.1875, 2.8125, 0.4375],
[0, 0, 1, 1.56, -4.28003],
[0, 0, 0, 1, -3.3595],
[0, 0, 0, 0, 1]
]
],
[
[
[1, 3, 5],
[6, 8, 2],
[5, 6, 8],
[7, 9, 9],
[5, 0, 6]
],
[
[1, 3, 5],
[0, 1, 2.8],
[0, 0, 1],
[0, 0, 0],
[0, 0, 0]
]
],
[
[
[8, 1, 3, 5],
[4, 6, 8, 2, 7],
[3, 5, 6, 8]
],
'Input is not a valid 2D matrix.'
],
[
[
[0, 7, 8, 1, 3, 5],
[0, 6, 4, 6, 8, 2],
[0, 7, 3, 5, 6, 8],
[6, 8, 1, 0, 0, 4],
[3, 3, 5, 7, 3, 1],
[1, 2, 1, 0, 9, 7],
[8, 8, 0, 2, 3, 1]
],
[
[1, 1.33333, 0.16667, 0, 0, 0.66667],
[0, 1, 0.66667, 1, 1.33333, 0.33333],
[0, 0, 1, 1.2, 1.99999, -3.4],
[0, 0, 0, 1, 1.3, -1.4],
[0, 0, 0, 0, 1, -2.32854],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0]
]
]
]

test.each(testCases)(
'Should return the matrix in row echelon form.',
(matrix, expected) => {
expect(rowEchelon(matrix)).toEqual(expected)
}
)
})