ooura_fourier_integrals_cosine_example.cpp 3.1 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. //#define BOOST_MATH_INSTRUMENT_OOURA // or -DBOOST_MATH_INSTRUMENT_OOURA etc for diagnostic output.
  8. #include <boost/math/quadrature/ooura_fourier_integrals.hpp> // For ooura_fourier_cos
  9. #include <boost/math/constants/constants.hpp> // For pi (including for multiprecision types, if used.)
  10. #include <cmath>
  11. #include <iostream>
  12. #include <limits>
  13. #include <iostream>
  14. int main()
  15. {
  16. try
  17. {
  18. std::cout.precision(std::numeric_limits<double>::max_digits10); // Show all potentially significant digits.
  19. using boost::math::quadrature::ooura_fourier_cos;
  20. using boost::math::constants::half_pi;
  21. using boost::math::constants::e;
  22. //[ooura_fourier_integrals_cosine_example_1
  23. auto integrator = ooura_fourier_cos<double>();
  24. // Use the default tolerance root_epsilon and eight levels for type double.
  25. auto f = [](double x)
  26. { // More complex example function.
  27. return 1 / (x * x + 1);
  28. };
  29. double omega = 1;
  30. auto [result, relative_error] = integrator.integrate(f, omega);
  31. std::cout << "Integral = " << result << ", relative error estimate " << relative_error << std::endl;
  32. //] [/ooura_fourier_integrals_cosine_example_1]
  33. //[ooura_fourier_integrals_cosine_example_2
  34. constexpr double expected = half_pi<double>() / e<double>();
  35. std::cout << "pi/(2e) = " << expected << ", difference " << result - expected << std::endl;
  36. //] [/ooura_fourier_integrals_cosine_example_2]
  37. }
  38. catch (std::exception const & ex)
  39. {
  40. // Lacking try&catch blocks, the program will abort after any throw, whereas the
  41. // message below from the thrown exception will give some helpful clues as to the cause of the problem.
  42. std::cout << "\n""Message from thrown exception was:\n " << ex.what() << std::endl;
  43. }
  44. } // int main()
  45. /*
  46. //[ooura_fourier_integrals_example_cosine_output_1
  47. ``
  48. Integral = 0.57786367489546109, relative error estimate 6.4177395404415149e-09
  49. pi/(2e) = 0.57786367489546087, difference 2.2204460492503131e-16
  50. ``
  51. //] [/ooura_fourier_integrals_example_cosine_output_1]
  52. //[ooura_fourier_integrals_example_cosine_diagnostic_output_1
  53. ``
  54. ooura_fourier_cos with relative error goal 1.4901161193847656e-08 & 8 levels.
  55. epsilon for type = 2.2204460492503131e-16
  56. h = 1.000000000000000, I_h = 0.588268622591776 = 0x1.2d318b7e96dbe00p-1, absolute error estimate = nan
  57. h = 0.500000000000000, I_h = 0.577871642184837 = 0x1.27decab8f07b200p-1, absolute error estimate = 1.039698040693926e-02
  58. h = 0.250000000000000, I_h = 0.577863671186883 = 0x1.27ddbf42969be00p-1, absolute error estimate = 7.970997954576120e-06
  59. h = 0.125000000000000, I_h = 0.577863674895461 = 0x1.27ddbf6271dc000p-1, absolute error estimate = 3.708578555361441e-09
  60. Integral = 5.778636748954611e-01, relative error estimate 6.417739540441515e-09
  61. pi/(2e) = 5.778636748954609e-01, difference 2.220446049250313e-16
  62. ``
  63. //] [/ooura_fourier_integrals_example_cosine_diagnostic_output_1]
  64. */