Ideals and Gröbner bases

Solving polynomial systems

A system of polynomial equations with finitely many solutions can be solved exactly: every solution found, counted with its multiplicity, and each coordinate given as an algebraic number, not a rounded decimal.

How many solutions?

Let II be the ideal generated by the equations, and GG a Gröbner basis of it. Three things can happen, and the basis says which.

  • No solutions, even over the complex numbers: exactly when G={1}G = \{1\}. (This is the Nullstellensatz.)
  • Finitely many: exactly when, for every variable xix_i, some power xikx_i^{k} is the leading monomial of an element of GG. Then the quotient ring K[x1,…,xn]/IK[x_1, \dots, x_n]/I is a finite-dimensional vector space, and its dimension is the number of complex solutions counted with multiplicity.
  • Infinitely many otherwise: a curve, a surface or more, whose dimension is I.dimension.
import { ring, ideal, solve } from "@ratmath/structures";

const R = ring("x, y");
const I = ideal(R, ["x^2 + y^2 - 1", "y - x^2"]);     // a circle and a parabola
I.dimension;        // 0
I.degree;           // 4 complex solutions, with multiplicity

Triangular form

With the lex order, a Gröbner basis of a system with finitely many solutions contains a polynomial in the last variable alone. Solve it, substitute each root into the rest, and continue upwards, just as with back-substitution in linear algebra.

import { groebner } from "@ratmath/structures";

const L = ring("x, y", { order: "lex" });
groebner([L.poly("x^2 + y^2 - 1"), L.poly("y - x^2")]).basis.map(String);
      // ["y² + y − 1", "x² − y"]:  y = (−1 ± √5)/2, then x = ±√y

That is fine by hand, but it means computing with roots of roots. The library uses a different route that needs only one polynomial in one variable.

One polynomial for all the solutions

Choose a linear form u=c1x1+⋯+cnxnu = c_1 x_1 + \dots + c_n x_n that takes a different value at each solution (a separating form; almost every choice works). Multiplication by uu is a linear map on the finite-dimensional quotient ring, and its minimal polynomial m(u)m(u) has exactly the values of uu at the solutions as its roots. Each coordinate is then a polynomial in uu:

m(u)=0,x1=p1(u),…,xn=pn(u). m(u) = 0, \qquad x_1 = p_1(u), \quad \dots, \quad x_n = p_n(u).

This is a rational univariate representation of the solution set. Every solution corresponds to one root of mm, so the solutions are counted and located by isolating the roots of a single polynomial with exact arithmetic.

const s = solve(R, ["x^2 + y^2 - 1", "y - x^2"]);
s.count;                                   // 4 distinct complex solutions
s.real.length;                             // 2 of them real
s.parametrization.form;                    // { x: 1, y: 0 }: u = x separates them
s.parametrization.minimalPolynomial.map(String);   // ["-1", "0", "1", "0", "1"]: u⁴ + u² − 1
s.parametrization.coordinates.y.map(String);       // ["0", "0", "1", "0"]: y = u²

The polynomial mm is then factored over the rationals. Each irreducible factor is one component: a set of solutions that are conjugate to each other, with coordinates in the number field Q(θ)\mathbb Q(\theta) that the factor defines. The solutions are algebraic numbers held exactly, so they can be compared, added, and printed to any number of digits.

const p = s.real[1];
String(p);                 // "(x = 0.786151377757…, y = 0.618033988749…)"
p.approx;                  // { x: 0.7861513777574233, y: 0.6180339887498948 }
p.values.y;                // the exact algebraic number (√5 − 1)/2
p.coordinates.y;           // θ², an element of Q(θ) with θ⁴ + θ² − 1 = 0
s.components.map((c) => [c.degree, c.realCount]);     // [[4, 2]]

solve(R, ["x^2 + y^2 - 5", "x y - 2"]).real.map(String);
      // ["(x = -2, y = -1)", "(x = -1, y = -2)", "(x = 1, y = 2)", "(x = 2, y = 1)"]

Multiplicity

A tangent line meets a parabola at one point that counts twice. In the algebra, the quotient ring has dimension 2 but only one point, and the ideal is not radical. The solver reports both numbers, and finds the points from the radical.

const t = solve(R, ["y - x^2", "y"]);     // the parabola and its tangent at the vertex
t.count;            // 1
t.multiplicity;     // 2
t.radical;          // false

Bézout's theorem says two plane curves of degrees dd and ee with no common component meet in dede points, counted with multiplicity, in the complex projective plane. The count here is of the points in the affine plane, so it is at most dede, and less when some intersections are at infinity.

Complex solutions, none, or too many

const c = solve(R, ["x^2 - y", "y^2 - x"], { complex: true });
c.real.map(String);       // ["(x = 0, y = 0)", "(x = 1, y = 1)"]
c.complex.map(String);    // ["(x = −1/2 − i√3/2, y = −1/2 + i√3/2)", "(x = −1/2 + i√3/2, y = −1/2 − i√3/2)"]

solve(R, ["x^2 + y^2 - 1", "x - 2"]).real;       // []: two complex solutions, none real
solve(R, ["x y - 1", "x"]).count;                // 0: no solutions at all
solve(R, ["x y - 1", "x^2 y - x"]);              // { finite: false, dimension: 1, … }: the whole hyperbola

When there are infinitely many solutions there is no list to give. Elimination then describes the solution set through its projections, and adding equations (a plane, a normalisation) cuts it down to points.

The solver works over the rationals. The linear algebra is exact and happens in a space whose dimension is the number of solutions, so systems with up to a few hundred solutions are practical.

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