mirror of
https://github.com/boostorg/math.git
synced 2026-02-14 00:42:38 +00:00
Quadrature: add gauss and gauss-kronrod quadrature.
This commit is contained in:
259
test/adaptive_gauss_kronrod_quadrature_test.cpp
Normal file
259
test/adaptive_gauss_kronrod_quadrature_test.cpp
Normal file
@@ -0,0 +1,259 @@
|
||||
// Copyright Nick Thompson, 2017
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_TEST_MODULE tanh_sinh_quadrature_test
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && !defined(BOOST_NO_SFINAE_EXPR)
|
||||
|
||||
#include <boost/math/concepts/real_concept.hpp>
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/test/floating_point_comparison.hpp>
|
||||
#include <boost/math/quadrature/gauss_kronrod.hpp>
|
||||
#include <boost/math/special_functions/sinc.hpp>
|
||||
#include <boost/multiprecision/cpp_bin_float.hpp>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4127) // Conditional expression is constant
|
||||
#endif
|
||||
|
||||
using std::expm1;
|
||||
using std::atan;
|
||||
using std::tan;
|
||||
using std::log;
|
||||
using std::log1p;
|
||||
using std::asinh;
|
||||
using std::atanh;
|
||||
using std::sqrt;
|
||||
using std::isnormal;
|
||||
using std::abs;
|
||||
using std::sinh;
|
||||
using std::tanh;
|
||||
using std::cosh;
|
||||
using std::pow;
|
||||
using std::exp;
|
||||
using std::sin;
|
||||
using std::cos;
|
||||
using std::string;
|
||||
using boost::math::quadrature::gauss_kronrod;
|
||||
using boost::math::constants::pi;
|
||||
using boost::math::constants::half_pi;
|
||||
using boost::math::constants::two_div_pi;
|
||||
using boost::math::constants::two_pi;
|
||||
using boost::math::constants::half;
|
||||
using boost::math::constants::third;
|
||||
using boost::math::constants::half;
|
||||
using boost::math::constants::third;
|
||||
using boost::math::constants::catalan;
|
||||
using boost::math::constants::ln_two;
|
||||
using boost::math::constants::root_two;
|
||||
using boost::math::constants::root_two_pi;
|
||||
using boost::math::constants::root_pi;
|
||||
using boost::multiprecision::cpp_bin_float_quad;
|
||||
|
||||
template <class Real>
|
||||
Real get_termination_condition()
|
||||
{
|
||||
return boost::math::tools::epsilon<Real>() * 1000;
|
||||
}
|
||||
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_linear()
|
||||
{
|
||||
std::cout << "Testing linear functions are integrated properly by gauss_kronrod on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = boost::math::tools::epsilon<Real>() * 10;
|
||||
Real error;
|
||||
auto f = [](const Real& x)
|
||||
{
|
||||
return 5*x + 7;
|
||||
};
|
||||
Real L1;
|
||||
Real Q = gauss_kronrod<Real, Points>::integrate(f, (Real) 0, (Real) 1, 15, get_termination_condition<Real>(), &error, &L1);
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, 9.5, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, 9.5, tol);
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_quadratic()
|
||||
{
|
||||
std::cout << "Testing quadratic functions are integrated properly by tanh_sinh on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = boost::math::tools::epsilon<Real>() * 10;
|
||||
Real error;
|
||||
|
||||
auto f = [](const Real& x) { return 5*x*x + 7*x + 12; };
|
||||
Real L1;
|
||||
Real Q = gauss_kronrod<Real, Points>::integrate(f, 0, 1, 15, get_termination_condition<Real>(), &error, &L1);
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, (Real) 17 + half<Real>()*third<Real>(), tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, (Real) 17 + half<Real>()*third<Real>(), tol);
|
||||
}
|
||||
|
||||
// Examples taken from
|
||||
//http://crd-legacy.lbl.gov/~dhbailey/dhbpapers/quadrature.pdf
|
||||
template<class Real, unsigned Points>
|
||||
void test_ca()
|
||||
{
|
||||
std::cout << "Testing integration of C(a) on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = boost::math::tools::epsilon<Real>() * 10;
|
||||
Real L1;
|
||||
Real error;
|
||||
|
||||
auto f1 = [](const Real& x) { return atan(x)/(x*(x*x + 1)) ; };
|
||||
Real Q = gauss_kronrod<Real, Points>::integrate(f1, 0, 1, 15, get_termination_condition<Real>(), &error, &L1);
|
||||
Real Q_expected = pi<Real>()*ln_two<Real>()/8 + catalan<Real>()*half<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
auto f2 = [](Real x)->Real { Real t0 = x*x + 1; Real t1 = sqrt(t0); return atan(t1)/(t0*t1); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f2, 0 , 1, 15, get_termination_condition<Real>(), &error, &L1);
|
||||
Q_expected = pi<Real>()/4 - pi<Real>()/root_two<Real>() + 3*atan(root_two<Real>())/root_two<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
auto f5 = [](Real t)->Real { return t*t*log(t)/((t*t - 1)*(t*t*t*t + 1)); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f5, 0, 1, 25);
|
||||
Q_expected = pi<Real>()*pi<Real>()*(2 - root_two<Real>())/32;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, 100 * tol);
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_three_quadrature_schemes_examples()
|
||||
{
|
||||
std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = boost::math::tools::epsilon<Real>() * 10;
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
|
||||
// Example 1:
|
||||
auto f1 = [](const Real& t) { return t*boost::math::log1p(t); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f1, 0 , 1);
|
||||
Q_expected = half<Real>()*half<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
|
||||
|
||||
// Example 2:
|
||||
auto f2 = [](const Real& t) { return t*t*atan(t); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f2, 0, 1);
|
||||
Q_expected = (pi<Real>() -2 + 2*ln_two<Real>())/12;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, 2 * tol);
|
||||
|
||||
// Example 3:
|
||||
auto f3 = [](const Real& t) { return exp(t)*cos(t); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f3, 0, half_pi<Real>());
|
||||
Q_expected = boost::math::expm1(half_pi<Real>())*half<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
|
||||
// Example 4:
|
||||
auto f4 = [](Real x)->Real { Real t0 = sqrt(x*x + 2); return atan(t0)/(t0*(x*x+1)); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f4, 0, 1);
|
||||
Q_expected = 5*pi<Real>()*pi<Real>()/96;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
}
|
||||
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_integration_over_real_line()
|
||||
{
|
||||
std::cout << "Testing integrals over entire real line in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = boost::math::tools::epsilon<Real>() * 10;
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
Real L1;
|
||||
Real error;
|
||||
|
||||
auto f1 = [](const Real& t) { return 1/(1+t*t);};
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f1, -boost::math::tools::max_value<Real>(), boost::math::tools::max_value<Real>(), 15, get_termination_condition<Real>(), &error, &L1);
|
||||
Q_expected = pi<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
auto f4 = [](const Real& t) { return 1/cosh(t);};
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f4, -boost::math::tools::max_value<Real>(), boost::math::tools::max_value<Real>(), 15, get_termination_condition<Real>(), &error, &L1);
|
||||
Q_expected = pi<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_right_limit_infinite()
|
||||
{
|
||||
std::cout << "Testing right limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = boost::math::tools::epsilon<Real>() * 10;
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
Real L1;
|
||||
Real error;
|
||||
|
||||
// Example 11:
|
||||
auto f1 = [](const Real& t) { return 1/(1+t*t);};
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f1, 0, boost::math::tools::max_value<Real>(), 15, get_termination_condition<Real>(), &error, &L1);
|
||||
Q_expected = half_pi<Real>();
|
||||
BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
|
||||
|
||||
auto f4 = [](const Real& t) { return 1/(1+t*t); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f4, 1, boost::math::tools::max_value<Real>(), 15, get_termination_condition<Real>(), &error, &L1);
|
||||
Q_expected = pi<Real>()/4;
|
||||
BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_left_limit_infinite()
|
||||
{
|
||||
std::cout << "Testing left limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = boost::math::tools::epsilon<Real>() * 10;
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
|
||||
// Example 11:
|
||||
auto f1 = [](const Real& t) { return 1/(1+t*t);};
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f1, -boost::math::tools::max_value<Real>(), 0);
|
||||
Q_expected = half_pi<Real>();
|
||||
BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(gauss_quadrature_test)
|
||||
{
|
||||
test_linear<double, 15>();
|
||||
test_quadratic<double, 15>();
|
||||
test_ca<double, 15>();
|
||||
test_three_quadrature_schemes_examples<double, 15>();
|
||||
test_integration_over_real_line<double, 15>();
|
||||
test_right_limit_infinite<double, 15>();
|
||||
test_left_limit_infinite<double, 15>();
|
||||
|
||||
test_linear<cpp_bin_float_quad, 21>();
|
||||
test_quadratic<cpp_bin_float_quad, 21>();
|
||||
test_ca<cpp_bin_float_quad, 21>();
|
||||
test_three_quadrature_schemes_examples<cpp_bin_float_quad, 21>();
|
||||
test_integration_over_real_line<cpp_bin_float_quad, 21>();
|
||||
test_right_limit_infinite<cpp_bin_float_quad, 21>();
|
||||
test_left_limit_infinite<cpp_bin_float_quad, 21>();
|
||||
|
||||
test_linear<cpp_bin_float_quad, 31>();
|
||||
test_quadratic<cpp_bin_float_quad, 31>();
|
||||
test_ca<cpp_bin_float_quad, 31>();
|
||||
test_three_quadrature_schemes_examples<cpp_bin_float_quad, 31>();
|
||||
test_integration_over_real_line<cpp_bin_float_quad, 31>();
|
||||
test_right_limit_infinite<cpp_bin_float_quad, 31>();
|
||||
test_left_limit_infinite<cpp_bin_float_quad, 31>();
|
||||
|
||||
test_linear<cpp_bin_float_quad, 41>();
|
||||
test_quadratic<cpp_bin_float_quad, 41>();
|
||||
test_ca<cpp_bin_float_quad, 41>();
|
||||
test_three_quadrature_schemes_examples<cpp_bin_float_quad, 41>();
|
||||
test_integration_over_real_line<cpp_bin_float_quad, 41>();
|
||||
test_right_limit_infinite<cpp_bin_float_quad, 41>();
|
||||
test_left_limit_infinite<cpp_bin_float_quad, 41>();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int main() { return 0; }
|
||||
|
||||
#endif
|
||||
377
test/gauss_kronrod_quadrature_test.cpp
Normal file
377
test/gauss_kronrod_quadrature_test.cpp
Normal file
@@ -0,0 +1,377 @@
|
||||
// Copyright Nick Thompson, 2017
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_TEST_MODULE tanh_sinh_quadrature_test
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && !defined(BOOST_NO_SFINAE_EXPR)
|
||||
|
||||
#include <boost/math/concepts/real_concept.hpp>
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/test/floating_point_comparison.hpp>
|
||||
#include <boost/math/quadrature/gauss_kronrod.hpp>
|
||||
#include <boost/math/special_functions/sinc.hpp>
|
||||
#include <boost/multiprecision/cpp_bin_float.hpp>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4127) // Conditional expression is constant
|
||||
#endif
|
||||
|
||||
using std::expm1;
|
||||
using std::atan;
|
||||
using std::tan;
|
||||
using std::log;
|
||||
using std::log1p;
|
||||
using std::asinh;
|
||||
using std::atanh;
|
||||
using std::sqrt;
|
||||
using std::isnormal;
|
||||
using std::abs;
|
||||
using std::sinh;
|
||||
using std::tanh;
|
||||
using std::cosh;
|
||||
using std::pow;
|
||||
using std::exp;
|
||||
using std::sin;
|
||||
using std::cos;
|
||||
using std::string;
|
||||
using boost::math::quadrature::gauss_kronrod;
|
||||
using boost::math::constants::pi;
|
||||
using boost::math::constants::half_pi;
|
||||
using boost::math::constants::two_div_pi;
|
||||
using boost::math::constants::two_pi;
|
||||
using boost::math::constants::half;
|
||||
using boost::math::constants::third;
|
||||
using boost::math::constants::half;
|
||||
using boost::math::constants::third;
|
||||
using boost::math::constants::catalan;
|
||||
using boost::math::constants::ln_two;
|
||||
using boost::math::constants::root_two;
|
||||
using boost::math::constants::root_two_pi;
|
||||
using boost::math::constants::root_pi;
|
||||
using boost::multiprecision::cpp_bin_float_quad;
|
||||
|
||||
//
|
||||
// Error rates depend only on the number of points in the approximation, not the type being tested,
|
||||
// define all our expected errors here:
|
||||
//
|
||||
|
||||
enum
|
||||
{
|
||||
test_ca_error_id,
|
||||
test_ca_error_id_2,
|
||||
test_three_quad_error_id,
|
||||
test_three_quad_error_id_2,
|
||||
test_integration_over_real_line_error_id,
|
||||
test_right_limit_infinite_error_id,
|
||||
test_left_limit_infinite_error_id
|
||||
};
|
||||
|
||||
template <unsigned Points>
|
||||
double expected_error(unsigned)
|
||||
{
|
||||
return 0; // placeholder, all tests will fail
|
||||
}
|
||||
|
||||
template <>
|
||||
double expected_error<15>(unsigned id)
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
case test_ca_error_id:
|
||||
return 1e-7;
|
||||
case test_ca_error_id_2:
|
||||
return 2e-5;
|
||||
case test_three_quad_error_id:
|
||||
return 1e-8;
|
||||
case test_three_quad_error_id_2:
|
||||
return 3.5e-3;
|
||||
case test_integration_over_real_line_error_id:
|
||||
return 6e-3;
|
||||
case test_right_limit_infinite_error_id:
|
||||
case test_left_limit_infinite_error_id:
|
||||
return 1e-5;
|
||||
}
|
||||
return 0; // placeholder, all tests will fail
|
||||
}
|
||||
|
||||
template <>
|
||||
double expected_error<21>(unsigned id)
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
case test_ca_error_id:
|
||||
return 1e-12;
|
||||
case test_ca_error_id_2:
|
||||
return 3e-6;
|
||||
case test_three_quad_error_id:
|
||||
return 2e-13;
|
||||
case test_three_quad_error_id_2:
|
||||
return 2e-3;
|
||||
case test_integration_over_real_line_error_id:
|
||||
return 6e-3; // doesn't get any better with more points!
|
||||
case test_right_limit_infinite_error_id:
|
||||
case test_left_limit_infinite_error_id:
|
||||
return 5e-8;
|
||||
}
|
||||
return 0; // placeholder, all tests will fail
|
||||
}
|
||||
|
||||
template <>
|
||||
double expected_error<31>(unsigned id)
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
case test_ca_error_id:
|
||||
return 6e-20;
|
||||
case test_ca_error_id_2:
|
||||
return 3e-7;
|
||||
case test_three_quad_error_id:
|
||||
return 1e-19;
|
||||
case test_three_quad_error_id_2:
|
||||
return 6e-4;
|
||||
case test_integration_over_real_line_error_id:
|
||||
return 6e-3; // doesn't get any better with more points!
|
||||
case test_right_limit_infinite_error_id:
|
||||
case test_left_limit_infinite_error_id:
|
||||
return 5e-11;
|
||||
}
|
||||
return 0; // placeholder, all tests will fail
|
||||
}
|
||||
|
||||
template <>
|
||||
double expected_error<41>(unsigned id)
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
case test_ca_error_id:
|
||||
return 1e-26;
|
||||
case test_ca_error_id_2:
|
||||
return 1e-7;
|
||||
case test_three_quad_error_id:
|
||||
return 3e-27;
|
||||
case test_three_quad_error_id_2:
|
||||
return 3e-4;
|
||||
case test_integration_over_real_line_error_id:
|
||||
return 5e-5; // doesn't get any better with more points!
|
||||
case test_right_limit_infinite_error_id:
|
||||
case test_left_limit_infinite_error_id:
|
||||
return 1e-15;
|
||||
}
|
||||
return 0; // placeholder, all tests will fail
|
||||
}
|
||||
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_linear()
|
||||
{
|
||||
std::cout << "Testing linear functions are integrated properly by gauss_kronrod on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = boost::math::tools::epsilon<Real>() * 10;
|
||||
Real error;
|
||||
auto f = [](const Real& x)
|
||||
{
|
||||
return 5*x + 7;
|
||||
};
|
||||
Real L1;
|
||||
Real Q = gauss_kronrod<Real, Points>::integrate(f, (Real) 0, (Real) 1, 0, 0, &error, &L1);
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, 9.5, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, 9.5, tol);
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_quadratic()
|
||||
{
|
||||
std::cout << "Testing quadratic functions are integrated properly by tanh_sinh on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = boost::math::tools::epsilon<Real>() * 10;
|
||||
Real error;
|
||||
|
||||
auto f = [](const Real& x) { return 5*x*x + 7*x + 12; };
|
||||
Real L1;
|
||||
Real Q = gauss_kronrod<Real, Points>::integrate(f, 0, 1, 0, 0, &error, &L1);
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, (Real) 17 + half<Real>()*third<Real>(), tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, (Real) 17 + half<Real>()*third<Real>(), tol);
|
||||
}
|
||||
|
||||
// Examples taken from
|
||||
//http://crd-legacy.lbl.gov/~dhbailey/dhbpapers/quadrature.pdf
|
||||
template<class Real, unsigned Points>
|
||||
void test_ca()
|
||||
{
|
||||
std::cout << "Testing integration of C(a) on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = expected_error<Points>(test_ca_error_id);
|
||||
Real L1;
|
||||
Real error;
|
||||
|
||||
auto f1 = [](const Real& x) { return atan(x)/(x*(x*x + 1)) ; };
|
||||
Real Q = gauss_kronrod<Real, Points>::integrate(f1, 0, 1, 0, 0, &error, &L1);
|
||||
Real Q_expected = pi<Real>()*ln_two<Real>()/8 + catalan<Real>()*half<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
auto f2 = [](Real x)->Real { Real t0 = x*x + 1; Real t1 = sqrt(t0); return atan(t1)/(t0*t1); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f2, 0 , 1, 0, 0, &error, &L1);
|
||||
Q_expected = pi<Real>()/4 - pi<Real>()/root_two<Real>() + 3*atan(root_two<Real>())/root_two<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
tol = expected_error<Points>(test_ca_error_id_2);
|
||||
auto f5 = [](Real t)->Real { return t*t*log(t)/((t*t - 1)*(t*t*t*t + 1)); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f5, 0, 1, 0);
|
||||
Q_expected = pi<Real>()*pi<Real>()*(2 - root_two<Real>())/32;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_three_quadrature_schemes_examples()
|
||||
{
|
||||
std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = expected_error<Points>(test_three_quad_error_id);
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
|
||||
// Example 1:
|
||||
auto f1 = [](const Real& t) { return t*boost::math::log1p(t); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f1, 0 , 1, 0);
|
||||
Q_expected = half<Real>()*half<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
|
||||
|
||||
// Example 2:
|
||||
auto f2 = [](const Real& t) { return t*t*atan(t); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f2, 0 , 1, 0);
|
||||
Q_expected = (pi<Real>() -2 + 2*ln_two<Real>())/12;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, 2 * tol);
|
||||
|
||||
// Example 3:
|
||||
auto f3 = [](const Real& t) { return exp(t)*cos(t); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f3, 0, half_pi<Real>(), 0);
|
||||
Q_expected = boost::math::expm1(half_pi<Real>())*half<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
|
||||
// Example 4:
|
||||
auto f4 = [](Real x)->Real { Real t0 = sqrt(x*x + 2); return atan(t0)/(t0*(x*x+1)); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f4, 0 , 1, 0);
|
||||
Q_expected = 5*pi<Real>()*pi<Real>()/96;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
|
||||
tol = expected_error<Points>(test_three_quad_error_id_2);
|
||||
// Example 5:
|
||||
auto f5 = [](const Real& t) { return sqrt(t)*log(t); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f5, 0 , 1, 0);
|
||||
Q_expected = -4/ (Real) 9;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
|
||||
// Example 6:
|
||||
auto f6 = [](const Real& t) { return sqrt(1 - t*t); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f6, 0 , 1, 0);
|
||||
Q_expected = pi<Real>()/4;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
}
|
||||
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_integration_over_real_line()
|
||||
{
|
||||
std::cout << "Testing integrals over entire real line in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = expected_error<Points>(test_integration_over_real_line_error_id);
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
Real L1;
|
||||
Real error;
|
||||
|
||||
auto f1 = [](const Real& t) { return 1/(1+t*t);};
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f1, -boost::math::tools::max_value<Real>(), boost::math::tools::max_value<Real>(), 0, 0, &error, &L1);
|
||||
Q_expected = pi<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
auto f4 = [](const Real& t) { return 1/cosh(t);};
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f4, -boost::math::tools::max_value<Real>(), boost::math::tools::max_value<Real>(), 0, 0, &error, &L1);
|
||||
Q_expected = pi<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_right_limit_infinite()
|
||||
{
|
||||
std::cout << "Testing right limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = expected_error<Points>(test_right_limit_infinite_error_id);
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
Real L1;
|
||||
Real error;
|
||||
|
||||
// Example 11:
|
||||
auto f1 = [](const Real& t) { return 1/(1+t*t);};
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f1, 0, boost::math::tools::max_value<Real>(), 0, 0, &error, &L1);
|
||||
Q_expected = half_pi<Real>();
|
||||
BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
|
||||
|
||||
auto f4 = [](const Real& t) { return 1/(1+t*t); };
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f4, 1, boost::math::tools::max_value<Real>(), 0, 0, &error, &L1);
|
||||
Q_expected = pi<Real>()/4;
|
||||
BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_left_limit_infinite()
|
||||
{
|
||||
std::cout << "Testing left limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = expected_error<Points>(test_left_limit_infinite_error_id);
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
|
||||
// Example 11:
|
||||
auto f1 = [](const Real& t) { return 1/(1+t*t);};
|
||||
Q = gauss_kronrod<Real, Points>::integrate(f1, -boost::math::tools::max_value<Real>(), Real(0), 0);
|
||||
Q_expected = half_pi<Real>();
|
||||
BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(gauss_quadrature_test)
|
||||
{
|
||||
test_linear<double, 15>();
|
||||
test_quadratic<double, 15>();
|
||||
test_ca<double, 15>();
|
||||
test_three_quadrature_schemes_examples<double, 15>();
|
||||
test_integration_over_real_line<double, 15>();
|
||||
test_right_limit_infinite<double, 15>();
|
||||
test_left_limit_infinite<double, 15>();
|
||||
|
||||
test_linear<cpp_bin_float_quad, 21>();
|
||||
test_quadratic<cpp_bin_float_quad, 21>();
|
||||
test_ca<cpp_bin_float_quad, 21>();
|
||||
test_three_quadrature_schemes_examples<cpp_bin_float_quad, 21>();
|
||||
test_integration_over_real_line<cpp_bin_float_quad, 21>();
|
||||
test_right_limit_infinite<cpp_bin_float_quad, 21>();
|
||||
test_left_limit_infinite<cpp_bin_float_quad, 21>();
|
||||
|
||||
test_linear<cpp_bin_float_quad, 31>();
|
||||
test_quadratic<cpp_bin_float_quad, 31>();
|
||||
test_ca<cpp_bin_float_quad, 31>();
|
||||
test_three_quadrature_schemes_examples<cpp_bin_float_quad, 31>();
|
||||
test_integration_over_real_line<cpp_bin_float_quad, 31>();
|
||||
test_right_limit_infinite<cpp_bin_float_quad, 31>();
|
||||
test_left_limit_infinite<cpp_bin_float_quad, 31>();
|
||||
|
||||
test_linear<cpp_bin_float_quad, 41>();
|
||||
test_quadratic<cpp_bin_float_quad, 41>();
|
||||
test_ca<cpp_bin_float_quad, 41>();
|
||||
test_three_quadrature_schemes_examples<cpp_bin_float_quad, 41>();
|
||||
test_integration_over_real_line<cpp_bin_float_quad, 41>();
|
||||
test_right_limit_infinite<cpp_bin_float_quad, 41>();
|
||||
test_left_limit_infinite<cpp_bin_float_quad, 41>();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int main() { return 0; }
|
||||
|
||||
#endif
|
||||
372
test/gauss_quadrature_test.cpp
Normal file
372
test/gauss_quadrature_test.cpp
Normal file
@@ -0,0 +1,372 @@
|
||||
// Copyright Nick Thompson, 2017
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_TEST_MODULE tanh_sinh_quadrature_test
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && !defined(BOOST_NO_SFINAE_EXPR)
|
||||
|
||||
#include <boost/math/concepts/real_concept.hpp>
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/test/floating_point_comparison.hpp>
|
||||
#include <boost/math/quadrature/gauss.hpp>
|
||||
#include <boost/math/special_functions/sinc.hpp>
|
||||
#include <boost/multiprecision/cpp_bin_float.hpp>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4127) // Conditional expression is constant
|
||||
#endif
|
||||
|
||||
using std::expm1;
|
||||
using std::atan;
|
||||
using std::tan;
|
||||
using std::log;
|
||||
using std::log1p;
|
||||
using std::asinh;
|
||||
using std::atanh;
|
||||
using std::sqrt;
|
||||
using std::isnormal;
|
||||
using std::abs;
|
||||
using std::sinh;
|
||||
using std::tanh;
|
||||
using std::cosh;
|
||||
using std::pow;
|
||||
using std::exp;
|
||||
using std::sin;
|
||||
using std::cos;
|
||||
using std::string;
|
||||
using boost::math::quadrature::gauss;
|
||||
using boost::math::constants::pi;
|
||||
using boost::math::constants::half_pi;
|
||||
using boost::math::constants::two_div_pi;
|
||||
using boost::math::constants::two_pi;
|
||||
using boost::math::constants::half;
|
||||
using boost::math::constants::third;
|
||||
using boost::math::constants::half;
|
||||
using boost::math::constants::third;
|
||||
using boost::math::constants::catalan;
|
||||
using boost::math::constants::ln_two;
|
||||
using boost::math::constants::root_two;
|
||||
using boost::math::constants::root_two_pi;
|
||||
using boost::math::constants::root_pi;
|
||||
using boost::multiprecision::cpp_bin_float_quad;
|
||||
|
||||
//
|
||||
// Error rates depend only on the number of points in the approximation, not the type being tested,
|
||||
// define all our expected errors here:
|
||||
//
|
||||
|
||||
enum
|
||||
{
|
||||
test_ca_error_id,
|
||||
test_ca_error_id_2,
|
||||
test_three_quad_error_id,
|
||||
test_three_quad_error_id_2,
|
||||
test_integration_over_real_line_error_id,
|
||||
test_right_limit_infinite_error_id,
|
||||
test_left_limit_infinite_error_id
|
||||
};
|
||||
|
||||
template <unsigned Points>
|
||||
double expected_error(unsigned)
|
||||
{
|
||||
return 0; // placeholder, all tests will fail
|
||||
}
|
||||
|
||||
template <>
|
||||
double expected_error<7>(unsigned id)
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
case test_ca_error_id:
|
||||
return 1e-7;
|
||||
case test_ca_error_id_2:
|
||||
return 2e-5;
|
||||
case test_three_quad_error_id:
|
||||
return 1e-8;
|
||||
case test_three_quad_error_id_2:
|
||||
return 3.5e-3;
|
||||
case test_integration_over_real_line_error_id:
|
||||
return 6e-3;
|
||||
case test_right_limit_infinite_error_id:
|
||||
case test_left_limit_infinite_error_id:
|
||||
return 1e-5;
|
||||
}
|
||||
return 0; // placeholder, all tests will fail
|
||||
}
|
||||
|
||||
template <>
|
||||
double expected_error<10>(unsigned id)
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
case test_ca_error_id:
|
||||
return 1e-12;
|
||||
case test_ca_error_id_2:
|
||||
return 3e-6;
|
||||
case test_three_quad_error_id:
|
||||
return 2e-13;
|
||||
case test_three_quad_error_id_2:
|
||||
return 2e-3;
|
||||
case test_integration_over_real_line_error_id:
|
||||
return 6e-3; // doesn't get any better with more points!
|
||||
case test_right_limit_infinite_error_id:
|
||||
case test_left_limit_infinite_error_id:
|
||||
return 5e-8;
|
||||
}
|
||||
return 0; // placeholder, all tests will fail
|
||||
}
|
||||
|
||||
template <>
|
||||
double expected_error<15>(unsigned id)
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
case test_ca_error_id:
|
||||
return 6e-20;
|
||||
case test_ca_error_id_2:
|
||||
return 3e-7;
|
||||
case test_three_quad_error_id:
|
||||
return 1e-19;
|
||||
case test_three_quad_error_id_2:
|
||||
return 6e-4;
|
||||
case test_integration_over_real_line_error_id:
|
||||
return 6e-3; // doesn't get any better with more points!
|
||||
case test_right_limit_infinite_error_id:
|
||||
case test_left_limit_infinite_error_id:
|
||||
return 5e-11;
|
||||
}
|
||||
return 0; // placeholder, all tests will fail
|
||||
}
|
||||
|
||||
template <>
|
||||
double expected_error<20>(unsigned id)
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
case test_ca_error_id:
|
||||
return 1e-26;
|
||||
case test_ca_error_id_2:
|
||||
return 1e-7;
|
||||
case test_three_quad_error_id:
|
||||
return 3e-27;
|
||||
case test_three_quad_error_id_2:
|
||||
return 3e-4;
|
||||
case test_integration_over_real_line_error_id:
|
||||
return 5e-5; // doesn't get any better with more points!
|
||||
case test_right_limit_infinite_error_id:
|
||||
case test_left_limit_infinite_error_id:
|
||||
return 1e-15;
|
||||
}
|
||||
return 0; // placeholder, all tests will fail
|
||||
}
|
||||
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_linear()
|
||||
{
|
||||
std::cout << "Testing linear functions are integrated properly by gauss on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = boost::math::tools::epsilon<Real>() * 10;
|
||||
auto f = [](const Real& x)
|
||||
{
|
||||
return 5*x + 7;
|
||||
};
|
||||
Real L1;
|
||||
Real Q = gauss<Real, Points>::integrate(f, (Real) 0, (Real) 1, &L1);
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, 9.5, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, 9.5, tol);
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_quadratic()
|
||||
{
|
||||
std::cout << "Testing quadratic functions are integrated properly by tanh_sinh on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = boost::math::tools::epsilon<Real>() * 10;
|
||||
|
||||
auto f = [](const Real& x) { return 5*x*x + 7*x + 12; };
|
||||
Real L1;
|
||||
Real Q = gauss<Real, Points>::integrate(f, 0, 1, &L1);
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, (Real) 17 + half<Real>()*third<Real>(), tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, (Real) 17 + half<Real>()*third<Real>(), tol);
|
||||
}
|
||||
|
||||
// Examples taken from
|
||||
//http://crd-legacy.lbl.gov/~dhbailey/dhbpapers/quadrature.pdf
|
||||
template<class Real, unsigned Points>
|
||||
void test_ca()
|
||||
{
|
||||
std::cout << "Testing integration of C(a) on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = expected_error<Points>(test_ca_error_id);
|
||||
Real L1;
|
||||
|
||||
auto f1 = [](const Real& x) { return atan(x)/(x*(x*x + 1)) ; };
|
||||
Real Q = gauss<Real, Points>::integrate(f1, 0, 1, &L1);
|
||||
Real Q_expected = pi<Real>()*ln_two<Real>()/8 + catalan<Real>()*half<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
auto f2 = [](Real x)->Real { Real t0 = x*x + 1; Real t1 = sqrt(t0); return atan(t1)/(t0*t1); };
|
||||
Q = gauss<Real, Points>::integrate(f2, 0 , 1, &L1);
|
||||
Q_expected = pi<Real>()/4 - pi<Real>()/root_two<Real>() + 3*atan(root_two<Real>())/root_two<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
tol = expected_error<Points>(test_ca_error_id_2);
|
||||
auto f5 = [](Real t)->Real { return t*t*log(t)/((t*t - 1)*(t*t*t*t + 1)); };
|
||||
Q = gauss<Real, Points>::integrate(f5, 0 , 1);
|
||||
Q_expected = pi<Real>()*pi<Real>()*(2 - root_two<Real>())/32;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_three_quadrature_schemes_examples()
|
||||
{
|
||||
std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = expected_error<Points>(test_three_quad_error_id);
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
|
||||
// Example 1:
|
||||
auto f1 = [](const Real& t) { return t*boost::math::log1p(t); };
|
||||
Q = gauss<Real, Points>::integrate(f1, 0 , 1);
|
||||
Q_expected = half<Real>()*half<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
|
||||
|
||||
// Example 2:
|
||||
auto f2 = [](const Real& t) { return t*t*atan(t); };
|
||||
Q = gauss<Real, Points>::integrate(f2, 0 , 1);
|
||||
Q_expected = (pi<Real>() -2 + 2*ln_two<Real>())/12;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, 2 * tol);
|
||||
|
||||
// Example 3:
|
||||
auto f3 = [](const Real& t) { return exp(t)*cos(t); };
|
||||
Q = gauss<Real, Points>::integrate(f3, 0, half_pi<Real>());
|
||||
Q_expected = boost::math::expm1(half_pi<Real>())*half<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
|
||||
// Example 4:
|
||||
auto f4 = [](Real x)->Real { Real t0 = sqrt(x*x + 2); return atan(t0)/(t0*(x*x+1)); };
|
||||
Q = gauss<Real, Points>::integrate(f4, 0 , 1);
|
||||
Q_expected = 5*pi<Real>()*pi<Real>()/96;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
|
||||
tol = expected_error<Points>(test_three_quad_error_id_2);
|
||||
// Example 5:
|
||||
auto f5 = [](const Real& t) { return sqrt(t)*log(t); };
|
||||
Q = gauss<Real, Points>::integrate(f5, 0 , 1);
|
||||
Q_expected = -4/ (Real) 9;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
|
||||
// Example 6:
|
||||
auto f6 = [](const Real& t) { return sqrt(1 - t*t); };
|
||||
Q = gauss<Real, Points>::integrate(f6, 0 , 1);
|
||||
Q_expected = pi<Real>()/4;
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
}
|
||||
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_integration_over_real_line()
|
||||
{
|
||||
std::cout << "Testing integrals over entire real line in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = expected_error<Points>(test_integration_over_real_line_error_id);
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
Real L1;
|
||||
|
||||
auto f1 = [](const Real& t) { return 1/(1+t*t);};
|
||||
Q = gauss<Real, Points>::integrate(f1, -boost::math::tools::max_value<Real>(), boost::math::tools::max_value<Real>(), &L1);
|
||||
Q_expected = pi<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
auto f4 = [](const Real& t) { return 1/cosh(t);};
|
||||
Q = gauss<Real, Points>::integrate(f4, -boost::math::tools::max_value<Real>(), boost::math::tools::max_value<Real>(), &L1);
|
||||
Q_expected = pi<Real>();
|
||||
BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
|
||||
BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
|
||||
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_right_limit_infinite()
|
||||
{
|
||||
std::cout << "Testing right limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = expected_error<Points>(test_right_limit_infinite_error_id);
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
Real L1;
|
||||
|
||||
// Example 11:
|
||||
auto f1 = [](const Real& t) { return 1/(1+t*t);};
|
||||
Q = gauss<Real, Points>::integrate(f1, 0, boost::math::tools::max_value<Real>(), &L1);
|
||||
Q_expected = half_pi<Real>();
|
||||
BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
|
||||
|
||||
auto f4 = [](const Real& t) { return 1/(1+t*t); };
|
||||
Q = gauss<Real, Points>::integrate(f4, 1, boost::math::tools::max_value<Real>(), &L1);
|
||||
Q_expected = pi<Real>()/4;
|
||||
BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
|
||||
}
|
||||
|
||||
template<class Real, unsigned Points>
|
||||
void test_left_limit_infinite()
|
||||
{
|
||||
std::cout << "Testing left limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
|
||||
Real tol = expected_error<Points>(test_left_limit_infinite_error_id);
|
||||
Real Q;
|
||||
Real Q_expected;
|
||||
|
||||
// Example 11:
|
||||
auto f1 = [](const Real& t) { return 1/(1+t*t);};
|
||||
Q = gauss<Real, Points>::integrate(f1, -boost::math::tools::max_value<Real>(), Real(0));
|
||||
Q_expected = half_pi<Real>();
|
||||
BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(gauss_quadrature_test)
|
||||
{
|
||||
test_linear<double, 7>();
|
||||
test_quadratic<double, 7>();
|
||||
test_ca<double, 7>();
|
||||
test_three_quadrature_schemes_examples<double, 7>();
|
||||
test_integration_over_real_line<double, 7>();
|
||||
test_right_limit_infinite<double, 7>();
|
||||
test_left_limit_infinite<double, 7>();
|
||||
|
||||
test_linear<cpp_bin_float_quad, 10>();
|
||||
test_quadratic<cpp_bin_float_quad, 10>();
|
||||
test_ca<cpp_bin_float_quad, 10>();
|
||||
test_three_quadrature_schemes_examples<cpp_bin_float_quad, 10>();
|
||||
test_integration_over_real_line<cpp_bin_float_quad, 10>();
|
||||
test_right_limit_infinite<cpp_bin_float_quad, 10>();
|
||||
test_left_limit_infinite<cpp_bin_float_quad, 10>();
|
||||
|
||||
test_linear<cpp_bin_float_quad, 15>();
|
||||
test_quadratic<cpp_bin_float_quad, 15>();
|
||||
test_ca<cpp_bin_float_quad, 15>();
|
||||
test_three_quadrature_schemes_examples<cpp_bin_float_quad, 15>();
|
||||
test_integration_over_real_line<cpp_bin_float_quad, 15>();
|
||||
test_right_limit_infinite<cpp_bin_float_quad, 15>();
|
||||
test_left_limit_infinite<cpp_bin_float_quad, 15>();
|
||||
|
||||
test_linear<cpp_bin_float_quad, 20>();
|
||||
test_quadratic<cpp_bin_float_quad, 20>();
|
||||
test_ca<cpp_bin_float_quad, 20>();
|
||||
test_three_quadrature_schemes_examples<cpp_bin_float_quad, 20>();
|
||||
test_integration_over_real_line<cpp_bin_float_quad, 20>();
|
||||
test_right_limit_infinite<cpp_bin_float_quad, 20>();
|
||||
test_left_limit_infinite<cpp_bin_float_quad, 20>();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int main() { return 0; }
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user