The OpenD Programming Language

integrate

Integrates function on the interval [a, b] using adaptive Gauss-Lobatto algorithm.

References: W. Gander and W. Gautschi. Adaptive Quadrature — Revisited

T
integrate
(
T
)
(
const T a
,
const T b
,
const T tolerance = T.epsilon
)
if (
__traits(isFloating, T)
)

Parameters

f

function to integrate. f should be valid on interval [a, b] including the bounds.

a T

finite left interval bound

b T

finite right interval bound

tolerance T

(optional) relative tolerance should be greater or equal to T.epsilon

Return Value

Type: T

Integral value

Examples

import mir.math.common;
import mir.math.constant;

alias cosh = x => 0.5 * (exp(x) + exp(-x));
enum Pi = double(PI);

assert(integrate!exp(0.0, 1.0).approxEqual(double(E - 1)));
assert(integrate!(x => x >= 3)(0.0, 10.0).approxEqual(7.0));
assert(integrate!sqrt(0.0, 1.0).approxEqual(2.0 / 3));
assert(integrate!(x => 23.0 / 25 * cosh(x) - cos(x))(-1.0, 1.0).approxEqual(0.479428226688801667));
assert(integrate!(x => 1 / (x ^^ 4 + x ^^ 2 + 0.9))(-1.0, 1.0).approxEqual(1.5822329637294));
assert(integrate!(x => sqrt(x ^^ 3))(0.0, 1.0).approxEqual(0.4));
assert(integrate!(x => x ? 1 / sqrt(x) : 0)(0.0, 1.0).approxEqual(2));
assert(integrate!(x => 1 / (1 + x ^^ 4))(0.0, 1.0).approxEqual(0.866972987339911));
assert(integrate!(x => 2 / (2 + sin(10 * Pi * x)))(0.0, 1.0).approxEqual(1.1547005383793));
assert(integrate!(x => 1 / (1 + x))(0.0, 1.0).approxEqual(0.6931471805599));
assert(integrate!(x => 1 / (1 + exp(x)))(0.0, 1.0).approxEqual(0.3798854930417));
assert(integrate!(x => exp(x) - 1 ? x / (exp(x) - 1) : 0)(0.0, 1.0).approxEqual(0.777504634112248));
assert(integrate!(x => sin(100 * Pi * x) / (Pi * x))(0.1, 1.0).approxEqual(0.0090986375391668));
assert(integrate!(x => sqrt(50.0) * exp(-50 * Pi * x ^^ 2))(0.0, 10.0).approxEqual(0.5));
assert(integrate!(x => 25 * exp(-25 * x))(0.0, 10.0).approxEqual(1.0));
assert(integrate!(x => 50 / Pi * (2500 * x ^^ 2 + 1))(0.0, 10.0).approxEqual(1.3263071079268e+7));
assert(integrate!(x => 50 * (sin(50 * Pi * x) / (50 * Pi * x)) ^^ 2)(0.01, 1.0).approxEqual(0.11213930374164));
assert(integrate!(x => cos(cos(x) + 3 * sin(x) + 2 * cos(2 * x) + 3 * sin(2 * x) + 3 * cos(3 * x)))(0.0, Pi).approxEqual(0.83867634269443));
assert(integrate!(x => x > 1e-15 ? log(x) : 0)(0.0, 1.0).approxEqual(-1));
assert(integrate!(x => 1 / (x ^^ 2 + 1.005))(-1.0, 1.0).approxEqual(1.5643964440690));
assert(integrate!(x => 1 / cosh(20 * (x - 0.2)) + 1 / cosh(400 * (x - 0.04)) + 1 / cosh(8000 * (x - 0.008)))(0.0, 1.0).approxEqual(0.16349495585710));
assert(integrate!(x => 4 * Pi ^^ 2 * x * sin(20 * Pi * x) * cos(2 * Pi * x))(0.0, 1.0).approxEqual(-0.6346651825434));
assert(integrate!(x => 1 / (1 + (230 * x - 30) ^^ 2))(0.0, 1.0).approxEqual(0.013492485649468));

Meta