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#cas (digits := d) [nintegrate x in [a, b]] fSome integrands have no elementary antiderivative, 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.
Examples (14)#
Basic Examples (2)
The Gaussian integral over , whose integrand has no elementary antiderivative:
#cas [nintegrate x in [0, 1]] (exp (-x^2))
certified value of the integral
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
Certified Simpson panels in one kernel check
|(∫ (x : ℝ) in 0..1, Real.exp (-x ^ 2)) - 746824 / 10 ^ 6| ≤ 1 / 10 ^ 6
The computation
Certified quadrature
Batch Simpson certificate with 16 panels; fourth-derivative bounds by interval evaluation of the simplified symbolic derivative
The integral of over is :
#cas [nintegrate x in [0, 1]] (1 / (1 + x^2))
certified value of the integral
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
Certified Simpson panels in one kernel check
|(∫ (x : ℝ) in 0..1, 1 / (1 + x ^ 2)) - 785398 / 10 ^ 6| ≤ 1 / 10 ^ 6
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:
#cas (digits := 8) [nintegrate x in [1, 2]] (log x)
certified value of the integral
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
Certified Simpson panels in one kernel check
|(∫ (x : ℝ) in 1..2, Real.log x) - 38629436 / 10 ^ 8| ≤ 1 / 10 ^ 8
The computation
Certified quadrature
Batch Simpson certificate with 32 panels; fourth-derivative bounds by interval evaluation of the simplified symbolic derivative
#cas (digits := 4) [nintegrate x in [0, 2]] (x * exp x)
certified value of the integral
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
Certified Simpson panels in one kernel check
|(∫ (x : ℝ) in 0..2, x * Real.exp x) - 83891 / 10 ^ 4| ≤ 1 / 10 ^ 4
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:
#cas [nintegrate x in [0, 1]] (sqrt x)
certified value of the integral
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
Substitution x = u ^ 2 and certified Simpson panels
|(∫ (x : ℝ) in 0..1, √x) - 666667 / 10 ^ 6| ≤ 1 / 10 ^ 6
The computation
Certified quadrature
substitution x = u^2 on [0, 1], 4 panels on [0, 1] in u
#cas [nintegrate x in [0, 1]] (sqrt x * exp x)
certified value of the integral
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
Substitution x = u ^ 2 and certified Simpson panels
|(∫ (x : ℝ) in 0..1, √x * Real.exp x) - 1255630 / 10 ^ 6| ≤ 1 / 10 ^ 6
The computation
Certified quadrature
substitution x = u^2 on [0, 1], 32 panels on [0, 1] in u
Fractional powers of work the same way:
#cas [nintegrate x in [0, 1]] (x ^ (1/3 : ℝ))
certified value of the integral
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
Substitution x = u ^ 3 and certified Simpson panels
|(∫ (x : ℝ) in 0..1, x ^ (1 / 3 : ℝ)) - 750000 / 10 ^ 6| ≤ 1 / 10 ^ 6
The computation
Certified quadrature
substitution x = u^3 on [0, 1], 4 panels on [0, 1] in u
#cas [nintegrate x in [0, 1]] (x ^ (2/3 : ℝ) / (1 + x))
certified value of the integral
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
Substitution x = u ^ 3 and certified Simpson panels
|(∫ (x : ℝ) in 0..1, x ^ (2 / 3 : ℝ) / (1 + x)) - 379348 / 10 ^ 6| ≤ 1 / 10 ^ 6
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:
#cas (digits := 8) [nintegrate x in [0, 1]] (sin x)
certified value of the integral
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
Certified Simpson panels in one kernel check
|(∫ (x : ℝ) in 0..1, Real.sin x) - 45969770 / 10 ^ 8| ≤ 1 / 10 ^ 8
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:
theorem nintegrate_gauss : |(∫ x in (0 : ℝ)..1, Real.exp (-x ^ 2)) - 746824 / 10 ^ 6| ≤ 1 / 10 ^ 6 := by cega #print axioms nintegrate_gauss
nintegrate_gauss depends on the axioms propext, Classical.choice, Quot.sound.
A one-sided bound is often all a proof needs:
example : (∫ x in (0 : ℝ)..1, Real.exp (-x ^ 2)) ≤ 3 / 4 := by cega
Lean accepts the proof.
Properties and Relations (2)
integrate gives exact values when it finds an antiderivative. Here both agree:
#cas [integrate x in [0, 1]] (x * exp x)
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
Definite integral, proved by
cas∫ (x : ℝ) in 0..1, x * Real.exp x = 1
The computation
Variables: x
#cas [nintegrate x in [0, 1]] (x * exp x)
certified value of the integral
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
Certified Simpson panels in one kernel check
|(∫ (x : ℝ) in 0..1, x * Real.exp x) - 1000000 / 10 ^ 6| ≤ 1 / 10 ^ 6
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:
#cas [integrate x in [0, 1]] (exp (x ^ 2))
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))`.
#cas [nintegrate x in [0, 1]] (exp (x ^ 2))
certified value of the integral
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
Certified Simpson panels in one kernel check
|(∫ (x : ℝ) in 0..1, Real.exp (x ^ 2)) - 1462652 / 10 ^ 6| ≤ 1 / 10 ^ 6
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. has no finite integral on , and no certificate exists:
#cas [nintegrate x in [0, 1]] (1 / x)
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:
#cas (digits := 16) [nintegrate x in [0, 1]] (x)
Error
nintegrate accepts between 1 and 12 digits
The smaller limit comes first:
#cas [nintegrate x in [1, 0]] (x)
Error
nintegrate needs rational limits a < b
Neat Examples (1)
A square root and an oscillation together, over :
#cas [nintegrate x in [0, 4]] (sqrt x * cos x)
certified value of the integral
-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
Substitution x = u ^ 2 and certified Simpson panels
|(∫ (x : ℝ) in 0..4, √x * Real.cos x) + 2318381 / 10 ^ 6| ≤ 1 / 10 ^ 6
The computation
Certified quadrature
substitution x = u^2 on [0, 4], 64 panels on [0, 2] in u
