Guide
Polynomials
Coefficients, degrees, division, gcds, interpolation and Gröbner bases, with theorems about Mathlib’s Polynomial and MvPolynomial.
In the algebra guide a polynomial is an expression in real variables. Here the polynomials themselves are the objects, together with the ideals they generate. In one variable you can read off coefficients and degrees, divide with remainder, take gcds and lcms and interpolate, and the theorems are about Mathlib’s ℚ[X]: its coefficients, its degree, gcd, lcm, / and %. In several variables they are about ideals of MvPolynomial (Fin n) ℚ, Mathlib’s polynomial ring in variables: a Gröbner basis spans the same ideal as its input, and a membership comes with its cofactors.
Coefficients and degrees
coefficientGives the coefficient of a power of one variable in a polynomial, with a theorem about Mathlib’sPolynomial.coeff.coefficientListGives the coefficients of a polynomial in one variable, constant term first, with a theorem about Mathlib’sPolynomial.coeff.exponentGives the degree of a polynomial in one variable, with a theorem about Mathlib’sPolynomial.natDegree.
Division and GCDs
polynomialGCDGives the monic gcd of two polynomials in one variable, with a theorem about Mathlib’sgcdinℚ[X].polynomialLCMGives the monic lcm of two nonzero polynomials in one variable, with a theorem about Mathlib’slcminℚ[X].polynomialQuotientGives the quotient of a division of polynomials in one variable, with a theorem about Mathlib’s/inℚ[X].polynomialRemainderGives the remainder of a division of polynomials in one variable, with a theorem about Mathlib’s%inℚ[X].polynomialQuotientRemainderGives the quotient and the remainder of a division of polynomials in one variable, with a theorem about Mathlib’s/and%inℚ[X].
Interpolation
interpolatingPolynomialGives the polynomial of least degree through given points, with a theorem about its values and its degree bound inℚ[X].
Ideals and Gröbner bases
What I like about the gcd is that its proof doesn’t replay the Euclidean algorithm. It comes with a certificate, the cofactors of both inputs and a Bézout identity, which together show that the gcd divides both inputs and that every common divisor divides it. The kernel checks those identities however long the algorithm ran, and the theorem is about Mathlib’s own gcd in ℚ[X]:
#cas [polynomialGCD] (x^2 - 1, x^2 - 2*x + 1)
monic gcd
x - 1- Domain
x ↦ X in ℚ[X]- Input (polynomials)
(x^2 - 1, x^2 - 2*x + 1)
Checked by Lean’s kernel: Statement in ℚ[X]
gcd (Polynomial.X ^ 2 - 1) (Polynomial.X ^ 2 - 2 * Polynomial.X + 1) = Polynomial.X - 1
The certificate: 1 checked step
Statement in ℚ[X]
gcd (Polynomial.X ^ 2 - 1) (Polynomial.X ^ 2 - 2 * Polynomial.X + 1) = Polynomial.X - 1
A Gröbner basis comes with two theorems: it spans the same ideal as the input, and it is a Gröbner basis for the lexicographic order. That order eliminates variables, so the last element is a polynomial in alone:
#cas [groebner x, y] [x^2 + y^2 - 5, x*y - 2]
Gröbner basis of the same ideal (checked)
2*x + y^3 - 5*y, y^4 - 5*y^2 + 4- Domain
ℚ[x, y] as MvPolynomial (Fin 2) ℚ, with x = X 0, y = X 1- Input (polynomials)
x^2 + y^2 - 5, x*y - 2
Checked by Lean’s kernel: Ideal of the inputs
Ideal.span {X 0 ^ 2 + X 1 ^ 2 - 5, X 0 * X 1 - 2} =
Ideal.span {2 * X 0 + X 1 ^ 3 - 5 * X 1, X 1 ^ 4 - 5 * X 1 ^ 2 + 4}The certificate: 2 checked steps
Ideal of the inputs
Ideal.span {X 0 ^ 2 + X 1 ^ 2 - 5, X 0 * X 1 - 2} = Ideal.span {2 * X 0 + X 1 ^ 3 - 5 * X 1, X 1 ^ 4 - 5 * X 1 ^ 2 + 4}Gröbner property of the basis
∀ p ∈ Ideal.span {2 * X 0 + X 1 ^ 3 - 5 * X 1, X 1 ^ 4 - 5 * X 1 ^ 2 + 4}, p ≠ 0 → ∃ g ∈ {2 * X 0 + X 1 ^ 3 - 5 * X 1, X 1 ^ 4 - 5 * X 1 ^ 2 + 4}, MonomialOrder.lex.leadingTerm g ∣ MonomialOrder.lex.leadingTerm p
The computation
Gröbner basis
the reduced basis for the lexicographic order x > y, each element scaled to coprime integer coefficients with a positive leading coefficient; computed by SymLean's Buchberger algorithm, 4 polynomials in the derivation, 2 in the basis
The last element factors as , so is or , and the first element then gives . That’s the point of the lexicographic order: the common zeros can be read off one variable at a time.
Division, gcds and lcms work in one variable with rational coefficients. Mathlib’s operations are total, with gcd 0 0 = 0, p / 0 = 0 and p % 0 = p, so a zero divisor, or two zero inputs to a gcd, is refused. The Gröbner search stops after 400 polynomials or 4000 pairs and suggests an oracle. The theorem says that the basis is a Gröbner basis of the same ideal, but not that it is the reduced one. And when member finds a nonzero remainder, it refuses, and the refusal proves nothing about non-membership.
In a proof, the tactic cas proves goals about coeff, natDegree, gcd, lcm, / and % in ℚ[X], and goals f ∈ Ideal.span {g₁, …, gₘ} in any commutative ring, with cofactors from a Gröbner basis. A certificate with denominators needs a ℚ-algebra.
