]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/ooura_fourier_integrals_cosine_example.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / math / example / ooura_fourier_integrals_cosine_example.cpp
1 // Copyright Paul A. Bristow, 2019
2 // Copyright Nick Thompson, 2019
3
4 // Use, modification and distribution are subject to the
5 // Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt
7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9 //#define BOOST_MATH_INSTRUMENT_OOURA // or -DBOOST_MATH_INSTRUMENT_OOURA etc for diagnostic output.
10
11 #include <boost/math/quadrature/ooura_fourier_integrals.hpp> // For ooura_fourier_cos
12 #include <boost/math/constants/constants.hpp> // For pi (including for multiprecision types, if used.)
13
14 #include <cmath>
15 #include <iostream>
16 #include <limits>
17 #include <iostream>
18
19 int main()
20 {
21 try
22 {
23 std::cout.precision(std::numeric_limits<double>::max_digits10); // Show all potentially significant digits.
24
25 using boost::math::quadrature::ooura_fourier_cos;
26 using boost::math::constants::half_pi;
27 using boost::math::constants::e;
28
29 //[ooura_fourier_integrals_cosine_example_1
30 auto integrator = ooura_fourier_cos<double>();
31 // Use the default tolerance root_epsilon and eight levels for type double.
32
33 auto f = [](double x)
34 { // More complex example function.
35 return 1 / (x * x + 1);
36 };
37
38 double omega = 1;
39
40 auto [result, relative_error] = integrator.integrate(f, omega);
41 std::cout << "Integral = " << result << ", relative error estimate " << relative_error << std::endl;
42
43 //] [/ooura_fourier_integrals_cosine_example_1]
44
45 //[ooura_fourier_integrals_cosine_example_2
46
47 constexpr double expected = half_pi<double>() / e<double>();
48 std::cout << "pi/(2e) = " << expected << ", difference " << result - expected << std::endl;
49 //] [/ooura_fourier_integrals_cosine_example_2]
50 }
51 catch (std::exception const & ex)
52 {
53 // Lacking try&catch blocks, the program will abort after any throw, whereas the
54 // message below from the thrown exception will give some helpful clues as to the cause of the problem.
55 std::cout << "\n""Message from thrown exception was:\n " << ex.what() << std::endl;
56 }
57
58 } // int main()
59
60 /*
61
62 //[ooura_fourier_integrals_example_cosine_output_1
63 ``
64 Integral = 0.57786367489546109, relative error estimate 6.4177395404415149e-09
65 pi/(2e) = 0.57786367489546087, difference 2.2204460492503131e-16
66 ``
67 //] [/ooura_fourier_integrals_example_cosine_output_1]
68
69
70 //[ooura_fourier_integrals_example_cosine_diagnostic_output_1
71 ``
72 ooura_fourier_cos with relative error goal 1.4901161193847656e-08 & 8 levels.
73 epsilon for type = 2.2204460492503131e-16
74 h = 1.000000000000000, I_h = 0.588268622591776 = 0x1.2d318b7e96dbe00p-1, absolute error estimate = nan
75 h = 0.500000000000000, I_h = 0.577871642184837 = 0x1.27decab8f07b200p-1, absolute error estimate = 1.039698040693926e-02
76 h = 0.250000000000000, I_h = 0.577863671186883 = 0x1.27ddbf42969be00p-1, absolute error estimate = 7.970997954576120e-06
77 h = 0.125000000000000, I_h = 0.577863674895461 = 0x1.27ddbf6271dc000p-1, absolute error estimate = 3.708578555361441e-09
78 Integral = 5.778636748954611e-01, relative error estimate 6.417739540441515e-09
79 pi/(2e) = 5.778636748954609e-01, difference 2.220446049250313e-16
80 ``
81 //] [/ooura_fourier_integrals_example_cosine_diagnostic_output_1]
82
83 */