2 * Copyright Nick Thompson, 2017
3 * Use, modification and distribution are subject to the
4 * Boost Software License, Version 1.0. (See accompanying file
5 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #define BOOST_TEST_MODULE trapezoidal_quadrature
10 #include <boost/config.hpp>
11 //#include <boost/multiprecision/mpc.hpp>
12 #include <boost/test/included/unit_test.hpp>
13 #include <boost/test/tools/floating_point_comparison.hpp>
14 #include <boost/math/concepts/real_concept.hpp>
15 #include <boost/math/special_functions/bessel.hpp>
16 #include <boost/math/quadrature/trapezoidal.hpp>
17 #include <boost/multiprecision/cpp_bin_float.hpp>
18 #include <boost/multiprecision/cpp_dec_float.hpp>
19 #ifdef BOOST_HAS_FLOAT128
20 #include <boost/multiprecision/complex128.hpp>
22 using boost::multiprecision::cpp_bin_float_50;
23 using boost::multiprecision::cpp_bin_float_100;
24 using boost::math::quadrature::trapezoidal;
26 // These tests come from:
27 // https://doi.org/10.1023/A:1025524324969
28 // "Computing special functions by using quadrature rules", Gil, Segura, and Temme.
29 template<class Complex>
30 void test_complex_bessel()
32 std::cout << "Testing that complex-valued integrands are integrated correctly by the adaptive trapezoidal routine on type " << boost::typeindex::type_id<Complex>().pretty_name() << "\n";
33 typedef typename Complex::value_type Real;
36 using boost::math::constants::pi;
37 auto bessel_integrand = [&n, &z](Real theta)->Complex
43 Complex arg = z*t1 + t2;
44 return cos(arg)/pi<Real>();
47 using boost::math::quadrature::trapezoidal;
51 Complex Jnz = trapezoidal<decltype(bessel_integrand), Real>(bessel_integrand, a, b);
52 // N[BesselJ[2, 2 + 3 I], 143]
53 // 1.257674591970511077630764085052638490387449039392695959943027966195657681586539389134094087028482099931927725892... +
54 // 2.318771368505683055818032722011594415038779144567369903204833213112457210243098545874099591376455981793627257060... i
55 Real Jnzx = boost::lexical_cast<Real>("1.257674591970511077630764085052638490387449039392695959943027966195657681586539389134094087028482099931927725892");
56 Real Jnzy = boost::lexical_cast<Real>("2.318771368505683055818032722011594415038779144567369903204833213112457210243098545874099591376455981793627257060");
57 Real tol = 10*std::numeric_limits<Real>::epsilon();
58 BOOST_CHECK_CLOSE_FRACTION(Jnz.real(), Jnzx, tol);
59 BOOST_CHECK_CLOSE_FRACTION(Jnz.imag(), Jnzy, tol);
62 template<class Complex>
63 void test_I0_complex()
65 std::cout << "Testing that complex-argument I0 is calculated correctly by the adaptive trapezoidal routine on type " << boost::typeindex::type_id<Complex>().pretty_name() << "\n";
66 typedef typename Complex::value_type Real;
68 using boost::math::constants::pi;
69 auto I0 = [&z](Real theta)->Complex
73 return exp(z*cos(theta))/pi<Real>();
76 using boost::math::quadrature::trapezoidal;
80 Complex I0z = trapezoidal<decltype(I0), Real>(I0, a, b);
81 // N[BesselI[0, 2 + 3 I], 143]
82 // -1.24923487960742219637619681391438589436703710701063561548156438052154090067526565701278826317992172207565649925713468090525951417141982808439560899101
83 // 0.947983792057734776114060623981442199525094227418764823692296622398838765371662384207319492908490909109393495109183270208372778907692930675595924819922 i
84 Real I0zx = boost::lexical_cast<Real>("-1.24923487960742219637619681391438589436703710701063561548156438052154090067526565701278826317992172207565649925713468090525951417141982808439560899101");
85 Real I0zy = boost::lexical_cast<Real>("0.947983792057734776114060623981442199525094227418764823692296622398838765371662384207319492908490909109393495109183270208372778907692930675595924819922");
86 Real tol = 10*std::numeric_limits<Real>::epsilon();
87 BOOST_CHECK_CLOSE_FRACTION(I0z.real(), I0zx, tol);
88 BOOST_CHECK_CLOSE_FRACTION(I0z.imag(), I0zy, tol);
92 template<class Complex>
95 std::cout << "Testing that complex-argument erfc is calculated correctly by the adaptive trapezoidal routine on type " << boost::typeindex::type_id<Complex>().pretty_name() << "\n";
96 typedef typename Complex::value_type Real;
98 using boost::math::constants::pi;
99 using boost::math::constants::two_pi;
100 auto erfc = [&z](Real theta)->Complex
104 Real t = tan(theta/2);
105 Complex arg = -z*z*(1+t*t);
106 return exp(arg)/two_pi<Real>();
109 using boost::math::quadrature::trapezoidal;
111 Real a = -pi<Real>();
113 Complex erfcz = trapezoidal<decltype(erfc), Real>(erfc, a, b, boost::math::tools::root_epsilon<Real>(), 17);
115 //-0.00360634272565175091291182820541914235532928536595056623793472801084629874817202107825472707423984408881473019087931573313969503235634965264302640170177
116 // - 0.0112590060288150250764009156316482248536651598819882163212627394923365188251633729432967232423246312345152595958230197778555210858871376231770868078020 i
117 Real erfczx = boost::lexical_cast<Real>("-0.00360634272565175091291182820541914235532928536595056623793472801084629874817202107825472707423984408881473019087931573313969503235634965264302640170177");
118 Real erfczy = boost::lexical_cast<Real>("-0.0112590060288150250764009156316482248536651598819882163212627394923365188251633729432967232423246312345152595958230197778555210858871376231770868078020");
119 Real tol = 5000*std::numeric_limits<Real>::epsilon();
120 BOOST_CHECK_CLOSE_FRACTION(erfcz.real(), erfczx, tol);
121 BOOST_CHECK_CLOSE_FRACTION(erfcz.imag(), erfczy, tol);
128 std::cout << "Testing constants are integrated correctly by the adaptive trapezoidal routine on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
130 auto f = [](Real)->Real { return boost::math::constants::half<Real>(); };
131 Real Q = trapezoidal<decltype(f), Real>(f, (Real) 0.0, (Real) 10.0);
132 BOOST_CHECK_CLOSE(Q, 5.0, 100*std::numeric_limits<Real>::epsilon());
137 void test_rational_periodic()
139 using boost::math::constants::two_pi;
140 using boost::math::constants::third;
141 std::cout << "Testing that rational periodic functions are integrated correctly by trapezoidal rule on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
143 auto f = [](Real x)->Real { return 1/(5 - 4*cos(x)); };
145 Real tol = 100*boost::math::tools::epsilon<Real>();
146 Real Q = trapezoidal(f, (Real) 0.0, two_pi<Real>(), tol);
148 BOOST_CHECK_CLOSE_FRACTION(Q, two_pi<Real>()*third<Real>(), 10*tol);
152 void test_bump_function()
154 std::cout << "Testing that bump functions are integrated correctly by trapezoidal rule on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
155 auto f = [](Real x)->Real {
156 if( x>= 1 || x <= -1)
160 return (Real) exp(-(Real) 1/(1-x*x));
162 Real tol = boost::math::tools::epsilon<Real>();
163 Real Q = trapezoidal(f, (Real) -1, (Real) 1, tol);
164 // 2*NIntegrate[Exp[-(1/(1 - x^2))], {x, 0, 1}, WorkingPrecision -> 210]
165 Real Q_exp = boost::lexical_cast<Real>("0.44399381616807943782304892117055266376120178904569749730748455394704");
166 BOOST_CHECK_CLOSE_FRACTION(Q, Q_exp, 30*tol);
170 void test_zero_function()
172 std::cout << "Testing that zero functions are integrated correctly by trapezoidal rule on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
173 auto f = [](Real)->Real { return (Real) 0;};
174 Real tol = 100* boost::math::tools::epsilon<Real>();
175 Real Q = trapezoidal(f, (Real) -1, (Real) 1, tol);
176 BOOST_CHECK_SMALL(Q, 100*tol);
182 std::cout << "Testing that sin(x)^2 is integrated correctly by the trapezoidal rule on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
183 auto f = [](Real x)->Real { return sin(10*x)*sin(10*x); };
184 Real tol = 100* boost::math::tools::epsilon<Real>();
185 Real Q = trapezoidal(f, (Real) 0, (Real) boost::math::constants::pi<Real>(), tol);
186 BOOST_CHECK_CLOSE_FRACTION(Q, boost::math::constants::half_pi<Real>(), tol);
191 void test_slowly_converging()
194 std::cout << "Testing that non-periodic functions are correctly integrated by the trapezoidal rule, even if slowly, on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
195 // This function is not periodic, so it should not be fast to converge:
196 auto f = [](Real x)->Real { using std::sqrt; return sqrt(1 - x*x); };
198 Real tol = sqrt(sqrt(boost::math::tools::epsilon<Real>()));
200 Real Q = trapezoidal(f, (Real) 0, (Real) 1, tol, 15, &error_estimate);
201 BOOST_CHECK_CLOSE_FRACTION(Q, boost::math::constants::half_pi<Real>()/2, 10*tol);
205 void test_rational_sin()
209 using boost::math::constants::two_pi;
210 using boost::math::constants::half;
211 std::cout << "Testing that a rational sin function is integrated correctly by the trapezoidal rule on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
213 auto f = [=](Real x)->Real { using std::sin; Real t = a + sin(x); return 1.0f / (t*t); };
215 Real expected = two_pi<Real>()*a/pow(a*a - 1, 3*half<Real>());
216 Real tol = 100* boost::math::tools::epsilon<Real>();
217 Real Q = trapezoidal(f, (Real) 0, (Real) boost::math::constants::two_pi<Real>(), tol);
218 BOOST_CHECK_CLOSE_FRACTION(Q, expected, tol);
221 BOOST_AUTO_TEST_CASE(trapezoidal_quadrature)
223 test_constant<float>();
224 test_constant<double>();
225 test_constant<long double>();
226 test_constant<boost::math::concepts::real_concept>();
227 test_constant<cpp_bin_float_50>();
228 test_constant<cpp_bin_float_100>();
230 test_rational_periodic<float>();
231 test_rational_periodic<double>();
232 test_rational_periodic<long double>();
233 test_rational_periodic<boost::math::concepts::real_concept>();
234 test_rational_periodic<cpp_bin_float_50>();
235 test_rational_periodic<cpp_bin_float_100>();
237 test_bump_function<float>();
238 test_bump_function<double>();
239 test_bump_function<long double>();
240 test_rational_periodic<boost::math::concepts::real_concept>();
241 test_rational_periodic<cpp_bin_float_50>();
243 test_zero_function<float>();
244 test_zero_function<double>();
245 test_zero_function<long double>();
246 test_zero_function<boost::math::concepts::real_concept>();
247 test_zero_function<cpp_bin_float_50>();
248 test_zero_function<cpp_bin_float_100>();
251 test_sinsq<double>();
252 test_sinsq<long double>();
253 test_sinsq<boost::math::concepts::real_concept>();
254 test_sinsq<cpp_bin_float_50>();
255 test_sinsq<cpp_bin_float_100>();
257 test_slowly_converging<float>();
258 test_slowly_converging<double>();
259 test_slowly_converging<long double>();
260 test_slowly_converging<boost::math::concepts::real_concept>();
262 test_rational_sin<float>();
263 test_rational_sin<double>();
264 test_rational_sin<long double>();
265 //test_rational_sin<boost::math::concepts::real_concept>();
266 test_rational_sin<cpp_bin_float_50>();
268 test_complex_bessel<std::complex<float>>();
269 test_complex_bessel<std::complex<double>>();
270 test_complex_bessel<std::complex<long double>>();
271 //test_complex_bessel<boost::multiprecision::mpc_complex_100>();
272 test_I0_complex<std::complex<float>>();
273 test_I0_complex<std::complex<double>>();
274 test_I0_complex<std::complex<long double>>();
275 //test_I0_complex<boost::multiprecision::mpc_complex_100>();
276 test_erfc<std::complex<float>>();
277 test_erfc<std::complex<double>>();
278 test_erfc<std::complex<long double>>();
279 //test_erfc<boost::multiprecision::number<boost::multiprecision::mpc_complex_backend<20>>>();
280 //test_erfc<boost::multiprecision::number<boost::multiprecision::mpc_complex_backend<30>>>();
281 //test_erfc<boost::multiprecision::mpc_complex_50>();
282 //test_erfc<boost::multiprecision::mpc_complex_100>();
284 #ifdef BOOST_HAS_FLOAT128
285 test_complex_bessel<boost::multiprecision::complex128>();
286 test_I0_complex<boost::multiprecision::complex128>();
287 test_erfc<boost::multiprecision::complex128>();