lambert_w.hpp 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  1. // Copyright John Maddock 2017.
  2. // Copyright Paul A. Bristow 2016, 2017, 2018.
  3. // Copyright Nicholas Thompson 2018
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or
  6. // copy at http ://www.boost.org/LICENSE_1_0.txt).
  7. #ifndef BOOST_MATH_SF_LAMBERT_W_HPP
  8. #define BOOST_MATH_SF_LAMBERT_W_HPP
  9. #ifdef _MSC_VER
  10. #pragma warning(disable : 4127)
  11. #endif
  12. /*
  13. Implementation of an algorithm for the Lambert W0 and W-1 real-only functions.
  14. This code is based in part on the algorithm by
  15. Toshio Fukushima,
  16. "Precise and fast computation of Lambert W-functions without transcendental function evaluations",
  17. J.Comp.Appl.Math. 244 (2013) 77-89,
  18. and on a C/C++ version by Darko Veberic, darko.veberic@ijs.si
  19. based on the Fukushima algorithm and Toshio Fukushima's FORTRAN version of his algorithm.
  20. First derivative of Lambert_w is derived from
  21. Princeton Companion to Applied Mathematics, 'The Lambert-W function', Section 1.3: Series and Generating Functions.
  22. */
  23. /*
  24. TODO revise this list of macros.
  25. Some macros that will show some (or much) diagnostic values if #defined.
  26. //[boost_math_instrument_lambert_w_macros
  27. // #define-able macros
  28. BOOST_MATH_INSTRUMENT_LAMBERT_W_HALLEY // Halley refinement diagnostics.
  29. BOOST_MATH_INSTRUMENT_LAMBERT_W_PRECISION // Precision.
  30. BOOST_MATH_INSTRUMENT_LAMBERT_WM1 // W1 branch diagnostics.
  31. BOOST_MATH_INSTRUMENT_LAMBERT_WM1_HALLEY // Halley refinement diagnostics only for W-1 branch.
  32. BOOST_MATH_INSTRUMENT_LAMBERT_WM1_TINY // K > 64, z > -1.0264389699511303e-26
  33. BOOST_MATH_INSTRUMENT_LAMBERT_WM1_LOOKUP // Show results from W-1 lookup table.
  34. BOOST_MATH_INSTRUMENT_LAMBERT_W_SCHROEDER // Schroeder refinement diagnostics.
  35. BOOST_MATH_INSTRUMENT_LAMBERT_W_TERMS // Number of terms used for near-singularity series.
  36. BOOST_MATH_INSTRUMENT_LAMBERT_W_SINGULARITY_SERIES // Show evaluation of series near branch singularity.
  37. BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  38. BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES_ITERATIONS // Show evaluation of series for small z.
  39. //] [/boost_math_instrument_lambert_w_macros]
  40. */
  41. #include <boost/math/tools/config.hpp>
  42. #include <boost/math/policies/error_handling.hpp>
  43. #include <boost/math/policies/policy.hpp>
  44. #include <boost/math/tools/promotion.hpp>
  45. #include <boost/math/special_functions/fpclassify.hpp>
  46. #include <boost/math/special_functions/log1p.hpp> // for log (1 + x)
  47. #include <boost/math/constants/constants.hpp> // For exp_minus_one == 3.67879441171442321595523770161460867e-01.
  48. #include <boost/math/special_functions/next.hpp> // for has_denorm_now
  49. #include <boost/math/special_functions/pow.hpp> // powers with compile time exponent, used in arbitrary precision code.
  50. #include <boost/math/tools/series.hpp> // series functor.
  51. //#include <boost/math/tools/polynomial.hpp> // polynomial.
  52. #include <boost/math/tools/rational.hpp> // evaluate_polynomial.
  53. #include <boost/math/tools/precision.hpp> // boost::math::tools::max_value().
  54. #include <boost/math/tools/big_constant.hpp>
  55. #include <boost/math/tools/cxx03_warn.hpp>
  56. #ifndef BOOST_MATH_STANDALONE
  57. #include <boost/lexical_cast.hpp>
  58. #endif
  59. #include <limits>
  60. #include <cmath>
  61. #include <limits>
  62. #include <exception>
  63. #include <type_traits>
  64. #include <cstdint>
  65. // Needed for testing and diagnostics only.
  66. #include <iostream>
  67. #include <typeinfo>
  68. #include <boost/math/special_functions/next.hpp> // For float_distance.
  69. using lookup_t = double; // Type for lookup table (double or float, or even long double?)
  70. //#include "J:\Cpp\Misc\lambert_w_lookup_table_generator\lambert_w_lookup_table.ipp"
  71. // #include "lambert_w_lookup_table.ipp" // Boost.Math version.
  72. #include <boost/math/special_functions/detail/lambert_w_lookup_table.ipp>
  73. #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
  74. //
  75. // This is the only way we can avoid
  76. // warning: non-standard suffix on floating constant [-Wpedantic]
  77. // when building with -Wall -pedantic. Neither __extension__
  78. // nor #pragma diagnostic ignored work :(
  79. //
  80. #pragma GCC system_header
  81. #endif
  82. namespace boost {
  83. namespace math {
  84. namespace lambert_w_detail {
  85. //! \brief Applies a single Halley step to make a better estimate of Lambert W.
  86. //! \details Used the simplified formulae obtained from
  87. //! http://www.wolframalpha.com/input/?i=%5B2(z+exp(z)-w)+d%2Fdx+(z+exp(z)-w)%5D+%2F+%5B2+(d%2Fdx+(z+exp(z)-w))%5E2+-+(z+exp(z)-w)+d%5E2%2Fdx%5E2+(z+exp(z)-w)%5D
  88. //! [2(z exp(z)-w) d/dx (z exp(z)-w)] / [2 (d/dx (z exp(z)-w))^2 - (z exp(z)-w) d^2/dx^2 (z exp(z)-w)]
  89. //! \tparam T floating-point (or fixed-point) type.
  90. //! \param w_est Lambert W estimate.
  91. //! \param z Argument z for Lambert_w function.
  92. //! \returns New estimate of Lambert W, hopefully improved.
  93. //!
  94. template <typename T>
  95. inline T lambert_w_halley_step(T w_est, const T z)
  96. {
  97. BOOST_MATH_STD_USING
  98. T e = exp(w_est);
  99. w_est -= 2 * (w_est + 1) * (e * w_est - z) / (z * (w_est + 2) + e * (w_est * (w_est + 2) + 2));
  100. return w_est;
  101. } // template <typename T> lambert_w_halley_step(T w_est, T z)
  102. //! \brief Halley iterate to refine Lambert_w estimate,
  103. //! taking at least one Halley_step.
  104. //! Repeat Halley steps until the *last step* had fewer than half the digits wrong,
  105. //! the step we've just taken should have been sufficient to have completed the iteration.
  106. //! \tparam T floating-point (or fixed-point) type.
  107. //! \param z Argument z for Lambert_w function.
  108. //! \param w_est Lambert w estimate.
  109. template <typename T>
  110. inline T lambert_w_halley_iterate(T w_est, const T z)
  111. {
  112. BOOST_MATH_STD_USING
  113. static const T max_diff = boost::math::tools::root_epsilon<T>() * fabs(w_est);
  114. T w_new = lambert_w_halley_step(w_est, z);
  115. T diff = fabs(w_est - w_new);
  116. while (diff > max_diff)
  117. {
  118. w_est = w_new;
  119. w_new = lambert_w_halley_step(w_est, z);
  120. diff = fabs(w_est - w_new);
  121. }
  122. return w_new;
  123. } // template <typename T> lambert_w_halley_iterate(T w_est, T z)
  124. // Two Halley function versions that either
  125. // single step (if std::false_type) or iterate (if std::true_type).
  126. // Selected at compile-time using parameter 3.
  127. template <typename T>
  128. inline T lambert_w_maybe_halley_iterate(T z, T w, std::false_type const&)
  129. {
  130. return lambert_w_halley_step(z, w); // Single step.
  131. }
  132. template <typename T>
  133. inline T lambert_w_maybe_halley_iterate(T z, T w, std::true_type const&)
  134. {
  135. return lambert_w_halley_iterate(z, w); // Iterate steps.
  136. }
  137. //! maybe_reduce_to_double function,
  138. //! Two versions that have a compile-time option to
  139. //! reduce argument z to double precision (if true_type).
  140. //! Version is selected at compile-time using parameter 2.
  141. template <typename T>
  142. inline double maybe_reduce_to_double(const T& z, const std::true_type&)
  143. {
  144. return static_cast<double>(z); // Reduce to double precision.
  145. }
  146. template <typename T>
  147. inline T maybe_reduce_to_double(const T& z, const std::false_type&)
  148. { // Don't reduce to double.
  149. return z;
  150. }
  151. template <typename T>
  152. inline double must_reduce_to_double(const T& z, const std::true_type&)
  153. {
  154. return static_cast<double>(z); // Reduce to double precision.
  155. }
  156. template <typename T>
  157. inline double must_reduce_to_double(const T& z, const std::false_type&)
  158. { // try a lexical_cast and hope for the best:
  159. #ifndef BOOST_MATH_STANDALONE
  160. #ifdef BOOST_MATH_USE_CHARCONV_FOR_CONVERSION
  161. // Catches the C++23 floating point types
  162. if constexpr (std::is_arithmetic_v<T>)
  163. {
  164. return static_cast<double>(z);
  165. }
  166. else
  167. {
  168. return boost::lexical_cast<double>(z);
  169. }
  170. #else
  171. return boost::lexical_cast<double>(z);
  172. #endif
  173. #else
  174. static_assert(sizeof(T) == 0, "Unsupported in standalone mode: don't know how to cast your number type to a double.");
  175. return 0.0;
  176. #endif
  177. }
  178. //! \brief Schroeder method, fifth-order update formula,
  179. //! \details See T. Fukushima page 80-81, and
  180. //! A. Householder, The Numerical Treatment of a Single Nonlinear Equation,
  181. //! McGraw-Hill, New York, 1970, section 4.4.
  182. //! Fukushima algorithm switches to @c schroeder_update after pre-computed bisections,
  183. //! chosen to ensure that the result will be achieve the +/- 10 epsilon target.
  184. //! \param w Lambert w estimate from bisection or series.
  185. //! \param y bracketing value from bisection.
  186. //! \returns Refined estimate of Lambert w.
  187. // Schroeder refinement, called unless NOT required by precision policy.
  188. template<typename T>
  189. inline T schroeder_update(const T w, const T y)
  190. {
  191. // Compute derivatives using 5th order Schroeder refinement.
  192. // Since this is the final step, it will always use the highest precision type T.
  193. // Example of Call:
  194. // result = schroeder_update(w, y);
  195. //where
  196. // w is estimate of Lambert W (from bisection or series).
  197. // y is z * e^-w.
  198. BOOST_MATH_STD_USING // Aid argument dependent lookup of abs.
  199. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SCHROEDER
  200. std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
  201. using boost::math::float_distance;
  202. T fd = float_distance<T>(w, y);
  203. std::cout << "Schroder ";
  204. if (abs(fd) < 214748000.)
  205. {
  206. std::cout << " Distance = "<< static_cast<int>(fd);
  207. }
  208. else
  209. {
  210. std::cout << "Difference w - y = " << (w - y) << ".";
  211. }
  212. std::cout << std::endl;
  213. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SCHROEDER
  214. // Fukushima equation 18, page 6.
  215. const T f0 = w - y; // f0 = w - y.
  216. const T f1 = 1 + y; // f1 = df/dW
  217. const T f00 = f0 * f0;
  218. const T f11 = f1 * f1;
  219. const T f0y = f0 * y;
  220. const T result =
  221. w - 4 * f0 * (6 * f1 * (f11 + f0y) + f00 * y) /
  222. (f11 * (24 * f11 + 36 * f0y) +
  223. f00 * (6 * y * y + 8 * f1 * y + f0y)); // Fukushima Page 81, equation 21 from equation 20.
  224. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SCHROEDER
  225. std::cout << "Schroeder refined " << w << " " << y << ", difference " << w-y << ", change " << w - result << ", to result " << result << std::endl;
  226. std::cout.precision(saved_precision); // Restore.
  227. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SCHROEDER
  228. return result;
  229. } // template<typename T = double> T schroeder_update(const T w, const T y)
  230. //! \brief Series expansion used near the singularity/branch point z = -exp(-1) = -3.6787944.
  231. //! Wolfram InverseSeries[Series[sqrt[2(p Exp[1 + p] + 1)], {p,-1, 20}]]
  232. //! Wolfram command used to obtain 40 series terms at 50 decimal digit precision was
  233. //! N[InverseSeries[Series[Sqrt[2(p Exp[1 + p] + 1)], { p,-1,40 }]], 50]
  234. //! -1+p-p^2/3+(11 p^3)/72-(43 p^4)/540+(769 p^5)/17280-(221 p^6)/8505+(680863 p^7)/43545600 ...
  235. //! Decimal values of specifications for built-in floating-point types below
  236. //! are at least 21 digits precision == max_digits10 for long double.
  237. //! Longer decimal digits strings are rationals evaluated using Wolfram.
  238. template<typename T>
  239. T lambert_w_singularity_series(const T p)
  240. {
  241. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SINGULARITY_SERIES
  242. std::size_t saved_precision = std::cout.precision(3);
  243. std::cout << "Singularity_series Lambert_w p argument = " << p << std::endl;
  244. std::cout
  245. //<< "Argument Type = " << typeid(T).name()
  246. //<< ", max_digits10 = " << std::numeric_limits<T>::max_digits10
  247. //<< ", epsilon = " << std::numeric_limits<T>::epsilon()
  248. << std::endl;
  249. std::cout.precision(saved_precision);
  250. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SINGULARITY_SERIES
  251. static const T q[] =
  252. {
  253. -static_cast<T>(1), // j0
  254. +T(1), // j1
  255. -T(1) / 3, // 1/3 j2
  256. +T(11) / 72, // 0.152777777777777778, // 11/72 j3
  257. -T(43) / 540, // 0.0796296296296296296, // 43/540 j4
  258. +T(769) / 17280, // 0.0445023148148148148, j5
  259. -T(221) / 8505, // 0.0259847148736037625, j6
  260. //+T(0.0156356325323339212L), // j7
  261. //+T(0.015635632532333921222810111699000587889476778365667L), // j7 from Wolfram N[680863/43545600, 50]
  262. +T(680863uLL) / 43545600uLL, // +0.0156356325323339212, j7
  263. //-T(0.00961689202429943171L), // j8
  264. -T(1963uLL) / 204120uLL, // 0.00961689202429943171, j8
  265. //-T(0.0096168920242994317068391142465216539290613364687439L), // j8 from Wolfram N[1963/204120, 50]
  266. +T(226287557uLL) / 37623398400uLL, // 0.00601454325295611786, j9
  267. -T(5776369uLL) / 1515591000uLL, // 0.00381129803489199923, j10
  268. //+T(0.00244087799114398267L), j11 0.0024408779911439826658968585286437530215699919795550
  269. +T(169709463197uLL) / 69528040243200uLL, // j11
  270. // -T(0.00157693034468678425L), // j12 -0.0015769303446867842539234095399314115973161850314723
  271. -T(1118511313uLL) / 709296588000uLL, // j12
  272. +T(667874164916771uLL) / 650782456676352000uLL, // j13
  273. //+T(0.00102626332050760715L), // j13 0.0010262633205076071544375481533906861056468041465973
  274. -T(500525573uLL) / 744761417400uLL, // j14
  275. // -T(0.000672061631156136204L), j14
  276. //+T(1003663334225097487uLL) / 234281684403486720000uLL, // j15 0.00044247306181462090993020760858473726479232802068800 error C2177: constant too big
  277. //+T(0.000442473061814620910L, // j15
  278. BOOST_MATH_BIG_CONSTANT(T, 64, +0.000442473061814620910), // j15
  279. // -T(0.000292677224729627445L), // j16
  280. BOOST_MATH_BIG_CONSTANT(T, 64, -0.000292677224729627445), // j16
  281. //+T(0.000194387276054539318L), // j17
  282. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000194387276054539318), // j17
  283. //-T(0.000129574266852748819L), // j18
  284. BOOST_MATH_BIG_CONSTANT(T, 64, -0.000129574266852748819), // j18
  285. //+T(0.0000866503580520812717L), // j19 N[+1150497127780071399782389/13277465363600276402995200000, 50] 0.000086650358052081271660451590462390293190597827783288
  286. BOOST_MATH_BIG_CONSTANT(T, 64, +0.0000866503580520812717), // j19
  287. //-T(0.0000581136075044138168L) // j20 N[2853534237182741069/49102686267859224000000, 50] 0.000058113607504413816772205464778828177256611844221913
  288. // -T(2853534237182741069uLL) / 49102686267859224000000uLL // j20 // error C2177: constant too big,
  289. // so must use BOOST_MATH_BIG_CONSTANT(T, ) format in hope of using suffix Q for quad or decimal digits string for others.
  290. //-T(0.000058113607504413816772205464778828177256611844221913L), // j20 N[2853534237182741069/49102686267859224000000, 50] 0.000058113607504413816772205464778828177256611844221913
  291. BOOST_MATH_BIG_CONSTANT(T, 113, -0.000058113607504413816772205464778828177256611844221913) // j20 - last used by Fukushima
  292. // More terms don't seem to give any improvement (worse in fact) and are not use for many z values.
  293. //BOOST_MATH_BIG_CONSTANT(T, +0.000039076684867439051635395583044527492132109160553593), // j21
  294. //BOOST_MATH_BIG_CONSTANT(T, -0.000026338064747231098738584082718649443078703982217219), // j22
  295. //BOOST_MATH_BIG_CONSTANT(T, +0.000017790345805079585400736282075184540383274460464169), // j23
  296. //BOOST_MATH_BIG_CONSTANT(T, -0.000012040352739559976942274116578992585158113153190354), // j24
  297. //BOOST_MATH_BIG_CONSTANT(T, +8.1635319824966121713827512573558687050675701559448E-6), // j25
  298. //BOOST_MATH_BIG_CONSTANT(T, -5.5442032085673591366657251660804575198155559225316E-6) // j26
  299. // -T(5.5442032085673591366657251660804575198155559225316E-6L) // j26
  300. // 21 to 26 Added for long double.
  301. }; // static const T q[]
  302. /*
  303. // Temporary copy of original double values for comparison; these are reproduced well.
  304. static const T q[] =
  305. {
  306. -1L, // j0
  307. +1L, // j1
  308. -0.333333333333333333L, // 1/3 j2
  309. +0.152777777777777778L, // 11/72 j3
  310. -0.0796296296296296296L, // 43/540
  311. +0.0445023148148148148L,
  312. -0.0259847148736037625L,
  313. +0.0156356325323339212L,
  314. -0.00961689202429943171L,
  315. +0.00601454325295611786L,
  316. -0.00381129803489199923L,
  317. +0.00244087799114398267L,
  318. -0.00157693034468678425L,
  319. +0.00102626332050760715L,
  320. -0.000672061631156136204L,
  321. +0.000442473061814620910L,
  322. -0.000292677224729627445L,
  323. +0.000194387276054539318L,
  324. -0.000129574266852748819L,
  325. +0.0000866503580520812717L,
  326. -0.0000581136075044138168L // j20
  327. };
  328. */
  329. // Decide how many series terms to use, increasing as z approaches the singularity,
  330. // balancing run-time versus computational noise from round-off.
  331. // In practice, we truncate the series expansion at a certain order.
  332. // If the order is too large, not only does the amount of computation increase,
  333. // but also the round-off errors accumulate.
  334. // See Fukushima equation 35, page 85 for logic of choice of number of series terms.
  335. BOOST_MATH_STD_USING // Aid argument dependent lookup (ADL) of abs.
  336. const T absp = abs(p);
  337. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_TERMS
  338. {
  339. int terms = 20; // Default to using all terms.
  340. if (absp < 0.01159)
  341. { // Very near singularity.
  342. terms = 6;
  343. }
  344. else if (absp < 0.0766)
  345. { // Near singularity.
  346. terms = 10;
  347. }
  348. std::streamsize saved_precision = std::cout.precision(3);
  349. std::cout << "abs(p) = " << absp << ", terms = " << terms << std::endl;
  350. std::cout.precision(saved_precision);
  351. }
  352. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_TERMS
  353. if (absp < T(0.01159))
  354. { // Only 6 near-singularity series terms are useful.
  355. return
  356. -1 +
  357. p * (1 +
  358. p * (q[2] +
  359. p * (q[3] +
  360. p * (q[4] +
  361. p * (q[5] +
  362. p * q[6]
  363. )))));
  364. }
  365. else if (absp < T(0.0766)) // Use 10 near-singularity series terms.
  366. { // Use 10 near-singularity series terms.
  367. return
  368. -1 +
  369. p * (1 +
  370. p * (q[2] +
  371. p * (q[3] +
  372. p * (q[4] +
  373. p * (q[5] +
  374. p * (q[6] +
  375. p * (q[7] +
  376. p * (q[8] +
  377. p * (q[9] +
  378. p * q[10]
  379. )))))))));
  380. }
  381. // Use all 20 near-singularity series terms.
  382. return
  383. -1 +
  384. p * (1 +
  385. p * (q[2] +
  386. p * (q[3] +
  387. p * (q[4] +
  388. p * (q[5] +
  389. p * (q[6] +
  390. p * (q[7] +
  391. p * (q[8] +
  392. p * (q[9] +
  393. p * (q[10] +
  394. p * (q[11] +
  395. p * (q[12] +
  396. p * (q[13] +
  397. p * (q[14] +
  398. p * (q[15] +
  399. p * (q[16] +
  400. p * (q[17] +
  401. p * (q[18] +
  402. p * (q[19] +
  403. p * q[20] // Last Fukushima term.
  404. )))))))))))))))))));
  405. // + // more terms for more precise T: long double ...
  406. //// but makes almost no difference, so don't use more terms?
  407. // p*q[21] +
  408. // p*q[22] +
  409. // p*q[23] +
  410. // p*q[24] +
  411. // p*q[25]
  412. // )))))))))))))))))));
  413. } // template<typename T = double> T lambert_w_singularity_series(const T p)
  414. /////////////////////////////////////////////////////////////////////////////////////////////
  415. //! \brief Series expansion used near zero (abs(z) < 0.05).
  416. //! \details
  417. //! Coefficients of the inverted series expansion of the Lambert W function around z = 0.
  418. //! Tosio Fukushima always uses all 17 terms of a Taylor series computed using Wolfram with
  419. //! InverseSeries[Series[z Exp[z],{z,0,17}]]
  420. //! Tosio Fukushima / Journal of Computational and Applied Mathematics 244 (2013) page 86.
  421. //! Decimal values of specifications for built-in floating-point types below
  422. //! are 21 digits precision == max_digits10 for long double.
  423. //! Care! Some coefficients might overflow some fixed_point types.
  424. //! This version is intended to allow use by user-defined types
  425. //! like Boost.Multiprecision quad and cpp_dec_float types.
  426. //! The three specializations below for built-in float, double
  427. //! (and perhaps long double) will be chosen in preference for these types.
  428. //! This version uses rationals computed by Wolfram as far as possible,
  429. //! limited by maximum size of uLL integers.
  430. //! For higher term, uses decimal digit strings computed by Wolfram up to the maximum possible using uLL rationals,
  431. //! and then higher coefficients are computed as necessary using function lambert_w0_small_z_series_term
  432. //! until the precision required by the policy is achieved.
  433. //! InverseSeries[Series[z Exp[z],{z,0,34}]] also computed.
  434. // Series evaluation for LambertW(z) as z -> 0.
  435. // See http://functions.wolfram.com/ElementaryFunctions/ProductLog/06/01/01/0003/
  436. // http://functions.wolfram.com/ElementaryFunctions/ProductLog/06/01/01/0003/MainEq1.L.gif
  437. //! \brief lambert_w0_small_z uses a tag_type to select a variant depending on the size of the type.
  438. //! The Lambert W is computed by lambert_w0_small_z for small z.
  439. //! The cutoff for z smallness determined by Tosio Fukushima by trial and error is (abs(z) < 0.05),
  440. //! but the optimum might be a function of the size of the type of z.
  441. //! \details
  442. //! The tag_type selection is based on the value @c std::numeric_limits<T>::max_digits10.
  443. //! This allows distinguishing between long double types that commonly vary between 64 and 80-bits,
  444. //! and also compilers that have a float type using 64 bits and/or long double using 128-bits.
  445. //! It assumes that max_digits10 is defined correctly or this might fail to make the correct selection.
  446. //! causing very small differences in computing lambert_w that would be very difficult to detect and diagnose.
  447. //! Cannot switch on @c std::numeric_limits<>::max() because comparison values may overflow the compiler limit.
  448. //! Cannot switch on @c std::numeric_limits<long double>::max_exponent10()
  449. //! because both 80 and 128 bit floating-point implementations use 11 bits for the exponent.
  450. //! So must rely on @c std::numeric_limits<long double>::max_digits10.
  451. //! Specialization of float zero series expansion used for small z (abs(z) < 0.05).
  452. //! Specializations of lambert_w0_small_z for built-in types.
  453. //! These specializations should be chosen in preference to T version.
  454. //! For example: lambert_w0_small_z(0.001F) should use the float version.
  455. //! (Parameter Policy is not used by built-in types when all terms are used during an inline computation,
  456. //! but for the tag_type selection to work, they all must include Policy in their signature.
  457. // Forward declaration of variants of lambert_w0_small_z.
  458. template <typename T, typename Policy>
  459. T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 0> const&); // for float (32-bit) type.
  460. template <typename T, typename Policy>
  461. T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 1> const&); // for double (64-bit) type.
  462. template <typename T, typename Policy>
  463. T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 2> const&); // for long double (double extended 80-bit) type.
  464. template <typename T, typename Policy>
  465. T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 3> const&); // for long double (128-bit) type.
  466. template <typename T, typename Policy>
  467. T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 4> const&); // for float128 quadmath Q type.
  468. template <typename T, typename Policy>
  469. T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 5> const&); // Generic multiprecision T.
  470. // Set tag_type depending on max_digits10.
  471. template <typename T, typename Policy>
  472. T lambert_w0_small_z(T x, const Policy& pol)
  473. { //std::numeric_limits<T>::max_digits10 == 36 ? 3 : // 128-bit long double.
  474. using tag_type = std::integral_constant<int,
  475. std::numeric_limits<T>::is_specialized == 0 ? 5 :
  476. #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
  477. std::numeric_limits<T>::max_digits10 <= 9 ? 0 : // for float 32-bit.
  478. std::numeric_limits<T>::max_digits10 <= 17 ? 1 : // for double 64-bit.
  479. std::numeric_limits<T>::max_digits10 <= 22 ? 2 : // for 80-bit double extended.
  480. std::numeric_limits<T>::max_digits10 < 37 ? 4 // for both 128-bit long double (3) and 128-bit quad suffix Q type (4).
  481. #else
  482. std::numeric_limits<T>::radix != 2 ? 5 :
  483. std::numeric_limits<T>::digits <= 24 ? 0 : // for float 32-bit.
  484. std::numeric_limits<T>::digits <= 53 ? 1 : // for double 64-bit.
  485. std::numeric_limits<T>::digits <= 64 ? 2 : // for 80-bit double extended.
  486. std::numeric_limits<T>::digits <= 113 ? 4 // for both 128-bit long double (3) and 128-bit quad suffix Q type (4).
  487. #endif
  488. : 5>; // All Generic multiprecision types.
  489. // std::cout << "\ntag type = " << tag_type << std::endl; // error C2275: 'tag_type': illegal use of this type as an expression.
  490. return lambert_w0_small_z(x, pol, tag_type());
  491. } // template <typename T> T lambert_w0_small_z(T x)
  492. //! Specialization of float (32-bit) series expansion used for small z (abs(z) < 0.05).
  493. // Only 9 Coefficients are computed to 21 decimal digits precision, ample for 32-bit float used by most platforms.
  494. // Taylor series coefficients used are computed by Wolfram to 50 decimal digits using instruction
  495. // N[InverseSeries[Series[z Exp[z],{z,0,34}]],50],
  496. // as proposed by Tosio Fukushima and implemented by Darko Veberic.
  497. template <typename T, typename Policy>
  498. T lambert_w0_small_z(T z, const Policy&, std::integral_constant<int, 0> const&)
  499. {
  500. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  501. std::streamsize prec = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
  502. std::cout << "\ntag_type 0 float lambert_w0_small_z called with z = " << z << " using " << 9 << " terms of precision "
  503. << std::numeric_limits<float>::max_digits10 << " decimal digits. " << std::endl;
  504. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  505. T result =
  506. z * (1 - // j1 z^1 term = 1
  507. z * (1 - // j2 z^2 term = -1
  508. z * (static_cast<float>(3uLL) / 2uLL - // 3/2 // j3 z^3 term = 1.5.
  509. z * (2.6666666666666666667F - // 8/3 // j4
  510. z * (5.2083333333333333333F - // -125/24 // j5
  511. z * (10.8F - // j6
  512. z * (23.343055555555555556F - // j7
  513. z * (52.012698412698412698F - // j8
  514. z * 118.62522321428571429F)))))))); // j9
  515. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  516. std::cout << "return w = " << result << std::endl;
  517. std::cout.precision(prec); // Restore.
  518. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  519. return result;
  520. } // template <typename T> T lambert_w0_small_z(T x, std::integral_constant<int, 0> const&)
  521. //! Specialization of double (64-bit double) series expansion used for small z (abs(z) < 0.05).
  522. // 17 Coefficients are computed to 21 decimal digits precision suitable for 64-bit double used by most platforms.
  523. // Taylor series coefficients used are computed by Wolfram to 50 decimal digits using instruction
  524. // N[InverseSeries[Series[z Exp[z],{z,0,34}]],50], as proposed by Tosio Fukushima and implemented by Veberic.
  525. template <typename T, typename Policy>
  526. T lambert_w0_small_z(const T z, const Policy&, std::integral_constant<int, 1> const&)
  527. {
  528. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  529. std::streamsize prec = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
  530. std::cout << "\ntag_type 1 double lambert_w0_small_z called with z = " << z << " using " << 17 << " terms of precision, "
  531. << std::numeric_limits<double>::max_digits10 << " decimal digits. " << std::endl;
  532. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  533. T result =
  534. z * (1. - // j1 z^1
  535. z * (1. - // j2 z^2
  536. z * (1.5 - // 3/2 // j3 z^3
  537. z * (2.6666666666666666667 - // 8/3 // j4
  538. z * (5.2083333333333333333 - // -125/24 // j5
  539. z * (10.8 - // j6
  540. z * (23.343055555555555556 - // j7
  541. z * (52.012698412698412698 - // j8
  542. z * (118.62522321428571429 - // j9
  543. z * (275.57319223985890653 - // j10
  544. z * (649.78717234347442681 - // j11
  545. z * (1551.1605194805194805 - // j12
  546. z * (3741.4497029592385495 - // j13
  547. z * (9104.5002411580189358 - // j14
  548. z * (22324.308512706601434 - // j15
  549. z * (55103.621972903835338 - // j16
  550. z * 136808.86090394293563)))))))))))))))); // j17 z^17
  551. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  552. std::cout << "return w = " << result << std::endl;
  553. std::cout.precision(prec); // Restore.
  554. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  555. return result;
  556. } // T lambert_w0_small_z(const T z, std::integral_constant<int, 1> const&)
  557. //! Specialization of long double (80-bit double extended) series expansion used for small z (abs(z) < 0.05).
  558. // 21 Coefficients are computed to 21 decimal digits precision suitable for 80-bit long double used by some
  559. // platforms including GCC and Clang when generating for Intel X86 floating-point processors with 80-bit operations enabled (the default).
  560. // (This is NOT used by Microsoft Visual Studio where double and long always both use only 64-bit type.
  561. // Nor used for 128-bit float128.)
  562. template <typename T, typename Policy>
  563. T lambert_w0_small_z(const T z, const Policy&, std::integral_constant<int, 2> const&)
  564. {
  565. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  566. std::streamsize precision = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
  567. std::cout << "\ntag_type 2 long double (80-bit double extended) lambert_w0_small_z called with z = " << z << " using " << 21 << " terms of precision, "
  568. << std::numeric_limits<long double>::max_digits10 << " decimal digits. " << std::endl;
  569. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  570. // T result =
  571. // z * (1.L - // j1 z^1
  572. // z * (1.L - // j2 z^2
  573. // z * (1.5L - // 3/2 // j3
  574. // z * (2.6666666666666666667L - // 8/3 // j4
  575. // z * (5.2083333333333333333L - // -125/24 // j5
  576. // z * (10.800000000000000000L - // j6
  577. // z * (23.343055555555555556L - // j7
  578. // z * (52.012698412698412698L - // j8
  579. // z * (118.62522321428571429L - // j9
  580. // z * (275.57319223985890653L - // j10
  581. // z * (649.78717234347442681L - // j11
  582. // z * (1551.1605194805194805L - // j12
  583. // z * (3741.4497029592385495L - // j13
  584. // z * (9104.5002411580189358L - // j14
  585. // z * (22324.308512706601434L - // j15
  586. // z * (55103.621972903835338L - // j16
  587. // z * (136808.86090394293563L - // j17 z^17 last term used by Fukushima double.
  588. // z * (341422.050665838363317L - // z^18
  589. // z * (855992.9659966075514633L - // z^19
  590. // z * (2.154990206091088289321e6L - // z^20
  591. // z * 5.4455529223144624316423e6L // z^21
  592. // ))))))))))))))))))));
  593. //
  594. T result =
  595. z * (1.L - // z j1
  596. z * (1.L - // z^2
  597. z * (1.500000000000000000000000000000000L - // z^3
  598. z * (2.666666666666666666666666666666666L - // z ^ 4
  599. z * (5.208333333333333333333333333333333L - // z ^ 5
  600. z * (10.80000000000000000000000000000000L - // z ^ 6
  601. z * (23.34305555555555555555555555555555L - // z ^ 7
  602. z * (52.01269841269841269841269841269841L - // z ^ 8
  603. z * (118.6252232142857142857142857142857L - // z ^ 9
  604. z * (275.5731922398589065255731922398589L - // z ^ 10
  605. z * (649.7871723434744268077601410934744L - // z ^ 11
  606. z * (1551.160519480519480519480519480519L - // z ^ 12
  607. z * (3741.449702959238549516327294105071L - //z ^ 13
  608. z * (9104.500241158018935796713574491352L - // z ^ 14
  609. z * (22324.308512706601434280005708577137L - // z ^ 15
  610. z * (55103.621972903835337697771560205422L - // z ^ 16
  611. z * (136808.86090394293563342215789305736L - // z ^ 17
  612. z * (341422.05066583836331735491399356945L - // z^18
  613. z * (855992.9659966075514633630250633224L - // z^19
  614. z * (2.154990206091088289321708745358647e6L // z^20 distance -5 without term 20
  615. ))))))))))))))))))));
  616. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  617. std::cout << "return w = " << result << std::endl;
  618. std::cout.precision(precision); // Restore.
  619. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  620. return result;
  621. } // long double lambert_w0_small_z(const T z, std::integral_constant<int, 1> const&)
  622. //! Specialization of 128-bit long double series expansion used for small z (abs(z) < 0.05).
  623. // 34 Taylor series coefficients used are computed by Wolfram to 50 decimal digits using instruction
  624. // N[InverseSeries[Series[z Exp[z],{z,0,34}]],50],
  625. // and are suffixed by L as they are assumed of type long double.
  626. // (This is NOT used for 128-bit quad boost::multiprecision::float128 type which required a suffix Q
  627. // nor multiprecision type cpp_bin_float_quad that can only be initialized at full precision of the type
  628. // constructed with a decimal digit string like "2.6666666666666666666666666666666666666666666666667".)
  629. template <typename T, typename Policy>
  630. T lambert_w0_small_z(const T z, const Policy&, std::integral_constant<int, 3> const&)
  631. {
  632. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  633. std::streamsize precision = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
  634. std::cout << "\ntag_type 3 long double (128-bit) lambert_w0_small_z called with z = " << z << " using " << 17 << " terms of precision, "
  635. << std::numeric_limits<double>::max_digits10 << " decimal digits. " << std::endl;
  636. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  637. T result =
  638. z * (1.L - // j1
  639. z * (1.L - // j2
  640. z * (1.5L - // 3/2 // j3
  641. z * (2.6666666666666666666666666666666666L - // 8/3 // j4
  642. z * (5.2052083333333333333333333333333333L - // -125/24 // j5
  643. z * (10.800000000000000000000000000000000L - // j6
  644. z * (23.343055555555555555555555555555555L - // j7
  645. z * (52.0126984126984126984126984126984126L - // j8
  646. z * (118.625223214285714285714285714285714L - // j9
  647. z * (275.57319223985890652557319223985890L - // * z ^ 10 - // j10
  648. z * (649.78717234347442680776014109347442680776014109347L - // j11
  649. z * (1551.1605194805194805194805194805194805194805194805L - // j12
  650. z * (3741.4497029592385495163272941050718828496606274384L - // j13
  651. z * (9104.5002411580189357967135744913522691300469078247L - // j14
  652. z * (22324.308512706601434280005708577137148565719994291L - // j15
  653. z * (55103.621972903835337697771560205422639285073147507L - // j16
  654. z * 136808.86090394293563342215789305736395683485630576L // j17
  655. ))))))))))))))));
  656. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  657. std::cout << "return w = " << result << std::endl;
  658. std::cout.precision(precision); // Restore.
  659. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  660. return result;
  661. } // T lambert_w0_small_z(const T z, std::integral_constant<int, 3> const&)
  662. //! Specialization of 128-bit quad series expansion used for small z (abs(z) < 0.05).
  663. // 34 Taylor series coefficients used were computed by Wolfram to 50 decimal digits using instruction
  664. // N[InverseSeries[Series[z Exp[z],{z,0,34}]],50],
  665. // and are suffixed by Q as they are assumed of type quad.
  666. // This could be used for 128-bit quad (which requires a suffix Q for full precision).
  667. // But experiments with GCC 7.2.0 show that while this gives full 128-bit precision
  668. // when the -f-ext-numeric-literals option is in force and the libquadmath library available,
  669. // over the range -0.049 to +0.049,
  670. // it is slightly slower than getting a double approximation followed by a single Halley step.
  671. #ifdef BOOST_HAS_FLOAT128
  672. template <typename T, typename Policy>
  673. T lambert_w0_small_z(const T z, const Policy&, std::integral_constant<int, 4> const&)
  674. {
  675. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  676. std::streamsize precision = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
  677. std::cout << "\ntag_type 4 128-bit quad float128 lambert_w0_small_z called with z = " << z << " using " << 34 << " terms of precision, "
  678. << std::numeric_limits<float128>::max_digits10 << " max decimal digits." << std::endl;
  679. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  680. T result =
  681. z * (1.Q - // z j1
  682. z * (1.Q - // z^2
  683. z * (1.500000000000000000000000000000000Q - // z^3
  684. z * (2.666666666666666666666666666666666Q - // z ^ 4
  685. z * (5.208333333333333333333333333333333Q - // z ^ 5
  686. z * (10.80000000000000000000000000000000Q - // z ^ 6
  687. z * (23.34305555555555555555555555555555Q - // z ^ 7
  688. z * (52.01269841269841269841269841269841Q - // z ^ 8
  689. z * (118.6252232142857142857142857142857Q - // z ^ 9
  690. z * (275.5731922398589065255731922398589Q - // z ^ 10
  691. z * (649.7871723434744268077601410934744Q - // z ^ 11
  692. z * (1551.160519480519480519480519480519Q - // z ^ 12
  693. z * (3741.449702959238549516327294105071Q - //z ^ 13
  694. z * (9104.500241158018935796713574491352Q - // z ^ 14
  695. z * (22324.308512706601434280005708577137Q - // z ^ 15
  696. z * (55103.621972903835337697771560205422Q - // z ^ 16
  697. z * (136808.86090394293563342215789305736Q - // z ^ 17
  698. z * (341422.05066583836331735491399356945Q - // z^18
  699. z * (855992.9659966075514633630250633224Q - // z^19
  700. z * (2.154990206091088289321708745358647e6Q - // 20
  701. z * (5.445552922314462431642316420035073e6Q - // 21
  702. z * (1.380733000216662949061923813184508e7Q - // 22
  703. z * (3.511704498513923292853869855945334e7Q - // 23
  704. z * (8.956800256102797693072819557780090e7Q - // 24
  705. z * (2.290416846187949813964782641734774e8Q - // 25
  706. z * (5.871035041171798492020292225245235e8Q - // 26
  707. z * (1.508256053857792919641317138812957e9Q - // 27
  708. z * (3.882630161293188940385873468413841e9Q - // 28
  709. z * (1.001394313665482968013913601565723e10Q - // 29
  710. z * (2.587356736265760638992878359024929e10Q - // 30
  711. z * (6.696209709358073856946120522333454e10Q - // 31
  712. z * (1.735711659599198077777078238043644e11Q - // 32
  713. z * (4.505680465642353886756098108484670e11Q - // 33
  714. z * (1.171223178256487391904047636564823e12Q //z^34
  715. ))))))))))))))))))))))))))))))))));
  716. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  717. std::cout << "return w = " << result << std::endl;
  718. std::cout.precision(precision); // Restore.
  719. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  720. return result;
  721. } // T lambert_w0_small_z(const T z, std::integral_constant<int, 4> const&) float128
  722. #else
  723. template <typename T, typename Policy>
  724. inline T lambert_w0_small_z(const T z, const Policy& pol, std::integral_constant<int, 4> const&)
  725. {
  726. return lambert_w0_small_z(z, pol, std::integral_constant<int, 5>());
  727. }
  728. #endif // BOOST_HAS_FLOAT128
  729. //! Series functor to compute series term using pow and factorial.
  730. //! \details Functor is called after evaluating polynomial with the coefficients as rationals below.
  731. template <typename T>
  732. struct lambert_w0_small_z_series_term
  733. {
  734. using result_type = T;
  735. //! \param _z Lambert W argument z.
  736. //! \param -term -pow<18>(z) / 6402373705728000uLL
  737. //! \param _k number of terms == initially 18
  738. // Note *after* evaluating N terms, its internal state has k = N and term = (-1)^N z^N.
  739. lambert_w0_small_z_series_term(T _z, T _term, int _k)
  740. : k(_k), z(_z), term(_term) { }
  741. T operator()()
  742. { // Called by sum_series until needs precision set by factor (policy::get_epsilon).
  743. using std::pow;
  744. ++k;
  745. term *= -z / k;
  746. //T t = pow(z, k) * pow(T(k), -1 + k) / factorial<T>(k); // (z^k * k(k-1)^k) / k!
  747. T result = term * pow(T(k), T(-1 + k)); // term * k^(k-1)
  748. // std::cout << " k = " << k << ", term = " << term << ", result = " << result << std::endl;
  749. return result; //
  750. }
  751. private:
  752. int k;
  753. T z;
  754. T term;
  755. }; // template <typename T> struct lambert_w0_small_z_series_term
  756. //! Generic variant for T a User-defined types like Boost.Multiprecision.
  757. template <typename T, typename Policy>
  758. inline T lambert_w0_small_z(T z, const Policy& pol, std::integral_constant<int, 5> const&)
  759. {
  760. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  761. std::streamsize precision = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
  762. std::cout << "Generic lambert_w0_small_z called with z = " << z << " using as many terms needed for precision." << std::endl;
  763. std::cout << "Argument z is of type " << typeid(T).name() << std::endl;
  764. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  765. // First several terms of the series are tabulated and evaluated as a polynomial:
  766. // this will save us a bunch of expensive calls to pow.
  767. // Then our series functor is initialized "as if" it had already reached term 18,
  768. // enough evaluation of built-in 64-bit double and float (and 80-bit long double?) types.
  769. // Coefficients should be stored such that the coefficients for the x^i terms are in poly[i].
  770. static const T coeff[] =
  771. {
  772. 0, // z^0 Care: zeroth term needed by tools::evaluate_polynomial, but not in the Wolfram equation, so indexes are one different!
  773. 1, // z^1 term.
  774. -1, // z^2 term
  775. static_cast<T>(3uLL) / 2uLL, // z^3 term.
  776. -static_cast<T>(8uLL) / 3uLL, // z^4
  777. static_cast<T>(125uLL) / 24uLL, // z^5
  778. -static_cast<T>(54uLL) / 5uLL, // z^6
  779. static_cast<T>(16807uLL) / 720uLL, // z^7
  780. -static_cast<T>(16384uLL) / 315uLL, // z^8
  781. static_cast<T>(531441uLL) / 4480uLL, // z^9
  782. -static_cast<T>(156250uLL) / 567uLL, // z^10
  783. static_cast<T>(2357947691uLL) / 3628800uLL, // z^11
  784. -static_cast<T>(2985984uLL) / 1925uLL, // z^12
  785. static_cast<T>(1792160394037uLL) / 479001600uLL, // z^13
  786. -static_cast<T>(7909306972uLL) / 868725uLL, // z^14
  787. static_cast<T>(320361328125uLL) / 14350336uLL, // z^15
  788. -static_cast<T>(35184372088832uLL) / 638512875uLL, // z^16
  789. static_cast<T>(2862423051509815793uLL) / 20922789888000uLL, // z^17 term
  790. -static_cast<T>(5083731656658uLL) / 14889875uLL,
  791. // z^18 term. = 136808.86090394293563342215789305735851647769682393
  792. // z^18 is biggest that can be computed as rational using the largest possible uLL integers,
  793. // so higher terms cannot be potentially compiler-computed as uLL rationals.
  794. // Wolfram (5083731656658 z ^ 18) / 14889875 or
  795. // -341422.05066583836331735491399356945575432970390954 z^18
  796. // See note below calling the functor to compute another term,
  797. // sufficient for 80-bit long double precision.
  798. // Wolfram -341422.05066583836331735491399356945575432970390954 z^19 term.
  799. // (5480386857784802185939 z^19)/6402373705728000
  800. // But now this variant is not used to compute long double
  801. // as specializations are provided above.
  802. }; // static const T coeff[]
  803. /*
  804. Table of 19 computed coefficients:
  805. #0 0
  806. #1 1
  807. #2 -1
  808. #3 1.5
  809. #4 -2.6666666666666666666666666666666665382713370408509
  810. #5 5.2083333333333333333333333333333330765426740817019
  811. #6 -10.800000000000000000000000000000000616297582203915
  812. #7 23.343055555555555555555555555555555076212991619177
  813. #8 -52.012698412698412698412698412698412659282693193402
  814. #9 118.62522321428571428571428571428571146835390992496
  815. #10 -275.57319223985890652557319223985891400375196748314
  816. #11 649.7871723434744268077601410934743969785223845882
  817. #12 -1551.1605194805194805194805194805194947599566007429
  818. #13 3741.4497029592385495163272941050719510009019331763
  819. #14 -9104.5002411580189357967135744913524243896052869184
  820. #15 22324.308512706601434280005708577137322392070452582
  821. #16 -55103.621972903835337697771560205423203318720697224
  822. #17 136808.86090394293563342215789305735851647769682393
  823. 136808.86090394293563342215789305735851647769682393 == Exactly same as Wolfram computed value.
  824. #18 -341422.05066583836331735491399356947486381600607416
  825. 341422.05066583836331735491399356945575432970390954 z^19 Wolfram value differs at 36 decimal digit, as expected.
  826. */
  827. using boost::math::policies::get_epsilon; // for type T.
  828. using boost::math::tools::sum_series;
  829. using boost::math::tools::evaluate_polynomial;
  830. // http://www.boost.org/doc/libs/release/libs/math/doc/html/math_toolkit/roots/rational.html
  831. // std::streamsize prec = std::cout.precision(std::numeric_limits <T>::max_digits10);
  832. T result = evaluate_polynomial(coeff, z);
  833. // template <std::size_t N, typename T, typename V>
  834. // V evaluate_polynomial(const T(&poly)[N], const V& val);
  835. // Size of coeff found from N
  836. //std::cout << "evaluate_polynomial(coeff, z); == " << result << std::endl;
  837. //std::cout << "result = " << result << std::endl;
  838. // It's an artefact of the way I wrote the functor: *after* evaluating N
  839. // terms, its internal state has k = N and term = (-1)^N z^N. So after
  840. // evaluating 18 terms, we initialize the functor to the term we've just
  841. // evaluated, and then when it's called, it increments itself to the next term.
  842. // So 18!is 6402373705728000, which is where that comes from.
  843. // The 19th coefficient of the polynomial is actually, 19 ^ 18 / 19!=
  844. // 104127350297911241532841 / 121645100408832000 which after removing GCDs
  845. // reduces down to Wolfram rational 5480386857784802185939 / 6402373705728000.
  846. // Wolfram z^19 term +(5480386857784802185939 z^19) /6402373705728000
  847. // +855992.96599660755146336302506332246623424823099755 z^19
  848. //! Evaluate Functor.
  849. lambert_w0_small_z_series_term<T> s(z, -pow<18>(z) / 6402373705728000uLL, 18);
  850. // Temporary to list the coefficients.
  851. //std::cout << " Table of coefficients" << std::endl;
  852. //std::streamsize saved_precision = std::cout.precision(50);
  853. //for (size_t i = 0; i != 19; i++)
  854. //{
  855. // std::cout << "#" << i << " " << coeff[i] << std::endl;
  856. //}
  857. //std::cout.precision(saved_precision);
  858. std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>(); // Max iterations from policy.
  859. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  860. std::cout << "max iter from policy = " << max_iter << std::endl;
  861. // // max iter from policy = 1000000 is default.
  862. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
  863. result = sum_series(s, get_epsilon<T, Policy>(), max_iter, result);
  864. // result == evaluate_polynomial.
  865. //sum_series(Functor& func, int bits, std::uintmax_t& max_terms, const U& init_value)
  866. // std::cout << "sum_series(s, get_epsilon<T, Policy>(), max_iter, result); = " << result << std::endl;
  867. //T epsilon = get_epsilon<T, Policy>();
  868. //std::cout << "epsilon from policy = " << epsilon << std::endl;
  869. // epsilon from policy = 1.93e-34 for T == quad
  870. // 5.35e-51 for t = cpp_bin_float_50
  871. // std::cout << " get eps = " << get_epsilon<T, Policy>() << std::endl; // quad eps = 1.93e-34, bin_float_50 eps = 5.35e-51
  872. policies::check_series_iterations<T>("boost::math::lambert_w0_small_z<%1%>(%1%)", max_iter, pol);
  873. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES_ITERATIONS
  874. std::cout << "z = " << z << " needed " << max_iter << " iterations." << std::endl;
  875. std::cout.precision(prec); // Restore.
  876. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES_ITERATIONS
  877. return result;
  878. } // template <typename T, typename Policy> inline T lambert_w0_small_z_series(T z, const Policy& pol)
  879. // Approximate lambert_w0 (used for z values that are outside range of lookup table or rational functions)
  880. // Corless equation 4.19, page 349, and Chapeau-Blondeau equation 20, page 2162.
  881. template <typename T>
  882. inline T lambert_w0_approx(T z)
  883. {
  884. BOOST_MATH_STD_USING
  885. T lz = log(z);
  886. T llz = log(lz);
  887. T w = lz - llz + (llz / lz); // Corless equation 4.19, page 349, and Chapeau-Blondeau equation 20, page 2162.
  888. return w;
  889. // std::cout << "w max " << max_w << std::endl; // double 703.227
  890. }
  891. //////////////////////////////////////////////////////////////////////////////////////////
  892. //! \brief Lambert_w0 implementations for float, double and higher precisions.
  893. //! 3rd parameter used to select which version is used.
  894. //! /details Rational polynomials are provided for several range of argument z.
  895. //! For very small values of z, and for z very near the branch singularity at -e^-1 (~= -0.367879),
  896. //! two other series functions are used.
  897. //! float precision polynomials are used for 32-bit (usually float) precision (for speed)
  898. //! double precision polynomials are used for 64-bit (usually double) precision.
  899. //! For higher precisions, a 64-bit double approximation is computed first,
  900. //! and then refined using Halley iterations.
  901. template <typename T>
  902. inline T do_get_near_singularity_param(T z)
  903. {
  904. BOOST_MATH_STD_USING
  905. const T p2 = 2 * (boost::math::constants::e<T>() * z + 1);
  906. const T p = sqrt(p2);
  907. return p;
  908. }
  909. template <typename T, typename Policy>
  910. inline T get_near_singularity_param(T z, const Policy)
  911. {
  912. using value_type = typename policies::evaluation<T, Policy>::type;
  913. return static_cast<T>(do_get_near_singularity_param(static_cast<value_type>(z)));
  914. }
  915. // Forward declarations:
  916. //template <typename T, typename Policy> T lambert_w0_small_z(T z, const Policy& pol);
  917. //template <typename T, typename Policy>
  918. //T lambert_w0_imp(T w, const Policy& pol, const std::integral_constant<int, 0>&); // 32 bit usually float.
  919. //template <typename T, typename Policy>
  920. //T lambert_w0_imp(T w, const Policy& pol, const std::integral_constant<int, 1>&); // 64 bit usually double.
  921. //template <typename T, typename Policy>
  922. //T lambert_w0_imp(T w, const Policy& pol, const std::integral_constant<int, 2>&); // 80-bit long double.
  923. template <typename T>
  924. T lambert_w_positive_rational_float(T z)
  925. {
  926. BOOST_MATH_STD_USING
  927. if (z < 2)
  928. {
  929. if (z < T(0.5))
  930. { // 0.05 < z < 0.5
  931. // Maximum Deviation Found: 2.993e-08
  932. // Expected Error Term : 2.993e-08
  933. // Maximum Relative Change in Control Points : 7.555e-04 Y offset : -8.196592331e-01
  934. static const T Y = 8.196592331e-01f;
  935. static const T P[] = {
  936. 1.803388345e-01f,
  937. -4.820256838e-01f,
  938. -1.068349741e+00f,
  939. -3.506624319e-02f,
  940. };
  941. static const T Q[] = {
  942. 1.000000000e+00f,
  943. 2.871703469e+00f,
  944. 1.690949264e+00f,
  945. };
  946. return z * (Y + boost::math::tools::evaluate_polynomial(P, z) / boost::math::tools::evaluate_polynomial(Q, z));
  947. }
  948. else
  949. { // 0.5 < z < 2
  950. // Max error in interpolated form: 1.018e-08
  951. static const T Y = 5.503368378e-01f;
  952. static const T P[] = {
  953. 4.493332766e-01f,
  954. 2.543432707e-01f,
  955. -4.808788799e-01f,
  956. -1.244425316e-01f,
  957. };
  958. static const T Q[] = {
  959. 1.000000000e+00f,
  960. 2.780661241e+00f,
  961. 1.830840318e+00f,
  962. 2.407221031e-01f,
  963. };
  964. return z * (Y + boost::math::tools::evaluate_rational(P, Q, z));
  965. }
  966. }
  967. else if (z < 6)
  968. {
  969. // 2 < z < 6
  970. // Max error in interpolated form: 2.944e-08
  971. static const T Y = 1.162393570e+00f;
  972. static const T P[] = {
  973. -1.144183394e+00f,
  974. -4.712732855e-01f,
  975. 1.563162512e-01f,
  976. 1.434010911e-02f,
  977. };
  978. static const T Q[] = {
  979. 1.000000000e+00f,
  980. 1.192626340e+00f,
  981. 2.295580708e-01f,
  982. 5.477869455e-03f,
  983. };
  984. return Y + boost::math::tools::evaluate_rational(P, Q, z);
  985. }
  986. else if (z < 18)
  987. {
  988. // 6 < z < 18
  989. // Max error in interpolated form: 5.893e-08
  990. static const T Y = 1.809371948e+00f;
  991. static const T P[] = {
  992. -1.689291769e+00f,
  993. -3.337812742e-01f,
  994. 3.151434873e-02f,
  995. 1.134178734e-03f,
  996. };
  997. static const T Q[] = {
  998. 1.000000000e+00f,
  999. 5.716915685e-01f,
  1000. 4.489521292e-02f,
  1001. 4.076716763e-04f,
  1002. };
  1003. return Y + boost::math::tools::evaluate_rational(P, Q, z);
  1004. }
  1005. else if (z < T(9897.12905874)) // 2.8 < log(z) < 9.2
  1006. {
  1007. // Max error in interpolated form: 1.771e-08
  1008. static const T Y = -1.402973175e+00f;
  1009. static const T P[] = {
  1010. 1.966174312e+00f,
  1011. 2.350864728e-01f,
  1012. -5.098074353e-02f,
  1013. -1.054818339e-02f,
  1014. };
  1015. static const T Q[] = {
  1016. 1.000000000e+00f,
  1017. 4.388208264e-01f,
  1018. 8.316639634e-02f,
  1019. 3.397187918e-03f,
  1020. -1.321489743e-05f,
  1021. };
  1022. T log_w = log(z);
  1023. return log_w + Y + boost::math::tools::evaluate_polynomial(P, log_w) / boost::math::tools::evaluate_polynomial(Q, log_w);
  1024. }
  1025. else if (z < T(7.896296e+13)) // 9.2 < log(z) <= 32
  1026. {
  1027. // Max error in interpolated form: 5.821e-08
  1028. static const T Y = -2.735729218e+00f;
  1029. static const T P[] = {
  1030. 3.424903470e+00f,
  1031. 7.525631787e-02f,
  1032. -1.427309584e-02f,
  1033. -1.435974178e-05f,
  1034. };
  1035. static const T Q[] = {
  1036. 1.000000000e+00f,
  1037. 2.514005579e-01f,
  1038. 6.118994652e-03f,
  1039. -1.357889535e-05f,
  1040. 7.312865624e-08f,
  1041. };
  1042. T log_w = log(z);
  1043. return log_w + Y + boost::math::tools::evaluate_polynomial(P, log_w) / boost::math::tools::evaluate_polynomial(Q, log_w);
  1044. }
  1045. // Max error in interpolated form: 1.491e-08
  1046. static const T Y = -4.012863159e+00f;
  1047. static const T P[] = {
  1048. 4.431629226e+00f,
  1049. 2.756690487e-01f,
  1050. -2.992956930e-03f,
  1051. -4.912259384e-05f,
  1052. };
  1053. static const T Q[] = {
  1054. 1.000000000e+00f,
  1055. 2.015434591e-01f,
  1056. 4.949426142e-03f,
  1057. 1.609659944e-05f,
  1058. -5.111523436e-09f,
  1059. };
  1060. T log_w = log(z);
  1061. return log_w + Y + boost::math::tools::evaluate_polynomial(P, log_w) / boost::math::tools::evaluate_polynomial(Q, log_w);
  1062. }
  1063. template <typename T, typename Policy>
  1064. T lambert_w_negative_rational_float(T z, const Policy& pol)
  1065. {
  1066. BOOST_MATH_STD_USING
  1067. if (z > T(-0.27))
  1068. {
  1069. if (z < T(-0.051))
  1070. {
  1071. // -0.27 < z < -0.051
  1072. // Max error in interpolated form: 5.080e-08
  1073. static const T Y = 1.255809784e+00f;
  1074. static const T P[] = {
  1075. -2.558083412e-01f,
  1076. -2.306524098e+00f,
  1077. -5.630887033e+00f,
  1078. -3.803974556e+00f,
  1079. };
  1080. static const T Q[] = {
  1081. 1.000000000e+00f,
  1082. 5.107680783e+00f,
  1083. 7.914062868e+00f,
  1084. 3.501498501e+00f,
  1085. };
  1086. return z * (Y + boost::math::tools::evaluate_rational(P, Q, z));
  1087. }
  1088. else
  1089. {
  1090. // Very small z so use a series function.
  1091. return lambert_w0_small_z(z, pol);
  1092. }
  1093. }
  1094. else if (z > T(-0.3578794411714423215955237701))
  1095. { // Very close to branch singularity.
  1096. // Max error in interpolated form: 5.269e-08
  1097. static const T Y = 1.220928431e-01f;
  1098. static const T P[] = {
  1099. -1.221787446e-01f,
  1100. -6.816155875e+00f,
  1101. 7.144582035e+01f,
  1102. 1.128444390e+03f,
  1103. };
  1104. static const T Q[] = {
  1105. 1.000000000e+00f,
  1106. 6.480326790e+01f,
  1107. 1.869145243e+02f,
  1108. -1.361804274e+03f,
  1109. 1.117826726e+03f,
  1110. };
  1111. T d = z + 0.367879441171442321595523770161460867445811f;
  1112. return -d / (Y + boost::math::tools::evaluate_polynomial(P, d) / boost::math::tools::evaluate_polynomial(Q, d));
  1113. }
  1114. return lambert_w_singularity_series(get_near_singularity_param(z, pol));
  1115. }
  1116. //! Lambert_w0 @b 'float' implementation, selected when T is 32-bit precision.
  1117. template <typename T, typename Policy>
  1118. inline T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 1>&)
  1119. {
  1120. static const char* function = "boost::math::lambert_w0<%1%>"; // For error messages.
  1121. BOOST_MATH_STD_USING // Aid ADL of std functions.
  1122. if ((boost::math::isnan)(z))
  1123. {
  1124. return boost::math::policies::raise_domain_error<T>(function, "Expected a value > -e^-1 (-0.367879...) but got %1%.", z, pol);
  1125. }
  1126. if ((boost::math::isinf)(z))
  1127. {
  1128. return boost::math::policies::raise_overflow_error<T>(function, "Expected a finite value but got %1%.", z, pol);
  1129. }
  1130. if (z >= T(0.05)) // Fukushima switch point.
  1131. // if (z >= 0.045) // 34 terms makes 128-bit 'exact' below 0.045.
  1132. { // Normal ranges using several rational polynomials.
  1133. return lambert_w_positive_rational_float(z);
  1134. }
  1135. else if (z <= -0.3678794411714423215955237701614608674458111310f)
  1136. {
  1137. if (z < -0.3678794411714423215955237701614608674458111310f)
  1138. return boost::math::policies::raise_domain_error<T>(function, "Expected z >= -e^-1 (-0.367879...) but got %1%.", z, pol);
  1139. return -1;
  1140. }
  1141. return lambert_w_negative_rational_float(z, pol);
  1142. } // T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 1>&) for 32-bit usually float.
  1143. template <typename T>
  1144. T lambert_w_positive_rational_double(T z)
  1145. {
  1146. BOOST_MATH_STD_USING
  1147. if (z < 2)
  1148. {
  1149. if (z < 0.5)
  1150. {
  1151. // Max error in interpolated form: 2.255e-17
  1152. static const T offset = 8.19659233093261719e-01;
  1153. static const T P[] = {
  1154. 1.80340766906685177e-01,
  1155. 3.28178241493119307e-01,
  1156. -2.19153620687139706e+00,
  1157. -7.24750929074563990e+00,
  1158. -7.28395876262524204e+00,
  1159. -2.57417169492512916e+00,
  1160. -2.31606948888704503e-01
  1161. };
  1162. static const T Q[] = {
  1163. 1.00000000000000000e+00,
  1164. 7.36482529307436604e+00,
  1165. 2.03686007856430677e+01,
  1166. 2.62864592096657307e+01,
  1167. 1.59742041380858333e+01,
  1168. 4.03760534788374589e+00,
  1169. 2.91327346750475362e-01
  1170. };
  1171. return z * (offset + boost::math::tools::evaluate_polynomial(P, z) / boost::math::tools::evaluate_polynomial(Q, z));
  1172. }
  1173. else
  1174. {
  1175. // Max error in interpolated form: 3.806e-18
  1176. static const T offset = 5.50335884094238281e-01;
  1177. static const T P[] = {
  1178. 4.49664083944098322e-01,
  1179. 1.90417666196776909e+00,
  1180. 1.99951368798255994e+00,
  1181. -6.91217310299270265e-01,
  1182. -1.88533935998617058e+00,
  1183. -7.96743968047750836e-01,
  1184. -1.02891726031055254e-01,
  1185. -3.09156013592636568e-03
  1186. };
  1187. static const T Q[] = {
  1188. 1.00000000000000000e+00,
  1189. 6.45854489419584014e+00,
  1190. 1.54739232422116048e+01,
  1191. 1.72606164253337843e+01,
  1192. 9.29427055609544096e+00,
  1193. 2.29040824649748117e+00,
  1194. 2.21610620995418981e-01,
  1195. 5.70597669908194213e-03
  1196. };
  1197. return z * (offset + boost::math::tools::evaluate_rational(P, Q, z));
  1198. }
  1199. }
  1200. else if (z < 6)
  1201. {
  1202. // 2 < z < 6
  1203. // Max error in interpolated form: 1.216e-17
  1204. static const T Y = 1.16239356994628906e+00;
  1205. static const T P[] = {
  1206. -1.16230494982099475e+00,
  1207. -3.38528144432561136e+00,
  1208. -2.55653717293161565e+00,
  1209. -3.06755172989214189e-01,
  1210. 1.73149743765268289e-01,
  1211. 3.76906042860014206e-02,
  1212. 1.84552217624706666e-03,
  1213. 1.69434126904822116e-05,
  1214. };
  1215. static const T Q[] = {
  1216. 1.00000000000000000e+00,
  1217. 3.77187616711220819e+00,
  1218. 4.58799960260143701e+00,
  1219. 2.24101228462292447e+00,
  1220. 4.54794195426212385e-01,
  1221. 3.60761772095963982e-02,
  1222. 9.25176499518388571e-04,
  1223. 4.43611344705509378e-06,
  1224. };
  1225. return Y + boost::math::tools::evaluate_rational(P, Q, z);
  1226. }
  1227. else if (z < 18)
  1228. {
  1229. // 6 < z < 18
  1230. // Max error in interpolated form: 1.985e-19
  1231. static const T offset = 1.80937194824218750e+00;
  1232. static const T P[] =
  1233. {
  1234. -1.80690935424793635e+00,
  1235. -3.66995929380314602e+00,
  1236. -1.93842957940149781e+00,
  1237. -2.94269984375794040e-01,
  1238. 1.81224710627677778e-03,
  1239. 2.48166798603547447e-03,
  1240. 1.15806592415397245e-04,
  1241. 1.43105573216815533e-06,
  1242. 3.47281483428369604e-09
  1243. };
  1244. static const T Q[] = {
  1245. 1.00000000000000000e+00,
  1246. 2.57319080723908597e+00,
  1247. 1.96724528442680658e+00,
  1248. 5.84501352882650722e-01,
  1249. 7.37152837939206240e-02,
  1250. 3.97368430940416778e-03,
  1251. 8.54941838187085088e-05,
  1252. 6.05713225608426678e-07,
  1253. 8.17517283816615732e-10
  1254. };
  1255. return offset + boost::math::tools::evaluate_rational(P, Q, z);
  1256. }
  1257. else if (z < 9897.12905874) // 2.8 < log(z) < 9.2
  1258. {
  1259. // Max error in interpolated form: 1.195e-18
  1260. static const T Y = -1.40297317504882812e+00;
  1261. static const T P[] = {
  1262. 1.97011826279311924e+00,
  1263. 1.05639945701546704e+00,
  1264. 3.33434529073196304e-01,
  1265. 3.34619153200386816e-02,
  1266. -5.36238353781326675e-03,
  1267. -2.43901294871308604e-03,
  1268. -2.13762095619085404e-04,
  1269. -4.85531936495542274e-06,
  1270. -2.02473518491905386e-08,
  1271. };
  1272. static const T Q[] = {
  1273. 1.00000000000000000e+00,
  1274. 8.60107275833921618e-01,
  1275. 4.10420467985504373e-01,
  1276. 1.18444884081994841e-01,
  1277. 2.16966505556021046e-02,
  1278. 2.24529766630769097e-03,
  1279. 9.82045090226437614e-05,
  1280. 1.36363515125489502e-06,
  1281. 3.44200749053237945e-09,
  1282. };
  1283. T log_w = log(z);
  1284. return log_w + Y + boost::math::tools::evaluate_rational(P, Q, log_w);
  1285. }
  1286. else if (z < 7.896296e+13) // 9.2 < log(z) <= 32
  1287. {
  1288. // Max error in interpolated form: 6.529e-18
  1289. static const T Y = -2.73572921752929688e+00;
  1290. static const T P[] = {
  1291. 3.30547638424076217e+00,
  1292. 1.64050071277550167e+00,
  1293. 4.57149576470736039e-01,
  1294. 4.03821227745424840e-02,
  1295. -4.99664976882514362e-04,
  1296. -1.28527893803052956e-04,
  1297. -2.95470325373338738e-06,
  1298. -1.76662025550202762e-08,
  1299. -1.98721972463709290e-11,
  1300. };
  1301. static const T Q[] = {
  1302. 1.00000000000000000e+00,
  1303. 6.91472559412458759e-01,
  1304. 2.48154578891676774e-01,
  1305. 4.60893578284335263e-02,
  1306. 3.60207838982301946e-03,
  1307. 1.13001153242430471e-04,
  1308. 1.33690948263488455e-06,
  1309. 4.97253225968548872e-09,
  1310. 3.39460723731970550e-12,
  1311. };
  1312. T log_w = log(z);
  1313. return log_w + Y + boost::math::tools::evaluate_rational(P, Q, log_w);
  1314. }
  1315. else if (z < 2.6881171e+43) // 32 < log(z) < 100
  1316. {
  1317. // Max error in interpolated form: 2.015e-18
  1318. static const T Y = -4.01286315917968750e+00;
  1319. static const T P[] = {
  1320. 5.07714858354309672e+00,
  1321. -3.32994414518701458e+00,
  1322. -8.61170416909864451e-01,
  1323. -4.01139705309486142e-02,
  1324. -1.85374201771834585e-04,
  1325. 1.08824145844270666e-05,
  1326. 1.17216905810452396e-07,
  1327. 2.97998248101385990e-10,
  1328. 1.42294856434176682e-13,
  1329. };
  1330. static const T Q[] = {
  1331. 1.00000000000000000e+00,
  1332. -4.85840770639861485e-01,
  1333. -3.18714850604827580e-01,
  1334. -3.20966129264610534e-02,
  1335. -1.06276178044267895e-03,
  1336. -1.33597828642644955e-05,
  1337. -6.27900905346219472e-08,
  1338. -9.35271498075378319e-11,
  1339. -2.60648331090076845e-14,
  1340. };
  1341. T log_w = log(z);
  1342. return log_w + Y + boost::math::tools::evaluate_rational(P, Q, log_w);
  1343. }
  1344. else // 100 < log(z) < 710
  1345. {
  1346. // Max error in interpolated form: 5.277e-18
  1347. static const T Y = -5.70115661621093750e+00;
  1348. static const T P[] = {
  1349. 6.42275660145116698e+00,
  1350. 1.33047964073367945e+00,
  1351. 6.72008923401652816e-02,
  1352. 1.16444069958125895e-03,
  1353. 7.06966760237470501e-06,
  1354. 5.48974896149039165e-09,
  1355. -7.00379652018853621e-11,
  1356. -1.89247635913659556e-13,
  1357. -1.55898770790170598e-16,
  1358. -4.06109208815303157e-20,
  1359. -2.21552699006496737e-24,
  1360. };
  1361. static const T Q[] = {
  1362. 1.00000000000000000e+00,
  1363. 3.34498588416632854e-01,
  1364. 2.51519862456384983e-02,
  1365. 6.81223810622416254e-04,
  1366. 7.94450897106903537e-06,
  1367. 4.30675039872881342e-08,
  1368. 1.10667669458467617e-10,
  1369. 1.31012240694192289e-13,
  1370. 6.53282047177727125e-17,
  1371. 1.11775518708172009e-20,
  1372. 3.78250395617836059e-25,
  1373. };
  1374. T log_w = log(z);
  1375. return log_w + Y + boost::math::tools::evaluate_rational(P, Q, log_w);
  1376. }
  1377. }
  1378. template <typename T, typename Policy>
  1379. T lambert_w_negative_rational_double(T z, const Policy& pol)
  1380. {
  1381. BOOST_MATH_STD_USING
  1382. if (z > -0.1)
  1383. {
  1384. if (z < -0.051)
  1385. {
  1386. // -0.1 < z < -0.051
  1387. // Maximum Deviation Found: 4.402e-22
  1388. // Expected Error Term : 4.240e-22
  1389. // Maximum Relative Change in Control Points : 4.115e-03
  1390. static const T Y = 1.08633995056152344e+00;
  1391. static const T P[] = {
  1392. -8.63399505615014331e-02,
  1393. -1.64303871814816464e+00,
  1394. -7.71247913918273738e+00,
  1395. -1.41014495545382454e+01,
  1396. -1.02269079949257616e+01,
  1397. -2.17236002836306691e+00,
  1398. };
  1399. static const T Q[] = {
  1400. 1.00000000000000000e+00,
  1401. 7.44775406945739243e+00,
  1402. 2.04392643087266541e+01,
  1403. 2.51001961077774193e+01,
  1404. 1.31256080849023319e+01,
  1405. 2.11640324843601588e+00,
  1406. };
  1407. return z * (Y + boost::math::tools::evaluate_rational(P, Q, z));
  1408. }
  1409. else
  1410. {
  1411. // Very small z > 0.051:
  1412. return lambert_w0_small_z(z, pol);
  1413. }
  1414. }
  1415. else if (z > -0.2)
  1416. {
  1417. // -0.2 < z < -0.1
  1418. // Maximum Deviation Found: 2.898e-20
  1419. // Expected Error Term : 2.873e-20
  1420. // Maximum Relative Change in Control Points : 3.779e-04
  1421. static const T Y = 1.20359611511230469e+00;
  1422. static const T P[] = {
  1423. -2.03596115108465635e-01,
  1424. -2.95029082937201859e+00,
  1425. -1.54287922188671648e+01,
  1426. -3.81185809571116965e+01,
  1427. -4.66384358235575985e+01,
  1428. -2.59282069989642468e+01,
  1429. -4.70140451266553279e+00,
  1430. };
  1431. static const T Q[] = {
  1432. 1.00000000000000000e+00,
  1433. 9.57921436074599929e+00,
  1434. 3.60988119290234377e+01,
  1435. 6.73977699505546007e+01,
  1436. 6.41104992068148823e+01,
  1437. 2.82060127225153607e+01,
  1438. 4.10677610657724330e+00,
  1439. };
  1440. return z * (Y + boost::math::tools::evaluate_rational(P, Q, z));
  1441. }
  1442. else if (z > -0.3178794411714423215955237)
  1443. {
  1444. // Max error in interpolated form: 6.996e-18
  1445. static const T Y = 3.49680423736572266e-01;
  1446. static const T P[] = {
  1447. -3.49729841718749014e-01,
  1448. -6.28207407760709028e+01,
  1449. -2.57226178029669171e+03,
  1450. -2.50271008623093747e+04,
  1451. 1.11949239154711388e+05,
  1452. 1.85684566607844318e+06,
  1453. 4.80802490427638643e+06,
  1454. 2.76624752134636406e+06,
  1455. };
  1456. static const T Q[] = {
  1457. 1.00000000000000000e+00,
  1458. 1.82717661215113000e+02,
  1459. 8.00121119810280100e+03,
  1460. 1.06073266717010129e+05,
  1461. 3.22848993926057721e+05,
  1462. -8.05684814514171256e+05,
  1463. -2.59223192927265737e+06,
  1464. -5.61719645211570871e+05,
  1465. 6.27765369292636844e+04,
  1466. };
  1467. T d = z + 0.367879441171442321595523770161460867445811;
  1468. return -d / (Y + boost::math::tools::evaluate_polynomial(P, d) / boost::math::tools::evaluate_polynomial(Q, d));
  1469. }
  1470. else if (z > -0.3578794411714423215955237701)
  1471. {
  1472. // Max error in interpolated form: 1.404e-17
  1473. static const T Y = 5.00126481056213379e-02;
  1474. static const T P[] = {
  1475. -5.00173570682372162e-02,
  1476. -4.44242461870072044e+01,
  1477. -9.51185533619946042e+03,
  1478. -5.88605699015429386e+05,
  1479. -1.90760843597427751e+06,
  1480. 5.79797663818311404e+08,
  1481. 1.11383352508459134e+10,
  1482. 5.67791253678716467e+10,
  1483. 6.32694500716584572e+10,
  1484. };
  1485. static const T Q[] = {
  1486. 1.00000000000000000e+00,
  1487. 9.08910517489981551e+02,
  1488. 2.10170163753340133e+05,
  1489. 1.67858612416470327e+07,
  1490. 4.90435561733227953e+08,
  1491. 4.54978142622939917e+09,
  1492. 2.87716585708739168e+09,
  1493. -4.59414247951143131e+10,
  1494. -1.72845216404874299e+10,
  1495. };
  1496. T d = z + 0.36787944117144232159552377016146086744581113103176804;
  1497. return -d / (Y + boost::math::tools::evaluate_polynomial(P, d) / boost::math::tools::evaluate_polynomial(Q, d));
  1498. }
  1499. else
  1500. { // z is very close (within 0.01) of the singularity at -e^-1,
  1501. // so use a series expansion from R. M. Corless et al.
  1502. const T p2 = 2 * (boost::math::constants::e<T>() * z + 1);
  1503. const T p = sqrt(p2);
  1504. return lambert_w_detail::lambert_w_singularity_series(p);
  1505. }
  1506. }
  1507. //! Lambert_w0 @b 'double' implementation, selected when T is 64-bit precision.
  1508. template <typename T, typename Policy>
  1509. inline T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 2>&)
  1510. {
  1511. static const char* function = "boost::math::lambert_w0<%1%>";
  1512. BOOST_MATH_STD_USING // Aid ADL of std functions.
  1513. // Detect unusual case of 32-bit double with a wider/64-bit long double
  1514. static_assert(std::numeric_limits<double>::digits >= 53,
  1515. "Our double precision coefficients will be truncated, "
  1516. "please file a bug report with details of your platform's floating point types "
  1517. "- or possibly edit the coefficients to have "
  1518. "an appropriate size-suffix for 64-bit floats on your platform - L?");
  1519. if ((boost::math::isnan)(z))
  1520. {
  1521. return boost::math::policies::raise_domain_error<T>(function, "Expected a value > -e^-1 (-0.367879...) but got %1%.", z, pol);
  1522. }
  1523. if ((boost::math::isinf)(z))
  1524. {
  1525. return boost::math::policies::raise_overflow_error<T>(function, "Expected a finite value but got %1%.", z, pol);
  1526. }
  1527. if (z >= 0.05)
  1528. {
  1529. return lambert_w_positive_rational_double(z);
  1530. }
  1531. else if (z <= -0.36787944117144232159552377016146086744581113103176804) // Precision is max_digits10(cpp_bin_float_50).
  1532. {
  1533. if (z < -0.36787944117144232159552377016146086744581113103176804)
  1534. {
  1535. return boost::math::policies::raise_domain_error<T>(function, "Expected z >= -e^-1 (-0.367879...) but got %1%.", z, pol);
  1536. }
  1537. return -1;
  1538. }
  1539. else
  1540. {
  1541. return lambert_w_negative_rational_double(z, pol);
  1542. }
  1543. } // T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 2>&) 64-bit precision, usually double.
  1544. //! lambert_W0 implementation for extended precision types including
  1545. //! long double (80-bit and 128-bit), ???
  1546. //! quad float128, Boost.Multiprecision types like cpp_bin_float_quad, cpp_bin_float_50...
  1547. template <typename T, typename Policy>
  1548. inline T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 0>&)
  1549. {
  1550. static const char* function = "boost::math::lambert_w0<%1%>";
  1551. BOOST_MATH_STD_USING // Aid ADL of std functions.
  1552. // Filter out special cases first:
  1553. if ((boost::math::isnan)(z))
  1554. {
  1555. return boost::math::policies::raise_domain_error<T>(function, "Expected z >= -e^-1 (-0.367879...) but got %1%.", z, pol);
  1556. }
  1557. if (fabs(z) <= 0.05f)
  1558. {
  1559. // Very small z:
  1560. return lambert_w0_small_z(z, pol);
  1561. }
  1562. if (z > (std::numeric_limits<double>::max)())
  1563. {
  1564. if ((boost::math::isinf)(z))
  1565. {
  1566. return policies::raise_overflow_error<T>(function, nullptr, pol);
  1567. // Or might return infinity if available else max_value,
  1568. // but other Boost.Math special functions raise overflow.
  1569. }
  1570. // z is larger than the largest double, so cannot use the polynomial to get an approximation,
  1571. // so use the asymptotic approximation and Halley iterate:
  1572. T w = lambert_w0_approx(z); // Make an inline function as also used elsewhere.
  1573. //T lz = log(z);
  1574. //T llz = log(lz);
  1575. //T w = lz - llz + (llz / lz); // Corless equation 4.19, page 349, and Chapeau-Blondeau equation 20, page 2162.
  1576. return lambert_w_halley_iterate(w, z);
  1577. }
  1578. if (z < -0.3578794411714423215955237701)
  1579. { // Very close to branch point so rational polynomials are not usable.
  1580. if (z <= -boost::math::constants::exp_minus_one<T>())
  1581. {
  1582. if (z == -boost::math::constants::exp_minus_one<T>())
  1583. { // Exactly at the branch point singularity.
  1584. return -1;
  1585. }
  1586. return boost::math::policies::raise_domain_error<T>(function, "Expected z >= -e^-1 (-0.367879...) but got %1%.", z, pol);
  1587. }
  1588. // z is very close (within 0.01) of the branch singularity at -e^-1
  1589. // so use a series approximation proposed by Corless et al.
  1590. const T p2 = 2 * (boost::math::constants::e<T>() * z + 1);
  1591. const T p = sqrt(p2);
  1592. T w = lambert_w_detail::lambert_w_singularity_series(p);
  1593. return lambert_w_halley_iterate(w, z);
  1594. }
  1595. // Phew! If we get here we are in the normal range of the function,
  1596. // so get a double precision approximation first, then iterate to full precision of T.
  1597. // We define a tag_type that is:
  1598. // true_type if there are so many digits precision wanted that iteration is necessary.
  1599. // false_type if a single Halley step is sufficient.
  1600. using precision_type = typename policies::precision<T, Policy>::type;
  1601. using tag_type = std::integral_constant<bool,
  1602. (precision_type::value == 0) || (precision_type::value > 113) ?
  1603. true // Unknown at compile-time, variable/arbitrary, or more than float128 or cpp_bin_quad 128-bit precision.
  1604. : false // float, double, float128, cpp_bin_quad 128-bit, so single Halley step.
  1605. >;
  1606. // For speed, we also cast z to type double when that is possible
  1607. // if (std::is_constructible<double, T>() == true).
  1608. T w = lambert_w0_imp(maybe_reduce_to_double(z, std::is_constructible<double, T>()), pol, std::integral_constant<int, 2>());
  1609. return lambert_w_maybe_halley_iterate(w, z, tag_type());
  1610. } // T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 0>&) all extended precision types.
  1611. // Lambert w-1 implementation
  1612. // ==============================================================================================
  1613. //! Lambert W for W-1 branch, -max(z) < z <= -1/e.
  1614. // TODO is -max(z) allowed?
  1615. template<typename T, typename Policy>
  1616. T lambert_wm1_imp(const T z, const Policy& pol)
  1617. {
  1618. // Catch providing an integer value as parameter x to lambert_w, for example, lambert_w(1).
  1619. // Need to ensure it is a floating-point type (of the desired type, float 1.F, double 1., or long double 1.L),
  1620. // or static_casted integer, for example: static_cast<float>(1) or static_cast<cpp_dec_float_50>(1).
  1621. // Want to allow fixed_point types too, so do not just test for floating-point.
  1622. // Integral types should be promoted to double by user Lambert w functions.
  1623. // If integral type provided to user function lambert_w0 or lambert_wm1,
  1624. // then should already have been promoted to double.
  1625. static_assert(!std::is_integral<T>::value,
  1626. "Must be floating-point or fixed type (not integer type), for example: lambert_wm1(1.), not lambert_wm1(1)!");
  1627. BOOST_MATH_STD_USING // Aid argument dependent lookup (ADL) of abs.
  1628. const char* function = "boost::math::lambert_wm1<RealType>(<RealType>)"; // Used for error messages.
  1629. // Check for edge and corner cases first:
  1630. if ((boost::math::isnan)(z))
  1631. {
  1632. return policies::raise_domain_error(function,
  1633. "Argument z is NaN!",
  1634. z, pol);
  1635. } // isnan
  1636. if ((boost::math::isinf)(z))
  1637. {
  1638. return policies::raise_domain_error(function,
  1639. "Argument z is infinite!",
  1640. z, pol);
  1641. } // isinf
  1642. if (z == static_cast<T>(0))
  1643. { // z is exactly zero so return -std::numeric_limits<T>::infinity();
  1644. if (std::numeric_limits<T>::has_infinity)
  1645. {
  1646. return -std::numeric_limits<T>::infinity();
  1647. }
  1648. else
  1649. {
  1650. return -tools::max_value<T>();
  1651. }
  1652. }
  1653. if (boost::math::detail::has_denorm_now<T>())
  1654. { // All real types except arbitrary precision.
  1655. if (!(boost::math::isnormal)(z))
  1656. { // Almost zero - might also just return infinity like z == 0 or max_value?
  1657. return policies::raise_overflow_error(function,
  1658. "Argument z = %1% is denormalized! (must be z > (std::numeric_limits<RealType>::min)() or z == 0)",
  1659. z, pol);
  1660. }
  1661. }
  1662. if (z > static_cast<T>(0))
  1663. { //
  1664. return policies::raise_domain_error(function,
  1665. "Argument z = %1% is out of range (z <= 0) for Lambert W-1 branch! (Try Lambert W0 branch?)",
  1666. z, pol);
  1667. }
  1668. if (z > -boost::math::tools::min_value<T>())
  1669. { // z is denormalized, so cannot be computed.
  1670. // -std::numeric_limits<T>::min() is smallest for type T,
  1671. // for example, for double: lambert_wm1(-2.2250738585072014e-308) = -714.96865723796634
  1672. return policies::raise_overflow_error(function,
  1673. "Argument z = %1% is too small (z < -std::numeric_limits<T>::min so denormalized) for Lambert W-1 branch!",
  1674. z, pol);
  1675. }
  1676. if (z == -boost::math::constants::exp_minus_one<T>()) // == singularity/branch point z = -exp(-1) = -0.36787944.
  1677. { // At singularity, so return exactly -1.
  1678. return -static_cast<T>(1);
  1679. }
  1680. // z is too negative for the W-1 (or W0) branch.
  1681. if (z < -boost::math::constants::exp_minus_one<T>()) // > singularity/branch point z = -exp(-1) = -0.36787944.
  1682. {
  1683. return policies::raise_domain_error(function,
  1684. "Argument z = %1% is out of range (z < -exp(-1) = -3.6787944... <= 0) for Lambert W-1 (or W0) branch!",
  1685. z, pol);
  1686. }
  1687. if (z < static_cast<T>(-0.35))
  1688. { // Close to singularity/branch point z = -0.3678794411714423215955237701614608727 but on W-1 branch.
  1689. const T p2 = 2 * (boost::math::constants::e<T>() * z + 1);
  1690. if (p2 == 0)
  1691. { // At the singularity at branch point.
  1692. return -1;
  1693. }
  1694. if (p2 > 0)
  1695. {
  1696. T w_series = lambert_w_singularity_series(T(-sqrt(p2)));
  1697. if (boost::math::tools::digits<T>() > 53)
  1698. { // Multiprecision, so try a Halley refinement.
  1699. w_series = lambert_w_detail::lambert_w_halley_iterate(w_series, z);
  1700. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1_NOT_BUILTIN
  1701. std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
  1702. std::cout << "Lambert W-1 Halley updated to " << w_series << std::endl;
  1703. std::cout.precision(saved_precision);
  1704. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_WM1_NOT_BUILTIN
  1705. }
  1706. return w_series;
  1707. }
  1708. // Should not get here.
  1709. return policies::raise_domain_error(function,
  1710. "Argument z = %1% is out of range for Lambert W-1 branch. (Should not get here - please report!)",
  1711. z, pol);
  1712. } // if (z < -0.35)
  1713. using lambert_w_lookup::wm1es;
  1714. using lambert_w_lookup::wm1zs;
  1715. using lambert_w_lookup::noof_wm1zs; // size == 64
  1716. // std::cout <<" Wm1zs[63] (== G[64]) = " << " " << wm1zs[63] << std::endl; // Wm1zs[63] (== G[64]) = -1.0264389699511283e-26
  1717. // Check that z argument value is not smaller than lookup_table G[64]
  1718. // std::cout << "(z > wm1zs[63]) = " << std::boolalpha << (z > wm1zs[63]) << std::endl;
  1719. if (z >= T(wm1zs[63])) // wm1zs[63] = -1.0264389699511282259046957018510946438e-26L W = 64.00000000000000000
  1720. { // z >= -1.0264389699511303e-26 (but z != 0 and z >= std::numeric_limits<T>::min() and so NOT denormalized).
  1721. // Some info on Lambert W-1 values for extreme values of z.
  1722. // std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
  1723. // std::cout << "-std::numeric_limits<float>::min() = " << -(std::numeric_limits<float>::min)() << std::endl;
  1724. // std::cout << "-std::numeric_limits<double>::min() = " << -(std::numeric_limits<double>::min)() << std::endl;
  1725. // -std::numeric_limits<float>::min() = -1.1754943508222875e-38
  1726. // -std::numeric_limits<double>::min() = -2.2250738585072014e-308
  1727. // N[productlog(-1, -1.1754943508222875 * 10^-38 ), 50] = -91.856775324595479509567756730093823993834155027858
  1728. // N[productlog(-1, -2.2250738585072014e-308 * 10^-308 ), 50] = -1424.8544521230553853558132180518404363617968042942
  1729. // N[productlog(-1, -1.4325445274604020119111357113179868158* 10^-27), 37] = -65.99999999999999999999999999999999955
  1730. // R.M.Corless, G.H.Gonnet, D.E.G.Hare, D.J.Jeffrey, and D.E.Knuth,
  1731. // On the Lambert W function, Adv.Comput.Math., vol. 5, pp. 329, 1996.
  1732. // Francois Chapeau-Blondeau and Abdelilah Monir
  1733. // Numerical Evaluation of the Lambert W Function
  1734. // IEEE Transactions On Signal Processing, VOL. 50, NO. 9, Sep 2002
  1735. // https://pdfs.semanticscholar.org/7a5a/76a9369586dd0dd34dda156d8f2779d1fd59.pdf
  1736. // Estimate Lambert W using ln(-z) ...
  1737. // This is roughly the power of ten * ln(10) ~= 2.3. n ~= 10^n
  1738. // and improve by adding a second term -ln(ln(-z))
  1739. T guess; // bisect lowest possible Gk[=64] (for lookup_t type)
  1740. T lz = log(-z);
  1741. T llz = log(-lz);
  1742. guess = lz - llz + (llz / lz); // Chapeau-Blondeau equation 20, page 2162.
  1743. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1_TINY
  1744. std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
  1745. std::cout << "z = " << z << ", guess = " << guess << ", ln(-z) = " << lz << ", ln(-ln(-z) = " << llz << ", llz/lz = " << (llz / lz) << std::endl;
  1746. // z = -1.0000000000000001e-30, guess = -73.312782616731482, ln(-z) = -69.077552789821368, ln(-ln(-z) = 4.2352298269101114, llz/lz = -0.061311231447304194
  1747. // z = -9.9999999999999999e-91, guess = -212.56650048504233, ln(-z) = -207.23265836946410, ln(-ln(-z) = 5.3338421155782205, llz/lz = -0.025738424423764311
  1748. // >z = -2.2250738585072014e-308, guess = -714.95942238244606, ln(-z) = -708.39641853226408, ln(-ln(-z) = 6.5630038501819854, llz/lz = -0.0092645920821846622
  1749. int d10 = policies::digits_base10<T, Policy>(); // policy template parameter digits10
  1750. int d2 = policies::digits<T, Policy>(); // digits base 2 from policy.
  1751. std::cout << "digits10 = " << d10 << ", digits2 = " << d2 // For example: digits10 = 1, digits2 = 5
  1752. << std::endl;
  1753. std::cout.precision(saved_precision);
  1754. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_WM1_TINY
  1755. if (policies::digits<T, Policy>() < 12)
  1756. { // For the worst case near w = 64, the error in the 'guess' is ~0.008, ratio ~ 0.0001 or 1 in 10,000 digits 10 ~= 4, or digits2 ~= 12.
  1757. return guess;
  1758. }
  1759. T result = lambert_w_detail::lambert_w_halley_iterate(guess, z);
  1760. return result;
  1761. // Was Fukushima
  1762. // G[k=64] == g[63] == -1.02643897e-26
  1763. //return policies::raise_domain_error(function,
  1764. // "Argument z = %1% is too small (< -1.02643897e-26) ! (Should not occur, please report.",
  1765. // z, pol);
  1766. } // Z too small so use approximation and Halley.
  1767. // Else Use a lookup table to find the nearest integer part of Lambert W-1 as starting point for Bisection.
  1768. if (boost::math::tools::digits<T>() > 53)
  1769. { // T is more precise than 64-bit double (or long double, or ?),
  1770. // so compute an approximate value using only one Schroeder refinement,
  1771. // (avoiding any double-precision Halley refinement from policy double_digits2<50> 53 - 3 = 50
  1772. // because are next going to use Halley refinement at full/high precision using this as an approximation).
  1773. using boost::math::policies::precision;
  1774. using boost::math::policies::digits10;
  1775. using boost::math::policies::digits2;
  1776. using boost::math::policies::policy;
  1777. // Compute a 50-bit precision approximate W0 in a double (no Halley refinement).
  1778. T double_approx(static_cast<T>(lambert_wm1_imp(must_reduce_to_double(z, std::is_constructible<double, T>()), policy<digits2<50>>())));
  1779. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1_NOT_BUILTIN
  1780. std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
  1781. std::cout << "Lambert_wm1 Argument Type " << typeid(T).name() << " approximation double = " << double_approx << std::endl;
  1782. std::cout.precision(saved_precision);
  1783. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_WM1
  1784. // Perform additional Halley refinement(s) to ensure that
  1785. // get a near as possible to correct result (usually +/- one epsilon).
  1786. T result = lambert_w_halley_iterate(double_approx, z);
  1787. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1
  1788. std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
  1789. std::cout << "Result " << typeid(T).name() << " precision Halley refinement = " << result << std::endl;
  1790. std::cout.precision(saved_precision);
  1791. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_WM1
  1792. return result;
  1793. } // digits > 53 - higher precision than double.
  1794. else // T is double or less precision.
  1795. { // Use a lookup table to find the nearest integer part of Lambert W as starting point for Bisection.
  1796. using namespace boost::math::lambert_w_detail::lambert_w_lookup;
  1797. // Bracketing sequence n = (2, 4, 8, 16, 32, 64) for W-1 branch. (0 is -infinity)
  1798. // Since z is probably quite small, start with lowest n (=2).
  1799. int n = 2;
  1800. if (T(wm1zs[n - 1]) > z)
  1801. {
  1802. goto bisect;
  1803. }
  1804. for (int j = 1; j <= 5; ++j)
  1805. {
  1806. n *= 2;
  1807. if (T(wm1zs[n - 1]) > z)
  1808. {
  1809. goto overshot;
  1810. }
  1811. }
  1812. // else z < g[63] == -1.0264389699511303e-26, so Lambert W-1 integer part > 64.
  1813. // This should not now occur (should be caught by test and code above) so should be a logic_error?
  1814. return policies::raise_domain_error(function,
  1815. "Argument z = %1% is too small (< -1.026439e-26) (logic error - please report!)",
  1816. z, pol);
  1817. overshot:
  1818. {
  1819. int nh = n / 2;
  1820. for (int j = 1; j <= 5; ++j)
  1821. {
  1822. nh /= 2; // halve step size.
  1823. if (nh <= 0)
  1824. {
  1825. break; // goto bisect;
  1826. }
  1827. if (T(wm1zs[n - nh - 1]) > z)
  1828. {
  1829. n -= nh;
  1830. }
  1831. }
  1832. }
  1833. bisect:
  1834. --n;
  1835. // g[n] now holds lambert W of floor integer n and g[n+1] the ceil part;
  1836. // these are used as initial values for bisection.
  1837. #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1_LOOKUP
  1838. std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
  1839. std::cout << "Result lookup W-1(" << z << ") bisection between wm1zs[" << n - 1 << "] = " << wm1zs[n - 1] << " and wm1zs[" << n << "] = " << wm1zs[n]
  1840. << ", bisect mean = " << (wm1zs[n - 1] + wm1zs[n]) / 2 << std::endl;
  1841. std::cout.precision(saved_precision);
  1842. #endif // BOOST_MATH_INSTRUMENT_LAMBERT_WM1_LOOKUP
  1843. // Compute bisections is the number of bisections computed from n,
  1844. // such that a single application of the fifth-order Schroeder update formula
  1845. // after the bisections is enough to evaluate Lambert W-1 with (near?) 53-bit accuracy.
  1846. // Fukushima established these by trial and error?
  1847. int bisections = 11; // Assume maximum number of bisections will be needed (most common case).
  1848. if (n >= 8)
  1849. {
  1850. bisections = 8;
  1851. }
  1852. else if (n >= 3)
  1853. {
  1854. bisections = 9;
  1855. }
  1856. else if (n >= 2)
  1857. {
  1858. bisections = 10;
  1859. }
  1860. // Bracketing, Fukushima section 2.3, page 82:
  1861. // (Avoiding using exponential function for speed).
  1862. // Only use @c lookup_t precision, default double, for bisection (again for speed),
  1863. // and use later Halley refinement for higher precisions.
  1864. using lambert_w_lookup::halves;
  1865. using lambert_w_lookup::sqrtwm1s;
  1866. using calc_type = typename std::conditional<std::is_constructible<lookup_t, T>::value, lookup_t, T>::type;
  1867. calc_type w = -static_cast<calc_type>(n); // Equation 25,
  1868. calc_type y = static_cast<calc_type>(z * T(wm1es[n - 1])); // Equation 26,
  1869. // Perform the bisections fractional bisections for necessary precision.
  1870. for (int j = 0; j < bisections; ++j)
  1871. { // Equation 27.
  1872. calc_type wj = w - halves[j]; // Subtract 1/2, 1/4, 1/8 ...
  1873. calc_type yj = y * sqrtwm1s[j]; // Multiply by sqrt(1/e), ...
  1874. if (wj < yj)
  1875. {
  1876. w = wj;
  1877. y = yj;
  1878. }
  1879. } // for j
  1880. return static_cast<T>(schroeder_update(w, y)); // Schroeder 5th order method refinement.
  1881. // else // Perform additional Halley refinement(s) to ensure that
  1882. // // get a near as possible to correct result (usually +/- epsilon).
  1883. // {
  1884. // // result = lambert_w_halley_iterate(result, z);
  1885. // result = lambert_w_halley_step(result, z); // Just one Halley step should be enough.
  1886. //#ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1_HALLEY
  1887. // std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
  1888. // std::cout << "Halley refinement estimate = " << result << std::endl;
  1889. // std::cout.precision(saved_precision);
  1890. //#endif // BOOST_MATH_INSTRUMENT_LAMBERT_W1_HALLEY
  1891. // return result; // Halley
  1892. // } // Schroeder or Schroeder and Halley.
  1893. }
  1894. } // template<typename T = double> T lambert_wm1_imp(const T z)
  1895. } // namespace lambert_w_detail
  1896. ///////////////////////////// User Lambert w functions. //////////////////////////////
  1897. //! Lambert W0 using User-defined policy.
  1898. template <typename T, typename Policy>
  1899. inline
  1900. typename boost::math::tools::promote_args<T>::type
  1901. lambert_w0(T z, const Policy& pol)
  1902. {
  1903. // Promote integer or expression template arguments to double,
  1904. // without doing any other internal promotion like float to double.
  1905. using result_type = typename tools::promote_args<T>::type;
  1906. // Work out what precision has been selected,
  1907. // based on the Policy and the number type.
  1908. using precision_type = typename policies::precision<result_type, Policy>::type;
  1909. // and then select the correct implementation based on that precision (not the type T):
  1910. using tag_type = std::integral_constant<int,
  1911. (precision_type::value == 0) || (precision_type::value > 53) ?
  1912. 0 // either variable precision (0), or greater than 64-bit precision.
  1913. : (precision_type::value <= 24) ? 1 // 32-bit (probably float) precision.
  1914. : 2 // 64-bit (probably double) precision.
  1915. >;
  1916. return lambert_w_detail::lambert_w0_imp(result_type(z), pol, tag_type()); //
  1917. } // lambert_w0(T z, const Policy& pol)
  1918. //! Lambert W0 using default policy.
  1919. template <typename T>
  1920. inline
  1921. typename tools::promote_args<T>::type
  1922. lambert_w0(T z)
  1923. {
  1924. // Promote integer or expression template arguments to double,
  1925. // without doing any other internal promotion like float to double.
  1926. using result_type = typename tools::promote_args<T>::type;
  1927. // Work out what precision has been selected, based on the Policy and the number type.
  1928. // For the default policy version, we want the *default policy* precision for T.
  1929. using precision_type = typename policies::precision<result_type, policies::policy<>>::type;
  1930. // and then select the correct implementation based on that (not the type T):
  1931. using tag_type = std::integral_constant<int,
  1932. (precision_type::value == 0) || (precision_type::value > 53) ?
  1933. 0 // either variable precision (0), or greater than 64-bit precision.
  1934. : (precision_type::value <= 24) ? 1 // 32-bit (probably float) precision.
  1935. : 2 // 64-bit (probably double) precision.
  1936. >;
  1937. return lambert_w_detail::lambert_w0_imp(result_type(z), policies::policy<>(), tag_type());
  1938. } // lambert_w0(T z) using default policy.
  1939. //! W-1 branch (-max(z) < z <= -1/e).
  1940. //! Lambert W-1 using User-defined policy.
  1941. template <typename T, typename Policy>
  1942. inline
  1943. typename tools::promote_args<T>::type
  1944. lambert_wm1(T z, const Policy& pol)
  1945. {
  1946. // Promote integer or expression template arguments to double,
  1947. // without doing any other internal promotion like float to double.
  1948. using result_type = typename tools::promote_args<T>::type;
  1949. return lambert_w_detail::lambert_wm1_imp(result_type(z), pol); //
  1950. }
  1951. //! Lambert W-1 using default policy.
  1952. template <typename T>
  1953. inline
  1954. typename tools::promote_args<T>::type
  1955. lambert_wm1(T z)
  1956. {
  1957. using result_type = typename tools::promote_args<T>::type;
  1958. return lambert_w_detail::lambert_wm1_imp(result_type(z), policies::policy<>());
  1959. } // lambert_wm1(T z)
  1960. // First derivative of Lambert W0 and W-1.
  1961. template <typename T, typename Policy>
  1962. inline typename tools::promote_args<T>::type
  1963. lambert_w0_prime(T z, const Policy& pol)
  1964. {
  1965. using result_type = typename tools::promote_args<T>::type;
  1966. using std::numeric_limits;
  1967. if (z == 0)
  1968. {
  1969. return static_cast<result_type>(1);
  1970. }
  1971. // This is the sensible choice if we regard the Lambert-W function as complex analytic.
  1972. // Of course on the real line, it's just undefined.
  1973. if (z == - boost::math::constants::exp_minus_one<result_type>())
  1974. {
  1975. return numeric_limits<result_type>::has_infinity ? numeric_limits<result_type>::infinity() : boost::math::tools::max_value<result_type>();
  1976. }
  1977. // if z < -1/e, we'll let lambert_w0 do the error handling:
  1978. result_type w = lambert_w0(result_type(z), pol);
  1979. // If w ~ -1, then presumably this can get inaccurate.
  1980. // Is there an accurate way to evaluate 1 + W(-1/e + eps)?
  1981. // Yes: This is discussed in the Princeton Companion to Applied Mathematics,
  1982. // 'The Lambert-W function', Section 1.3: Series and Generating Functions.
  1983. // 1 + W(-1/e + x) ~ sqrt(2ex).
  1984. // Nick is not convinced this formula is more accurate than the naive one.
  1985. // However, for z != -1/e, we never get rounded to w = -1 in any precision I've tested (up to cpp_bin_float_100).
  1986. return w / (z * (1 + w));
  1987. } // lambert_w0_prime(T z)
  1988. template <typename T>
  1989. inline typename tools::promote_args<T>::type
  1990. lambert_w0_prime(T z)
  1991. {
  1992. return lambert_w0_prime(z, policies::policy<>());
  1993. }
  1994. template <typename T, typename Policy>
  1995. inline typename tools::promote_args<T>::type
  1996. lambert_wm1_prime(T z, const Policy& pol)
  1997. {
  1998. using std::numeric_limits;
  1999. using result_type = typename tools::promote_args<T>::type;
  2000. //if (z == 0)
  2001. //{
  2002. // return static_cast<result_type>(1);
  2003. //}
  2004. //if (z == - boost::math::constants::exp_minus_one<result_type>())
  2005. if (z == 0 || z == - boost::math::constants::exp_minus_one<result_type>())
  2006. {
  2007. return numeric_limits<result_type>::has_infinity ? -numeric_limits<result_type>::infinity() : -boost::math::tools::max_value<result_type>();
  2008. }
  2009. result_type w = lambert_wm1(z, pol);
  2010. return w/(z*(1+w));
  2011. } // lambert_wm1_prime(T z)
  2012. template <typename T>
  2013. inline typename tools::promote_args<T>::type
  2014. lambert_wm1_prime(T z)
  2015. {
  2016. return lambert_wm1_prime(z, policies::policy<>());
  2017. }
  2018. }} //boost::math namespaces
  2019. #endif // #ifdef BOOST_MATH_SF_LAMBERT_W_HPP