Mode of #cas

collect

Writes a polynomial as a sum of powers of one variable, with coefficients in the other variables, and proves that the two are equal.

#cas [collect x] e
writes the polynomial e as a sum of powers of x whose coefficients are expanded polynomials in the other variables, and proves that the two are equal.

A polynomial with several parameters is often easier to read once its terms are grouped by the powers of one variable, the way you would write a2x2+(2a+b)x+1 on paper. collect does that regrouping. The theorem is an identity for all real values of all the variables, proved by Mathlib’s ring1, so there are no conditions to carry around.

Details and Options

  • The theorem is ∀x,y,…,e=∑kckxk about Mathlib’s real numbers, where the ck are the printed coefficients, proved by ring1.
  • The input is a polynomial with rational coefficients in real variables, and x is one of them. A quotient by a term with a variable is refused.
  • The terms are printed from the highest power of x down, and a coefficient that is a sum is put in parentheses, except the constant term.
  • coefficient gives one coefficient and coefficientList all of them, with theorems about Mathlib’s Polynomial.coeff. exponent gives the degree.
  • The tactic cas proves these identities.
  • There are no options.

Examples (10)#

Basic Examples (2)

A quadratic in x with the parameters a and b:

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

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


The terms are gathered by the powers of x:

In[2]:=
#cas [collect x] (x*y + x^2*y + x + y^2*x^2)
Out[2]=

polynomial in x

(y2+y)x2+(y+1)x
(y^2 + y)*x^2 + (y + 1)*x
Input (polynomial)
(x*y + x^2*y + x + y^2*x^2)

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

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

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

    Checked by Lean’s kernel

Scope (1)

The same polynomial collected in y:

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

polynomial in y

x2y2+(x2+x)y+x
x^2*y^2 + (x^2 + x)*y + x
Input (polynomial)
(x*y + x^2*y + x + y^2*x^2)

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

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

    ∀ (y x : ℝ), x * y + x ^ 2 * y + x + y ^ 2 * x ^ 2 = x ^ 2 * y ^ 2 + (x ^ 2 + x) * y + x

    Checked by Lean’s kernel

Applications (1)

The first example as a theorem, for all real x, a and b. Only the three standard axioms are used:

In[4]:=
theorem collect_in_x (x a b : ℝ) :
    (1 + a * x) ^ 2 + b * x = a ^ 2 * x ^ 2 + (2 * a + b) * x + 1 := by
  cas

#print axioms collect_in_x
Out[4]=

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

Properties and Relations (4)

expand gives the monomials one by one:

In[5]:=
#cas [expand] ((x + 1)^3 * (y - 2))
Out[5]=
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]

collect groups them by the powers of x:

In[6]:=
#cas [collect x] ((x + 1)^3 * (y - 2))
Out[6]=

polynomial in x

(y−2)x3+(3y−6)x2+(3y−6)x+y−2
(y - 2)*x^3 + (3*y - 6)*x^2 + (3*y - 6)*x + y - 2
Input (polynomial)
((x + 1)^3 * (y - 2))

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

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

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

    Checked by Lean’s kernel


coefficient reads off one coefficient:

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

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


coefficientList reads off all of them, the constant term first:

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

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


exponent gives the degree in x. It is 2 under the hypothesis a≠0:

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

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

Possible Issues (2)

A quotient by a term with a variable is refused:

In[10]:=
#cas [collect x] (1/x + x)
Out[10]=

Error

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

For a rational function, apart splits off the polynomial part:

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

partial fractions over the written factors, refined by gcds

x−xx2+1
x - x/(x^2 + 1)
Input (rational function)
(x^3/(x^2+1))
Conditions
x2+1≠0

Checked by Lean’s kernel: Identity with the partial fraction decomposition

∀ (x : ℝ), x ^ 2 + 1 ≠ 0 → x ^ 3 / (x ^ 2 + 1) = x - x / (x ^ 2 + 1)
The certificate: 1 checked step
  1. Identity with the partial fraction decomposition

    ∀ (x : ℝ), x ^ 2 + 1 ≠ 0 → x ^ 3 / (x ^ 2 + 1) = x - x / (x ^ 2 + 1)

    Checked by Lean’s kernel

See Also

coefficient · coefficientList · exponent · expand