test_lambert_w_integrals_float.cpp 11 KB

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