Gröbner bases
A Gröbner basis is a set of generators for which division with remainder works. With one in hand, questions about a system of polynomial equations become mechanical: is this polynomial a consequence of those, are there solutions, how many.
The trouble with division
With one variable, dividing by answers the question "is a multiple of ?": yes exactly when the remainder is zero. With several variables and several divisors, the question becomes "is a combination ?", that is, does lie in the ideal the generate. Division by a list still produces a remainder, but the remainder depends on the order in which the divisors are tried, and it can be nonzero for a polynomial that is in the ideal.
import { ring, groebner } from "@ratmath/structures";
const R = ring("x, y");
const g1 = R.poly("x y - 1");
const g2 = R.poly("y^2 - 1");
const f = R.poly("x^2 y + x y^2 + y^2");
String(f.rem([g1, g2])); // "x + y + 1"
String(f.rem([g2, g1])); // "2x + 1": a different remainder
String(R.poly("x - y").rem([g1, g2])); // "x − y", yet x − y = y·g1 − x·g2 is in the ideal
The cure is to replace the generators by a better set that generates the same ideal.
What a Gröbner basis is
Fix a monomial order, and write for the leading monomial of . A finite set inside an ideal is a Gröbner basis of when
Division by such a set behaves the way division should. The remainder no longer depends on the order of the divisors; it is the unique representative of modulo built from monomials that no leading monomial of divides (the normal form of ). In particular
Every ideal has one (this is Hilbert's basis theorem at work), and once the basis is reduced, meaning each element has leading coefficient 1 and no term divisible by another element's leading monomial, it is unique. So two ideals are equal exactly when their reduced Gröbner bases are the same list.
const G = groebner([g1, g2]);
G.basis.map(String); // ["x − y", "y² − 1"]
String(G.reduce(f)); // "2y + 1", whatever the order of division
G.contains(R.poly("x - y")); // true
Buchberger's algorithm
Division gets stuck because leading terms can cancel. The simplest cancellation between two polynomials is their S-polynomial: multiply each by the smallest monomial that makes the leading terms equal, and subtract.
Buchberger's criterion. is a Gröbner basis of the ideal it generates exactly when every leaves remainder zero on division by .
That is a finite test, and it is also an algorithm: whenever an S-polynomial leaves a nonzero remainder, add the remainder to the set and continue. The ideal of leading monomials grows each time, and such a chain cannot grow for ever, so the process stops.
Most pairs never need to be reduced. If two leading monomials share no variable, the S-polynomial always reduces to zero. And a pair can be skipped when a third element's leading monomial divides their lcm and both pairs with it are dealt with. This library applies both tests (in the form due to Gebauer and Möller), takes the pair with the smallest lcm first, and over the rationals works with whole-number coefficients so that no fractions are carried. G.stats says how much work was done.
import { sPolynomial } from "@ratmath/structures";
String(sPolynomial(g1, g2)); // "x − y": y·g1 − x·g2
G.stats; // { pairs: 2, zero: 1, skipped: 1, added: 3 }
Certificates
An answer of this kind should be checkable without trusting the algorithm. Two checks are built in.
G.express(f)writes a member of the ideal as a combination of the original generators, , and multiplies it out to confirm. That is a proof of membership anyone can verify by expanding.G.verify()re-runs Buchberger's criterion from scratch on the finished basis, and checks that the basis and the generators define the same ideal.
const ex = G.express(R.poly("x - y"));
ex.cofactors.map(String); // ["y", "−x"]: x − y = y·(xy − 1) − x·(y² − 1)
ex.check; // true
G.verify(); // { generatorsReduce: true, sPairsReduce: true, basisInIdeal: true, sPairsChecked: 2, ok: true }
The order matters
The basis depends on the monomial order, and different orders are good for different things.
- lex eliminates: the basis contains polynomials in the last variable alone, then in the last two, and so on, like a triangular linear system. It is also the slowest and its polynomials are the largest.
- grevlex (the default) usually gives the smallest basis in the least time. Use it for membership, dimension, and counting solutions.
eliminationOrder(n, [i, j])is the middle way: it ranks the listed variables above the rest and is grevlex within each block. This is what elimination uses.
const L = ring("x, y, z", { order: "lex" });
const twisted = ["y - x^2", "z - x^3"]; // the twisted cubic (t, t², t³)
groebner(twisted.map((t) => L.poly(t))).basis.map(String);
// ["y³ − z²", "xz − y²", "xy − z", "x² − y"]: the first involves y and z only
const V = ring("x, y, z"); // grevlex
groebner(twisted.map((t) => V.poly(t))).basis.map(String);
// ["y² − xz", "xy − z", "x² − y"]: the three quadrics that cut out the curve
What the basis tells you at once
The leading monomials alone carry a good deal of information.
- The basis is exactly when the equations have no common solution, even over the complex numbers (
G.isTrivial). - The monomials that no leading monomial divides are the standard monomials. They form a basis of the quotient ring as a vector space.
- There are finitely many standard monomials exactly when the system has finitely many solutions, and then their number is the number of solutions counted with multiplicity (
G.quotientDimension). G.dimensionis the dimension of the solution set: 0 for points, 1 for curves, 2 for surfaces.
const C = groebner([R.poly("x^2 + y^2 - 1"), R.poly("x y - 1/2")]);
C.dimension; // 0
C.standardMonomials().map(String); // ["1", "y", "x", "y²"]
C.quotientDimension; // 4: a circle and a hyperbola meet in 4 points, with multiplicity
How far it goes
The worst case for Gröbner bases is very bad (degrees can grow doubly exponentially in the number of variables), but the systems that come up in practice are usually far from it. As a guide, on standard benchmarks this implementation takes about 20 ms for cyclic-5 (5 variables, 70 solutions), 50 ms for katsura-5 (6 variables, 32 solutions) and a few seconds for cyclic-6 (156 solutions). The options maxPairs and timeLimit (milliseconds) stop a run that is going nowhere. Coefficients may come from any exact field: the rationals, a prime field, a finite field or a number field.
@ratmath/structures and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.