ooura_fourier_integrals_example.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright Paul A. Bristow, 2019
  2. // Copyright Nick Thompson, 2019
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifdef BOOST_NO_CXX11_LAMBDAS
  8. # error "This example requires a C++11 compiler that supports lambdas. Try C++11 or later."
  9. #endif
  10. //#define BOOST_MATH_INSTRUMENT_OOURA // or -DBOOST_MATH_INSTRUMENT_OOURA etc for diagnostics.
  11. #include <boost/math/quadrature/ooura_fourier_integrals.hpp>
  12. #include <boost/math/constants/constants.hpp> // For pi (including for multiprecision types, if used.)
  13. #include <cmath>
  14. #include <iostream>
  15. #include <limits>
  16. #include <iostream>
  17. int main()
  18. {
  19. try
  20. {
  21. std::cout.precision(std::numeric_limits<double>::max_digits10); // Show all potentially significant digits.
  22. using boost::math::quadrature::ooura_fourier_sin;
  23. using boost::math::constants::half_pi;
  24. //[ooura_fourier_integrals_example_1
  25. ooura_fourier_sin<double>integrator = ooura_fourier_sin<double>();
  26. // Use the default tolerance root_epsilon and eight levels for type double.
  27. auto f = [](double x)
  28. { // Simple reciprocal function for sinc.
  29. return 1 / x;
  30. };
  31. double omega = 1;
  32. std::pair<double, double> result = integrator.integrate(f, omega);
  33. std::cout << "Integral = " << result.first << ", relative error estimate " << result.second << std::endl;
  34. //] [/ooura_fourier_integrals_example_1]
  35. //[ooura_fourier_integrals_example_2
  36. constexpr double expected = half_pi<double>();
  37. std::cout << "pi/2 = " << expected << ", difference " << result.first - expected << std::endl;
  38. //] [/ooura_fourier_integrals_example_2]
  39. }
  40. catch (std::exception const & ex)
  41. {
  42. // Lacking try&catch blocks, the program will abort after any throw, whereas the
  43. // message below from the thrown exception will give some helpful clues as to the cause of the problem.
  44. std::cout << "\n""Message from thrown exception was:\n " << ex.what() << std::endl;
  45. }
  46. } // int main()
  47. /*
  48. //[ooura_fourier_integrals_example_output_1
  49. integral = 1.5707963267948966, relative error estimate 1.2655356398390254e-11
  50. pi/2 = 1.5707963267948966, difference 0
  51. //] [/ooura_fourier_integrals_example_output_1]
  52. //[ooura_fourier_integrals_example_diagnostic_output_1
  53. ooura_fourier_sin with relative error goal 1.4901161193847656e-08 & 8 levels.
  54. h = 1.000000000000000, I_h = 1.571890732004545 = 0x1.92676e56d853500p+0, absolute error estimate = nan
  55. h = 0.500000000000000, I_h = 1.570793292491940 = 0x1.921f825c076f600p+0, absolute error estimate = 1.097439512605325e-03
  56. h = 0.250000000000000, I_h = 1.570796326814776 = 0x1.921fb54458acf00p+0, absolute error estimate = 3.034322835882008e-06
  57. h = 0.125000000000000, I_h = 1.570796326794897 = 0x1.921fb54442d1800p+0, absolute error estimate = 1.987898734512328e-11
  58. Integral = 1.570796326794897e+00, relative error estimate 1.265535639839025e-11
  59. pi/2 = 1.570796326794897e+00, difference 0.000000000000000e+00
  60. //] [/ooura_fourier_integrals_example_diagnostic_output_1]
  61. */