Inverses and fast exact solving
Row reduce the augmented matrix [A | I]. If the left half becomes I, the same operations applied to I built A⁻¹ on the right. If a pivot is missing, A is singular.
Guess, then check
Calculator problems usually have answers with small denominators. So the library first solves in floating point, turns each value into the nearest simple fraction with continued fractions, and checks A·x = b exactly. The check takes about n² exact operations, much less than elimination, and a modular determinant test proves A is nonsingular so the answer is the only one. A guess is never returned without the check.
p-adic lifting
For larger systems whose answers are not simple, the library inverts A once modulo a prime p and then peels off the solution one base-p digit at a time: xᵢ = A⁻¹r mod p, then r ← (r − A·xᵢ)/p. After k digits the partial sum satisfies A·X ≡ b mod pᵏ, and rational reconstruction (the extended Euclidean algorithm, stopped halfway) turns it into fractions. An exact check decides when enough digits have been found. This is Dixon's method; it beats elimination from about 60×60 upward.
The step-by-step version uses p = 97 so the digits are readable. The library uses a prime near 2²⁶, so each digit carries about 26 bits.
In code
import { inverse, norm, cond, algebraic } from "@ratmath/linalg";
const A = [[4, 7], [2, 6]];
inverse(A).exact().toStrings(); // [["3/5","-7/10"], ["-1/5","2/5"]]
inverse([[1, 2], [2, 4]]).exact(); // null: singular
// How much an inverse can amplify an error, exactly
algebraic.format(norm(A, 1).exact()); // "13"
algebraic.format(cond(A, 1).exact()); // "143/10"
algebraic.format(cond(A).exact()); // "(21 + 5√17)/4"
cond(A).quick.value; // 10.403882032022075
@ratmath/linalg and Core's Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.