]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/math/test/gauss_kronrod_quadrature_test.cpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / libs / math / test / gauss_kronrod_quadrature_test.cpp
CommitLineData
b32b8144
FG
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)
6
7#define BOOST_TEST_MODULE Gauss Kronrod_quadrature_test
8
92f5a8d4 9#include <complex>
b32b8144
FG
10#include <boost/config.hpp>
11#include <boost/detail/workaround.hpp>
12
13#if !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && !defined(BOOST_NO_SFINAE_EXPR)
14
15#include <boost/math/concepts/real_concept.hpp>
16#include <boost/test/included/unit_test.hpp>
92f5a8d4 17#include <boost/test/tools/floating_point_comparison.hpp>
b32b8144
FG
18#include <boost/math/quadrature/gauss_kronrod.hpp>
19#include <boost/math/special_functions/sinc.hpp>
20#include <boost/multiprecision/cpp_bin_float.hpp>
21#include <boost/multiprecision/cpp_dec_float.hpp>
22#include <boost/multiprecision/debug_adaptor.hpp>
23
92f5a8d4
TL
24#ifdef BOOST_HAS_FLOAT128
25#include <boost/multiprecision/complex128.hpp>
26#endif
27
b32b8144
FG
28#if !defined(TEST1) && !defined(TEST1A) && !defined(TEST2) && !defined(TEST3)
29# define TEST1
30# define TEST1A
31# define TEST2
32# define TEST3
33#endif
34
35#ifdef _MSC_VER
36#pragma warning(disable:4127) // Conditional expression is constant
37#endif
38
39using std::expm1;
40using std::atan;
41using std::tan;
42using std::log;
43using std::log1p;
44using std::asinh;
45using std::atanh;
46using std::sqrt;
47using std::isnormal;
48using std::abs;
49using std::sinh;
50using std::tanh;
51using std::cosh;
52using std::pow;
53using std::exp;
54using std::sin;
55using std::cos;
56using std::string;
57using boost::math::quadrature::gauss_kronrod;
58using boost::math::constants::pi;
59using boost::math::constants::half_pi;
60using boost::math::constants::two_div_pi;
61using boost::math::constants::two_pi;
62using boost::math::constants::half;
63using boost::math::constants::third;
64using boost::math::constants::half;
65using boost::math::constants::third;
66using boost::math::constants::catalan;
67using boost::math::constants::ln_two;
68using boost::math::constants::root_two;
69using boost::math::constants::root_two_pi;
70using boost::math::constants::root_pi;
71using boost::multiprecision::cpp_bin_float_quad;
72using boost::multiprecision::cpp_dec_float_50;
73using boost::multiprecision::debug_adaptor;
74using boost::multiprecision::number;
75
76//
77// Error rates depend only on the number of points in the approximation, not the type being tested,
78// define all our expected errors here:
79//
80
81enum
82{
83 test_ca_error_id,
84 test_ca_error_id_2,
85 test_three_quad_error_id,
86 test_three_quad_error_id_2,
87 test_integration_over_real_line_error_id,
88 test_right_limit_infinite_error_id,
89 test_left_limit_infinite_error_id
90};
91
92template <unsigned Points>
93double expected_error(unsigned)
94{
95 return 0; // placeholder, all tests will fail
96}
97
98template <>
99double expected_error<15>(unsigned id)
100{
101 switch (id)
102 {
103 case test_ca_error_id:
104 return 1e-7;
105 case test_ca_error_id_2:
106 return 2e-5;
107 case test_three_quad_error_id:
108 return 1e-8;
109 case test_three_quad_error_id_2:
110 return 3.5e-3;
111 case test_integration_over_real_line_error_id:
112 return 6e-3;
113 case test_right_limit_infinite_error_id:
114 case test_left_limit_infinite_error_id:
115 return 1e-5;
116 }
117 return 0; // placeholder, all tests will fail
118}
119
120template <>
121double expected_error<17>(unsigned id)
122{
123 switch (id)
124 {
125 case test_ca_error_id:
126 return 1e-7;
127 case test_ca_error_id_2:
128 return 2e-5;
129 case test_three_quad_error_id:
130 return 1e-8;
131 case test_three_quad_error_id_2:
132 return 3.5e-3;
133 case test_integration_over_real_line_error_id:
134 return 6e-3;
135 case test_right_limit_infinite_error_id:
136 case test_left_limit_infinite_error_id:
137 return 1e-5;
138 }
139 return 0; // placeholder, all tests will fail
140}
141
142template <>
143double expected_error<21>(unsigned id)
144{
145 switch (id)
146 {
147 case test_ca_error_id:
148 return 1e-12;
149 case test_ca_error_id_2:
150 return 3e-6;
151 case test_three_quad_error_id:
152 return 2e-13;
153 case test_three_quad_error_id_2:
154 return 2e-3;
155 case test_integration_over_real_line_error_id:
156 return 6e-3; // doesn't get any better with more points!
157 case test_right_limit_infinite_error_id:
158 case test_left_limit_infinite_error_id:
159 return 5e-8;
160 }
161 return 0; // placeholder, all tests will fail
162}
163
164template <>
165double expected_error<31>(unsigned id)
166{
167 switch (id)
168 {
169 case test_ca_error_id:
170 return 6e-20;
171 case test_ca_error_id_2:
172 return 3e-7;
173 case test_three_quad_error_id:
174 return 1e-19;
175 case test_three_quad_error_id_2:
176 return 6e-4;
177 case test_integration_over_real_line_error_id:
178 return 6e-3; // doesn't get any better with more points!
179 case test_right_limit_infinite_error_id:
180 case test_left_limit_infinite_error_id:
181 return 5e-11;
182 }
183 return 0; // placeholder, all tests will fail
184}
185
186template <>
187double expected_error<41>(unsigned id)
188{
189 switch (id)
190 {
191 case test_ca_error_id:
192 return 1e-26;
193 case test_ca_error_id_2:
194 return 1e-7;
195 case test_three_quad_error_id:
196 return 3e-27;
197 case test_three_quad_error_id_2:
198 return 3e-4;
199 case test_integration_over_real_line_error_id:
200 return 5e-5; // doesn't get any better with more points!
201 case test_right_limit_infinite_error_id:
202 case test_left_limit_infinite_error_id:
203 return 1e-15;
204 }
205 return 0; // placeholder, all tests will fail
206}
207
208template <>
209double expected_error<51>(unsigned id)
210{
211 switch (id)
212 {
213 case test_ca_error_id:
214 return 5e-33;
215 case test_ca_error_id_2:
216 return 1e-8;
217 case test_three_quad_error_id:
218 return 1e-32;
219 case test_three_quad_error_id_2:
220 return 3e-4;
221 case test_integration_over_real_line_error_id:
222 return 1e-14;
223 case test_right_limit_infinite_error_id:
224 case test_left_limit_infinite_error_id:
225 return 3e-19;
226 }
227 return 0; // placeholder, all tests will fail
228}
229
230template <>
231double expected_error<61>(unsigned id)
232{
233 switch (id)
234 {
235 case test_ca_error_id:
236 return 5e-34;
237 case test_ca_error_id_2:
238 return 5e-9;
239 case test_three_quad_error_id:
240 return 4e-34;
241 case test_three_quad_error_id_2:
242 return 1e-4;
243 case test_integration_over_real_line_error_id:
244 return 1e-16;
245 case test_right_limit_infinite_error_id:
246 case test_left_limit_infinite_error_id:
247 return 3e-23;
248 }
249 return 0; // placeholder, all tests will fail
250}
251
252
253template<class Real, unsigned Points>
254void test_linear()
255{
256 std::cout << "Testing linear functions are integrated properly by gauss_kronrod on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
257 Real tol = boost::math::tools::epsilon<Real>() * 10;
258 Real error;
92f5a8d4 259 auto f = [](const Real& x)->Real
b32b8144
FG
260 {
261 return 5*x + 7;
262 };
263 Real L1;
264 Real Q = gauss_kronrod<Real, Points>::integrate(f, (Real) 0, (Real) 1, 0, 0, &error, &L1);
265 BOOST_CHECK_CLOSE_FRACTION(Q, 9.5, tol);
266 BOOST_CHECK_CLOSE_FRACTION(L1, 9.5, tol);
f67539c2
TL
267
268 Q = gauss_kronrod<Real, Points>::integrate(f, (Real) 1, (Real) 0, 0, 0, &error, &L1);
269 BOOST_CHECK_CLOSE_FRACTION(Q, -9.5, tol);
270 BOOST_CHECK_CLOSE_FRACTION(L1, 9.5, tol);
271
272 Q = gauss_kronrod<Real, Points>::integrate(f, (Real) 0, (Real) 0, 0, 0, &error, &L1);
273 BOOST_CHECK_CLOSE(Q, Real(0), tol);
b32b8144
FG
274}
275
276template<class Real, unsigned Points>
277void test_quadratic()
278{
279 std::cout << "Testing quadratic functions are integrated properly by Gauss Kronrod on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
280 Real tol = boost::math::tools::epsilon<Real>() * 10;
281 Real error;
282
92f5a8d4 283 auto f = [](const Real& x)->Real { return 5*x*x + 7*x + 12; };
b32b8144
FG
284 Real L1;
285 Real Q = gauss_kronrod<Real, Points>::integrate(f, 0, 1, 0, 0, &error, &L1);
286 BOOST_CHECK_CLOSE_FRACTION(Q, (Real) 17 + half<Real>()*third<Real>(), tol);
287 BOOST_CHECK_CLOSE_FRACTION(L1, (Real) 17 + half<Real>()*third<Real>(), tol);
288}
289
290// Examples taken from
291//http://crd-legacy.lbl.gov/~dhbailey/dhbpapers/quadrature.pdf
292template<class Real, unsigned Points>
293void test_ca()
294{
295 std::cout << "Testing integration of C(a) on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
296 Real tol = expected_error<Points>(test_ca_error_id);
297 Real L1;
298 Real error;
299
92f5a8d4 300 auto f1 = [](const Real& x)->Real { return atan(x)/(x*(x*x + 1)) ; };
b32b8144
FG
301 Real Q = gauss_kronrod<Real, Points>::integrate(f1, 0, 1, 0, 0, &error, &L1);
302 Real Q_expected = pi<Real>()*ln_two<Real>()/8 + catalan<Real>()*half<Real>();
303 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
304 BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
305
306 auto f2 = [](Real x)->Real { Real t0 = x*x + 1; Real t1 = sqrt(t0); return atan(t1)/(t0*t1); };
307 Q = gauss_kronrod<Real, Points>::integrate(f2, 0 , 1, 0, 0, &error, &L1);
308 Q_expected = pi<Real>()/4 - pi<Real>()/root_two<Real>() + 3*atan(root_two<Real>())/root_two<Real>();
309 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
310 BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
311
312 tol = expected_error<Points>(test_ca_error_id_2);
313 auto f5 = [](Real t)->Real { return t*t*log(t)/((t*t - 1)*(t*t*t*t + 1)); };
314 Q = gauss_kronrod<Real, Points>::integrate(f5, 0, 1, 0);
315 Q_expected = pi<Real>()*pi<Real>()*(2 - root_two<Real>())/32;
316 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
317}
318
319template<class Real, unsigned Points>
320void test_three_quadrature_schemes_examples()
321{
322 std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
323 Real tol = expected_error<Points>(test_three_quad_error_id);
324 Real Q;
325 Real Q_expected;
326
327 // Example 1:
328 auto f1 = [](const Real& t)->Real { return t*boost::math::log1p(t); };
329 Q = gauss_kronrod<Real, Points>::integrate(f1, 0 , 1, 0);
330 Q_expected = half<Real>()*half<Real>();
331 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
332
333
334 // Example 2:
335 auto f2 = [](const Real& t)->Real { return t*t*atan(t); };
336 Q = gauss_kronrod<Real, Points>::integrate(f2, 0 , 1, 0);
337 Q_expected = (pi<Real>() -2 + 2*ln_two<Real>())/12;
338 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, 2 * tol);
339
340 // Example 3:
341 auto f3 = [](const Real& t)->Real { return exp(t)*cos(t); };
342 Q = gauss_kronrod<Real, Points>::integrate(f3, 0, half_pi<Real>(), 0);
343 Q_expected = boost::math::expm1(half_pi<Real>())*half<Real>();
344 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
345
346 // Example 4:
347 auto f4 = [](Real x)->Real { Real t0 = sqrt(x*x + 2); return atan(t0)/(t0*(x*x+1)); };
348 Q = gauss_kronrod<Real, Points>::integrate(f4, 0 , 1, 0);
349 Q_expected = 5*pi<Real>()*pi<Real>()/96;
350 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
351
352 tol = expected_error<Points>(test_three_quad_error_id_2);
353 // Example 5:
354 auto f5 = [](const Real& t)->Real { return sqrt(t)*log(t); };
355 Q = gauss_kronrod<Real, Points>::integrate(f5, 0 , 1, 0);
356 Q_expected = -4/ (Real) 9;
357 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
358
359 // Example 6:
360 auto f6 = [](const Real& t)->Real { return sqrt(1 - t*t); };
361 Q = gauss_kronrod<Real, Points>::integrate(f6, 0 , 1, 0);
362 Q_expected = pi<Real>()/4;
363 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
364}
365
366
367template<class Real, unsigned Points>
368void test_integration_over_real_line()
369{
370 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";
371 Real tol = expected_error<Points>(test_integration_over_real_line_error_id);
372 Real Q;
373 Real Q_expected;
374 Real L1;
375 Real error;
376
92f5a8d4 377 auto f1 = [](const Real& t)->Real { return 1/(1+t*t);};
b32b8144
FG
378 Q = gauss_kronrod<Real, Points>::integrate(f1, -boost::math::tools::max_value<Real>(), boost::math::tools::max_value<Real>(), 0, 0, &error, &L1);
379 Q_expected = pi<Real>();
380 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
381 BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
382}
383
384template<class Real, unsigned Points>
385void test_right_limit_infinite()
386{
387 std::cout << "Testing right limit infinite for Gauss Kronrod in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
388 Real tol = expected_error<Points>(test_right_limit_infinite_error_id);
389 Real Q;
390 Real Q_expected;
391 Real L1;
392 Real error;
393
394 // Example 11:
92f5a8d4 395 auto f1 = [](const Real& t)->Real { return 1/(1+t*t);};
b32b8144
FG
396 Q = gauss_kronrod<Real, Points>::integrate(f1, 0, boost::math::tools::max_value<Real>(), 0, 0, &error, &L1);
397 Q_expected = half_pi<Real>();
398 BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
399
92f5a8d4 400 auto f4 = [](const Real& t)->Real { return 1/(1+t*t); };
b32b8144
FG
401 Q = gauss_kronrod<Real, Points>::integrate(f4, 1, boost::math::tools::max_value<Real>(), 0, 0, &error, &L1);
402 Q_expected = pi<Real>()/4;
403 BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
404}
405
406template<class Real, unsigned Points>
407void test_left_limit_infinite()
408{
409 std::cout << "Testing left limit infinite for Gauss Kronrod in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
410 Real tol = expected_error<Points>(test_left_limit_infinite_error_id);
411 Real Q;
412 Real Q_expected;
413
414 // Example 11:
92f5a8d4 415 auto f1 = [](const Real& t)->Real { return 1/(1+t*t);};
b32b8144
FG
416 Q = gauss_kronrod<Real, Points>::integrate(f1, -boost::math::tools::max_value<Real>(), Real(0), 0);
417 Q_expected = half_pi<Real>();
418 BOOST_CHECK_CLOSE(Q, Q_expected, 100*tol);
419}
420
92f5a8d4
TL
421template<class Complex>
422void test_complex_lambert_w()
423{
424 std::cout << "Testing that complex-valued integrands are integrated correctly by Gaussian quadrature on type " << boost::typeindex::type_id<Complex>().pretty_name() << "\n";
425 typedef typename Complex::value_type Real;
426 Real tol = 10e-9;
427 using boost::math::constants::pi;
428 Complex z{2, 3};
429 auto lw = [&z](Real v)->Complex {
430 using std::cos;
431 using std::sin;
432 using std::exp;
433 Real sinv = sin(v);
434 Real cosv = cos(v);
435
436 Real cotv = cosv/sinv;
437 Real cscv = 1/sinv;
438 Real t = (1-v*cotv)*(1-v*cotv) + v*v;
439 Real x = v*cscv*exp(-v*cotv);
440 Complex den = z + x;
441 Complex num = t*(z/pi<Real>());
442 Complex res = num/den;
443 return res;
444 };
445
446 //N[ProductLog[2+3*I], 150]
447 boost::math::quadrature::gauss_kronrod<Real, 61> integrator;
448 Complex Q = integrator.integrate(lw, (Real) 0, pi<Real>());
449 BOOST_CHECK_CLOSE_FRACTION(Q.real(), boost::lexical_cast<Real>("1.09007653448579084630177782678166964987102108635357778056449870727913321296238687023915522935120701763447787503167111962008709116746523970476893277703"), tol);
450 BOOST_CHECK_CLOSE_FRACTION(Q.imag(), boost::lexical_cast<Real>("0.530139720774838801426860213574121741928705631382703178297940568794784362495390544411799468140433404536019992695815009036975117285537382995180319280835"), tol);
451}
452
b32b8144
FG
453BOOST_AUTO_TEST_CASE(gauss_quadrature_test)
454{
455#ifdef TEST1
456 std::cout << "Testing 15 point approximation:\n";
457 test_linear<double, 15>();
458 test_quadratic<double, 15>();
459 test_ca<double, 15>();
460 test_three_quadrature_schemes_examples<double, 15>();
461 test_integration_over_real_line<double, 15>();
462 test_right_limit_infinite<double, 15>();
463 test_left_limit_infinite<double, 15>();
92f5a8d4 464
b32b8144
FG
465 // test one case where we do not have pre-computed constants:
466 std::cout << "Testing 17 point approximation:\n";
467 test_linear<double, 17>();
468 test_quadratic<double, 17>();
469 test_ca<double, 17>();
470 test_three_quadrature_schemes_examples<double, 17>();
471 test_integration_over_real_line<double, 17>();
472 test_right_limit_infinite<double, 17>();
473 test_left_limit_infinite<double, 17>();
92f5a8d4
TL
474 test_complex_lambert_w<std::complex<double>>();
475 test_complex_lambert_w<std::complex<long double>>();
b32b8144
FG
476#endif
477#ifdef TEST1A
478 std::cout << "Testing 21 point approximation:\n";
479 test_linear<cpp_bin_float_quad, 21>();
480 test_quadratic<cpp_bin_float_quad, 21>();
481 test_ca<cpp_bin_float_quad, 21>();
482 test_three_quadrature_schemes_examples<cpp_bin_float_quad, 21>();
483 test_integration_over_real_line<cpp_bin_float_quad, 21>();
484 test_right_limit_infinite<cpp_bin_float_quad, 21>();
485 test_left_limit_infinite<cpp_bin_float_quad, 21>();
486
487 std::cout << "Testing 31 point approximation:\n";
488 test_linear<cpp_bin_float_quad, 31>();
489 test_quadratic<cpp_bin_float_quad, 31>();
490 test_ca<cpp_bin_float_quad, 31>();
491 test_three_quadrature_schemes_examples<cpp_bin_float_quad, 31>();
492 test_integration_over_real_line<cpp_bin_float_quad, 31>();
493 test_right_limit_infinite<cpp_bin_float_quad, 31>();
494 test_left_limit_infinite<cpp_bin_float_quad, 31>();
495#endif
496#ifdef TEST2
497 std::cout << "Testing 41 point approximation:\n";
498 test_linear<cpp_bin_float_quad, 41>();
499 test_quadratic<cpp_bin_float_quad, 41>();
500 test_ca<cpp_bin_float_quad, 41>();
501 test_three_quadrature_schemes_examples<cpp_bin_float_quad, 41>();
502 test_integration_over_real_line<cpp_bin_float_quad, 41>();
503 test_right_limit_infinite<cpp_bin_float_quad, 41>();
504 test_left_limit_infinite<cpp_bin_float_quad, 41>();
505
506 std::cout << "Testing 51 point approximation:\n";
507 test_linear<cpp_bin_float_quad, 51>();
508 test_quadratic<cpp_bin_float_quad, 51>();
509 test_ca<cpp_bin_float_quad, 51>();
510 test_three_quadrature_schemes_examples<cpp_bin_float_quad, 51>();
511 test_integration_over_real_line<cpp_bin_float_quad, 51>();
512 test_right_limit_infinite<cpp_bin_float_quad, 51>();
513 test_left_limit_infinite<cpp_bin_float_quad, 51>();
514#endif
515#ifdef TEST3
516 // Need at least one set of tests with expression templates turned on:
517 std::cout << "Testing 61 point approximation:\n";
518 test_linear<cpp_dec_float_50, 61>();
519 test_quadratic<cpp_dec_float_50, 61>();
520 test_ca<cpp_dec_float_50, 61>();
521 test_three_quadrature_schemes_examples<cpp_dec_float_50, 61>();
522 test_integration_over_real_line<cpp_dec_float_50, 61>();
523 test_right_limit_infinite<cpp_dec_float_50, 61>();
524 test_left_limit_infinite<cpp_dec_float_50, 61>();
92f5a8d4
TL
525#ifdef BOOST_HAS_FLOAT128
526 test_complex_lambert_w<boost::multiprecision::complex128>();
527#endif
b32b8144
FG
528#endif
529}
530
531#else
532
533int main() { return 0; }
534
535#endif