test_lambert_w_integrals_quad.cpp 11 KB

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