Mode of #cas

coefficient

Gives the coefficient of a power of one variable in a polynomial, with a theorem about Mathlib’s Polynomial.coeff.

#cas [coefficient x k] e
gives the coefficient of x ^ k in the polynomial e and proves it about Mathlib’s Polynomial.coeff.

coefficient reads off one coefficient of a polynomial without making you keep the whole expansion. The theorem is about Mathlib’s polynomials: x is read as Polynomial.X, and the statement is P.coeff k = c.

Any other variable enters as a constant. For (x+y)3 and k=2 the theorem is ∀ y : ℝ, ((X + C y) ^ 3).coeff 2 = 3 * y, in ℝ[X], with y inside the polynomial as the constant C y.

Details and Options

  • The theorem is P.coeff k = c in ℚ[X] when e has no other variable, and ∀ y … : ℝ, P.coeff k = c in ℝ[X], with the other variables as constants C y, when it has.
  • The input is a polynomial with rational coefficients, as for collect, and k is a natural number literal.
  • A quotient by a term with a variable is refused.
  • The tactic cas proves coeff goals in ℚ[X], and it refuses a wrong value with the right one in its message.
  • coefficientList gives all the coefficients, and exponent the degree.
  • There are no options.

Examples (10)#

Basic Examples (2)

The coefficient of x2 in (x+y)3. The other variable stays in the coefficient:

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

coefficient of x^2

3*y
Domain
x ↦ X in ℝ[X], parameters in ℝ
Input (polynomial)
((x + y)^3)

Checked by Lean’s kernel: Statement about the polynomial

∀ (y : ℝ), ((Polynomial.X + Polynomial.C y) ^ 3).coeff 2 = 3 * y
The certificate: 1 checked step
  1. Statement about the polynomial

    ∀ (y : ℝ), ((Polynomial.X + Polynomial.C y) ^ 3).coeff 2 = 3 * y

    Checked by Lean’s kernel


A binomial coefficient, (52)=10:

In[2]:=
#cas [coefficient x 2] ((x + 1)^5)
Out[2]=

coefficient of x^2

10
Domain
x ↦ X in ℚ[X]
Input (polynomial)
((x + 1)^5)

Checked by Lean’s kernel: Statement about the polynomial

((Polynomial.X + 1) ^ 5).coeff 2 = 10
The certificate: 1 checked step
  1. Statement about the polynomial

    ((Polynomial.X + 1) ^ 5).coeff 2 = 10

    Checked by Lean’s kernel

Scope (1)

The factor 2 of 2x comes in with its third power, (53)·23=80:

In[3]:=
#cas [coefficient x 3] ((1 + 2*x)^5)
Out[3]=

coefficient of x^3

80
Domain
x ↦ X in ℚ[X]
Input (polynomial)
((1 + 2*x)^5)

Checked by Lean’s kernel: Statement about the polynomial

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

    ((1 + 2 * Polynomial.X) ^ 5).coeff 3 = 80

    Checked by Lean’s kernel

Applications (1)

The binomial coefficient as a theorem about Polynomial.coeff. Only the three standard axioms are used:

In[4]:=
open Polynomial in
theorem binomial_coefficient_two : ((X + 1) ^ 5 : ℚ[X]).coeff 2 = 10 := by
  cas

#print axioms binomial_coefficient_two
Out[4]=

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

Properties and Relations (3)

coefficientList gives all the coefficients at once, the constant term first:

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

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


collect shows every coefficient in place:

In[6]:=
#cas [collect x] ((1 + a*x)^2 + b*x)
Out[6]=

polynomial in x

a2x2+(2a+b)x+1
a^2*x^2 + (2*a + b)*x + 1
Input (polynomial)
((1 + a*x)^2 + b*x)

Checked by Lean’s kernel: Identity with the collected form

∀ (x a b : ℝ), (1 + a * x) ^ 2 + b * x = a ^ 2 * x ^ 2 + (2 * a + b) * x + 1
The certificate: 1 checked step
  1. Identity with the collected form

    ∀ (x a b : ℝ), (1 + a * x) ^ 2 + b * x = a ^ 2 * x ^ 2 + (2 * a + b) * x + 1

    Checked by Lean’s kernel


exponent gives the degree. The x3 terms cancel here:

In[7]:=
#cas [exponent x] ((x + 1)^3 - x^3)
Out[7]=

degree

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

Checked by Lean’s kernel: Statement about the polynomial

((Polynomial.X + 1) ^ 3 - Polynomial.X ^ 3).natDegree = 2
The certificate: 1 checked step
  1. Statement about the polynomial

    ((Polynomial.X + 1) ^ 3 - Polynomial.X ^ 3).natDegree = 2

    Checked by Lean’s kernel

Possible Issues (3)

A quotient by a term with a variable is refused:

In[8]:=
#cas [coefficient x 1] (1/x)
Out[8]=

Error

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

Lean’s total division makes 1/(1+x·(1/x)) equal to 12 for x≠0 and to 1 at x=0. It is not a polynomial, and it is refused:

In[9]:=
#cas [coefficient x 0] (1 / (1 + x * (1 / x)))
Out[9]=

Error

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

The tactic cas refuses a wrong coefficient and gives the right one:

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

Error

cas: the coefficient is 10, not 5

See Also

coefficientList · collect · exponent · expand