lambert_w.hpp 94 KB

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