Mode of #cas

exponent

Gives the degree of a polynomial in one variable, with a theorem about Mathlib’s Polynomial.natDegree.

#cas [exponent x] e
gives the degree of the polynomial e in x and proves it about Mathlib’s Polynomial.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 (x+1)3−x3 has degree 2. 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 ax2+x+1 the theorem is ∀ a : ℝ, a ≠ 0 → (C a * X ^ 2 + X + 1).natDegree = 2, and the report prints the condition a≠0. I would much rather see that condition spelled out than get a degree that is wrong for one value of a.

Details and Options

  • The theorem is P.natDegree = d, where P is e read as a polynomial as for coefficient: in ℚ[X], or in ℝ[X] for all values of the other variables, which enter as constants C y.
  • When the leading coefficient c involves the other variables, c≠0 is a hypothesis of the theorem: c≠0→ P.natDegree = d.
  • The degree is that of the expanded polynomial, so terms that cancel do not count.
  • The zero polynomial is refused. Mathlib sets natDegree 0 = 0 and degree 0 = ⊥.
  • The input is a polynomial with rational coefficients, as for collect. A quotient by a term with a variable is refused.
  • There are no options. The tactic cas proves natDegree goals in ℚ[X], and it refuses a wrong degree with the right one in its message.

Examples (10)#

Basic Examples (2)

The x3 terms cancel, and the degree is 2:

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

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


With a parameter in the leading coefficient, the degree comes with a condition:

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

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

Scope (1)

A product is expanded first, and its degree is the sum of the degrees of its factors:

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

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
  1. Statement about the polynomial

    ((Polynomial.X ^ 2 + 1) ^ 5 * (Polynomial.X - 1)).natDegree = 11

    Checked by Lean’s kernel

Applications (1)

The first example as a theorem about Polynomial.natDegree. Only the three standard axioms are used:

In[4]:=
open Polynomial in
theorem degree_of_difference : ((X + 1) ^ 3 - X ^ 3 : ℚ[X]).natDegree = 2 := by
  cas

#print axioms degree_of_difference
Out[4]=

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

Properties and Relations (3)

coefficient gives the leading coefficient, the coefficient of xd:

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

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
  1. Statement about the polynomial

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

    Checked by Lean’s kernel


coefficientList has one entry more than the degree:

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

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


A polynomial of degree 5:

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

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
  1. Statement about the polynomial

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

    Checked by Lean’s kernel

It has at most 5 roots. roots finds 4 real ones, since 1 is a double root:

In[8]:=
#cas [roots x] ((x - 1)^2 * (x + 2) * (x^2 - 2))
Out[8]=

all 4 real solutions; enclosed ones by 20 decimals, truncated

−2,1,−1.41421356237309504880,1.41421356237309504880
-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
  1. 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)

    Checked by Lean’s kernel · CAS.Polynomial.RealRoot.of_exprRootCheck

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:

In[9]:=
#cas [exponent x] (0)
Out[9]=

Error

exponent: the polynomial is zero

A quotient by the variable is not a polynomial:

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

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:

In[11]:=
open Polynomial in
example : (X ^ 3 + 1 : ℚ[X]).natDegree = 2 := by
  cas
Out[11]=

Error

cas: the degree is 3, not 2

See Also

coefficient · coefficientList · collect · roots