Mode of #cas

coefficientList

Gives the coefficients of a polynomial in one variable, constant term first, with a theorem about Mathlib’s Polynomial.coeff.

#cas [coefficientList x] e
gives the list of coefficients of the polynomial e in x, constant term first, and proves that entry k is P.coeff k for every k.

coefficientList gives all the coefficients of a polynomial, in order. The list starts with the constant term, so the entry at position k is the coefficient of xk, and the theorem says exactly that: ∀ k, P.coeff k = [c₀, …, c_d].getD k 0. One statement covers every k, even past the end of the list, where getD reads 0, which is the coefficient there.

Details and Options

  • The theorem is ∀ k, P.coeff k = [c₀, …, c_d].getD k 0, where P is e read as a polynomial as for coefficient: in ℚ[X], or in ℝ[X] with the other variables as constants C y. By AddMonoidAlgebra.ext_iff (in Mathlib v4.36.0 a polynomial is an AddMonoidAlgebra over ℕ, and two are equal when their coefficient functions are) and Mathlib’s coeff_ofCoeffList it is equivalent to the identity P = ofCoeffList [c₀, …, c_d], which is proved by evaluation at every point (Polynomial.funext).
  • The input is a polynomial with rational coefficients, as for collect. The other variables stay symbolic in the coefficients. The zero polynomial is refused.
  • The order is Mathematica’s. SymPy’s Poly.all_coeffs lists the highest degree first.
  • The tactic cas does not prove the list statement. It proves single coefficients, as in coefficient. A single entry also follows from the list statement by specializing k (see Applications).
  • There are no options.

Examples (10)#

Basic Examples (2)

The coefficients of (x+1)3−x/2:

In[1]:=
#cas [coefficientList x] ((x + 1)^3 - x/2)
Out[1]=

coefficients, constant term first

[1, 52, 3, 1]
[1, 5/2, 3, 1]
Domain
x ↦ X in ℚ[X]
Input (polynomial)
((x + 1)^3 - x/2)

Checked by Lean’s kernel: Statement about the polynomial

∀ (k : ℕ),
  ((Polynomial.X + 1) ^ 3 - Polynomial.C (1 / 2) * Polynomial.X).coeff k = [1, 5 / 2, 3, 1].getD k 0
The certificate: 1 checked step
  1. Statement about the polynomial

    ∀ (k : ℕ),
      ((Polynomial.X + 1) ^ 3 - Polynomial.C (1 / 2) * Polynomial.X).coeff k = [1, 5 / 2, 3, 1].getD k 0

    Checked by Lean’s kernel


With parameters, the coefficients are symbolic:

In[2]:=
#cas [coefficientList x] (a*x^2 + b*x + c)
Out[2]=

coefficients, constant term first

[c, b, a]
[c, b, a]
Domain
x ↦ X in ℝ[X], parameters in ℝ
Input (polynomial)
(a*x^2 + b*x + c)

Checked by Lean’s kernel: Statement about the polynomial

∀ (a b c : ℝ) (k : ℕ),
  (Polynomial.C a * Polynomial.X ^ 2 + Polynomial.C b * Polynomial.X + Polynomial.C c).coeff k =
    [c, b, a].getD k 0
The certificate: 1 checked step
  1. Statement about the polynomial

    ∀ (a b c : ℝ) (k : ℕ),
      (Polynomial.C a * Polynomial.X ^ 2 + Polynomial.C b * Polynomial.X + Polynomial.C c).coeff k =
        [c, b, a].getD k 0

    Checked by Lean’s kernel

Scope (1)

Zero coefficients are listed:

In[3]:=
#cas [coefficientList x] (x^3 + 1)
Out[3]=

coefficients, constant term first

[1, 0, 0, 1]
[1, 0, 0, 1]
Domain
x ↦ X in ℚ[X]
Input (polynomial)
(x^3 + 1)

Checked by Lean’s kernel: Statement about the polynomial

∀ (k : ℕ), (Polynomial.X ^ 3 + 1).coeff k = [1, 0, 0, 1].getD k 0
The certificate: 1 checked step
  1. Statement about the polynomial

    ∀ (k : ℕ), (Polynomial.X ^ 3 + 1).coeff k = [1, 0, 0, 1].getD k 0

    Checked by Lean’s kernel

Applications (2)

The list statement gives each coefficient by specializing k. Only the three standard axioms are used:

In[4]:=
open Polynomial in
theorem coefficients_of_cube (h : ∀ k, ((X + 1) ^ 3 : ℚ[X]).coeff k = [1, 3, 3, 1].getD k 0) :
    ((X + 1) ^ 3 : ℚ[X]).coeff 1 = 3 :=
  h 1

#print axioms coefficients_of_cube
Out[4]=

coefficients_of_cube depends on the axioms propext, Classical.choice, Quot.sound.


The tactic cas proves the single coefficient directly:

In[5]:=
open Polynomial in
example : ((X + 1) ^ 3 : ℚ[X]).coeff 1 = 3 := by
  cas
Out[5]=

Lean accepts the proof.

Properties and Relations (3)

coefficient gives one entry of the list:

In[6]:=
#cas [coefficient x 1] ((x + 1)^3 - x/2)
Out[6]=

coefficient of x^1

52
5/2
Domain
x ↦ X in ℚ[X]
Input (polynomial)
((x + 1)^3 - x/2)

Checked by Lean’s kernel: Statement about the polynomial

((Polynomial.X + 1) ^ 3 - Polynomial.C (1 / 2) * Polynomial.X).coeff 1 = 5 / 2
The certificate: 1 checked step
  1. Statement about the polynomial

    ((Polynomial.X + 1) ^ 3 - Polynomial.C (1 / 2) * Polynomial.X).coeff 1 = 5 / 2

    Checked by Lean’s kernel


exponent gives the degree. With a parameter in the leading coefficient, the theorem has the hypothesis that this coefficient is not zero:

In[7]:=
#cas [exponent x] (a*x^2 + x + 1)
Out[7]=

degree

2
Domain
x ↦ X in ℝ[X], parameters in ℝ
Input (polynomial)
(a*x^2 + x + 1)
Conditions
a≠0

Checked by Lean’s kernel: Statement about the polynomial

∀ (a : ℝ), a ≠ 0 → (Polynomial.C a * Polynomial.X ^ 2 + Polynomial.X + 1).natDegree = 2
The certificate: 1 checked step
  1. Statement about the polynomial

    ∀ (a : ℝ), a ≠ 0 → (Polynomial.C a * Polynomial.X ^ 2 + Polynomial.X + 1).natDegree = 2

    Checked by Lean’s kernel


interpolatingPolynomial builds a polynomial from its values:

In[8]:=
#cas [interpolatingPolynomial x] [(0, 1), (1, 3), (2, 7)]
Out[8]=

polynomial of degree at most 2

x2+x+1
x^2 + x + 1
Domain
x ↦ X in ℚ[X]
Input (points)
(0, 1), (1, 3), (2, 7)

Checked by Lean’s kernel: Values and degree in ℚ[X]

Polynomial.eval 0 (Polynomial.X ^ 2 + Polynomial.X + 1) = 1 ∧
  Polynomial.eval 1 (Polynomial.X ^ 2 + Polynomial.X + 1) = 3 ∧
    Polynomial.eval 2 (Polynomial.X ^ 2 + Polynomial.X + 1) = 7 ∧
      (Polynomial.X ^ 2 + Polynomial.X + 1).natDegree ≤ 2
The certificate: 1 checked step
  1. Values and degree in ℚ[X]

    Polynomial.eval 0 (Polynomial.X ^ 2 + Polynomial.X + 1) = 1 ∧
      Polynomial.eval 1 (Polynomial.X ^ 2 + Polynomial.X + 1) = 3 ∧
        Polynomial.eval 2 (Polynomial.X ^ 2 + Polynomial.X + 1) = 7 ∧
          (Polynomial.X ^ 2 + Polynomial.X + 1).natDegree ≤ 2

    Checked by Lean’s kernel

Possible Issues (2)

The mode reads coefficients of polynomials only:

In[9]:=
#cas [coefficientList x] (x + 1/x)
Out[9]=

Error

coefficientList: the expression is not a polynomial (it divides by a term with a variable)

The zero polynomial is refused:

In[10]:=
#cas [coefficientList x] (0)
Out[10]=

Error

coefficientList: the polynomial is zero

See Also

coefficient · collect · exponent · interpolatingPolynomial