Exact-fraction Gauss-Jordan in JavaScript, with every row operation written out
Floating-point row reduction is a bad way to show students (or yourself) the Gauss-Jordan method. A pivot of 1/3 becomes 0.3333333333333333 , the next elimination step drifts, and the “step-by-step” write-up no longer matches the exact arithmetic you would do by hand. I needed the opposite of that for a math site I work on, MathAI GPT (I built it, so treat this as a maintainer’s write-up, not a…
The provided JavaScript library, matrix-rref-steps, offers an exact-fraction Gauss-Jordan elimination method that maintains every entry as an exact BigInt fraction throughout the process. This library prints each row operation step-by-step, allowing students to compare the exact arithmetic with their own manual calculations. The code is MIT-licensed and can be installed directly from GitHub without the need for an npm registry.
The library provides additional functionality, such as calculating 2×2–4×4 cofactor determinants and finding 2×2/3×3 eigenvalues using the characteristic polynomial. Rational roots for eigenvalues are identified exactly, while complex and irrational cases are handled using numeric values. The test suite validates the library's results against sympy.Matrix, confirming its accuracy in 39 instances.
To use this library, one can simply require 'matrix-rref-steps' and call the rrefWithSteps function, passing an array of arrays representing the augmented matrix. The library will return an object containing the reduced row echelon form (rref), the rank of the matrix, and an array of step-by-step operations performed during the elimination process.
The library accepts both decimal and fractional inputs, converting them to their exact fractional equivalents. This means that inputs like 0.75 or "7/4" are both valid and treated as 3/4. The determinantWithSteps function allows users to compute determinants using cofactor expansion along row 1, showing every minor in the process.
Eigenvalue calculations for 2×2 and 3×3 matrices are also provided, along with the characteristic polynomial. The library can find exact rational roots for eigenvalues and, if necessary, fall back to numeric values for complex or irrational cases.
The RREF calculator with row-operation steps, which utilizes this library, is available as a free online tool at RREF calculator with row-operation steps. No account is required to use the calculator. Two other MIT-licensed libraries, vaso81/ti84-stats-steps and vaso81/polynomial-factor-steps, complement this tool by offering additional functionalities, such as normalcdf/invNorm/binomial calculations for TI-84 calculators and polynomial factoring using the rational root theorem.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to