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] eA 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 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.
Examples (10)#
Basic Examples (2)
A quadratic in with the parameters and :
#cas [collect x] ((1 + a*x)^2 + b*x)
polynomial in x
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
Identity with the collected form
∀ (x a b : ℝ), (1 + a * x) ^ 2 + b * x = a ^ 2 * x ^ 2 + (2 * a + b) * x + 1
The terms are gathered by the powers of :
#cas [collect x] (x*y + x^2*y + x + y^2*x^2)
polynomial in 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
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
Scope (1)
The same polynomial collected in :
#cas [collect y] (x*y + x^2*y + x + y^2*x^2)
polynomial in y
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
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
Applications (1)
The first example as a theorem, for all real x, a and b. Only the three standard axioms are used:
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
collect_in_x depends on the axioms propext, Classical.choice, Quot.sound.
Properties and Relations (4)
expand gives the monomials one by one:
#cas [expand] ((x + 1)^3 * (y - 2))
x^3*y - 2*x^3 + 3*x^2*y - 6*x^2 + 3*x*y - 6*x + y - 2Checked 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
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 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 :
#cas [collect x] ((x + 1)^3 * (y - 2))
polynomial in x
(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
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)
coefficient reads off one coefficient:
#cas [coefficient x 2] ((x + y)^3)
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
Statement about the polynomial
∀ (y : ℝ), ((Polynomial.X + Polynomial.C y) ^ 3).coeff 2 = 3 * y
coefficientList reads off all of them, the constant term first:
#cas [coefficientList x] (a*x^2 + b*x + c)
coefficients, constant term first
[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
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
exponent gives the degree in . It is under the hypothesis :
#cas [exponent x] (a*x^2 + x + 1)
degree
2- Domain
x ↦ X in ℝ[X], parameters in ℝ- Input (polynomial)
(a*x^2 + x + 1)- Conditions
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
Statement about the polynomial
∀ (a : ℝ), a ≠ 0 → (Polynomial.C a * Polynomial.X ^ 2 + Polynomial.X + 1).natDegree = 2
Possible Issues (2)
A quotient by a term with a variable is refused:
#cas [collect x] (1/x + x)
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:
#cas [apart] (x^3/(x^2+1))
partial fractions over the written factors, refined by gcds
x - x/(x^2 + 1)- Input (rational function)
(x^3/(x^2+1))- Conditions
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
Identity with the partial fraction decomposition
∀ (x : ℝ), x ^ 2 + 1 ≠ 0 → x ^ 3 / (x ^ 2 + 1) = x - x / (x ^ 2 + 1)
