|
| 1 | +/** |
| 2 | + * Given a two dimensional matrix, find its row echelon form. |
| 3 | + * |
| 4 | + * For more info: https://en.wikipedia.org/wiki/Row_echelon_form |
| 5 | + * |
| 6 | + * @param {number[[]]} matrix - Two dimensional array of rational numbers. |
| 7 | + * @returns {number[[]]} - Two dimensional array of rational numbers (row echelon form). |
| 8 | + * |
| 9 | + * @example |
| 10 | + * const matrix = [ |
| 11 | + * [2,3,4,5,7], |
| 12 | + * [9,8,4,0,9], |
| 13 | + * [5,7,4,3,9], |
| 14 | + * [3,4,0,2,1] |
| 15 | + * ] |
| 16 | + * |
| 17 | + * const result = rowEchelon(matrix) |
| 18 | + * |
| 19 | + * // The function returns the corresponding row echelon form: |
| 20 | + * // result: |
| 21 | + * // [ |
| 22 | + * // [1, 1.5, 2, 2.5, 3.5], |
| 23 | + * // [0, 1, 2.54545, 4.09091, 4.09091], |
| 24 | + * // [0, 0, 1, 1.57692, 1.36539], |
| 25 | + * // [0, 0, 0, 1, -0.25] |
| 26 | + * // ] |
| 27 | + */ |
| 28 | + |
| 29 | +// Set a tolerance value for floating-point comparisons |
| 30 | +const tolerance = 0.000001 |
| 31 | + |
| 32 | +// Check if all the rows have same length of elements |
| 33 | +const isMatrixValid = (matrix) => { |
| 34 | + let numRows = matrix.length |
| 35 | + let numCols = matrix[0].length |
| 36 | + for (let i = 0; i < numRows; i++) { |
| 37 | + if (numCols !== matrix[i].length) { |
| 38 | + return false |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Check for input other than a 2D matrix |
| 43 | + if ( |
| 44 | + !Array.isArray(matrix) || |
| 45 | + matrix.length === 0 || |
| 46 | + !Array.isArray(matrix[0]) |
| 47 | + ) { |
| 48 | + return false |
| 49 | + } |
| 50 | + return true |
| 51 | +} |
| 52 | + |
| 53 | +const checkNonZero = (currentRow, currentCol, matrix) => { |
| 54 | + let numRows = matrix.length |
| 55 | + for (let i = currentRow; i < numRows; i++) { |
| 56 | + // Checks if the current element is not very near to zero. |
| 57 | + if (!isTolerant(0, matrix[i][currentCol], tolerance)) { |
| 58 | + return true |
| 59 | + } |
| 60 | + } |
| 61 | + return false |
| 62 | +} |
| 63 | + |
| 64 | +const swapRows = (currentRow, withRow, matrix) => { |
| 65 | + let numCols = matrix[0].length |
| 66 | + let tempValue = 0 |
| 67 | + for (let j = 0; j < numCols; j++) { |
| 68 | + tempValue = matrix[currentRow][j] |
| 69 | + matrix[currentRow][j] = matrix[withRow][j] |
| 70 | + matrix[withRow][j] = tempValue |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Select a pivot element in the current column to facilitate row operations. |
| 75 | +// Pivot element is the first non-zero element found from the current row |
| 76 | +// down to the last row. |
| 77 | +const selectPivot = (currentRow, currentCol, matrix) => { |
| 78 | + let numRows = matrix.length |
| 79 | + for (let i = currentRow; i < numRows; i++) { |
| 80 | + if (matrix[i][currentCol] !== 0) { |
| 81 | + swapRows(currentRow, i, matrix) |
| 82 | + return |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Multiply each element of the given row with a factor. |
| 88 | +const scalarMultiplication = (currentRow, factor, matrix) => { |
| 89 | + let numCols = matrix[0].length |
| 90 | + for (let j = 0; j < numCols; j++) { |
| 91 | + matrix[currentRow][j] *= factor |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// Subtract one row from another row |
| 96 | +const subtractRow = (currentRow, fromRow, matrix) => { |
| 97 | + let numCols = matrix[0].length |
| 98 | + for (let j = 0; j < numCols; j++) { |
| 99 | + matrix[fromRow][j] -= matrix[currentRow][j] |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Check if two numbers are equal within a given tolerance |
| 104 | +const isTolerant = (a, b, tolerance) => { |
| 105 | + const absoluteDifference = Math.abs(a - b) |
| 106 | + return absoluteDifference <= tolerance |
| 107 | +} |
| 108 | + |
| 109 | +const rowEchelon = (matrix) => { |
| 110 | + // Check if the input matrix is valid; if not, throw an error. |
| 111 | + if (!isMatrixValid(matrix)) { |
| 112 | + throw new Error('Input is not a valid 2D matrix.') |
| 113 | + } |
| 114 | + |
| 115 | + let numRows = matrix.length |
| 116 | + let numCols = matrix[0].length |
| 117 | + let result = matrix |
| 118 | + |
| 119 | + // Iterate through the rows (i) and columns (j) of the matrix. |
| 120 | + for (let i = 0, j = 0; i < numRows && j < numCols; ) { |
| 121 | + // If the current column has all zero elements below the current row, |
| 122 | + // move to the next column. |
| 123 | + if (!checkNonZero(i, j, result)) { |
| 124 | + j++ |
| 125 | + continue |
| 126 | + } |
| 127 | + |
| 128 | + // Select a pivot element and normalize the current row. |
| 129 | + selectPivot(i, j, result) |
| 130 | + let factor = 1 / result[i][j] |
| 131 | + scalarMultiplication(i, factor, result) |
| 132 | + |
| 133 | + // Make elements below the pivot element zero by performing |
| 134 | + // row operations on subsequent rows. |
| 135 | + for (let x = i + 1; x < numRows; x++) { |
| 136 | + factor = result[x][j] |
| 137 | + if (isTolerant(0, factor, tolerance)) { |
| 138 | + continue |
| 139 | + } |
| 140 | + scalarMultiplication(i, factor, result) |
| 141 | + subtractRow(i, x, result) |
| 142 | + factor = 1 / factor |
| 143 | + scalarMultiplication(i, factor, result) |
| 144 | + } |
| 145 | + i++ |
| 146 | + } |
| 147 | + return result |
| 148 | +} |
| 149 | + |
| 150 | +export { rowEchelon } |
0 commit comments