@ratmath/linalg

Exact answers, without the wait

A JavaScript linear algebra library for exact rational arithmetic. Every computation answers twice: a floating-point preview in about a millisecond, then the exact answer, which is the one that stays.

Move the slider. The preview follows your hand, computed in ordinary floating point. When you stop, the exact solution arrives as fractions. If you keep moving, the exact work for positions you have already left is dropped, so nothing piles up.

Why both

Exact arithmetic is the point of RatMath: 1/3 stays 1/3, a singular matrix is singular for certain, and a determinant is an integer you can trust. But exact arithmetic costs more, and someone dragging a slider wants a picture now, not a proof. So the library gives each operation a fast form and an exact form, and a small protocol, the Refinement, that lets a page show the first and settle on the second.

Some answers are not rational, such as eigenvalues like √3 or entries of exp(A). The library still gives them exactly: as a polynomial together with an interval holding exactly one of its roots, or as an interval certain to contain the true value. You choose how many digits to see.

Two ways to use it

The main functions are built for computing: they are fast and return values. The teach module is built for explaining. It runs the textbook algorithms one step at a time and describes each step in words. Every topic page below uses both: the panel on the right of each demo is the fast library, and Step by step opens the teaching form.

import { solve, det, eigenvalues, teach } from "@ratmath/linalg";

const job = solve([[2, 1, 1], [1, 3, 2], [1, 0, 0]], [4, 5, 6]);
job.quick.value.x;    // [[6], [15.000000000000004], [-23.000000000000004]]
job.exact().x;        // Matrix [[6], [15], [-23]]

det([["1/2", "1/3"], ["1/4", "1/5"]]).exact();   // Rational 1/60
eigenvalues([[1, 1], [1, 0]]).exact().real[1].value.toString(30);
                      // "1.618033988749894848204586834365…"

teach.solveSystem([[2, 1], [1, 3]], [1, 2]).steps;   // each row operation, explained

Topics

Try itEdit the code and run it with @ratmath/linalg and Core's Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs