![]() |
Home | Libraries | People | FAQ | More |
#include <boost/math/quadrature/gauss_jacobi.hpp>
namespace boost{ namespace math{ namespace quadrature{ template <class Real, class Policy = boost::math::policies::policy<> > class gauss_jacobi { public: gauss_jacobi(unsigned N, Real alpha, Real beta); const std::vector<Real>& abscissa() const; const std::vector<Real>& weights() const; bool anti_gauss_is_internal() const; const std::vector<Real>& anti_gauss_abscissa() const; const std::vector<Real>& anti_gauss_weights() const; template <class F> auto integrate(F f, Real* pL1 = nullptr) const -> decltype(std::declval<F>()(std::declval<Real>())); template <class F, class Norm> auto integrate(F f, const decltype(f(Real(0)))& zero, Norm norm, Real* pL1 = nullptr) const -> decltype(f(Real(0))); template <class F> auto integrate_averaged(F f, Real* error = nullptr, Real* pL1 = nullptr) const -> decltype(std::declval<F>()(std::declval<Real>())); template <class F, class Norm> auto integrate_averaged(F f, const decltype(f(Real(0)))& zero, Norm norm, Real* error = nullptr, Real* pL1 = nullptr) const -> decltype(f(Real(0))); }; }}} // namespaces
The gauss_jacobi class template
computes
∫-11 f(x) (1 - x)α (1 + x)β dx ≈ ∑i wi f(xi)
using N evaluations of f, where the
abscissas xi are the zeros of the Jacobi polynomial PN(α,
β), which jacobi_zeros
also returns. The rule is exact when f is a polynomial
of degree at most 2N - 1. Note that the weight function
(1 - x)α (1 + x)β is not part of f:
pass only the factor that multiplies it. This is what makes the rule effective
for integrands with algebraic singularities at the ends of the interval; α =
β = 0 gives Gauss-Legendre quadrature, α = β = -1/2 Gauss-Chebyshev quadrature, and
other intervals [a, b] follow from
the change of variables t = ((b -
a)x + a + b)/2.
Like gauss,
this is a "one shot" non-adaptive routine, intended for smooth f.
Unlike gauss, the number of
points is a run-time argument, and the rule is computed by the constructor.
For N + 1 more evaluations of f,
integrate_averaged is exact
to higher degree and estimates its error.
gauss_jacobi(unsigned N, Real alpha, Real beta);
Constructs the N-point rule. N must be positive, and α and β finite and greater than -1: otherwise the constructor raises a domain_error through the Policy. If the policy does not throw, the abscissas and weights are NaN, and so is every integral.
const std::vector<Real>& abscissa() const; const std::vector<Real>& weights() const;
These provide direct access to the abscissas, all in (-1, 1) and in increasing order, and their weights.
bool anti_gauss_is_internal() const; const std::vector<Real>& anti_gauss_abscissa() const; const std::vector<Real>& anti_gauss_weights() const;
These describe the anti-Gauss rule A of integrate_averaged, below. Its nodes lie
in [-1, 1] unless α or β is less than -1/2 and N is large,
or near -1/2 for some N (Laurie, Theorem 3), and anti_gauss_is_internal tells which: when
it returns false there is no averaged
rule, and the two vectors are empty. For α or β equal to -1/2 the anti-Gauss rule
can have nodes on -1 and 1, where f is then evaluated.
The rule is computed the first time it is needed, then cached.
template <class F> auto integrate(F f, Real* pL1 = nullptr) const -> decltype(std::declval<F>()(std::declval<Real>()));
Integrates f(x) (1 - x)α (1 + x)β over [-1, 1] with the N-point Gauss rule G. f may return a real or complex value.
If pL1 is not null, *pL1 is set
to the corresponding integral of |f|. If that is substantially
larger than the absolute value of the result, then the sum was ill-conditioned.
template <class F, class Norm> auto integrate(F f, const decltype(f(Real(0)))& zero, Norm norm, Real* pL1 = nullptr) const -> decltype(f(Real(0)));
As for gauss,
this overload handles vector- and matrix-valued integrands: zero
is the additive identity of the returned type, and norm
returns a nonnegative scalar convertible to Real.
The L1 norm is measured with norm.
template <class F> auto integrate_averaged(F f, Real* error = nullptr, Real* pL1 = nullptr) const -> decltype(std::declval<F>()(std::declval<Real>())); template <class F, class Norm> auto integrate_averaged(F f, const decltype(f(Real(0)))& zero, Norm norm, Real* error = nullptr, Real* pL1 = nullptr) const -> decltype(f(Real(0)));
Returns Laurie's averaged rule (G(f)
+ A(f))2, at the cost of
/N + 1 more evaluations of f. A
is the anti-Gauss rule: the rule with N + 1 nodes whose
error on every polynomial of degree up to 2N + 1 is exactly
that of G with the opposite sign, so the average is exact
for polynomials of degree 2N + 1. Its nodes interlace
the Gauss nodes and its weights are positive. If anti_gauss_is_internal() is false,
these functions raise an evaluation_error
through the Policy; if the policy does not throw, the
result, *error
and *pL1
are NaN.
If error is not null, *error is
set to |A(f) - G(f)|/2,
but never less than 2ε times the L1 norm. When G and A
bracket the integral, as they do for smooth f once the
error falls steadily with N, this bounds the error of
the average; it is then also close to the error of G.
Otherwise it is a heuristic, and integrands that alias onto the null space
of A - G can fool it. If pL1 is not null, *pL1 is set to the L1 norm of the averaged
rule, measured with norm in
the second overload.
With α = β = -1/2,
∫-11 ex / √(1 - x2) dx = π I0(1)
is computed by
using boost::math::quadrature::gauss_jacobi; gauss_jacobi<double> rule(10, -0.5, -0.5); double error; double I = rule.integrate_averaged([](double x) { return std::exp(x); }, &error);
I is the exact value rounded
to double precision, and error
is 1.8e-15, its floor of 2ε times the L1 norm.
The abscissas and weights are found in O(N) operations
by following the differential equation of the Jacobi functions from zero to
zero in twice the working precision (Glaser, Liu and Rokhlin), and from the
series of the polynomials about the end points near those. Where that method
cannot vouch for a zero, the rule comes from the three-term recurrence instead,
which overflows for large N combined with large α or β (in
double, for example N
= 20000 and α = β = 200). If the rule cannot be computed, integrate
and integrate_averaged raise
an evaluation_error
through the Policy; if the policy does not throw, the
result is a NaN. Construct the rule once and reuse it.