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 be the ideal generated by the equations, and a Gröbner basis of it. Three things can happen, and the basis says which.
- No solutions, even over the complex numbers: exactly when . (This is the Nullstellensatz.)
- Finitely many: exactly when, for every variable , some power is the leading monomial of an element of . Then the quotient ring 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 that takes a different value at each solution (a separating form; almost every choice works). Multiplication by is a linear map on the finite-dimensional quotient ring, and its minimal polynomial has exactly the values of at the solutions as its roots. Each coordinate is then a polynomial in :
This is a rational univariate representation of the solution set. Every solution corresponds to one root of , 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 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 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 and with no common component meet in 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 , 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.
@ratmath/structures and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.