ooura_fourier_integrals_multiprecision_example.cpp 5.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #if !defined(__cpp_structured_bindings) || (__cpp_structured_bindings < 201606L)
  8. # error "This example requires a C++17 compiler that supports 'structured bindings'. Try /std:c++17 or -std=c++17 or later."
  9. #endif
  10. //#define BOOST_MATH_INSTRUMENT_OOURA // or -DBOOST_MATH_INSTRUMENT_OOURA etc for diagnostic output.
  11. #include <boost/math/quadrature/ooura_fourier_integrals.hpp>
  12. #include <boost/multiprecision/cpp_bin_float.hpp> // for cpp_bin_float_quad, cpp_bin_float_50...
  13. #include <boost/math/constants/constants.hpp> // For pi (including for multiprecision types, if used.)
  14. #include <cmath>
  15. #include <iostream>
  16. #include <limits>
  17. #include <iostream>
  18. #include <exception>
  19. int main()
  20. {
  21. try
  22. {
  23. typedef boost::multiprecision::cpp_bin_float_quad Real;
  24. std::cout.precision(std::numeric_limits<Real>::max_digits10); // Show all potentially significant digits.
  25. using boost::math::quadrature::ooura_fourier_cos;
  26. using boost::math::constants::half_pi;
  27. using boost::math::constants::e;
  28. //[ooura_fourier_integrals_multiprecision_example_1
  29. // Use the default parameters for tolerance root_epsilon and eight levels for a type of 8 bytes.
  30. //auto integrator = ooura_fourier_cos<Real>();
  31. // Decide on a (tight) tolerance.
  32. const Real tol = 2 * std::numeric_limits<Real>::epsilon();
  33. auto integrator = ooura_fourier_cos<Real>(tol, 8); // Loops or gets worse for more than 8.
  34. auto f = [](Real x)
  35. { // More complex example function.
  36. return 1 / (x * x + 1);
  37. };
  38. double omega = 1;
  39. auto [result, relative_error] = integrator.integrate(f, omega);
  40. //] [/ooura_fourier_integrals_multiprecision_example_1]
  41. //[ooura_fourier_integrals_multiprecision_example_2
  42. std::cout << "Integral = " << result << ", relative error estimate " << relative_error << std::endl;
  43. const Real expected = half_pi<Real>() / e<Real>(); // Expect integral = 1/(2e)
  44. std::cout << "pi/(2e) = " << expected << ", difference " << result - expected << std::endl;
  45. //] [/ooura_fourier_integrals_multiprecision_example_2]
  46. }
  47. catch (std::exception const & ex)
  48. {
  49. // Lacking try&catch blocks, the program will abort after any throw, whereas the
  50. // message below from the thrown exception will give some helpful clues as to the cause of the problem.
  51. std::cout << "\n""Message from thrown exception was:\n " << ex.what() << std::endl;
  52. }
  53. } // int main()
  54. /*
  55. //[ooura_fourier_integrals_example_multiprecision_output_1
  56. ``
  57. Integral = 0.5778636748954608589550465916563501587, relative error estimate 4.609814684522163895264277312610830278e-17
  58. pi/(2e) = 0.5778636748954608659545328919193707407, difference -6.999486300263020581921171645255733758e-18
  59. ``
  60. //] [/ooura_fourier_integrals_example_multiprecision_output_1]
  61. //[ooura_fourier_integrals_example_multiprecision_diagnostic_output_1
  62. ``
  63. ooura_fourier_cos with relative error goal 3.851859888774471706111955885169854637e-34 & 15 levels.
  64. epsilon for type = 1.925929944387235853055977942584927319e-34
  65. h = 1.000000000000000000000000000000000, I_h = 0.588268622591776615359568690603776 = 0.5882686225917766153595686906037760, absolute error estimate = nan
  66. h = 0.500000000000000000000000000000000, I_h = 0.577871642184837461311756940493259 = 0.5778716421848374613117569404932595, absolute error estimate = 1.039698040693915404781175011051656e-02
  67. h = 0.250000000000000000000000000000000, I_h = 0.577863671186882539559996800783122 = 0.5778636711868825395599968007831220, absolute error estimate = 7.970997954921751760139710137450075e-06
  68. h = 0.125000000000000000000000000000000, I_h = 0.577863674895460885593491133506723 = 0.5778636748954608855934911335067232, absolute error estimate = 3.708578346033494332723601147051768e-09
  69. h = 0.062500000000000000000000000000000, I_h = 0.577863674895460858955046591656350 = 0.5778636748954608589550465916563502, absolute error estimate = 2.663844454185037302771663314961535e-17
  70. h = 0.031250000000000000000000000000000, I_h = 0.577863674895460858955046591656348 = 0.5778636748954608589550465916563484, absolute error estimate = 1.733336949948512267750380148326435e-33
  71. h = 0.015625000000000000000000000000000, I_h = 0.577863674895460858955046591656348 = 0.5778636748954608589550465916563479, absolute error estimate = 4.814824860968089632639944856462318e-34
  72. h = 0.007812500000000000000000000000000, I_h = 0.577863674895460858955046591656347 = 0.5778636748954608589550465916563473, absolute error estimate = 6.740754805355325485695922799047246e-34
  73. h = 0.003906250000000000000000000000000, I_h = 0.577863674895460858955046591656347 = 0.5778636748954608589550465916563475, absolute error estimate = 1.925929944387235853055977942584927e-34
  74. Integral = 5.778636748954608589550465916563475e-01, relative error estimate 3.332844800697411177051445985473052e-34
  75. pi/(2e) = 5.778636748954608589550465916563481e-01, difference -6.740754805355325485695922799047246e-34
  76. ``
  77. //] [/ooura_fourier_integrals_example_multiprecision_diagnostic_output_1]
  78. */