1 // Copyright Nick Thompson, 2017
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #define BOOST_TEST_MODULE tanh_sinh_quadrature_test
9 #include <boost/config.hpp>
10 #include <boost/detail/workaround.hpp>
12 #if !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && !defined(BOOST_NO_SFINAE_EXPR)
14 #include <boost/math/concepts/real_concept.hpp>
15 #include <boost/test/included/unit_test.hpp>
16 #include <boost/test/floating_point_comparison.hpp>
17 #include <boost/math/quadrature/gauss.hpp>
18 #include <boost/math/special_functions/sinc.hpp>
19 #include <boost/multiprecision/cpp_bin_float.hpp>
22 #pragma warning(disable:4127) // Conditional expression is constant
25 #if !defined(TEST1) && !defined(TEST2)
48 using boost::math::quadrature::gauss
;
49 using boost::math::constants::pi
;
50 using boost::math::constants::half_pi
;
51 using boost::math::constants::two_div_pi
;
52 using boost::math::constants::two_pi
;
53 using boost::math::constants::half
;
54 using boost::math::constants::third
;
55 using boost::math::constants::half
;
56 using boost::math::constants::third
;
57 using boost::math::constants::catalan
;
58 using boost::math::constants::ln_two
;
59 using boost::math::constants::root_two
;
60 using boost::math::constants::root_two_pi
;
61 using boost::math::constants::root_pi
;
62 using boost::multiprecision::cpp_bin_float_quad
;
65 // Error rates depend only on the number of points in the approximation, not the type being tested,
66 // define all our expected errors here:
73 test_three_quad_error_id
,
74 test_three_quad_error_id_2
,
75 test_integration_over_real_line_error_id
,
76 test_right_limit_infinite_error_id
,
77 test_left_limit_infinite_error_id
80 template <unsigned Points
>
81 double expected_error(unsigned)
83 return 0; // placeholder, all tests will fail
87 double expected_error
<7>(unsigned id
)
91 case test_ca_error_id
:
93 case test_ca_error_id_2
:
95 case test_three_quad_error_id
:
97 case test_three_quad_error_id_2
:
99 case test_integration_over_real_line_error_id
:
101 case test_right_limit_infinite_error_id
:
102 case test_left_limit_infinite_error_id
:
105 return 0; // placeholder, all tests will fail
109 double expected_error
<9>(unsigned id
)
113 case test_ca_error_id
:
115 case test_ca_error_id_2
:
117 case test_three_quad_error_id
:
119 case test_three_quad_error_id_2
:
121 case test_integration_over_real_line_error_id
:
123 case test_right_limit_infinite_error_id
:
124 case test_left_limit_infinite_error_id
:
127 return 0; // placeholder, all tests will fail
131 double expected_error
<10>(unsigned id
)
135 case test_ca_error_id
:
137 case test_ca_error_id_2
:
139 case test_three_quad_error_id
:
141 case test_three_quad_error_id_2
:
143 case test_integration_over_real_line_error_id
:
144 return 6e-3; // doesn't get any better with more points!
145 case test_right_limit_infinite_error_id
:
146 case test_left_limit_infinite_error_id
:
149 return 0; // placeholder, all tests will fail
153 double expected_error
<15>(unsigned id
)
157 case test_ca_error_id
:
159 case test_ca_error_id_2
:
161 case test_three_quad_error_id
:
163 case test_three_quad_error_id_2
:
165 case test_integration_over_real_line_error_id
:
166 return 6e-3; // doesn't get any better with more points!
167 case test_right_limit_infinite_error_id
:
168 case test_left_limit_infinite_error_id
:
171 return 0; // placeholder, all tests will fail
175 double expected_error
<20>(unsigned id
)
179 case test_ca_error_id
:
181 case test_ca_error_id_2
:
183 case test_three_quad_error_id
:
185 case test_three_quad_error_id_2
:
187 case test_integration_over_real_line_error_id
:
188 return 5e-5; // doesn't get any better with more points!
189 case test_right_limit_infinite_error_id
:
190 case test_left_limit_infinite_error_id
:
193 return 0; // placeholder, all tests will fail
197 double expected_error
<25>(unsigned id
)
201 case test_ca_error_id
:
203 case test_ca_error_id_2
:
205 case test_three_quad_error_id
:
207 case test_three_quad_error_id_2
:
209 case test_integration_over_real_line_error_id
:
211 case test_right_limit_infinite_error_id
:
212 case test_left_limit_infinite_error_id
:
215 return 0; // placeholder, all tests will fail
219 double expected_error
<30>(unsigned id
)
223 case test_ca_error_id
:
225 case test_ca_error_id_2
:
227 case test_three_quad_error_id
:
229 case test_three_quad_error_id_2
:
231 case test_integration_over_real_line_error_id
:
233 case test_right_limit_infinite_error_id
:
234 case test_left_limit_infinite_error_id
:
237 return 0; // placeholder, all tests will fail
241 template<class Real
, unsigned Points
>
244 std::cout
<< "Testing linear functions are integrated properly by gauss on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
245 Real tol
= boost::math::tools::epsilon
<Real
>() * 10;
246 auto f
= [](const Real
& x
)
251 Real Q
= gauss
<Real
, Points
>::integrate(f
, (Real
) 0, (Real
) 1, &L1
);
252 BOOST_CHECK_CLOSE_FRACTION(Q
, 9.5, tol
);
253 BOOST_CHECK_CLOSE_FRACTION(L1
, 9.5, tol
);
256 template<class Real
, unsigned Points
>
257 void test_quadratic()
259 std::cout
<< "Testing quadratic functions are integrated properly by tanh_sinh on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
260 Real tol
= boost::math::tools::epsilon
<Real
>() * 10;
262 auto f
= [](const Real
& x
) { return 5*x
*x
+ 7*x
+ 12; };
264 Real Q
= gauss
<Real
, Points
>::integrate(f
, 0, 1, &L1
);
265 BOOST_CHECK_CLOSE_FRACTION(Q
, (Real
) 17 + half
<Real
>()*third
<Real
>(), tol
);
266 BOOST_CHECK_CLOSE_FRACTION(L1
, (Real
) 17 + half
<Real
>()*third
<Real
>(), tol
);
269 // Examples taken from
270 //http://crd-legacy.lbl.gov/~dhbailey/dhbpapers/quadrature.pdf
271 template<class Real
, unsigned Points
>
274 std::cout
<< "Testing integration of C(a) on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
275 Real tol
= expected_error
<Points
>(test_ca_error_id
);
278 auto f1
= [](const Real
& x
) { return atan(x
)/(x
*(x
*x
+ 1)) ; };
279 Real Q
= gauss
<Real
, Points
>::integrate(f1
, 0, 1, &L1
);
280 Real Q_expected
= pi
<Real
>()*ln_two
<Real
>()/8 + catalan
<Real
>()*half
<Real
>();
281 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
282 BOOST_CHECK_CLOSE_FRACTION(L1
, Q_expected
, tol
);
284 auto f2
= [](Real x
)->Real
{ Real t0
= x
*x
+ 1; Real t1
= sqrt(t0
); return atan(t1
)/(t0
*t1
); };
285 Q
= gauss
<Real
, Points
>::integrate(f2
, 0 , 1, &L1
);
286 Q_expected
= pi
<Real
>()/4 - pi
<Real
>()/root_two
<Real
>() + 3*atan(root_two
<Real
>())/root_two
<Real
>();
287 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
288 BOOST_CHECK_CLOSE_FRACTION(L1
, Q_expected
, tol
);
290 tol
= expected_error
<Points
>(test_ca_error_id_2
);
291 auto f5
= [](Real t
)->Real
{ return t
*t
*log(t
)/((t
*t
- 1)*(t
*t
*t
*t
+ 1)); };
292 Q
= gauss
<Real
, Points
>::integrate(f5
, 0 , 1);
293 Q_expected
= pi
<Real
>()*pi
<Real
>()*(2 - root_two
<Real
>())/32;
294 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
297 template<class Real
, unsigned Points
>
298 void test_three_quadrature_schemes_examples()
300 std::cout
<< "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
301 Real tol
= expected_error
<Points
>(test_three_quad_error_id
);
306 auto f1
= [](const Real
& t
) { return t
*boost::math::log1p(t
); };
307 Q
= gauss
<Real
, Points
>::integrate(f1
, 0 , 1);
308 Q_expected
= half
<Real
>()*half
<Real
>();
309 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
313 auto f2
= [](const Real
& t
) { return t
*t
*atan(t
); };
314 Q
= gauss
<Real
, Points
>::integrate(f2
, 0 , 1);
315 Q_expected
= (pi
<Real
>() -2 + 2*ln_two
<Real
>())/12;
316 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, 2 * tol
);
319 auto f3
= [](const Real
& t
) { return exp(t
)*cos(t
); };
320 Q
= gauss
<Real
, Points
>::integrate(f3
, 0, half_pi
<Real
>());
321 Q_expected
= boost::math::expm1(half_pi
<Real
>())*half
<Real
>();
322 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
325 auto f4
= [](Real x
)->Real
{ Real t0
= sqrt(x
*x
+ 2); return atan(t0
)/(t0
*(x
*x
+1)); };
326 Q
= gauss
<Real
, Points
>::integrate(f4
, 0 , 1);
327 Q_expected
= 5*pi
<Real
>()*pi
<Real
>()/96;
328 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
330 tol
= expected_error
<Points
>(test_three_quad_error_id_2
);
332 auto f5
= [](const Real
& t
) { return sqrt(t
)*log(t
); };
333 Q
= gauss
<Real
, Points
>::integrate(f5
, 0 , 1);
334 Q_expected
= -4/ (Real
) 9;
335 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
338 auto f6
= [](const Real
& t
) { return sqrt(1 - t
*t
); };
339 Q
= gauss
<Real
, Points
>::integrate(f6
, 0 , 1);
340 Q_expected
= pi
<Real
>()/4;
341 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
345 template<class Real
, unsigned Points
>
346 void test_integration_over_real_line()
348 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";
349 Real tol
= expected_error
<Points
>(test_integration_over_real_line_error_id
);
354 auto f1
= [](const Real
& t
) { return 1/(1+t
*t
);};
355 Q
= gauss
<Real
, Points
>::integrate(f1
, -boost::math::tools::max_value
<Real
>(), boost::math::tools::max_value
<Real
>(), &L1
);
356 Q_expected
= pi
<Real
>();
357 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
358 BOOST_CHECK_CLOSE_FRACTION(L1
, Q_expected
, tol
);
361 template<class Real
, unsigned Points
>
362 void test_right_limit_infinite()
364 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";
365 Real tol
= expected_error
<Points
>(test_right_limit_infinite_error_id
);
371 auto f1
= [](const Real
& t
) { return 1/(1+t
*t
);};
372 Q
= gauss
<Real
, Points
>::integrate(f1
, 0, boost::math::tools::max_value
<Real
>(), &L1
);
373 Q_expected
= half_pi
<Real
>();
374 BOOST_CHECK_CLOSE(Q
, Q_expected
, 100*tol
);
376 auto f4
= [](const Real
& t
) { return 1/(1+t
*t
); };
377 Q
= gauss
<Real
, Points
>::integrate(f4
, 1, boost::math::tools::max_value
<Real
>(), &L1
);
378 Q_expected
= pi
<Real
>()/4;
379 BOOST_CHECK_CLOSE(Q
, Q_expected
, 100*tol
);
382 template<class Real
, unsigned Points
>
383 void test_left_limit_infinite()
385 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";
386 Real tol
= expected_error
<Points
>(test_left_limit_infinite_error_id
);
391 auto f1
= [](const Real
& t
) { return 1/(1+t
*t
);};
392 Q
= gauss
<Real
, Points
>::integrate(f1
, -boost::math::tools::max_value
<Real
>(), Real(0));
393 Q_expected
= half_pi
<Real
>();
394 BOOST_CHECK_CLOSE(Q
, Q_expected
, 100*tol
);
397 BOOST_AUTO_TEST_CASE(gauss_quadrature_test
)
400 test_linear
<double, 7>();
401 test_quadratic
<double, 7>();
402 test_ca
<double, 7>();
403 test_three_quadrature_schemes_examples
<double, 7>();
404 test_integration_over_real_line
<double, 7>();
405 test_right_limit_infinite
<double, 7>();
406 test_left_limit_infinite
<double, 7>();
408 test_linear
<double, 9>();
409 test_quadratic
<double, 9>();
410 test_ca
<double, 9>();
411 test_three_quadrature_schemes_examples
<double, 9>();
412 test_integration_over_real_line
<double, 9>();
413 test_right_limit_infinite
<double, 9>();
414 test_left_limit_infinite
<double, 9>();
416 test_linear
<cpp_bin_float_quad
, 10>();
417 test_quadratic
<cpp_bin_float_quad
, 10>();
418 test_ca
<cpp_bin_float_quad
, 10>();
419 test_three_quadrature_schemes_examples
<cpp_bin_float_quad
, 10>();
420 test_integration_over_real_line
<cpp_bin_float_quad
, 10>();
421 test_right_limit_infinite
<cpp_bin_float_quad
, 10>();
422 test_left_limit_infinite
<cpp_bin_float_quad
, 10>();
425 test_linear
<cpp_bin_float_quad
, 15>();
426 test_quadratic
<cpp_bin_float_quad
, 15>();
427 test_ca
<cpp_bin_float_quad
, 15>();
428 test_three_quadrature_schemes_examples
<cpp_bin_float_quad
, 15>();
429 test_integration_over_real_line
<cpp_bin_float_quad
, 15>();
430 test_right_limit_infinite
<cpp_bin_float_quad
, 15>();
431 test_left_limit_infinite
<cpp_bin_float_quad
, 15>();
433 test_linear
<cpp_bin_float_quad
, 20>();
434 test_quadratic
<cpp_bin_float_quad
, 20>();
435 test_ca
<cpp_bin_float_quad
, 20>();
436 test_three_quadrature_schemes_examples
<cpp_bin_float_quad
, 20>();
437 test_integration_over_real_line
<cpp_bin_float_quad
, 20>();
438 test_right_limit_infinite
<cpp_bin_float_quad
, 20>();
439 test_left_limit_infinite
<cpp_bin_float_quad
, 20>();
441 test_linear
<cpp_bin_float_quad
, 25>();
442 test_quadratic
<cpp_bin_float_quad
, 25>();
443 test_ca
<cpp_bin_float_quad
, 25>();
444 test_three_quadrature_schemes_examples
<cpp_bin_float_quad
, 25>();
445 test_integration_over_real_line
<cpp_bin_float_quad
, 25>();
446 test_right_limit_infinite
<cpp_bin_float_quad
, 25>();
447 test_left_limit_infinite
<cpp_bin_float_quad
, 25>();
449 test_linear
<cpp_bin_float_quad
, 30>();
450 test_quadratic
<cpp_bin_float_quad
, 30>();
451 test_ca
<cpp_bin_float_quad
, 30>();
452 test_three_quadrature_schemes_examples
<cpp_bin_float_quad
, 30>();
453 test_integration_over_real_line
<cpp_bin_float_quad
, 30>();
454 test_right_limit_infinite
<cpp_bin_float_quad
, 30>();
455 test_left_limit_infinite
<cpp_bin_float_quad
, 30>();
461 int main() { return 0; }