]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/math/test/test_lambert_w_integrals_double.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / math / test / test_lambert_w_integrals_double.cpp
CommitLineData
92f5a8d4
TL
1// Copyright Paul A. Bristow 2016, 2017, 2018.
2// Copyright John Maddock 2016.
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// test_lambert_w_integrals.cpp
10//! \brief quadrature tests that cover the whole range of the Lambert W0 function.
11
12#include <boost/config.hpp> // for BOOST_MSVC definition etc.
13#include <boost/version.hpp> // for BOOST_MSVC versions.
14
15// Boost macros
16#define BOOST_TEST_MAIN
17#define BOOST_LIB_DIAGNOSTIC "on" // Report library file details.
18#include <boost/test/included/unit_test.hpp> // Boost.Test
19// #include <boost/test/unit_test.hpp> // Boost.Test
20#include <boost/test/tools/floating_point_comparison.hpp>
21
22#include <boost/array.hpp>
92f5a8d4 23#include <boost/type_traits/is_constructible.hpp>
f67539c2 24#include <boost/math/special_functions/fpclassify.hpp> // isnan, isfinite.
92f5a8d4
TL
25#include <boost/math/special_functions/next.hpp> // float_next, float_prior
26using boost::math::float_next;
27using boost::math::float_prior;
28#include <boost/math/special_functions/ulp.hpp> // ulp
29
30#include <boost/math/tools/test_value.hpp> // for create_test_value and macro BOOST_MATH_TEST_VALUE.
31#include <boost/math/policies/policy.hpp>
32using boost::math::policies::digits2;
33using boost::math::policies::digits10;
34#include <boost/math/special_functions/lambert_w.hpp> // For Lambert W lambert_w function.
35using boost::math::lambert_wm1;
36using boost::math::lambert_w0;
37
38#include <limits>
39#include <cmath>
40#include <typeinfo>
41#include <iostream>
42#include <type_traits>
43#include <exception>
44
45std::string show_versions(void);
46
47// Added code and test for Integral of the Lambert W function: by Nick Thompson.
48// https://en.wikipedia.org/wiki/Lambert_W_function#Definite_integrals
49
50#include <boost/math/constants/constants.hpp> // for integral tests.
51#include <boost/math/quadrature/tanh_sinh.hpp> // for integral tests.
52#include <boost/math/quadrature/exp_sinh.hpp> // for integral tests.
53
54 using boost::math::policies::policy;
55 using boost::math::policies::make_policy;
56
57// using statements needed for changing error handling policy.
58using boost::math::policies::evaluation_error;
59using boost::math::policies::domain_error;
60using boost::math::policies::overflow_error;
61using boost::math::policies::ignore_error;
62using boost::math::policies::throw_on_error;
63
64typedef policy<
65 domain_error<throw_on_error>,
66 overflow_error<ignore_error>
67> no_throw_policy;
68
69// Assumes that function has a throw policy, for example:
70// NOT lambert_w0<T>(1 / (x * x), no_throw_policy());
71// Error in function boost::math::quadrature::exp_sinh<double>::integrate:
72// The exp_sinh quadrature evaluated your function at a singular point and resulted in inf.
73// Please ensure your function evaluates to a finite number of its entire domain.
74template <typename T>
75T debug_integration_proc(T x)
76{
77 T result; // warning C4701: potentially uninitialized local variable 'result' used
78 // T result = 0 ; // But result may not be assigned below?
79 try
80 {
81 // Assign function call to result in here...
82 if (x <= sqrt(boost::math::tools::min_value<T>()) )
83 {
84 result = 0;
85 }
86 else
87 {
88 result = lambert_w0<T>(1 / (x * x));
89 }
90 // result = lambert_w0<T>(1 / (x * x), no_throw_policy()); // Bad idea, less helpful diagnostic message is:
91 // Error in function boost::math::quadrature::exp_sinh<double>::integrate:
92 // The exp_sinh quadrature evaluated your function at a singular point and resulted in inf.
93 // Please ensure your function evaluates to a finite number of its entire domain.
94
95 } // try
96 catch (const std::exception& e)
97 {
98 std::cout << "Exception " << e.what() << std::endl;
99 // set breakpoint here:
100 std::cout << "Unexpected exception thrown in integration code at abscissa (x): " << x << "." << std::endl;
101 if (!std::isfinite(result))
102 {
103 // set breakpoint here:
104 std::cout << "Unexpected non-finite result in integration code at abscissa (x): " << x << "." << std::endl;
105 }
106 if (std::isnan(result))
107 {
108 // set breakpoint here:
109 std::cout << "Unexpected non-finite result in integration code at abscissa (x): " << x << "." << std::endl;
110 }
111 } // catch
112 return result;
113} // T debug_integration_proc(T x)
114
115template<class Real>
116void test_integrals()
117{
118 // Integral of the Lambert W function:
119 // https://en.wikipedia.org/wiki/Lambert_W_function
120 using boost::math::quadrature::tanh_sinh;
121 using boost::math::quadrature::exp_sinh;
122 // file:///I:/modular-boost/libs/math/doc/html/math_toolkit/quadrature/double_exponential/de_tanh_sinh.html
123 using std::sqrt;
124
125 std::cout << "Integration of type " << typeid(Real).name() << std::endl;
126
127 Real tol = std::numeric_limits<Real>::epsilon();
128 { // // Integrate for function lambert_W0(z);
129 tanh_sinh<Real> ts;
130 Real a = 0;
131 Real b = boost::math::constants::e<Real>();
132 auto f = [](Real z)->Real
133 {
134 return lambert_w0<Real>(z);
135 };
136 Real z = ts.integrate(f, a, b); // OK without any decltype(f)
137 BOOST_CHECK_CLOSE_FRACTION(z, boost::math::constants::e<Real>() - 1, tol);
138 }
139 {
140 // Integrate for function lambert_W0(z/(z sqrt(z)).
141 exp_sinh<Real> es;
142 auto f = [](Real z)->Real
143 {
144 return lambert_w0<Real>(z)/(z * sqrt(z));
145 };
146 Real z = es.integrate(f); // OK
147 BOOST_CHECK_CLOSE_FRACTION(z, 2 * boost::math::constants::root_two_pi<Real>(), tol);
148 }
149 {
150 // Integrate for function lambert_W0(1/z^2).
151 exp_sinh<Real> es;
152 //const Real sqrt_min = sqrt(boost::math::tools::min_value<Real>()); // 1.08420217e-19 fo 32-bit float.
153 // error C3493: 'sqrt_min' cannot be implicitly captured because no default capture mode has been specified
154 auto f = [](Real z)->Real
155 {
156 if (z <= sqrt(boost::math::tools::min_value<Real>()) )
157 { // Too small would underflow z * z and divide by zero to overflow 1/z^2 for lambert_w0 z parameter.
158 return static_cast<Real>(0);
159 }
160 else
161 {
162 return lambert_w0<Real>(1 / (z * z)); // warning C4756: overflow in constant arithmetic, even though cannot happen.
163 }
164 };
165 Real z = es.integrate(f);
166 BOOST_CHECK_CLOSE_FRACTION(z, boost::math::constants::root_two_pi<Real>(), tol);
167 }
168} // template<class Real> void test_integrals()
169
170
171BOOST_AUTO_TEST_CASE( integrals )
172{
173 std::cout << "Macro BOOST_MATH_LAMBERT_W0_INTEGRALS is defined." << std::endl;
174 BOOST_TEST_MESSAGE("\nTest Lambert W0 integrals.");
175 try
176 {
177 // using statements needed to change precision policy.
178 using boost::math::policies::policy;
179 using boost::math::policies::make_policy;
180 using boost::math::policies::precision;
181 using boost::math::policies::digits2;
182 using boost::math::policies::digits10;
183
184 // using statements needed for changing error handling policy.
185 using boost::math::policies::evaluation_error;
186 using boost::math::policies::domain_error;
187 using boost::math::policies::overflow_error;
188 using boost::math::policies::ignore_error;
189 using boost::math::policies::throw_on_error;
190
191 /*
192 typedef policy<
193 domain_error<throw_on_error>,
194 overflow_error<ignore_error>
195 > no_throw_policy;
196
197 // Experiment with better diagnostics.
198 typedef float Real;
199
200 Real inf = std::numeric_limits<Real>::infinity();
201 Real max = (std::numeric_limits<Real>::max)();
202 std::cout.precision(std::numeric_limits<Real>::max_digits10);
203 //std::cout << "lambert_w0(inf) = " << lambert_w0(inf) << std::endl; // lambert_w0(inf) = 1.79769e+308
204 std::cout << "lambert_w0(inf, throw_policy()) = " << lambert_w0(inf, no_throw_policy()) << std::endl; // inf
205 std::cout << "lambert_w0(max) = " << lambert_w0(max) << std::endl; // lambert_w0(max) = 703.227
206 //std::cout << lambert_w0(inf) << std::endl; // inf - will throw.
207 std::cout << "lambert_w0(0) = " << lambert_w0(0.) << std::endl; // 0
208 std::cout << "lambert_w0(std::numeric_limits<Real>::denorm_min()) = " << lambert_w0(std::numeric_limits<Real>::denorm_min()) << std::endl; // 4.94066e-324
209 std::cout << "lambert_w0(std::numeric_limits<Real>::min()) = " << lambert_w0((std::numeric_limits<Real>::min)()) << std::endl; // 2.22507e-308
210
211 // Approximate the largest lambert_w you can get for type T?
212 float max_w_f = boost::math::lambert_w_detail::lambert_w0_approx((std::numeric_limits<float>::max)()); // Corless equation 4.19, page 349, and Chapeau-Blondeau equation 20, page 2162.
213 std::cout << "w max_f " << max_w_f << std::endl; // 84.2879
214 Real max_w = boost::math::lambert_w_detail::lambert_w0_approx((std::numeric_limits<Real>::max)()); // Corless equation 4.19, page 349, and Chapeau-Blondeau equation 20, page 2162.
215 std::cout << "w max " << max_w << std::endl; // 703.227
216
217 std::cout << "lambert_w0(7.2416706213544837e-163) = " << lambert_w0(7.2416706213544837e-163) << std::endl; //
218 std::cout << "test integral 1/z^2" << std::endl;
219 std::cout << "ULP = " << boost::math::ulp(1., policy<digits2<> >()) << std::endl; // ULP = 2.2204460492503131e-16
220 std::cout << "ULP = " << boost::math::ulp(1e-10, policy<digits2<> >()) << std::endl; // ULP = 2.2204460492503131e-16
221 std::cout << "ULP = " << boost::math::ulp(1., policy<digits2<11> >()) << std::endl; // ULP = 2.2204460492503131e-16
222 std::cout << "epsilon = " << std::numeric_limits<Real>::epsilon() << std::endl; //
223 std::cout << "sqrt(max) = " << sqrt(boost::math::tools::max_value<float>() ) << std::endl; // sqrt(max) = 1.8446742974197924e+19
224 std::cout << "sqrt(min) = " << sqrt(boost::math::tools::min_value<float>() ) << std::endl; // sqrt(min) = 1.0842021724855044e-19
225
226
227
228// Demo debug version.
229Real tol = std::numeric_limits<Real>::epsilon();
230Real x;
231{
232 using boost::math::quadrature::exp_sinh;
233 exp_sinh<Real> es;
234 // Function to be integrated, lambert_w0(1/z^2).
235
236 //auto f = [](Real z)->Real
237 //{ // Naive - no protection against underflow and subsequent divide by zero.
238 // return lambert_w0<Real>(1 / (z * z));
239 //};
240 // Diagnostic is:
241 // Error in function boost::math::lambert_w0<Real>: Expected a finite value but got inf
242
243 auto f = [](Real z)->Real
244 { // Debug with diagnostics for underflow and subsequent divide by zero and other bad things.
245 return debug_integration_proc(z);
246 };
247 // Exception Error in function boost::math::lambert_w0<double>: Expected a finite value but got inf.
248
249 // Unexpected exception thrown in integration code at abscissa: 7.2416706213544837e-163.
250 // Unexpected exception thrown in integration code at abscissa (x): 3.478765835953569e-23.
251 x = es.integrate(f);
252 std::cout << "es.integrate(f) = " << x << std::endl;
253 BOOST_CHECK_CLOSE_FRACTION(x, boost::math::constants::root_two_pi<Real>(), tol);
254 // root_two_pi<double = 2.506628274631000502
255 }
256 */
257
258 test_integrals<double>();
259 }
260 catch (std::exception& ex)
261 {
262 std::cout << ex.what() << std::endl;
263 }
264}
265