mirror of
https://github.com/boostorg/math.git
synced 2026-01-19 04:22:09 +00:00
[CI SKIP] added 3 examples of using fourier integrals, providing Quickbook code snippets for docs.
This commit is contained in:
@@ -6,11 +6,13 @@
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// This example requires C++17.
|
||||
#ifdef BOOST_NO_CXX11_LAMBDAS
|
||||
# error "This example requires a C++17 compiler that supports 'structured bindings'. Try /std:c++17 or -std=c++17 or later."
|
||||
#endif
|
||||
|
||||
//#define BOOST_MATH_INSTRUMENT_OOURA // or -DBOOST_MATH_INSTRUMENT_OOURA etc for diagnostic output.
|
||||
|
||||
#include <boost/math/quadrature/ooura_fourier_integrals.hpp> //
|
||||
#include <boost/math/quadrature/ooura_fourier_integrals.hpp> // For ooura_fourier_cos
|
||||
|
||||
#include <boost/math/constants/constants.hpp> // For pi (including for multiprecision types, if used.)
|
||||
|
||||
@@ -21,15 +23,14 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
std::cout.precision(std::numeric_limits<double>::max_digits10); // Show all potentially significant digits.
|
||||
|
||||
using boost::math::quadrature::ooura_fourier_cos;
|
||||
using boost::math::constants::half_pi;
|
||||
using boost::math::constants::e;
|
||||
|
||||
// constexpr double double_tol = 10 * std::numeric_limits<double>::epsilon(); // Tolerance.
|
||||
|
||||
//[ooura_fourier_integrals_cosine_example_1
|
||||
auto integrator = ooura_fourier_cos<double>();
|
||||
// Use the default tolerance root_epsilon and eight levels for type double.
|
||||
@@ -49,8 +50,15 @@ int main()
|
||||
//[ooura_fourier_integrals_cosine_example_2
|
||||
|
||||
constexpr double expected = half_pi<double>() / e<double>();
|
||||
std::cout << "pi/(2e) = " << expected << ", difference " << result - expected << std::endl;
|
||||
std::cout << "pi/(2e) = " << expected << ", difference " << result - expected << std::endl;
|
||||
//] [/ooura_fourier_integrals_cosine_example_2]
|
||||
}
|
||||
catch (std::exception ex)
|
||||
{
|
||||
// Lacking try&catch blocks, the program will abort after any throw, whereas the
|
||||
// message below from the thrown exception will give some helpful clues as to the cause of the problem.
|
||||
std::cout << "\n""Message from thrown exception was:\n " << ex.what() << std::endl;
|
||||
}
|
||||
|
||||
} // int main()
|
||||
|
||||
@@ -59,19 +67,21 @@ int main()
|
||||
//[ooura_fourier_integrals_example_cosine_output_1
|
||||
|
||||
Integral = 0.57786367489546109, relative error estimate 6.4177395404415149e-09
|
||||
pi/2 = 0.57786367489546087, difference 2.2204460492503131e-16
|
||||
pi/(2e) = 0.57786367489546087, difference 2.2204460492503131e-16
|
||||
|
||||
//] [/ooura_fourier_integrals_example_cosine_output_1]
|
||||
|
||||
|
||||
//[ooura_fourier_integrals_example_cosine_diagnostic_output_1
|
||||
|
||||
ooura_fourier_cos with relative error goal 1.4901161193847656e-08 & 8 levels.
|
||||
epsilon for type = 2.2204460492503131e-16
|
||||
h = 1.000000000000000, I_h = 0.588268622591776 = 0x1.2d318b7e96dbe00p-1, absolute error estimate = nan
|
||||
h = 0.500000000000000, I_h = 0.577871642184837 = 0x1.27decab8f07b200p-1, absolute error estimate = 1.039698040693926e-02
|
||||
h = 0.250000000000000, I_h = 0.577863671186883 = 0x1.27ddbf42969be00p-1, absolute error estimate = 7.970997954576120e-06
|
||||
h = 0.125000000000000, I_h = 0.577863674895461 = 0x1.27ddbf6271dc000p-1, absolute error estimate = 3.708578555361441e-09
|
||||
Integral = 5.778636748954611e-01, relative error estimate 6.417739540441515e-09
|
||||
pi/2 = 5.778636748954609e-01, difference 2.220446049250313e-16
|
||||
pi/(2e) = 5.778636748954609e-01, difference 2.220446049250313e-16
|
||||
|
||||
//] [/ooura_fourier_integrals_example_cosine_diagnostic_output_1]
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// This example requires C++11.
|
||||
#ifdef BOOST_NO_CXX11_LAMBDAS
|
||||
# error "This example requires a C++11 compiler that supports lambdas. Try C++11 or later."
|
||||
#endif
|
||||
|
||||
#define BOOST_MATH_INSTRUMENT_OOURA // or -DBOOST_MATH_INSTRUMENT_OOURA etc for diagnostics.
|
||||
|
||||
@@ -21,14 +23,13 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
std::cout.precision(std::numeric_limits<double>::max_digits10); // Show all potentially significant digits.
|
||||
|
||||
using boost::math::quadrature::ooura_fourier_sin;
|
||||
using boost::math::constants::half_pi;
|
||||
|
||||
// constexpr double double_tol = 10 * std::numeric_limits<double>::epsilon(); // Tolerance.
|
||||
|
||||
//[ooura_fourier_integrals_example_1
|
||||
ooura_fourier_sin<double>integrator = ooura_fourier_sin<double>();
|
||||
// Use the default tolerance root_epsilon and eight levels for type double.
|
||||
@@ -47,9 +48,15 @@ int main()
|
||||
//[ooura_fourier_integrals_example_2
|
||||
|
||||
constexpr double expected = half_pi<double>();
|
||||
std::cout << "pi/2 = " << expected << ", difference " << result.first - expected << std::endl;
|
||||
std::cout << "pi/2 = " << expected << ", difference " << result.first - expected << std::endl;
|
||||
//] [/ooura_fourier_integrals_example_2]
|
||||
|
||||
}
|
||||
catch (std::exception ex)
|
||||
{
|
||||
// Lacking try&catch blocks, the program will abort after any throw, whereas the
|
||||
// message below from the thrown exception will give some helpful clues as to the cause of the problem.
|
||||
std::cout << "\n""Message from thrown exception was:\n " << ex.what() << std::endl;
|
||||
}
|
||||
} // int main()
|
||||
|
||||
/*
|
||||
@@ -57,7 +64,7 @@ int main()
|
||||
//[ooura_fourier_integrals_example_output_1
|
||||
|
||||
integral = 1.5707963267948966, relative error estimate 1.2655356398390254e-11
|
||||
pi/2 = 1.5707963267948966, difference 0
|
||||
pi/2 = 1.5707963267948966, difference 0
|
||||
|
||||
//] [/ooura_fourier_integrals_example_output_1]
|
||||
|
||||
@@ -70,7 +77,7 @@ h = 0.500000000000000, I_h = 1.570793292491940 = 0x1.921f825c076f600p+0, absolut
|
||||
h = 0.250000000000000, I_h = 1.570796326814776 = 0x1.921fb54458acf00p+0, absolute error estimate = 3.034322835882008e-06
|
||||
h = 0.125000000000000, I_h = 1.570796326794897 = 0x1.921fb54442d1800p+0, absolute error estimate = 1.987898734512328e-11
|
||||
Integral = 1.570796326794897e+00, relative error estimate 1.265535639839025e-11
|
||||
pi/2 = 1.570796326794897e+00, difference 0.000000000000000e+00
|
||||
pi/2 = 1.570796326794897e+00, difference 0.000000000000000e+00
|
||||
|
||||
//] [/ooura_fourier_integrals_example_diagnostic_output_1]
|
||||
|
||||
|
||||
@@ -38,10 +38,9 @@ int main()
|
||||
|
||||
// Use the default parameters for tolerance root_epsilon and eight levels for the type.
|
||||
//auto integrator = ooura_fourier_cos<Real>();
|
||||
// Decide a (tight) tolerance.
|
||||
//const Real tol = 2 * std::numeric_limits<Real>::epsilon(); // Tolerance.
|
||||
const Real tol = 1 * std::numeric_limits<Real>::epsilon();
|
||||
auto integrator = ooura_fourier_cos<Real>(tol, 8); // Loops for 16
|
||||
// Decide on a (tight) tolerance.
|
||||
const Real tol = 2 * std::numeric_limits<Real>::epsilon();
|
||||
auto integrator = ooura_fourier_cos<Real>(tol, 8); // Loops or gets worse for more than 8.
|
||||
|
||||
auto f = [](Real x)
|
||||
{ // More complex example function.
|
||||
@@ -49,23 +48,21 @@ int main()
|
||||
};
|
||||
|
||||
double omega = 1;
|
||||
|
||||
auto [result, relative_error] = integrator.integrate(f, omega);
|
||||
std::cout << "Integral = " << result << ", relative error estimate " << relative_error << std::endl;
|
||||
|
||||
//] [/ooura_fourier_integrals_multiprecision_example_1]
|
||||
|
||||
//[ooura_fourier_integrals_multiprecision_example_2
|
||||
std::cout << "Integral = " << result << ", relative error estimate " << relative_error << std::endl;
|
||||
|
||||
const Real expected = half_pi<Real>() / e<Real>();
|
||||
const Real expected = half_pi<Real>() / e<Real>(); // Expect integral = 1/(2e)
|
||||
std::cout << "pi/(2e) = " << expected << ", difference " << result - expected << std::endl;
|
||||
//] [/ooura_fourier_integrals_multiprecision_example_2]
|
||||
}
|
||||
catch (std::exception ex)
|
||||
{
|
||||
// Lacking try& catch blocks, the program will abort, whereas the
|
||||
//message below from the thrown exception will give some helpful clues as to the cause of the problem.
|
||||
|
||||
// Lacking try&catch blocks, the program will abort after any throw, whereas the
|
||||
// message below from the thrown exception will give some helpful clues as to the cause of the problem.
|
||||
std::cout << "\n""Message from thrown exception was:\n " << ex.what() << std::endl;
|
||||
}
|
||||
|
||||
@@ -98,6 +95,10 @@ h = 0.003906250000000000000000000000000, I_h = 0.5778636748954608589550465916563
|
||||
Integral = 5.778636748954608589550465916563475e-01, relative error estimate 3.332844800697411177051445985473052e-34
|
||||
pi/(2e) = 5.778636748954608589550465916563481e-01, difference -6.740754805355325485695922799047246e-34
|
||||
|
||||
//] [/ooura_fourier_integrals_example_multiprecision_diagnostic_output_1]
|
||||
|
||||
|
||||
Example of it going wrong below
|
||||
|
||||
>ooura_fourier_cos with relative error goal 1.925929944387235853055977942584927319e-34 & 15 levels.
|
||||
1>epsilon for type = 1.925929944387235853055977942584927319e-34
|
||||
@@ -120,6 +121,6 @@ pi/(2e) = 5.778636748954608589550465916563481e-01, difference -6.74075480535532
|
||||
|
||||
|
||||
|
||||
//] [/ooura_fourier_integrals_example_multiprecision_diagnostic_output_1]
|
||||
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user