Other number systems
Elimination only needs to add, subtract, multiply, divide and tell whether a number is zero. The field module runs Gauss–Jordan, determinants and inverses on any number system given as an object with those operations:
{ zero, one, add, sub, mul, div, neg,
sign(v) → 0 (certainly zero), 1 (certainly not), or null (can't tell) }
Three come with the library. The workbench uses all of them through its Numbers setting.
Integers mod p
field.primeField(p): arithmetic modulo a prime, where every nonzero number has an inverse. A rational a/b maps to a·b⁻¹ mod p, unless p divides b. This is the arithmetic inside the modular determinant and Dixon lifting, and the setting of coding theory and cryptography. A matrix can be invertible over Q yet singular mod p, exactly when p divides its determinant.
Ranges
field.intervalField: entries are rational intervals and results are enclosures, certain to contain every possible answer. Zero tests are where this bites: a pivot range that contains 0 might be zero or not, so sign answers null and elimination reports the pivot as undecided rather than guess. Narrow the ranges, or use solveIntervals, which proves its enclosure with the Krawczyk test.
Real algebraic numbers
field.algebraicField: entries are AlgebraicReals like √2 or an eigenvalue. Each is a polynomial with an interval holding exactly one of its roots. Sums and products come from resultants, the polynomial whose roots are all sums or products of the two numbers' conjugates, followed by refining intervals until only one candidate root fits. Zero tests are exact. Degrees multiply as you go (√2 + √3 needs degree 4), so this suits small matrices.
import { algebraic } from "@ratmath/linalg";
const r2 = algebraic.sqrt(algebraic.fromRational([2n, 1n]));
algebraic.add(r2, algebraic.sqrt(algebraic.fromRational([3n, 1n]))).toString(20);
// "3.14626436994197234232…", the root of x⁴ − 10x² + 1 near 3.146
Complex algebraic numbers
complexField does the same for real and complex algebraic numbers together, so a matrix with entries like i or √2 can be reduced, inverted and multiplied exactly. algebraicMatrix(rows) builds the rows from rationals and algebraic numbers already made. The Matrix class itself still holds rationals only.
import { field, complexField, algebraicMatrix, algebraic } from "@ratmath/linalg";
const r2 = algebraic.sqrt(algebraic.fromRational([2n, 1n]));
const A = algebraicMatrix([[r2, 1], [1, r2]]);
algebraic.format(field.det(A, complexField)); // "1"
field.inverse(A, complexField).map((row) => row.map(algebraic.format)); // [["√2", "−1"], ["−1", "√2"]]
@ratmath/linalg and Core's Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.