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
10 //#include <boost/multiprecision/mpc.hpp>
11 #include <boost/config.hpp>
12 #include <boost/detail/workaround.hpp>
14 #if !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && !defined(BOOST_NO_SFINAE_EXPR)
16 #include <boost/math/concepts/real_concept.hpp>
17 #include <boost/test/included/unit_test.hpp>
18 #include <boost/test/tools/floating_point_comparison.hpp>
19 #include <boost/math/quadrature/gauss.hpp>
20 #include <boost/math/special_functions/sinc.hpp>
21 #include <boost/math/tools/test_value.hpp>
22 #include <boost/multiprecision/cpp_bin_float.hpp>
23 #include <boost/multiprecision/cpp_complex.hpp>
25 #ifdef BOOST_HAS_FLOAT128
26 #include <boost/multiprecision/complex128.hpp>
30 #pragma warning(disable:4127) // Conditional expression is constant
33 #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3)
57 using boost::math::quadrature::gauss
;
58 using boost::math::constants::pi
;
59 using boost::math::constants::half_pi
;
60 using boost::math::constants::two_div_pi
;
61 using boost::math::constants::two_pi
;
62 using boost::math::constants::half
;
63 using boost::math::constants::third
;
64 using boost::math::constants::half
;
65 using boost::math::constants::third
;
66 using boost::math::constants::catalan
;
67 using boost::math::constants::ln_two
;
68 using boost::math::constants::root_two
;
69 using boost::math::constants::root_two_pi
;
70 using boost::math::constants::root_pi
;
71 using boost::multiprecision::cpp_bin_float_quad
;
74 // Error rates depend only on the number of points in the approximation, not the type being tested,
75 // define all our expected errors here:
82 test_three_quad_error_id
,
83 test_three_quad_error_id_2
,
84 test_integration_over_real_line_error_id
,
85 test_right_limit_infinite_error_id
,
86 test_left_limit_infinite_error_id
89 template <unsigned Points
>
90 double expected_error(unsigned)
92 return 0; // placeholder, all tests will fail
96 double expected_error
<7>(unsigned id
)
100 case test_ca_error_id
:
102 case test_ca_error_id_2
:
104 case test_three_quad_error_id
:
106 case test_three_quad_error_id_2
:
108 case test_integration_over_real_line_error_id
:
110 case test_right_limit_infinite_error_id
:
111 case test_left_limit_infinite_error_id
:
114 return 0; // placeholder, all tests will fail
118 double expected_error
<9>(unsigned id
)
122 case test_ca_error_id
:
124 case test_ca_error_id_2
:
126 case test_three_quad_error_id
:
128 case test_three_quad_error_id_2
:
130 case test_integration_over_real_line_error_id
:
132 case test_right_limit_infinite_error_id
:
133 case test_left_limit_infinite_error_id
:
136 return 0; // placeholder, all tests will fail
140 double expected_error
<10>(unsigned id
)
144 case test_ca_error_id
:
146 case test_ca_error_id_2
:
148 case test_three_quad_error_id
:
150 case test_three_quad_error_id_2
:
152 case test_integration_over_real_line_error_id
:
153 return 6e-3; // doesn't get any better with more points!
154 case test_right_limit_infinite_error_id
:
155 case test_left_limit_infinite_error_id
:
158 return 0; // placeholder, all tests will fail
162 double expected_error
<15>(unsigned id
)
166 case test_ca_error_id
:
168 case test_ca_error_id_2
:
170 case test_three_quad_error_id
:
172 case test_three_quad_error_id_2
:
174 case test_integration_over_real_line_error_id
:
175 return 6e-3; // doesn't get any better with more points!
176 case test_right_limit_infinite_error_id
:
177 case test_left_limit_infinite_error_id
:
180 return 0; // placeholder, all tests will fail
184 double expected_error
<20>(unsigned id
)
188 case test_ca_error_id
:
190 case test_ca_error_id_2
:
192 case test_three_quad_error_id
:
194 case test_three_quad_error_id_2
:
196 case test_integration_over_real_line_error_id
:
197 return 5e-5; // doesn't get any better with more points!
198 case test_right_limit_infinite_error_id
:
199 case test_left_limit_infinite_error_id
:
202 return 0; // placeholder, all tests will fail
206 double expected_error
<25>(unsigned id
)
210 case test_ca_error_id
:
212 case test_ca_error_id_2
:
214 case test_three_quad_error_id
:
216 case test_three_quad_error_id_2
:
218 case test_integration_over_real_line_error_id
:
220 case test_right_limit_infinite_error_id
:
221 case test_left_limit_infinite_error_id
:
224 return 0; // placeholder, all tests will fail
228 double expected_error
<30>(unsigned id
)
232 case test_ca_error_id
:
234 case test_ca_error_id_2
:
236 case test_three_quad_error_id
:
238 case test_three_quad_error_id_2
:
240 case test_integration_over_real_line_error_id
:
242 case test_right_limit_infinite_error_id
:
243 case test_left_limit_infinite_error_id
:
246 return 0; // placeholder, all tests will fail
250 template<class Real
, unsigned Points
>
253 std::cout
<< "Testing linear functions are integrated properly by gauss on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
254 Real tol
= boost::math::tools::epsilon
<Real
>() * 10;
255 auto f
= [](const Real
& x
)
260 Real Q
= gauss
<Real
, Points
>::integrate(f
, (Real
) 0, (Real
) 1, &L1
);
261 BOOST_CHECK_CLOSE_FRACTION(Q
, 9.5, tol
);
262 BOOST_CHECK_CLOSE_FRACTION(L1
, 9.5, tol
);
263 Q
= gauss
<Real
, Points
>::integrate(f
, (Real
) 0, (Real
) 0, &L1
);
264 BOOST_CHECK_CLOSE(Q
, 0, tol
);
265 Q
= gauss
<Real
, Points
>::integrate(f
, (Real
) 1, (Real
) 0, &L1
);
266 BOOST_CHECK_CLOSE_FRACTION(Q
, -9.5, tol
);
269 template<class Real
, unsigned Points
>
270 void test_quadratic()
272 std::cout
<< "Testing quadratic functions are integrated properly by Gaussian quadrature on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
273 Real tol
= boost::math::tools::epsilon
<Real
>() * 10;
275 auto f
= [](const Real
& x
) { return 5*x
*x
+ 7*x
+ 12; };
277 Real Q
= gauss
<Real
, Points
>::integrate(f
, 0, 1, &L1
);
278 BOOST_CHECK_CLOSE_FRACTION(Q
, (Real
) 17 + half
<Real
>()*third
<Real
>(), tol
);
279 BOOST_CHECK_CLOSE_FRACTION(L1
, (Real
) 17 + half
<Real
>()*third
<Real
>(), tol
);
282 // Examples taken from
283 //http://crd-legacy.lbl.gov/~dhbailey/dhbpapers/quadrature.pdf
284 template<class Real
, unsigned Points
>
287 std::cout
<< "Testing integration of C(a) on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
288 Real tol
= expected_error
<Points
>(test_ca_error_id
);
291 auto f1
= [](const Real
& x
) { return atan(x
)/(x
*(x
*x
+ 1)) ; };
292 Real Q
= gauss
<Real
, Points
>::integrate(f1
, 0, 1, &L1
);
293 Real Q_expected
= pi
<Real
>()*ln_two
<Real
>()/8 + catalan
<Real
>()*half
<Real
>();
294 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
295 BOOST_CHECK_CLOSE_FRACTION(L1
, Q_expected
, tol
);
297 auto f2
= [](Real x
)->Real
{ Real t0
= x
*x
+ 1; Real t1
= sqrt(t0
); return atan(t1
)/(t0
*t1
); };
298 Q
= gauss
<Real
, Points
>::integrate(f2
, 0 , 1, &L1
);
299 Q_expected
= pi
<Real
>()/4 - pi
<Real
>()/root_two
<Real
>() + 3*atan(root_two
<Real
>())/root_two
<Real
>();
300 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
301 BOOST_CHECK_CLOSE_FRACTION(L1
, Q_expected
, tol
);
303 tol
= expected_error
<Points
>(test_ca_error_id_2
);
304 auto f5
= [](Real t
)->Real
{ return t
*t
*log(t
)/((t
*t
- 1)*(t
*t
*t
*t
+ 1)); };
305 Q
= gauss
<Real
, Points
>::integrate(f5
, 0 , 1);
306 Q_expected
= pi
<Real
>()*pi
<Real
>()*(2 - root_two
<Real
>())/32;
307 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
310 template<class Real
, unsigned Points
>
311 void test_three_quadrature_schemes_examples()
313 std::cout
<< "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
314 Real tol
= expected_error
<Points
>(test_three_quad_error_id
);
319 auto f1
= [](const Real
& t
) { return t
*boost::math::log1p(t
); };
320 Q
= gauss
<Real
, Points
>::integrate(f1
, 0 , 1);
321 Q_expected
= half
<Real
>()*half
<Real
>();
322 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
326 auto f2
= [](const Real
& t
) { return t
*t
*atan(t
); };
327 Q
= gauss
<Real
, Points
>::integrate(f2
, 0 , 1);
328 Q_expected
= (pi
<Real
>() -2 + 2*ln_two
<Real
>())/12;
329 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, 2 * tol
);
332 auto f3
= [](const Real
& t
) { return exp(t
)*cos(t
); };
333 Q
= gauss
<Real
, Points
>::integrate(f3
, 0, half_pi
<Real
>());
334 Q_expected
= boost::math::expm1(half_pi
<Real
>())*half
<Real
>();
335 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
338 auto f4
= [](Real x
)->Real
{ Real t0
= sqrt(x
*x
+ 2); return atan(t0
)/(t0
*(x
*x
+1)); };
339 Q
= gauss
<Real
, Points
>::integrate(f4
, 0 , 1);
340 Q_expected
= 5*pi
<Real
>()*pi
<Real
>()/96;
341 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
343 tol
= expected_error
<Points
>(test_three_quad_error_id_2
);
345 auto f5
= [](const Real
& t
) { return sqrt(t
)*log(t
); };
346 Q
= gauss
<Real
, Points
>::integrate(f5
, 0 , 1);
347 Q_expected
= -4/ (Real
) 9;
348 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
351 auto f6
= [](const Real
& t
) { return sqrt(1 - t
*t
); };
352 Q
= gauss
<Real
, Points
>::integrate(f6
, 0 , 1);
353 Q_expected
= pi
<Real
>()/4;
354 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
358 template<class Real
, unsigned Points
>
359 void test_integration_over_real_line()
361 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";
362 Real tol
= expected_error
<Points
>(test_integration_over_real_line_error_id
);
367 auto f1
= [](const Real
& t
) { return 1/(1+t
*t
);};
368 Q
= gauss
<Real
, Points
>::integrate(f1
, -boost::math::tools::max_value
<Real
>(), boost::math::tools::max_value
<Real
>(), &L1
);
369 Q_expected
= pi
<Real
>();
370 BOOST_CHECK_CLOSE_FRACTION(Q
, Q_expected
, tol
);
371 BOOST_CHECK_CLOSE_FRACTION(L1
, Q_expected
, tol
);
374 template<class Real
, unsigned Points
>
375 void test_right_limit_infinite()
377 std::cout
<< "Testing right limit infinite for Gaussian quadrature in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
378 Real tol
= expected_error
<Points
>(test_right_limit_infinite_error_id
);
384 auto f1
= [](const Real
& t
) { return 1/(1+t
*t
);};
385 Q
= gauss
<Real
, Points
>::integrate(f1
, 0, boost::math::tools::max_value
<Real
>(), &L1
);
386 Q_expected
= half_pi
<Real
>();
387 BOOST_CHECK_CLOSE(Q
, Q_expected
, 100*tol
);
389 auto f4
= [](const Real
& t
) { return 1/(1+t
*t
); };
390 Q
= gauss
<Real
, Points
>::integrate(f4
, 1, boost::math::tools::max_value
<Real
>(), &L1
);
391 Q_expected
= pi
<Real
>()/4;
392 BOOST_CHECK_CLOSE(Q
, Q_expected
, 100*tol
);
395 template<class Real
, unsigned Points
>
396 void test_left_limit_infinite()
398 std::cout
<< "Testing left limit infinite for Gaussian quadrature in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
399 Real tol
= expected_error
<Points
>(test_left_limit_infinite_error_id
);
404 auto f1
= [](const Real
& t
) { return 1/(1+t
*t
);};
405 Q
= gauss
<Real
, Points
>::integrate(f1
, -boost::math::tools::max_value
<Real
>(), Real(0));
406 Q_expected
= half_pi
<Real
>();
407 BOOST_CHECK_CLOSE(Q
, Q_expected
, 100*tol
);
410 template<class Complex
>
411 void test_complex_lambert_w()
413 std::cout
<< "Testing that complex-valued integrands are integrated correctly by Gaussian quadrature on type " << boost::typeindex::type_id
<Complex
>().pretty_name() << "\n";
414 typedef typename
Complex::value_type Real
;
416 using boost::math::constants::pi
;
418 auto lw
= [&z
](Real v
)->Complex
{
425 Real cotv
= cosv
/sinv
;
427 Real t
= (1-v
*cotv
)*(1-v
*cotv
) + v
*v
;
428 Real x
= v
*cscv
*exp(-v
*cotv
);
430 Complex num
= t
*(z
/pi
<Real
>());
431 Complex res
= num
/den
;
435 //N[ProductLog[2+3*I], 150]
436 #ifndef BOOST_MATH_STANDALONE
437 Complex Q
= gauss
<Real
, 30>::integrate(lw
, (Real
) 0, pi
<Real
>());
438 BOOST_CHECK_CLOSE_FRACTION(Q
.real(), BOOST_MATH_TEST_VALUE(Real
, 1.0900765344857908463017778267816696498710210863535777805644), tol
);
439 BOOST_CHECK_CLOSE_FRACTION(Q
.imag(), BOOST_MATH_TEST_VALUE(Real
, 0.5301397207748388014268602135741217419287056313827031782979), tol
);
443 BOOST_AUTO_TEST_CASE(gauss_quadrature_test
)
447 test_linear
<double, 7>();
448 test_quadratic
<double, 7>();
449 test_ca
<double, 7>();
450 test_three_quadrature_schemes_examples
<double, 7>();
451 test_integration_over_real_line
<double, 7>();
452 test_right_limit_infinite
<double, 7>();
453 test_left_limit_infinite
<double, 7>();
455 test_linear
<double, 9>();
456 test_quadratic
<double, 9>();
457 test_ca
<double, 9>();
458 test_three_quadrature_schemes_examples
<double, 9>();
459 test_integration_over_real_line
<double, 9>();
460 test_right_limit_infinite
<double, 9>();
461 test_left_limit_infinite
<double, 9>();
463 test_linear
<cpp_bin_float_quad
, 10>();
464 test_quadratic
<cpp_bin_float_quad
, 10>();
465 test_ca
<cpp_bin_float_quad
, 10>();
466 test_three_quadrature_schemes_examples
<cpp_bin_float_quad
, 10>();
467 test_integration_over_real_line
<cpp_bin_float_quad
, 10>();
468 test_right_limit_infinite
<cpp_bin_float_quad
, 10>();
469 test_left_limit_infinite
<cpp_bin_float_quad
, 10>();
472 test_linear
<cpp_bin_float_quad
, 15>();
473 test_quadratic
<cpp_bin_float_quad
, 15>();
474 test_ca
<cpp_bin_float_quad
, 15>();
475 test_three_quadrature_schemes_examples
<cpp_bin_float_quad
, 15>();
476 test_integration_over_real_line
<cpp_bin_float_quad
, 15>();
477 test_right_limit_infinite
<cpp_bin_float_quad
, 15>();
478 test_left_limit_infinite
<cpp_bin_float_quad
, 15>();
480 test_linear
<cpp_bin_float_quad
, 20>();
481 test_quadratic
<cpp_bin_float_quad
, 20>();
482 test_ca
<cpp_bin_float_quad
, 20>();
483 test_three_quadrature_schemes_examples
<cpp_bin_float_quad
, 20>();
484 test_integration_over_real_line
<cpp_bin_float_quad
, 20>();
485 test_right_limit_infinite
<cpp_bin_float_quad
, 20>();
486 test_left_limit_infinite
<cpp_bin_float_quad
, 20>();
488 test_linear
<cpp_bin_float_quad
, 25>();
489 test_quadratic
<cpp_bin_float_quad
, 25>();
490 test_ca
<cpp_bin_float_quad
, 25>();
491 test_three_quadrature_schemes_examples
<cpp_bin_float_quad
, 25>();
492 test_integration_over_real_line
<cpp_bin_float_quad
, 25>();
493 test_right_limit_infinite
<cpp_bin_float_quad
, 25>();
494 test_left_limit_infinite
<cpp_bin_float_quad
, 25>();
496 test_linear
<cpp_bin_float_quad
, 30>();
497 test_quadratic
<cpp_bin_float_quad
, 30>();
498 test_ca
<cpp_bin_float_quad
, 30>();
499 test_three_quadrature_schemes_examples
<cpp_bin_float_quad
, 30>();
500 test_integration_over_real_line
<cpp_bin_float_quad
, 30>();
501 test_right_limit_infinite
<cpp_bin_float_quad
, 30>();
502 test_left_limit_infinite
<cpp_bin_float_quad
, 30>();
507 test_left_limit_infinite
<cpp_bin_float_quad
, 30>();
508 test_complex_lambert_w
<std::complex<double>>();
509 test_complex_lambert_w
<std::complex<long double>>();
510 #ifdef BOOST_HAS_FLOAT128
511 test_left_limit_infinite
<boost::multiprecision::float128
, 30>();
512 test_complex_lambert_w
<boost::multiprecision::complex128
>();
514 test_complex_lambert_w
<boost::multiprecision::cpp_complex_quad
>();
520 int main() { return 0; }