Determinants
The determinant of a square matrix is the signed volume scale of the map it describes. It is zero exactly when the columns are dependent, which is why it decides invertibility. There are several ways to compute it, and comparing them shows why exact computation needs care.
Cofactor expansion
det A = Σ (−1)^(1+j) a₁ⱼ det M₁ⱼ, where M₁ⱼ is A without row 1 and column j. It is the definition most courses start from, but it needs about n! multiplications, so it is only for small matrices.
Elimination
Row operations change the determinant in known ways: a swap flips its sign, adding a multiple of a row changes nothing, scaling a row scales it. Reduce to triangular form and multiply the diagonal: about n³/3 operations.
Fraction-free elimination
Elimination over the rationals creates fractions whose numerators and denominators can grow very fast. Bareiss's method works with integers only: each update is (p·a − b·c) divided by the previous pivot, and that division is always exact because every intermediate entry is a minor of the original matrix (Sylvester's identity). The numbers never grow larger than the minors themselves.
Primes and the Chinese remainder theorem
Hadamard's inequality bounds |det A| by the product of the row lengths. So instead of one computation with large numbers, compute det A modulo several primes, where every number stays small, and combine the residues with the Chinese remainder theorem until the product of the primes passes twice the bound. The library uses primes just below 2²⁶, so all the O(n³) work happens in ordinary floating-point registers; the demo uses small primes so the residues are readable.
In code
import { det, teach } from "@ratmath/linalg";
const H = [["1/2", "1/3", "1/4"], ["1/3", "1/4", "1/5"], ["1/4", "1/5", "1/6"]];
const d = det(H);
d.quick.value; // 0.00002314814814814762, at once
d.quick.trust; // "good"
d.exact().toString(); // "1/43200"
teach.bareiss([[2, 1, 1], [1, 3, 2], [1, 0, 0]]).steps; // the fraction-free steps the demo shows
@ratmath/linalg and Core's Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.