Topic

Elimination and solving

A system of linear equations is a matrix of coefficients. Three row operations never change its solutions: swapping two rows, multiplying a row by a nonzero number, and adding a multiple of one row to another. Gauss–Jordan elimination uses them to reach reduced row echelon form: each nonzero row starts with a 1, that 1 is alone in its column, and the leading 1s step to the right going down.

From that form everything can be read: the rank is the number of pivots, the pivot columns of the original matrix span its column space, and each column without a pivot is a free unknown.

Reading a solution

Augment A with b and reduce. A row 0 0 … 0 | c with c ≠ 0 says 0 = c: no solution. Otherwise each pivot row gives one pivot unknown in terms of the free ones. With no free unknowns the solution is unique; with k free unknowns the solutions form a k-dimensional family.

Do it yourself

Choose the operations yourself. The reducer applies them exactly, keeps a history you can undo, says when you have reached echelon and reduced form, and can suggest the step Gauss–Jordan would take next.

What the fast version does differently

Textbook elimination divides as it goes, and the fractions grow quickly. The library instead clears denominators row by row and uses fraction-free (Bareiss) elimination, which keeps every number an integer minor of the input. For square systems it first tries to guess the answer from floats and check it exactly, and for large systems it uses p-adic lifting.

In code

import { rref, solve, rank } from "@ratmath/linalg";

const A = [[1, 2, -1], [2, 3, 1], [3, 5, 0]];
const R = rref(A).exact();
R.matrix.toStrings();   // [["1","0","5"], ["0","1","-3"], ["0","0","0"]]
R.pivots;               // [0, 1]

const s = solve(A, [1, 3, 4]).exact();
s.status;                               // "infinite"
s.x.toStrings();                        // one solution: [["3"], ["-1"], ["0"]]
s.basis.map((v) => v.toStrings());      // plus any multiple of [["-5"], ["3"], ["1"]]
rank(A).quick.value;                    // 2, from the float preview
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