This library provides a numerical integration method by Double Exponential (DE) formula.
On DE formula, a integration is approximated as Eq. (1),
where
In this library, Eq.(1) can computed by the following code:
import deint;
// Now we assume that a, b, w, N, t_a, t_b, and f are defined as Eq.(1).
auto deint = makeDEInt!real(a, b, No.isExpDecay, N, t_a, t_b);
// compute Eq.(1)
real ans = deint.integrate((real x) => f(x));
- The default values of
N
,t_a
, andt_b
are100
,-5
, and5
, respectively. - If the integrand function
f(x)
is an exponential-decay function on|x| -> infinity
, you should change the flagNo.isExpDecay
toYes.isExpDecay
.
For example:
- Integrate f(x) on (0, 1).
where
and
// DEInt!real is a struct which computes x_k and w_k in advance.
auto int01 = makeDEInt!real(0, 1);
// When f(x) = x, int_0^1 x dx = 0.5
assert(int01.integrate((real x) => x).approxEqual(0.5));
// `DEInt!real` is reusable.
// When f(x) = x^^2, int_0^1 x^^2 dx = 1/3
assert(int01.integrate((real x) => x^^2).approxEqual(1/3.0));
- Integrate f(x) on (-inf, inf)
where
and
// integration on [-inf, inf]
auto intII = makeDEInt!real(-real.infinity, real.infinity);
// Gaussian integral
assert(intII.integrate((real x) => exp(-x^^2)).approxEqual(sqrt(PI)));
- Integrate f(x) = g(x) exp(-x) on (1, inf)
where
and
// integrate f(x) = g(x)exp(-x) on (1, inf)
// Now, we know that the integrand f(x) decay exponentially.
auto intFI = makeDEInt!real(1, real.infinity, Yes.isExpDecay);
// incomplete gamma function
assert(intFI.integrate((real x) => x * exp(-x)).approxEqual(gammaIncompleteCompl(2, 1) * gamma(2)));
// Also, we can use `withWeight` which pre-computes and stores weight function.
// The `withWeight` is useful when integrands have same weights.
auto intFIW = intFI.withWeight((real x) => exp(-x));
// incomplete gamma function
assert(intFIW.integrate((real x) => x).approxEqual(gammaIncompleteCompl(2, 1) * gamma(2)));
assert(intFIW.integrate((real x) => x^^2).approxEqual(gammaIncompleteCompl(3, 1) * gamma(3)));
assert(intFIW.integrate((real x) => x^^3).approxEqual(gammaIncompleteCompl(4, 1) * gamma(4)));