Mode of #cas

expand

Multiplies out a polynomial, with a theorem that the expansion equals the input for all real values.

#cas [expand] e
multiplies out the products and natural powers of the polynomial e and proves that the expansion equals e.

expand multiplies an expression out so you can see its terms and their coefficients. The theorem is an identity for all real values of the variables, proved by Mathlib’s tactic ring, so it holds with no conditions at all.

It also knows the classical families of polynomials. Ask for ChebyshevT 5 x and the theorem is about Mathlib’s own Chebyshev polynomial Polynomial.Chebyshev.T, which is the one you want when the expansion goes into a proof.

Details and Options

  • The theorem is ∀x1,…,xn,e=p about Mathlib’s real numbers, where p is the printed expansion, proved by ring.
  • The input is a polynomial with rational coefficients in real variables, or a call ChebyshevT n x, ChebyshevU n x, HermiteH n x, HermiteHe n x, LegendreP n x or BernoulliB n x. For a call, the theorem is the expansion of Mathlib’s polynomial, or for LegendreP of SymLean’s CAS.Analysis.SpecialFunctions.legendre. The call pages, such as ChebyshevT, have the details.
  • An expression that is not a polynomial, such as sin (x + y) or 1 / (x + 1) ^ 2, is printed unchanged. trigExpand expands sines and cosines of sums, and together and apart rewrite rational functions.
  • collect groups an expansion by the powers of one variable, and coefficient and coefficientList read off its coefficients.
  • There are no options. The tactic cas proves the same identities.

Examples (13)#

Basic Examples (3)

A product in two variables:

In[1]:=
#cas [expand] ((x + 1)^3 * (y - 2))
Out[1]=
x3y−2x3+3x2y−6x2+3xy−6x+y−2
x^3*y - 2*x^3 + 3*x^2*y - 6*x^2 + 3*x*y - 6*x + y - 2

Checked by Lean’s kernel: Identity with the expanded polynomial

∀ (x y : ℝ),
  (x + 1) ^ 3 * (y - 2) = x ^ 3 * y - 2 * x ^ 3 + 3 * x ^ 2 * y - 6 * x ^ 2 + 3 * x * y - 6 * x + y - 2
The certificate: 1 checked step
  1. Identity with the expanded polynomial

    ∀ (x y : ℝ),
      (x + 1) ^ 3 * (y - 2) = x ^ 3 * y - 2 * x ^ 3 + 3 * x ^ 2 * y - 6 * x ^ 2 + 3 * x * y - 6 * x + y - 2

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    expanded polynomial: x^3*y - 2*x^3 + 3*x^2*y - 6*x^2 + 3*x*y - 6*x + y - 2  [leafCount 39, conditions 0]
    input: (x + 1)^3*(y - 2)  [leafCount 9, conditions 0]

The binomial theorem for n=4:

In[2]:=
#cas [expand] ((a + b)^4)
Out[2]=
a4+4a3b+6a2b2+4ab3+b4
a^4 + 4*a^3*b + 6*a^2*b^2 + 4*a*b^3 + b^4

Checked by Lean’s kernel: Identity with the expanded polynomial

∀ (a b : ℝ), (a + b) ^ 4 = a ^ 4 + 4 * a ^ 3 * b + 6 * a ^ 2 * b ^ 2 + 4 * a * b ^ 3 + b ^ 4
The certificate: 1 checked step
  1. Identity with the expanded polynomial

    ∀ (a b : ℝ), (a + b) ^ 4 = a ^ 4 + 4 * a ^ 3 * b + 6 * a ^ 2 * b ^ 2 + 4 * a * b ^ 3 + b ^ 4

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    expanded polynomial: a^4 + 4*a^3*b + 6*a^2*b^2 + 4*a*b^3 + b^4  [leafCount 33, conditions 0]
    input: (a + b)^4  [leafCount 5, conditions 0]

Rational coefficients stay exact:

In[3]:=
#cas [expand] ((x - 1/2)^2 * (x + 2))
Out[3]=
x3+x2−74x+12
x^3 + x^2 - 7/4*x + 1/2

Checked by Lean’s kernel: Identity with the expanded polynomial

∀ (x : ℝ), (x - 1 / 2) ^ 2 * (x + 2) = x ^ 3 + x ^ 2 - 7 / 4 * x + 1 / 2
The certificate: 1 checked step
  1. Identity with the expanded polynomial

    ∀ (x : ℝ), (x - 1 / 2) ^ 2 * (x + 2) = x ^ 3 + x ^ 2 - 7 / 4 * x + 1 / 2

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    expanded polynomial: x^3 + x^2 - 7/4*x + 1/2  [leafCount 17, conditions 0]
    input: (x - 1/2)^2*(x + 2)  [leafCount 11, conditions 0]

Scope (4)

The Chebyshev polynomial T5. The theorem holds in every commutative ring:

In[4]:=
#cas [expand] (ChebyshevT 5 x)
Out[4]=

polynomial with integer coefficients

16x5−20x3+5x
16*x^5 - 20*x^3 + 5*x
Domain
Polynomial.Chebyshev.T R 5, every commutative ring R : Type
Input (polynomial in x)
ChebyshevT 5 x

Checked by Lean’s kernel: Monomial expansion of ChebyshevT 5

∀ (R : Type) [inst : CommRing R],
  Polynomial.Chebyshev.T R 5 = 16 * Polynomial.X ^ 5 - 20 * Polynomial.X ^ 3 + 5 * Polynomial.X
The certificate: 1 checked step
  1. Monomial expansion of ChebyshevT 5

    ∀ (R : Type) [inst : CommRing R],
      Polynomial.Chebyshev.T R 5 = 16 * Polynomial.X ^ 5 - 20 * Polynomial.X ^ 3 + 5 * Polynomial.X

    Checked by Lean’s kernel


The Legendre polynomial P5, with rational coefficients:

In[5]:=
#cas [expand] (LegendreP 5 x)
Out[5]=

polynomial with exact rational coefficients

638x5−354x3+158x
63/8*x^5 - 35/4*x^3 + 15/8*x
Input (polynomial in x)
LegendreP 5 x

Checked by Lean’s kernel: Monomial expansion of the Legendre polynomial

∀ (x : ℝ),
  Polynomial.eval x (CAS.Analysis.SpecialFunctions.legendre 5) = 63 / 8 * x ^ 5 - 35 / 4 * x ^ 3 + 15 / 8 * x

from CAS.Analysis.SpecialFunctions.legendre_eval_eq_sum_coeff

The certificate: 1 checked step
  1. Monomial expansion of the Legendre polynomial

    ∀ (x : ℝ),
      Polynomial.eval x (CAS.Analysis.SpecialFunctions.legendre 5) = 63 / 8 * x ^ 5 - 35 / 4 * x ^ 3 + 15 / 8 * x

    Checked by Lean’s kernel · CAS.Analysis.SpecialFunctions.legendre_eval_eq_sum_coeff

The computation
  • Checked coefficients

    Coefficients of x^0, …, x^5: [0, 15/8, 0, -35/4, 0, 63/8]
    Guarantee: the displayed polynomial equals P_5 at every real x

The Hermite polynomials, in the physicists’ and the probabilists’ normalization:

In[6]:=
#cas [expand] (HermiteH 5 x)
Out[6]=

polynomial with integer coefficients

32x5−160x3+120x
32*x^5 - 160*x^3 + 120*x
Domain
√2 ^ 5 Polynomial.hermite 5 (√2 x) (physicists'), x ∈ ℝ
Input (polynomial in x)
HermiteH 5 x

Checked by Lean’s kernel: Monomial expansion of HermiteH 5

∀ (x : ℝ),
  √2 ^ 5 * (Polynomial.aeval (√2 * x)) (Polynomial.hermite 5) = 32 * x ^ 5 - 160 * x ^ 3 + 120 * x
The certificate: 1 checked step
  1. Monomial expansion of HermiteH 5

    ∀ (x : ℝ),
      √2 ^ 5 * (Polynomial.aeval (√2 * x)) (Polynomial.hermite 5) = 32 * x ^ 5 - 160 * x ^ 3 + 120 * x

    Checked by Lean’s kernel

In[7]:=
#cas [expand] (HermiteHe 3 x)
Out[7]=

polynomial with integer coefficients

x3−3x
x^3 - 3*x
Domain
Polynomial.hermite 3 (probabilists'), ℤ[X]
Input (polynomial in x)
HermiteHe 3 x

Checked by Lean’s kernel: Monomial expansion of HermiteHe 3

Polynomial.hermite 3 = Polynomial.X ^ 3 - 3 * Polynomial.X
The certificate: 1 checked step
  1. Monomial expansion of HermiteHe 3

    Polynomial.hermite 3 = Polynomial.X ^ 3 - 3 * Polynomial.X

    Checked by Lean’s kernel


A Chebyshev polynomial of the second kind and a Bernoulli polynomial:

In[8]:=
#cas [expand] (ChebyshevU 2 x)
Out[8]=

polynomial with integer coefficients

4x2−1
4*x^2 - 1
Domain
Polynomial.Chebyshev.U R 2, every commutative ring R : Type
Input (polynomial in x)
ChebyshevU 2 x

Checked by Lean’s kernel: Monomial expansion of ChebyshevU 2

∀ (R : Type) [inst : CommRing R], Polynomial.Chebyshev.U R 2 = 4 * Polynomial.X ^ 2 - 1
The certificate: 1 checked step
  1. Monomial expansion of ChebyshevU 2

    ∀ (R : Type) [inst : CommRing R], Polynomial.Chebyshev.U R 2 = 4 * Polynomial.X ^ 2 - 1

    Checked by Lean’s kernel

In[9]:=
#cas [expand] (BernoulliB 2 y)
Out[9]=

polynomial with exact rational coefficients

y2−y+16
y^2 - y + 1/6
Domain
Polynomial.bernoulli 2, y ∈ ℝ
Input (polynomial in y)
BernoulliB 2 y

Checked by Lean’s kernel: Monomial expansion of BernoulliB 2

∀ (y : ℝ), (Polynomial.aeval y) (Polynomial.bernoulli 2) = y ^ 2 - y + 1 / 6
The certificate: 1 checked step
  1. Monomial expansion of BernoulliB 2

    ∀ (y : ℝ), (Polynomial.aeval y) (Polynomial.bernoulli 2) = y ^ 2 - y + 1 / 6

    Checked by Lean’s kernel

Applications (1)

The first expansion as a theorem. It holds for all real x and y, and only the three standard axioms are used:

In[10]:=
theorem cube_times_linear (x y : ℝ) :
    (x + 1) ^ 3 * (y - 2) =
      x ^ 3 * y - 2 * x ^ 3 + 3 * x ^ 2 * y - 6 * x ^ 2 + 3 * x * y - 6 * x + y - 2 := by
  cas

#print axioms cube_times_linear
Out[10]=

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

Properties and Relations (2)

factor goes the other way:

In[11]:=
#cas [factor] (2*x^3 - 3*x^2 + 1)
Out[11]=
(x−1)2(2x+1)
(x - 1)^2*(2*x + 1)

Checked by Lean’s kernel: Identity with the factored polynomial

∀ (x : ℝ), 2 * x ^ 3 - 3 * x ^ 2 + 1 = (x - 1) ^ 2 * (2 * x + 1)
The certificate: 1 checked step
  1. Identity with the factored polynomial

    ∀ (x : ℝ), 2 * x ^ 3 - 3 * x ^ 2 + 1 = (x - 1) ^ 2 * (2 * x + 1)

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    factored polynomial: (x - 1)^2*(2*x + 1)  [leafCount 11, conditions 0]
    input: 2*x^3 - 3*x^2 + 1  [leafCount 13, conditions 0]

collect groups the terms by the powers of one variable, with the other variables in the coefficients:

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

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

Possible Issues (3)

The argument of a function is not expanded:

In[13]:=
#cas [expand] (sin (x + y))
Out[13]=
sin(x+y)
sin(x + y)

Checked by Lean’s kernel: Identity with the input

∀ (x y : ℝ), Real.sin (x + y) = Real.sin (x + y)
The certificate: 1 checked step
  1. Identity with the input

    ∀ (x y : ℝ), Real.sin (x + y) = Real.sin (x + y)

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    input: sin(x + y)  [leafCount 4, conditions 0]

trigExpand expands sines and cosines of sums:

In[14]:=
#cas [trigExpand] (sin (x + y))
Out[14]=
sin(x)cos(y)+cos(x)sin(y)
sin(x)*cos(y) + cos(x)*sin(y)

Checked by Lean’s kernel: Identity with the trigonometric expansion

∀ (x y : ℝ), Real.sin (x + y) = Real.sin x * Real.cos y + Real.cos x * Real.sin y
The certificate: 1 checked step
  1. Identity with the trigonometric expansion

    ∀ (x y : ℝ), Real.sin (x + y) = Real.sin x * Real.cos y + Real.cos x * Real.sin y

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    trigonometric expansion: sin(x)*cos(y) + cos(x)*sin(y)  [leafCount 11, conditions 0]
    input: sin(x + y)  [leafCount 4, conditions 0]

A quotient is left as it is. together and apart rewrite rational functions:

In[15]:=
#cas [expand] (1 / (x + 1) ^ 2)
Out[15]=
1(x+1)2
1/(x + 1)^2

Checked by Lean’s kernel: Identity with the input

∀ (x : ℝ), 1 / (x + 1) ^ 2 = 1 / (x + 1) ^ 2
The certificate: 1 checked step
  1. Identity with the input

    ∀ (x : ℝ), 1 / (x + 1) ^ 2 = 1 / (x + 1) ^ 2

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    input: 1/(x + 1)^2  [leafCount 7, conditions 0]

See Also

factor · collect · trigExpand · fullSimplify · coefficientList