Fast, then exact
Every operation returns a Refinement. Reading job.quick runs a floating-point version at once. The exact version is a generator that does a small unit of work (one pivot, one prime) and then yields, so it can run in slices between frames and stop as soon as nobody needs it.
const job = det(A);
job.quick; // { stage: "float", value: -0.9999999999999998, trust: "good" }
await job.settle({ // runs in 8 ms slices, gives the page a turn in between
signal, // abort when the input changes
onStage: (s) => draw(s),
}); // Rational -1
How far to trust a preview
Each preview carries a trust label. good means the matrix is well conditioned and the digits shown are very likely right. rough means it is ill conditioned: the shape of the answer is right but trailing digits are not. unsure means a decision (is this pivot zero? what is the rank?) fell inside rounding noise, so the exact answer may even be a different kind of answer. A display can show rough previews with fewer digits and mark unsure ones as provisional.
Try the Hilbert-like entries 1/1, 1/2, 1/3 in each row, shifted by one each row, to see trust drop to rough. Make two rows equal to see unsure and a status change once the exact answer lands.
Sliders: live
const update = live((t) => solve(A(t), b), (stage) => draw(stage), { idleMs: 30 });
slider.oninput = () => update(slider.value);
Each update shows the preview immediately. The exact pass starts once the input has been still for idleMs, and a newer update cancels it. The demo on the introduction is exactly this.
Workers
For long exact computations, the exact stage can run in a Web Worker while the preview stays on the page. Results travel as plain data and come back as the same classes, so an eigenvalue from a worker can still be refined.
// worker.js
import { serveLinalg } from "@ratmath/linalg/worker";
serveLinalg(self);
// page
import { workerClient } from "@ratmath/linalg/worker";
const la = workerClient(new Worker("worker.js", { type: "module" }));
const job = la.eigenvalues(A); // job.quick is ready now
const exact = await job.settle();
The three ways to get the exact answer differ in where the work happens. exact() runs to the end at once and freezes the page meanwhile. settle() runs in short slices with a break for the page between them, so drawing continues but the answer takes a little longer. A worker does the work on another thread entirely.
Shared shape
The same Refinement class is used by the RatMath graphics and analytic libraries, so a calculator can drive a graph, a solve and an integral with one loop.
@ratmath/linalg and Core's Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.