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 n 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’s Polynomial.coeff.
  • coefficientListGives the coefficients of a polynomial in one variable, constant term first, with a theorem about Mathlib’s Polynomial.coeff.
  • exponentGives the degree of a polynomial in one variable, with a theorem about Mathlib’s Polynomial.natDegree.

Division and GCDs

  • polynomialGCDGives the monic gcd of two polynomials in one variable, with a theorem about Mathlib’s gcd in ℚ[X].
  • polynomialLCMGives the monic lcm of two nonzero polynomials in one variable, with a theorem about Mathlib’s lcm in ℚ[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

  • groebnerGives the reduced lexicographic Gröbner basis of a polynomial ideal, with theorems about Ideal.span in MvPolynomial.
  • memberProves that a polynomial lies in the ideal of given polynomials, and gives it as a combination of them.

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]:

In[1]:=
#cas [polynomialGCD] (x^2 - 1, x^2 - 2*x + 1)
Out[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
  1. Statement in ℚ[X]

    gcd (Polynomial.X ^ 2 - 1) (Polynomial.X ^ 2 - 2 * Polynomial.X + 1) = Polynomial.X - 1

    Checked by Lean’s kernel

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 y alone:

In[2]:=
#cas [groebner x, y] [x^2 + y^2 - 5, x*y - 2]
Out[2]=

Gröbner basis of the same ideal (checked)

2x+y3−5y,y4−5y2+4
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
  1. 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}

    Checked by Lean’s kernel

  2. 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

    Checked by Lean’s kernel

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 (y2−1)(y2−4), so y is ±1 or ±2, and the first element then gives x. 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.