Mode of #cas

nintegrate

A definite integral to a fixed number of decimal places, with a theorem that bounds the error.

#cas [nintegrate x in [a, b]] f
gives the integral of f from a to b as a decimal c with 6 places, and proves |∫abfdx−c|≤10−6.
#cas (digits := d) [nintegrate x in [a, b]] f
the same with d decimal places, from 1 to 12.

Some integrands have no elementary antiderivative, e−x2 among them, and even when there is one, I often find a decimal with an error bound more useful than the closed formula. That is what nintegrate gives you: a decimal, and a theorem about Mathlib’s interval integral that says how far the decimal can be from the true value.

Details and Options

  • For the printed c±10−d the theorem is |(∫abfdx)−c|≤1/10d, stated with ∫ x in a..b.
  • The proof is a composite Simpson rule. Its remainder comes from an enclosure of the fourth derivative of f on every panel, and the number of panels doubles from 4 to 1024 until the decimals are fixed. Decimals that 1024 panels do not fix are refused, and the message says to ask for fewer digits.
  • A domain checker proves that f and its first four derivatives are defined on [a,b]. A factor x or a fractional power of x at 0 is first removed by a substitution, so ∫01xdx works.
  • The limits are closed rational expressions with a<b, such as -1 or 3/2. f is an expression in x alone, in the grammar of N.
  • (digits := d) sets the number of decimal places, from 1 to 12, by default 6.
  • The tactic cega proves the printed statement in a proof, and also bounds such as ∫abf≤c for integrands that are only continuous.
  • With CASOracle.Numeric imported, #cas [nintegrate x in [a, b], oracle := sympy] f asks an external system for the value. sage, pari and flint work the same way. Lean proves the same statement about the decimal that the system proposes, by the same Simpson rule, or refuses it. See oracle.

Examples (14)#

Basic Examples (2)

The Gaussian integral over [0,1], whose integrand has no elementary antiderivative:

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

certified value of the integral

0.746824±10−6
0.746824 ± 10^-6
Domain
x ∈ [0, 1]

Checked by Lean’s kernel: Certified Simpson panels in one kernel check

|(∫ (x : ℝ) in 0..1, Real.exp (-x ^ 2)) - 746824 / 10 ^ 6| ≤ 1 / 10 ^ 6
The certificate: 1 checked step
  1. Certified Simpson panels in one kernel check

    |(∫ (x : ℝ) in 0..1, Real.exp (-x ^ 2)) - 746824 / 10 ^ 6| ≤ 1 / 10 ^ 6

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    Batch Simpson certificate with 16 panels; fourth-derivative bounds by interval evaluation of the simplified symbolic derivative

The integral of 1/(1+x2) over [0,1] is π/4=0.785398…:

In[2]:=
#cas [nintegrate x in [0, 1]] (1 / (1 + x^2))
Out[2]=

certified value of the integral

0.785398±10−6
0.785398 ± 10^-6
Domain
x ∈ [0, 1]

Checked by Lean’s kernel: Certified Simpson panels in one kernel check

|(∫ (x : ℝ) in 0..1, 1 / (1 + x ^ 2)) - 785398 / 10 ^ 6| ≤ 1 / 10 ^ 6
The certificate: 1 checked step
  1. Certified Simpson panels in one kernel check

    |(∫ (x : ℝ) in 0..1, 1 / (1 + x ^ 2)) - 785398 / 10 ^ 6| ≤ 1 / 10 ^ 6

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    Batch Simpson certificate with 16 panels; fourth-derivative bounds by interval evaluation of the simplified symbolic derivative

Scope (3)

Logarithms, products and longer intervals:

In[3]:=
#cas (digits := 8) [nintegrate x in [1, 2]] (log x)
Out[3]=

certified value of the integral

0.38629436±10−8
0.38629436 ± 10^-8
Domain
x ∈ [1, 2]

Checked by Lean’s kernel: Certified Simpson panels in one kernel check

|(∫ (x : ℝ) in 1..2, Real.log x) - 38629436 / 10 ^ 8| ≤ 1 / 10 ^ 8
The certificate: 1 checked step
  1. Certified Simpson panels in one kernel check

    |(∫ (x : ℝ) in 1..2, Real.log x) - 38629436 / 10 ^ 8| ≤ 1 / 10 ^ 8

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    Batch Simpson certificate with 32 panels; fourth-derivative bounds by interval evaluation of the simplified symbolic derivative
In[4]:=
#cas (digits := 4) [nintegrate x in [0, 2]] (x * exp x)
Out[4]=

certified value of the integral

8.3891±10−4
8.3891 ± 10^-4
Domain
x ∈ [0, 2]

Checked by Lean’s kernel: Certified Simpson panels in one kernel check

|(∫ (x : ℝ) in 0..2, x * Real.exp x) - 83891 / 10 ^ 4| ≤ 1 / 10 ^ 4
The certificate: 1 checked step
  1. Certified Simpson panels in one kernel check

    |(∫ (x : ℝ) in 0..2, x * Real.exp x) - 83891 / 10 ^ 4| ≤ 1 / 10 ^ 4

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    Batch Simpson certificate with 8 panels; fourth-derivative bounds by interval evaluation of the simplified symbolic derivative

A square root at the left end. A substitution removes it before the Simpson rule runs:

In[5]:=
#cas [nintegrate x in [0, 1]] (sqrt x)
Out[5]=

certified value of the integral

0.666667±10−6
0.666667 ± 10^-6
Domain
x ∈ [0, 1]

Checked by Lean’s kernel: Substitution x = u ^ 2 and certified Simpson panels

|(∫ (x : ℝ) in 0..1, √x) - 666667 / 10 ^ 6| ≤ 1 / 10 ^ 6
The certificate: 1 checked step
  1. Substitution x = u ^ 2 and certified Simpson panels

    |(∫ (x : ℝ) in 0..1, √x) - 666667 / 10 ^ 6| ≤ 1 / 10 ^ 6

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    substitution x = u^2 on [0, 1], 4 panels on [0, 1] in u
In[6]:=
#cas [nintegrate x in [0, 1]] (sqrt x * exp x)
Out[6]=

certified value of the integral

1.255630±10−6
1.255630 ± 10^-6
Domain
x ∈ [0, 1]

Checked by Lean’s kernel: Substitution x = u ^ 2 and certified Simpson panels

|(∫ (x : ℝ) in 0..1, √x * Real.exp x) - 1255630 / 10 ^ 6| ≤ 1 / 10 ^ 6
The certificate: 1 checked step
  1. Substitution x = u ^ 2 and certified Simpson panels

    |(∫ (x : ℝ) in 0..1, √x * Real.exp x) - 1255630 / 10 ^ 6| ≤ 1 / 10 ^ 6

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    substitution x = u^2 on [0, 1], 32 panels on [0, 1] in u

Fractional powers of x work the same way:

In[7]:=
#cas [nintegrate x in [0, 1]] (x ^ (1/3 : ℝ))
Out[7]=

certified value of the integral

0.750000±10−6
0.750000 ± 10^-6
Domain
x ∈ [0, 1]

Checked by Lean’s kernel: Substitution x = u ^ 3 and certified Simpson panels

|(∫ (x : ℝ) in 0..1, x ^ (1 / 3 : ℝ)) - 750000 / 10 ^ 6| ≤ 1 / 10 ^ 6
The certificate: 1 checked step
  1. Substitution x = u ^ 3 and certified Simpson panels

    |(∫ (x : ℝ) in 0..1, x ^ (1 / 3 : ℝ)) - 750000 / 10 ^ 6| ≤ 1 / 10 ^ 6

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    substitution x = u^3 on [0, 1], 4 panels on [0, 1] in u
In[8]:=
#cas [nintegrate x in [0, 1]] (x ^ (2/3 : ℝ) / (1 + x))
Out[8]=

certified value of the integral

0.379348±10−6
0.379348 ± 10^-6
Domain
x ∈ [0, 1]

Checked by Lean’s kernel: Substitution x = u ^ 3 and certified Simpson panels

|(∫ (x : ℝ) in 0..1, x ^ (2 / 3 : ℝ) / (1 + x)) - 379348 / 10 ^ 6| ≤ 1 / 10 ^ 6
The certificate: 1 checked step
  1. Substitution x = u ^ 3 and certified Simpson panels

    |(∫ (x : ℝ) in 0..1, x ^ (2 / 3 : ℝ) / (1 + x)) - 379348 / 10 ^ 6| ≤ 1 / 10 ^ 6

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    substitution x = u^3 on [0, 1], 32 panels on [0, 1] in u

Options (1)

(digits := 8) asks for eight places:

In[9]:=
#cas (digits := 8) [nintegrate x in [0, 1]] (sin x)
Out[9]=

certified value of the integral

0.45969770±10−8
0.45969770 ± 10^-8
Domain
x ∈ [0, 1]

Checked by Lean’s kernel: Certified Simpson panels in one kernel check

|(∫ (x : ℝ) in 0..1, Real.sin x) - 45969770 / 10 ^ 8| ≤ 1 / 10 ^ 8
The certificate: 1 checked step
  1. Certified Simpson panels in one kernel check

    |(∫ (x : ℝ) in 0..1, Real.sin x) - 45969770 / 10 ^ 8| ≤ 1 / 10 ^ 8

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    Batch Simpson certificate with 16 panels; fourth-derivative bounds by interval evaluation of the simplified symbolic derivative

Applications (2)

The printed value in a proof. cega proves the same statement, and only the three standard axioms are used:

In[10]:=
theorem nintegrate_gauss :
    |(∫ x in (0 : ℝ)..1, Real.exp (-x ^ 2)) - 746824 / 10 ^ 6| ≤ 1 / 10 ^ 6 := by
  cega

#print axioms nintegrate_gauss
Out[10]=

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


A one-sided bound is often all a proof needs:

In[11]:=
example : (∫ x in (0 : ℝ)..1, Real.exp (-x ^ 2)) ≤ 3 / 4 := by
  cega
Out[11]=

Lean accepts the proof.

Properties and Relations (2)

integrate gives exact values when it finds an antiderivative. Here both agree:

In[12]:=
#cas [integrate x in [0, 1]] (x * exp x)
Out[12]=

definite integral

1
Primitive
exp(x)*(x - 1)

Checked by Lean’s kernel: Definite integral, proved by cas

∫ (x : ℝ) in 0..1, x * Real.exp x = 1
The certificate: 1 checked step
  1. Definite integral, proved by cas

    ∫ (x : ℝ) in 0..1, x * Real.exp x = 1

    Checked by Lean’s kernel

The computation
  • Variables: x
In[13]:=
#cas [nintegrate x in [0, 1]] (x * exp x)
Out[13]=

certified value of the integral

1.000000±10−6
1.000000 ± 10^-6
Domain
x ∈ [0, 1]

Checked by Lean’s kernel: Certified Simpson panels in one kernel check

|(∫ (x : ℝ) in 0..1, x * Real.exp x) - 1000000 / 10 ^ 6| ≤ 1 / 10 ^ 6
The certificate: 1 checked step
  1. Certified Simpson panels in one kernel check

    |(∫ (x : ℝ) in 0..1, x * Real.exp x) - 1000000 / 10 ^ 6| ≤ 1 / 10 ^ 6

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    Batch Simpson certificate with 16 panels; fourth-derivative bounds by interval evaluation of the simplified symbolic derivative

When there is no elementary antiderivative, integrate finds none and nintegrate still gives digits:

In[14]:=
#cas [integrate x in [0, 1]] (exp (x ^ 2))
Out[14]=

Error

the checked integrator found no supported antiderivative in coordinate 'x'. For a possible numerical enclosure, try `#cas [nintegrate x in [0, 1]] (exp (x ^ 2))`.
In[15]:=
#cas [nintegrate x in [0, 1]] (exp (x ^ 2))
Out[15]=

certified value of the integral

1.462652±10−6
1.462652 ± 10^-6
Domain
x ∈ [0, 1]

Checked by Lean’s kernel: Certified Simpson panels in one kernel check

|(∫ (x : ℝ) in 0..1, Real.exp (x ^ 2)) - 1462652 / 10 ^ 6| ≤ 1 / 10 ^ 6
The certificate: 1 checked step
  1. Certified Simpson panels in one kernel check

    |(∫ (x : ℝ) in 0..1, Real.exp (x ^ 2)) - 1462652 / 10 ^ 6| ≤ 1 / 10 ^ 6

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    Batch Simpson certificate with 16 panels; fourth-derivative bounds by interval evaluation of the simplified symbolic derivative

Possible Issues (3)

A pole in the interval. 1/x has no finite integral on [0,1], and no certificate exists:

In[16]:=
#cas [nintegrate x in [0, 1]] (1 / x)
Out[16]=

Error

nintegrate: the domain checker found no fourth-derivative validity certificate for the integrand on [0, 1] and no substitution removes the singularity; a bound such as `(∫ x in a..b, f x) ≤ c` is proved by `cega`, whose range panels need only continuity

At most 12 places:

In[17]:=
#cas (digits := 16) [nintegrate x in [0, 1]] (x)
Out[17]=

Error

nintegrate accepts between 1 and 12 digits

The smaller limit comes first:

In[18]:=
#cas [nintegrate x in [1, 0]] (x)
Out[18]=

Error

nintegrate needs rational limits a < b

Neat Examples (1)

A square root and an oscillation together, over [0,4]:

In[19]:=
#cas [nintegrate x in [0, 4]] (sqrt x * cos x)
Out[19]=

certified value of the integral

−2.318381±10−6
-2.318381 ± 10^-6
Domain
x ∈ [0, 4]

Checked by Lean’s kernel: Substitution x = u ^ 2 and certified Simpson panels

|(∫ (x : ℝ) in 0..4, √x * Real.cos x) + 2318381 / 10 ^ 6| ≤ 1 / 10 ^ 6
The certificate: 1 checked step
  1. Substitution x = u ^ 2 and certified Simpson panels

    |(∫ (x : ℝ) in 0..4, √x * Real.cos x) + 2318381 / 10 ^ 6| ≤ 1 / 10 ^ 6

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    substitution x = u^2 on [0, 4], 64 panels on [0, 2] in u

See Also

integrate · N · enclose · cega · ndsolve