Mode of #cas

findRoot

A certified simple root of a function in an interval, to a chosen number of decimals, with a uniqueness theorem.

#cas [findRoot x in [a, b]] f
finds a simple root of f in [a,b], prints it with 10 decimal places, and proves that an interval around the printed value, of half-width at most 10−10, contains exactly one root. f is an expression or an equation g = h.
#cas (digits := d) [findRoot x in [a, b]] f
the same with d decimal places, from 1 to 30.

For an equation such as cosx=x I don’t need a formula. I need a root I can trust, in an interval I chose, and that is what findRoot gives you.

The search is simple. SymLean samples f at 65 equally spaced points of the interval and narrows the first sign change from the left by bisection. The check is the part that matters. The kernel verifies opposite signs at the two ends of a small interval, and an enclosure of the derivative that has one sign on it, so the interval holds exactly one root. The sign change alone would only give existence, and the derivative is what gives uniqueness.

Details and Options

  • For the printed x = m ± 10^-d, the theorem is ∃! x, x ∈ Set.Icc lo hi ∧ f x = 0, where [lo,hi] is [m−10−d,m+10−d] clipped to the bracket of the sign change. For an equation g = h the statement is g x = h x.
  • The endpoints a<b are closed rational expressions, such as -1, 3/2 or 10^200. f is an expression or an equation in x alone, in the calculator grammar of N.
  • A root where the derivative vanishes and a sign change across a pole are refused. Two roots between the same two sample points show no sign change and are not found. log and sqrt are evaluated only at positive arguments. Sample points where such an argument is 0 or less are skipped, so [a,b] may contain them away from the root. A sign change across such points is refused, even where Mathlib’s total functions have a root.
  • (digits := d) is the number of decimal places, from 1 to 30, by default 10.
  • In a proof, cas proves that a root lies between two rational bounds, such as ∃ x : ℝ, Real.cos x = x ∧ 0.739 < x ∧ x < 0.74, and that it is unique in an interval where the derivative has one sign.
  • With CASOracle.Numeric imported, #cas [nsolve x in [a, b], oracle := sympy] (g = h) asks an external system for the roots in the interval. Lean proves that a root lies within 10−d of each decimal with d places, and does not check that the root is simple or that the list is complete. See oracle.

Examples (15)#

Basic Examples (2)

The root of cosx=x, with the theorem that it is the only one within 10−10 of the printed value:

In[1]:=
#cas [findRoot x in [0, 1]] (cos x = x)
Out[1]=

certified simple root

x=0.7390851332±10−10
x = 0.7390851332 ± 10^-10
Domain
x ∈ [0, 1]
Input (equation in ℝ)
(cos x = x)

Checked by Lean’s kernel: Unique root in the interval

∃! x, x ∈ Set.Icc (7390851331 / 10 ^ 10) (7390851333 / 10 ^ 10) ∧ Real.cos x = x
The certificate: 1 checked step
  1. Unique root in the interval

    ∃! x, x ∈ Set.Icc (7390851331 / 10 ^ 10) (7390851333 / 10 ^ 10) ∧ Real.cos x = x

    Checked by Lean’s kernel

The computation
  • Certified root

    Sign change found by interval sampling and bisection (not proof)
    Unique root in [0.7390851331, 0.7390851333], within 10^-10 of 0.7390851332: endpoint signs and a derivative of one sign, checked by the kernel

An expression stands for the equation f=0:

In[2]:=
#cas [findRoot x in [1, 2]] (x^2 - 2)
Out[2]=

certified simple root

x=1.4142135624±10−10
x = 1.4142135624 ± 10^-10
Domain
x ∈ [1, 2]

Checked by Lean’s kernel: Unique root in the interval

∃! x, x ∈ Set.Icc (14142135623 / 10 ^ 10) (14142135625 / 10 ^ 10) ∧ x ^ 2 - 2 = 0
The certificate: 1 checked step
  1. Unique root in the interval

    ∃! x, x ∈ Set.Icc (14142135623 / 10 ^ 10) (14142135625 / 10 ^ 10) ∧ x ^ 2 - 2 = 0

    Checked by Lean’s kernel

The computation
  • Certified root

    Sign change found by interval sampling and bisection (not proof)
    Unique root in [1.4142135623, 1.4142135625], within 10^-10 of 1.4142135624: endpoint signs and a derivative of one sign, checked by the kernel

Scope (3)

A logarithm, on an interval of positive numbers:

In[3]:=
#cas [findRoot x in [1, 3]] (log x + x - 2)
Out[3]=

certified simple root

x=1.5571455990±10−10
x = 1.5571455990 ± 10^-10
Domain
x ∈ [1, 3]

Checked by Lean’s kernel: Unique root in the interval

∃! x, x ∈ Set.Icc (15571455989 / 10 ^ 10) (15571455991 / 10 ^ 10) ∧ Real.log x + x - 2 = 0
The certificate: 1 checked step
  1. Unique root in the interval

    ∃! x, x ∈ Set.Icc (15571455989 / 10 ^ 10) (15571455991 / 10 ^ 10) ∧ Real.log x + x - 2 = 0

    Checked by Lean’s kernel

The computation
  • Certified root

    Sign change found by interval sampling and bisection (not proof)
    Unique root in [1.5571455989, 1.5571455991], within 10^-10 of 1.5571455990: endpoint signs and a derivative of one sign, checked by the kernel

The root of sinx between 2 and 4 is π:

In[4]:=
#cas [findRoot x in [2, 4]] (sin x)
Out[4]=

certified simple root

x=3.1415926536±10−10
x = 3.1415926536 ± 10^-10
Domain
x ∈ [2, 4]

Checked by Lean’s kernel: Unique root in the interval

∃! x, x ∈ Set.Icc (31415926535 / 10 ^ 10) (31415926537 / 10 ^ 10) ∧ Real.sin x = 0
The certificate: 1 checked step
  1. Unique root in the interval

    ∃! x, x ∈ Set.Icc (31415926535 / 10 ^ 10) (31415926537 / 10 ^ 10) ∧ Real.sin x = 0

    Checked by Lean’s kernel

The computation
  • Certified root

    Sign change found by interval sampling and bisection (not proof)
    Unique root in [3.1415926535, 3.1415926537], within 10^-10 of 3.1415926536: endpoint signs and a derivative of one sign, checked by the kernel

The interval may be wide, with a large endpoint:

In[5]:=
#cas [findRoot x in [0, 10^200]] (x - 1)
Out[5]=

certified simple root

x=1.0000000000±10−10
x = 1.0000000000 ± 10^-10
Domain
x ∈ [0, 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]

Checked by Lean’s kernel: Unique root in the interval

∃! x, x ∈ Set.Icc (9999999999 / 10 ^ 10) (10000000001 / 10 ^ 10) ∧ x - 1 = 0
The certificate: 1 checked step
  1. Unique root in the interval

    ∃! x, x ∈ Set.Icc (9999999999 / 10 ^ 10) (10000000001 / 10 ^ 10) ∧ x - 1 = 0

    Checked by Lean’s kernel

The computation
  • Certified root

    Sign change found by interval sampling and bisection (not proof)
    Unique root in [0.9999999999, 1.0000000001], within 10^-10 of 1.0000000000: endpoint signs and a derivative of one sign, checked by the kernel

Options (2)

(digits := 20) gives 20 decimal places, here of log3:

In[6]:=
#cas (digits := 20) [findRoot x in [0, 2]] (exp x - 3)
Out[6]=

certified simple root

x=1.09861228866810969140±10−20
x = 1.09861228866810969140 ± 10^-20
Domain
x ∈ [0, 2]

Checked by Lean’s kernel: Unique root in the interval

∃! x,
  x ∈ Set.Icc (109861228866810969139 / 10 ^ 20) (109861228866810969141 / 10 ^ 20) ∧ Real.exp x - 3 = 0
The certificate: 1 checked step
  1. Unique root in the interval

    ∃! x,
      x ∈ Set.Icc (109861228866810969139 / 10 ^ 20) (109861228866810969141 / 10 ^ 20) ∧ Real.exp x - 3 = 0

    Checked by Lean’s kernel

The computation
  • Certified root

    Sign change found by interval sampling and bisection (not proof)
    Unique root in [1.09861228866810969139, 1.09861228866810969141], within 10^-20 of 1.09861228866810969140: endpoint signs and a derivative of one sign, checked by the kernel

At most 30 places:

In[7]:=
#cas (digits := 30) [findRoot x in [1, 2]] (x^2 - 2)
Out[7]=

certified simple root

x=1.414213562373095048801688724210±10−30
x = 1.414213562373095048801688724210 ± 10^-30
Domain
x ∈ [1, 2]

Checked by Lean’s kernel: Unique root in the interval

∃! x,
  x ∈ Set.Icc (1414213562373095048801688724209 / 10 ^ 30) (1414213562373095048801688724211 / 10 ^ 30) ∧
    x ^ 2 - 2 = 0
The certificate: 1 checked step
  1. Unique root in the interval

    ∃! x,
      x ∈ Set.Icc (1414213562373095048801688724209 / 10 ^ 30) (1414213562373095048801688724211 / 10 ^ 30) ∧
        x ^ 2 - 2 = 0

    Checked by Lean’s kernel

The computation
  • Certified root

    Sign change found by interval sampling and bisection (not proof)
    Unique root in [1.414213562373095048801688724209, 1.414213562373095048801688724211], within 10^-30 of 1.414213562373095048801688724210: endpoint signs and a derivative of one sign, checked by the kernel

Applications (2)

Bounds on the root in a proof. cas proves them, and only the three standard axioms are used:

In[8]:=
theorem findRoot_cos : ∃ x : ℝ, Real.cos x = x ∧ 0.739 < x ∧ x < 0.74 := by
  cas

#print axioms findRoot_cos
Out[8]=

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


Uniqueness on the whole interval needs a derivative of one sign on it, and −sinx−1<0 on [0,1]:

In[9]:=
example : ∃! x : ℝ, x ∈ Set.Icc (0 : ℝ) 1 ∧ Real.cos x = x := by cas
Out[9]=

Lean accepts the proof.

Properties and Relations (1)

For a polynomial, roots finds every real root at once, here both square roots of 2:

In[10]:=
#cas (digits := 40) [roots x] (x^2 = 2)
Out[10]=

all 2 real solutions; enclosed ones by 40 decimals, truncated

−1.4142135623730950488016887242096980785696,1.4142135623730950488016887242096980785696
-1.4142135623730950488016887242096980785696, 1.4142135623730950488016887242096980785696
Input (polynomial equation)
(x^2 = 2)

Checked by Lean’s kernel: Complete list of real solutions

((∃! x,
      x ∈
          Set.Ioo (-14142135623730950488016887242096980785697 / 10 ^ 40)
            (-14142135623730950488016887242096980785696 / 10 ^ 40) ∧
        x ^ 2 - 2 = 0) ∧
    ∃! x,
      x ∈
          Set.Ioo (14142135623730950488016887242096980785696 / 10 ^ 40)
            (14142135623730950488016887242096980785697 / 10 ^ 40) ∧
        x ^ 2 - 2 = 0) ∧
  ∀ (x : ℝ),
    x ^ 2 - 2 = 0 →
      x ∈
          Set.Ioo (-14142135623730950488016887242096980785697 / 10 ^ 40)
            (-14142135623730950488016887242096980785696 / 10 ^ 40) ∨
        x ∈
          Set.Ioo (14142135623730950488016887242096980785696 / 10 ^ 40)
            (14142135623730950488016887242096980785697 / 10 ^ 40)

from CAS.Polynomial.RealRoot.of_exprRootCheck

The certificate: 1 checked step
  1. Complete list of real solutions

    ((∃! x,
          x ∈
              Set.Ioo (-14142135623730950488016887242096980785697 / 10 ^ 40)
                (-14142135623730950488016887242096980785696 / 10 ^ 40) ∧
            x ^ 2 - 2 = 0) ∧
        ∃! x,
          x ∈
              Set.Ioo (14142135623730950488016887242096980785696 / 10 ^ 40)
                (14142135623730950488016887242096980785697 / 10 ^ 40) ∧
            x ^ 2 - 2 = 0) ∧
      ∀ (x : ℝ),
        x ^ 2 - 2 = 0 →
          x ∈
              Set.Ioo (-14142135623730950488016887242096980785697 / 10 ^ 40)
                (-14142135623730950488016887242096980785696 / 10 ^ 40) ∨
            x ∈
              Set.Ioo (14142135623730950488016887242096980785696 / 10 ^ 40)
                (14142135623730950488016887242096980785697 / 10 ^ 40)

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

The computation
  • Certified isolation

    Square-free polynomial of degree 2, Cauchy bound 3, 6 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)

A double root does not change sign. Find the root of the derivative, or use roots for a polynomial:

In[11]:=
#cas [findRoot x in [-1, 1]] (x^2)
Out[11]=

Error

findRoot: no sign change of the expression at 65 equally spaced points of [-1, 1]; a root of even multiplicity, or two roots closer than the sample spacing, has no certificate here

At a triple root the derivative vanishes too, and the sign near the root cannot be decided:

In[12]:=
#cas [findRoot x in [-1, 1]] (x^3)
Out[12]=

Error

findRoot: the sign change could not be narrowed below [-1/34359738368, 7/274877906944] (interval evaluation does not decide the sign near the root); ask for fewer digits

1/(x−1) changes sign at its pole and has no root. Choose an interval without poles:

In[13]:=
#cas [findRoot x in [0, 2]] (1 / (x - 1))
Out[13]=

Error

findRoot: the derivative cannot be evaluated on [9999999999/10000000000, 10000000001/10000000000], where the sign changes; the expression may have a singularity there rather than a root

Neat Examples (2)

The omega constant, the root of xex=1:

In[14]:=
#cas [findRoot x in [0, 1]] (x * exp x - 1)
Out[14]=

certified simple root

x=0.5671432904±10−10
x = 0.5671432904 ± 10^-10
Domain
x ∈ [0, 1]

Checked by Lean’s kernel: Unique root in the interval

∃! x, x ∈ Set.Icc (5671432903 / 10 ^ 10) (5671432905 / 10 ^ 10) ∧ x * Real.exp x - 1 = 0
The certificate: 1 checked step
  1. Unique root in the interval

    ∃! x, x ∈ Set.Icc (5671432903 / 10 ^ 10) (5671432905 / 10 ^ 10) ∧ x * Real.exp x - 1 = 0

    Checked by Lean’s kernel

The computation
  • Certified root

    Sign change found by interval sampling and bisection (not proof)
    Unique root in [0.5671432903, 0.5671432905], within 10^-10 of 0.5671432904: endpoint signs and a derivative of one sign, checked by the kernel

The same root as a goal:

In[15]:=
example : ∃ x ∈ Set.Icc (0 : ℝ) 1, x * Real.exp x = 1 := by cas
Out[15]=

Lean accepts the proof.

See Also

roots · solve · enclose · N · cas