Mode of #cas
exponent
Gives the degree of a polynomial in one variable, with a theorem about Mathlib’s Polynomial.natDegree.
#cas [exponent x] ePolynomial.natDegree.The degree of a polynomial isn’t always the one you see on the page, because terms can cancel. exponent reads the degree off the expanded form, after the cancellations, so has degree . The theorem is P.natDegree = d about Mathlib’s polynomials.
Parameters make this more delicate than it looks. When the leading coefficient involves the other variables it could be zero, so the theorem assumes that it isn’t. For the theorem is ∀ a : ℝ, a ≠ 0 → (C a * X ^ 2 + X + 1).natDegree = 2, and the report prints the condition . I would much rather see that condition spelled out than get a degree that is wrong for one value of .
Examples (10)#
Basic Examples (2)
The terms cancel, and the degree is :
#cas [exponent x] ((x + 1)^3 - x^3)
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
Statement about the polynomial
((Polynomial.X + 1) ^ 3 - Polynomial.X ^ 3).natDegree = 2
With a parameter in the leading coefficient, the degree comes with a condition:
#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
Scope (1)
A product is expanded first, and its degree is the sum of the degrees of its factors:
#cas [exponent x] ((x^2 + 1)^5 * (x - 1))
degree
11- Domain
x ↦ X in ℚ[X]- Input (polynomial)
((x^2 + 1)^5 * (x - 1))
Checked by Lean’s kernel: Statement about the polynomial
((Polynomial.X ^ 2 + 1) ^ 5 * (Polynomial.X - 1)).natDegree = 11
The certificate: 1 checked step
Statement about the polynomial
((Polynomial.X ^ 2 + 1) ^ 5 * (Polynomial.X - 1)).natDegree = 11
Applications (1)
The first example as a theorem about Polynomial.natDegree. Only the three standard axioms are used:
open Polynomial in theorem degree_of_difference : ((X + 1) ^ 3 - X ^ 3 : ℚ[X]).natDegree = 2 := by cas #print axioms degree_of_difference
degree_of_difference depends on the axioms propext, Classical.choice, Quot.sound.
Properties and Relations (3)
coefficient gives the leading coefficient, the coefficient of :
#cas [coefficient x 2] ((x + 1)^3 - x^3)
coefficient of x^2
3- 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).coeff 2 = 3
The certificate: 1 checked step
Statement about the polynomial
((Polynomial.X + 1) ^ 3 - Polynomial.X ^ 3).coeff 2 = 3
coefficientList has one entry more than the degree:
#cas [coefficientList x] ((x + 1)^3 - x/2)
coefficients, constant term first
[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
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
A polynomial of degree :
#cas [exponent x] ((x - 1)^2 * (x + 2) * (x^2 - 2))
degree
5- Domain
x ↦ X in ℚ[X]- Input (polynomial)
((x - 1)^2 * (x + 2) * (x^2 - 2))
Checked by Lean’s kernel: Statement about the polynomial
((Polynomial.X - 1) ^ 2 * (Polynomial.X + 2) * (Polynomial.X ^ 2 - 2)).natDegree = 5
The certificate: 1 checked step
Statement about the polynomial
((Polynomial.X - 1) ^ 2 * (Polynomial.X + 2) * (Polynomial.X ^ 2 - 2)).natDegree = 5
It has at most roots. roots finds real ones, since is a double root:
#cas [roots x] ((x - 1)^2 * (x + 2) * (x^2 - 2))
all 4 real solutions; enclosed ones by 20 decimals, truncated
-2, 1, -1.41421356237309504880, 1.41421356237309504880- Input (polynomial equation)
((x - 1)^2 * (x + 2) * (x^2 - 2))
Checked by Lean’s kernel: Complete list of real solutions
((∃! x,
x ∈ Set.Ioo (-141421356237309504881 / 10 ^ 20) (-141421356237309504880 / 10 ^ 20) ∧
(x - 1) ^ 2 * (x + 2) * (x ^ 2 - 2) = 0) ∧
∃! x,
x ∈ Set.Ioo (141421356237309504880 / 10 ^ 20) (141421356237309504881 / 10 ^ 20) ∧
(x - 1) ^ 2 * (x + 2) * (x ^ 2 - 2) = 0) ∧
((-2 - 1) ^ 2 * (-2 + 2) * ((-2) ^ 2 - 2) = 0 ∧ (1 - 1) ^ 2 * (1 + 2) * (1 ^ 2 - 2) = 0) ∧
∀ (x : ℝ),
(x - 1) ^ 2 * (x + 2) * (x ^ 2 - 2) = 0 →
(x = -2 ∨ x = 1) ∨
x ∈ Set.Ioo (-141421356237309504881 / 10 ^ 20) (-141421356237309504880 / 10 ^ 20) ∨
x ∈ Set.Ioo (141421356237309504880 / 10 ^ 20) (141421356237309504881 / 10 ^ 20)from CAS.Polynomial.RealRoot.of_exprRootCheck
The certificate: 1 checked step
Complete list of real solutions
((∃! x, x ∈ Set.Ioo (-141421356237309504881 / 10 ^ 20) (-141421356237309504880 / 10 ^ 20) ∧ (x - 1) ^ 2 * (x + 2) * (x ^ 2 - 2) = 0) ∧ ∃! x, x ∈ Set.Ioo (141421356237309504880 / 10 ^ 20) (141421356237309504881 / 10 ^ 20) ∧ (x - 1) ^ 2 * (x + 2) * (x ^ 2 - 2) = 0) ∧ ((-2 - 1) ^ 2 * (-2 + 2) * ((-2) ^ 2 - 2) = 0 ∧ (1 - 1) ^ 2 * (1 + 2) * (1 ^ 2 - 2) = 0) ∧ ∀ (x : ℝ), (x - 1) ^ 2 * (x + 2) * (x ^ 2 - 2) = 0 → (x = -2 ∨ x = 1) ∨ x ∈ Set.Ioo (-141421356237309504881 / 10 ^ 20) (-141421356237309504880 / 10 ^ 20) ∨ x ∈ Set.Ioo (141421356237309504880 / 10 ^ 20) (141421356237309504881 / 10 ^ 20)
The computation
Certified isolation
2 rational roots split off; square-free remaining factor of degree 2, Cauchy bound 3, 4 cells Guarantee: the listed solutions are distinct, each rational is a solution, each decimal interval contains exactly one solution, and there are no other real solutions
Possible Issues (3)
The zero polynomial is refused:
#cas [exponent x] (0)
Error
exponent: the polynomial is zero
A quotient by the variable is not a polynomial:
#cas [exponent x] (x + 1/x)
Error
exponent: the expression is not a polynomial (it divides by a term with a variable)
The tactic cas refuses a wrong degree and gives the right one:
open Polynomial in example : (X ^ 3 + 1 : ℚ[X]).natDegree = 2 := by cas
Error
cas: the degree is 3, not 2
