Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:16

0001 // Copyright John Maddock 2017.
0002 // Copyright Paul A. Bristow 2016, 2017, 2018.
0003 // Copyright Nicholas Thompson 2018
0004 
0005 // Distributed under the Boost Software License, Version 1.0.
0006 // (See accompanying file LICENSE_1_0.txt or
0007 //  copy at http ://www.boost.org/LICENSE_1_0.txt).
0008 
0009 #ifndef BOOST_MATH_SF_LAMBERT_W_HPP
0010 #define BOOST_MATH_SF_LAMBERT_W_HPP
0011 
0012 #ifdef _MSC_VER
0013 #pragma warning(disable : 4127)
0014 #endif
0015 
0016 /*
0017 Implementation of an algorithm for the Lambert W0 and W-1 real-only functions.
0018 
0019 This code is based in part on the algorithm by
0020 Toshio Fukushima,
0021 "Precise and fast computation of Lambert W-functions without transcendental function evaluations",
0022 J.Comp.Appl.Math. 244 (2013) 77-89,
0023 and on a C/C++ version by Darko Veberic, darko.veberic@ijs.si
0024 based on the Fukushima algorithm and Toshio Fukushima's FORTRAN version of his algorithm.
0025 
0026 First derivative of Lambert_w is derived from
0027 Princeton Companion to Applied Mathematics, 'The Lambert-W function', Section 1.3: Series and Generating Functions.
0028 
0029 */
0030 
0031 /*
0032 TODO revise this list of macros.
0033 Some macros that will show some (or much) diagnostic values if #defined.
0034 //[boost_math_instrument_lambert_w_macros
0035 
0036 // #define-able macros
0037 BOOST_MATH_INSTRUMENT_LAMBERT_W_HALLEY                     // Halley refinement diagnostics.
0038 BOOST_MATH_INSTRUMENT_LAMBERT_W_PRECISION                  // Precision.
0039 BOOST_MATH_INSTRUMENT_LAMBERT_WM1                          // W1 branch diagnostics.
0040 BOOST_MATH_INSTRUMENT_LAMBERT_WM1_HALLEY                   // Halley refinement diagnostics only for W-1 branch.
0041 BOOST_MATH_INSTRUMENT_LAMBERT_WM1_TINY                     // K > 64, z > -1.0264389699511303e-26
0042 BOOST_MATH_INSTRUMENT_LAMBERT_WM1_LOOKUP                   // Show results from W-1 lookup table.
0043 BOOST_MATH_INSTRUMENT_LAMBERT_W_SCHROEDER                  // Schroeder refinement diagnostics.
0044 BOOST_MATH_INSTRUMENT_LAMBERT_W_TERMS                      // Number of terms used for near-singularity series.
0045 BOOST_MATH_INSTRUMENT_LAMBERT_W_SINGULARITY_SERIES         // Show evaluation of series near branch singularity.
0046 BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0047 BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES_ITERATIONS  // Show evaluation of series for small z.
0048 //] [/boost_math_instrument_lambert_w_macros]
0049 */
0050 
0051 #include <boost/math/tools/config.hpp>
0052 #include <boost/math/policies/error_handling.hpp>
0053 #include <boost/math/policies/policy.hpp>
0054 #include <boost/math/tools/promotion.hpp>
0055 #include <boost/math/special_functions/fpclassify.hpp>
0056 #include <boost/math/special_functions/log1p.hpp> // for log (1 + x)
0057 #include <boost/math/constants/constants.hpp> // For exp_minus_one == 3.67879441171442321595523770161460867e-01.
0058 #include <boost/math/special_functions/next.hpp>  // for has_denorm_now
0059 #include <boost/math/special_functions/pow.hpp> // powers with compile time exponent, used in arbitrary precision code.
0060 #include <boost/math/tools/series.hpp> // series functor.
0061 //#include <boost/math/tools/polynomial.hpp>  // polynomial.
0062 #include <boost/math/tools/rational.hpp>  // evaluate_polynomial.
0063 #include <boost/math/tools/precision.hpp> // boost::math::tools::max_value().
0064 #include <boost/math/tools/big_constant.hpp>
0065 #include <boost/math/tools/cxx03_warn.hpp>
0066 
0067 #ifndef BOOST_MATH_STANDALONE
0068 #include <boost/lexical_cast.hpp>
0069 #endif
0070 
0071 #include <limits>
0072 #include <cmath>
0073 #include <limits>
0074 #include <exception>
0075 #include <type_traits>
0076 #include <cstdint>
0077 
0078 // Needed for testing and diagnostics only.
0079 #include <iostream>
0080 #include <typeinfo>
0081 #include <boost/math/special_functions/next.hpp>  // For float_distance.
0082 
0083 using lookup_t = double; // Type for lookup table (double or float, or even long double?)
0084 
0085 //#include "J:\Cpp\Misc\lambert_w_lookup_table_generator\lambert_w_lookup_table.ipp"
0086 // #include "lambert_w_lookup_table.ipp" // Boost.Math version.
0087 #include <boost/math/special_functions/detail/lambert_w_lookup_table.ipp>
0088 
0089 #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
0090 //
0091 // This is the only way we can avoid
0092 // warning: non-standard suffix on floating constant [-Wpedantic]
0093 // when building with -Wall -pedantic.  Neither __extension__
0094 // nor #pragma diagnostic ignored work :(
0095 //
0096 #pragma GCC system_header
0097 #endif
0098 
0099 namespace boost {
0100 namespace math {
0101 namespace lambert_w_detail {
0102 
0103 //! \brief Applies a single Halley step to make a better estimate of Lambert W.
0104 //! \details Used the simplified formulae obtained from
0105 //! 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
0106 //! [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)]
0107 
0108 //! \tparam T floating-point (or fixed-point) type.
0109 //! \param w_est Lambert W estimate.
0110 //! \param z Argument z for Lambert_w function.
0111 //! \returns New estimate of Lambert W, hopefully improved.
0112 //!
0113 template <typename T>
0114 inline T lambert_w_halley_step(T w_est, const T z)
0115 {
0116   BOOST_MATH_STD_USING
0117   T e = exp(w_est);
0118   w_est -= 2 * (w_est + 1) * (e * w_est - z) / (z * (w_est + 2) + e * (w_est * (w_est + 2) + 2));
0119   return w_est;
0120 } // template <typename T> lambert_w_halley_step(T w_est, T z)
0121 
0122 //! \brief Halley iterate to refine Lambert_w estimate,
0123 //! taking at least one Halley_step.
0124 //! Repeat Halley steps until the *last step* had fewer than half the digits wrong,
0125 //! the step we've just taken should have been sufficient to have completed the iteration.
0126 
0127 //! \tparam T floating-point (or fixed-point) type.
0128 //! \param z Argument z for Lambert_w function.
0129 //! \param w_est Lambert w estimate.
0130 template <typename T>
0131 inline T lambert_w_halley_iterate(T w_est, const T z)
0132 {
0133   BOOST_MATH_STD_USING
0134   static const T max_diff = boost::math::tools::root_epsilon<T>() * fabs(w_est);
0135 
0136   T w_new = lambert_w_halley_step(w_est, z);
0137   T diff = fabs(w_est - w_new);
0138   while (diff > max_diff)
0139   {
0140     w_est = w_new;
0141     w_new = lambert_w_halley_step(w_est, z);
0142     diff = fabs(w_est - w_new);
0143   }
0144   return w_new;
0145 } // template <typename T> lambert_w_halley_iterate(T w_est, T z)
0146 
0147 // Two Halley function versions that either
0148 // single step (if std::false_type) or iterate (if std::true_type).
0149 // Selected at compile-time using parameter 3.
0150 template <typename T>
0151 inline T lambert_w_maybe_halley_iterate(T z, T w, std::false_type const&)
0152 {
0153    return lambert_w_halley_step(z, w); // Single step.
0154 }
0155 
0156 template <typename T>
0157 inline T lambert_w_maybe_halley_iterate(T z, T w, std::true_type const&)
0158 {
0159    return lambert_w_halley_iterate(z, w); // Iterate steps.
0160 }
0161 
0162 //! maybe_reduce_to_double function,
0163 //! Two versions that have a compile-time option to
0164 //! reduce argument z to double precision (if true_type).
0165 //! Version is selected at compile-time using parameter 2.
0166 
0167 template <typename T>
0168 inline double maybe_reduce_to_double(const T& z, const std::true_type&)
0169 {
0170   return static_cast<double>(z); // Reduce to double precision.
0171 }
0172 
0173 template <typename T>
0174 inline T maybe_reduce_to_double(const T& z, const std::false_type&)
0175 { // Don't reduce to double.
0176   return z;
0177 }
0178 
0179 template <typename T>
0180 inline double must_reduce_to_double(const T& z, const std::true_type&)
0181 {
0182    return static_cast<double>(z); // Reduce to double precision.
0183 }
0184 
0185 template <typename T>
0186 inline double must_reduce_to_double(const T& z, const std::false_type&)
0187 { // try a lexical_cast and hope for the best:
0188 #ifndef BOOST_MATH_STANDALONE
0189 
0190    #ifdef BOOST_MATH_USE_CHARCONV_FOR_CONVERSION
0191 
0192    // Catches the C++23 floating point types
0193    if constexpr (std::is_arithmetic_v<T>)
0194    {
0195       return static_cast<double>(z);
0196    }
0197    else
0198    {
0199       return boost::lexical_cast<double>(z);
0200    }
0201 
0202    #else
0203    
0204    return boost::lexical_cast<double>(z);
0205    
0206    #endif
0207 
0208 #else
0209    static_assert(sizeof(T) == 0, "Unsupported in standalone mode: don't know how to cast your number type to a double.");
0210    return 0.0;
0211 #endif
0212 }
0213 
0214 //! \brief Schroeder method, fifth-order update formula,
0215 //! \details See T. Fukushima page 80-81, and
0216 //! A. Householder, The Numerical Treatment of a Single Nonlinear Equation,
0217 //! McGraw-Hill, New York, 1970, section 4.4.
0218 //! Fukushima algorithm switches to @c schroeder_update after pre-computed bisections,
0219 //! chosen to ensure that the result will be achieve the +/- 10 epsilon target.
0220 //! \param w Lambert w estimate from bisection or series.
0221 //! \param y bracketing value from bisection.
0222 //! \returns Refined estimate of Lambert w.
0223 
0224 // Schroeder refinement, called unless NOT required by precision policy.
0225 template<typename T>
0226 inline T schroeder_update(const T w, const T y)
0227 {
0228   // Compute derivatives using 5th order Schroeder refinement.
0229   // Since this is the final step, it will always use the highest precision type T.
0230   // Example of Call:
0231   //   result = schroeder_update(w, y);
0232   //where
0233   // w is estimate of Lambert W (from bisection or series).
0234   // y is z * e^-w.
0235 
0236   BOOST_MATH_STD_USING // Aid argument dependent lookup of abs.
0237 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SCHROEDER
0238     std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
0239   using boost::math::float_distance;
0240   T fd = float_distance<T>(w, y);
0241   std::cout << "Schroder ";
0242   if (abs(fd) < 214748000.)
0243   {
0244     std::cout << " Distance = "<< static_cast<int>(fd);
0245   }
0246   else
0247   {
0248     std::cout << "Difference w - y = " << (w - y) << ".";
0249   }
0250   std::cout << std::endl;
0251 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SCHROEDER
0252   //  Fukushima equation 18, page 6.
0253   const T f0 = w - y; // f0 = w - y.
0254   const T f1 = 1 + y; // f1 = df/dW
0255   const T f00 = f0 * f0;
0256   const T f11 = f1 * f1;
0257   const T f0y = f0 * y;
0258   const T result =
0259     w - 4 * f0 * (6 * f1 * (f11 + f0y)  +  f00 * y) /
0260     (f11 * (24 * f11 + 36 * f0y) +
0261       f00 * (6 * y * y  +  8 * f1 * y  +  f0y)); // Fukushima Page 81, equation 21 from equation 20.
0262 
0263 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SCHROEDER
0264   std::cout << "Schroeder refined " << w << "  " << y << ", difference  " << w-y  << ", change " << w - result << ", to result " << result << std::endl;
0265   std::cout.precision(saved_precision); // Restore.
0266 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SCHROEDER
0267 
0268   return result;
0269 } // template<typename T = double> T schroeder_update(const T w, const T y)
0270 
0271   //! \brief Series expansion used near the singularity/branch point z = -exp(-1) = -3.6787944.
0272   //! Wolfram InverseSeries[Series[sqrt[2(p Exp[1 + p] + 1)], {p,-1, 20}]]
0273   //! Wolfram command used to obtain 40 series terms at 50 decimal digit precision was
0274   //! N[InverseSeries[Series[Sqrt[2(p Exp[1 + p] + 1)], { p,-1,40 }]], 50]
0275   //! -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 ...
0276   //! Decimal values of specifications for built-in floating-point types below
0277   //! are at least 21 digits precision == max_digits10 for long double.
0278   //! Longer decimal digits strings are rationals evaluated using Wolfram.
0279 
0280 template<typename T>
0281 T lambert_w_singularity_series(const T p)
0282 {
0283 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SINGULARITY_SERIES
0284   std::size_t saved_precision = std::cout.precision(3);
0285   std::cout << "Singularity_series Lambert_w p argument = " << p << std::endl;
0286   std::cout
0287     //<< "Argument Type = " << typeid(T).name()
0288     //<< ", max_digits10 = " << std::numeric_limits<T>::max_digits10
0289     //<< ", epsilon = " << std::numeric_limits<T>::epsilon()
0290     << std::endl;
0291   std::cout.precision(saved_precision);
0292 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SINGULARITY_SERIES
0293 
0294   static const T q[] =
0295   {
0296     -static_cast<T>(1), // j0
0297     +T(1), // j1
0298     -T(1) / 3, // 1/3  j2
0299     +T(11) / 72, // 0.152777777777777778, // 11/72 j3
0300     -T(43) / 540, // 0.0796296296296296296, // 43/540 j4
0301     +T(769) / 17280, // 0.0445023148148148148,  j5
0302     -T(221) / 8505, // 0.0259847148736037625,  j6
0303     //+T(0.0156356325323339212L), // j7
0304     //+T(0.015635632532333921222810111699000587889476778365667L), // j7 from Wolfram N[680863/43545600, 50]
0305     +T(680863uLL) / 43545600uLL, // +0.0156356325323339212, j7
0306     //-T(0.00961689202429943171L), // j8
0307     -T(1963uLL) / 204120uLL, // 0.00961689202429943171, j8
0308     //-T(0.0096168920242994317068391142465216539290613364687439L), // j8 from Wolfram N[1963/204120, 50]
0309     +T(226287557uLL) / 37623398400uLL, // 0.00601454325295611786, j9
0310     -T(5776369uLL) / 1515591000uLL, // 0.00381129803489199923, j10
0311     //+T(0.00244087799114398267L), j11 0.0024408779911439826658968585286437530215699919795550
0312     +T(169709463197uLL) / 69528040243200uLL, // j11
0313     // -T(0.00157693034468678425L), // j12  -0.0015769303446867842539234095399314115973161850314723
0314     -T(1118511313uLL) / 709296588000uLL, // j12
0315     +T(667874164916771uLL) / 650782456676352000uLL, // j13
0316     //+T(0.00102626332050760715L), // j13 0.0010262633205076071544375481533906861056468041465973
0317     -T(500525573uLL) / 744761417400uLL, // j14
0318     // -T(0.000672061631156136204L), j14
0319     //+T(1003663334225097487uLL) / 234281684403486720000uLL, // j15 0.00044247306181462090993020760858473726479232802068800 error C2177: constant too big
0320     //+T(0.000442473061814620910L, // j15
0321     BOOST_MATH_BIG_CONSTANT(T, 64, +0.000442473061814620910), // j15
0322     // -T(0.000292677224729627445L), // j16
0323     BOOST_MATH_BIG_CONSTANT(T, 64, -0.000292677224729627445), // j16
0324     //+T(0.000194387276054539318L), // j17
0325     BOOST_MATH_BIG_CONSTANT(T, 64, 0.000194387276054539318), // j17
0326     //-T(0.000129574266852748819L), // j18
0327     BOOST_MATH_BIG_CONSTANT(T, 64, -0.000129574266852748819), // j18
0328     //+T(0.0000866503580520812717L), // j19 N[+1150497127780071399782389/13277465363600276402995200000, 50] 0.000086650358052081271660451590462390293190597827783288
0329     BOOST_MATH_BIG_CONSTANT(T, 64, +0.0000866503580520812717), // j19
0330     //-T(0.0000581136075044138168L) // j20  N[2853534237182741069/49102686267859224000000, 50] 0.000058113607504413816772205464778828177256611844221913
0331     // -T(2853534237182741069uLL) / 49102686267859224000000uLL  // j20 // error C2177: constant too big,
0332     // so must use BOOST_MATH_BIG_CONSTANT(T, ) format in hope of using suffix Q for quad or decimal digits string for others.
0333     //-T(0.000058113607504413816772205464778828177256611844221913L), // j20  N[2853534237182741069/49102686267859224000000, 50] 0.000058113607504413816772205464778828177256611844221913
0334     BOOST_MATH_BIG_CONSTANT(T, 113, -0.000058113607504413816772205464778828177256611844221913) // j20  - last used by Fukushima
0335     // More terms don't seem to give any improvement (worse in fact) and are not use for many z values.
0336     //BOOST_MATH_BIG_CONSTANT(T, +0.000039076684867439051635395583044527492132109160553593), // j21
0337     //BOOST_MATH_BIG_CONSTANT(T, -0.000026338064747231098738584082718649443078703982217219), // j22
0338     //BOOST_MATH_BIG_CONSTANT(T, +0.000017790345805079585400736282075184540383274460464169), // j23
0339     //BOOST_MATH_BIG_CONSTANT(T, -0.000012040352739559976942274116578992585158113153190354), // j24
0340     //BOOST_MATH_BIG_CONSTANT(T, +8.1635319824966121713827512573558687050675701559448E-6), // j25
0341     //BOOST_MATH_BIG_CONSTANT(T, -5.5442032085673591366657251660804575198155559225316E-6) // j26
0342     // -T(5.5442032085673591366657251660804575198155559225316E-6L) // j26
0343     // 21 to 26 Added for long double.
0344   }; // static const T q[]
0345 
0346      /*
0347      // Temporary copy of original double values for comparison; these are reproduced well.
0348      static const T q[] =
0349      {
0350      -1L,  // j0
0351      +1L,  // j1
0352      -0.333333333333333333L, // 1/3 j2
0353      +0.152777777777777778L, // 11/72 j3
0354      -0.0796296296296296296L, // 43/540
0355      +0.0445023148148148148L,
0356      -0.0259847148736037625L,
0357      +0.0156356325323339212L,
0358      -0.00961689202429943171L,
0359      +0.00601454325295611786L,
0360      -0.00381129803489199923L,
0361      +0.00244087799114398267L,
0362      -0.00157693034468678425L,
0363      +0.00102626332050760715L,
0364      -0.000672061631156136204L,
0365      +0.000442473061814620910L,
0366      -0.000292677224729627445L,
0367      +0.000194387276054539318L,
0368      -0.000129574266852748819L,
0369      +0.0000866503580520812717L,
0370      -0.0000581136075044138168L // j20
0371      };
0372      */
0373 
0374      // Decide how many series terms to use, increasing as z approaches the singularity,
0375      // balancing run-time versus computational noise from round-off.
0376      // In practice, we truncate the series expansion at a certain order.
0377      // If the order is too large, not only does the amount of computation increase,
0378      // but also the round-off errors accumulate.
0379      // See Fukushima equation 35, page 85 for logic of choice of number of series terms.
0380 
0381   BOOST_MATH_STD_USING // Aid argument dependent lookup (ADL) of abs.
0382 
0383     const T absp = abs(p);
0384 
0385 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_TERMS
0386   {
0387     int terms = 20; // Default to using all terms.
0388     if (absp < 0.01159)
0389     { // Very near singularity.
0390       terms = 6;
0391     }
0392     else if (absp < 0.0766)
0393     { // Near singularity.
0394       terms = 10;
0395     }
0396     std::streamsize saved_precision = std::cout.precision(3);
0397     std::cout << "abs(p) = " << absp << ", terms = " << terms << std::endl;
0398     std::cout.precision(saved_precision);
0399   }
0400 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_TERMS
0401 
0402   if (absp < T(0.01159))
0403   { // Only 6 near-singularity series terms are useful.
0404     return
0405       -1 +
0406       p * (1 +
0407         p * (q[2] +
0408           p * (q[3] +
0409             p * (q[4] +
0410               p * (q[5] +
0411                 p * q[6]
0412                 )))));
0413   }
0414   else if (absp < T(0.0766)) // Use 10 near-singularity series terms.
0415   { // Use 10 near-singularity series terms.
0416     return
0417       -1 +
0418       p * (1 +
0419         p * (q[2] +
0420           p * (q[3] +
0421             p * (q[4] +
0422               p * (q[5] +
0423                 p * (q[6] +
0424                   p * (q[7] +
0425                     p * (q[8] +
0426                       p * (q[9] +
0427                         p * q[10]
0428                         )))))))));
0429   }
0430    // Use all 20 near-singularity series terms.
0431     return
0432       -1 +
0433       p * (1 +
0434         p * (q[2] +
0435           p * (q[3] +
0436             p * (q[4] +
0437               p * (q[5] +
0438                 p * (q[6] +
0439                   p * (q[7] +
0440                     p * (q[8] +
0441                       p * (q[9] +
0442                         p * (q[10] +
0443                           p * (q[11] +
0444                             p * (q[12] +
0445                               p * (q[13] +
0446                                 p * (q[14] +
0447                                   p * (q[15] +
0448                                     p * (q[16] +
0449                                       p * (q[17] +
0450                                         p * (q[18] +
0451                                           p * (q[19] +
0452                                             p * q[20] // Last Fukushima term.
0453                                             )))))))))))))))))));
0454     //                                                + // more terms for more precise T: long double ...
0455     //// but makes almost no difference, so don't use more terms?
0456     //                                          p*q[21] +
0457     //                                            p*q[22] +
0458     //                                              p*q[23] +
0459     //                                                p*q[24] +
0460     //                                                 p*q[25]
0461     //                                         )))))))))))))))))));
0462 
0463 } // template<typename T = double> T lambert_w_singularity_series(const T p)
0464 
0465 
0466  /////////////////////////////////////////////////////////////////////////////////////////////
0467 
0468   //! \brief Series expansion used near zero (abs(z) < 0.05).
0469   //! \details
0470   //! Coefficients of the inverted series expansion of the Lambert W function around z = 0.
0471   //! Tosio Fukushima always uses all 17 terms of a Taylor series computed using Wolfram with
0472   //!   InverseSeries[Series[z Exp[z],{z,0,17}]]
0473   //! Tosio Fukushima / Journal of Computational and Applied Mathematics 244 (2013) page 86.
0474 
0475   //! Decimal values of specifications for built-in floating-point types below
0476   //! are 21 digits precision == max_digits10 for long double.
0477   //! Care! Some coefficients might overflow some fixed_point types.
0478 
0479   //! This version is intended to allow use by user-defined types
0480   //! like Boost.Multiprecision quad and cpp_dec_float types.
0481   //! The three specializations below for built-in float, double
0482   //! (and perhaps long double) will be chosen in preference for these types.
0483 
0484   //! This version uses rationals computed by Wolfram as far as possible,
0485   //! limited by maximum size of uLL integers.
0486   //! For higher term, uses decimal digit strings computed by Wolfram up to the maximum possible using uLL rationals,
0487   //! and then higher coefficients are computed as necessary using function lambert_w0_small_z_series_term
0488   //! until the precision required by the policy is achieved.
0489   //! InverseSeries[Series[z Exp[z],{z,0,34}]] also computed.
0490 
0491   // Series evaluation for LambertW(z) as z -> 0.
0492   // See http://functions.wolfram.com/ElementaryFunctions/ProductLog/06/01/01/0003/
0493   //  http://functions.wolfram.com/ElementaryFunctions/ProductLog/06/01/01/0003/MainEq1.L.gif
0494 
0495   //! \brief  lambert_w0_small_z uses a tag_type to select a variant depending on the size of the type.
0496   //! The Lambert W is computed by lambert_w0_small_z for small z.
0497   //! The cutoff for z smallness determined by Tosio Fukushima by trial and error is (abs(z) < 0.05),
0498   //! but the optimum might be a function of the size of the type of z.
0499 
0500   //! \details
0501   //! The tag_type selection is based on the value @c std::numeric_limits<T>::max_digits10.
0502   //! This allows distinguishing between long double types that commonly vary between 64 and 80-bits,
0503   //! and also compilers that have a float type using 64 bits and/or long double using 128-bits.
0504   //! It assumes that max_digits10 is defined correctly or this might fail to make the correct selection.
0505   //! causing very small differences in computing lambert_w that would be very difficult to detect and diagnose.
0506   //! Cannot switch on @c std::numeric_limits<>::max() because comparison values may overflow the compiler limit.
0507   //! Cannot switch on @c std::numeric_limits<long double>::max_exponent10()
0508   //! because both 80 and 128 bit floating-point implementations use 11 bits for the exponent.
0509   //! So must rely on @c std::numeric_limits<long double>::max_digits10.
0510 
0511   //! Specialization of float zero series expansion used for small z (abs(z) < 0.05).
0512   //! Specializations of lambert_w0_small_z for built-in types.
0513   //! These specializations should be chosen in preference to T version.
0514   //! For example: lambert_w0_small_z(0.001F) should use the float version.
0515   //! (Parameter Policy is not used by built-in types when all terms are used during an inline computation,
0516   //! but for the tag_type selection to work, they all must include Policy in their signature.
0517 
0518   // Forward declaration of variants of lambert_w0_small_z.
0519 template <typename T, typename Policy>
0520 T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 0> const&);   //  for float (32-bit) type.
0521 
0522 template <typename T, typename Policy>
0523 T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 1> const&);   //  for double (64-bit) type.
0524 
0525 template <typename T, typename Policy>
0526 T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 2> const&);   //  for long double (double extended 80-bit) type.
0527 
0528 template <typename T, typename Policy>
0529 T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 3> const&);   //  for long double (128-bit) type.
0530 
0531 template <typename T, typename Policy>
0532 T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 4> const&);   //  for float128 quadmath Q type.
0533 
0534 template <typename T, typename Policy>
0535 T lambert_w0_small_z(T x, const Policy&, std::integral_constant<int, 5> const&);   //  Generic multiprecision T.
0536                                                                         // Set tag_type depending on max_digits10.
0537 template <typename T, typename Policy>
0538 T lambert_w0_small_z(T x, const Policy& pol)
0539 { //std::numeric_limits<T>::max_digits10 == 36 ? 3 : // 128-bit long double.
0540   using tag_type = std::integral_constant<int,
0541      std::numeric_limits<T>::is_specialized == 0 ? 5 :
0542 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
0543     std::numeric_limits<T>::max_digits10 <=  9 ? 0 : // for float 32-bit.
0544     std::numeric_limits<T>::max_digits10 <= 17 ? 1 : // for double 64-bit.
0545     std::numeric_limits<T>::max_digits10 <= 22 ? 2 : // for 80-bit double extended.
0546     std::numeric_limits<T>::max_digits10 <  37 ? 4  // for both 128-bit long double (3) and 128-bit quad suffix Q type (4).
0547 #else
0548      std::numeric_limits<T>::radix != 2 ? 5 :
0549      std::numeric_limits<T>::digits <= 24 ? 0 : // for float 32-bit.
0550      std::numeric_limits<T>::digits <= 53 ? 1 : // for double 64-bit.
0551      std::numeric_limits<T>::digits <= 64 ? 2 : // for 80-bit double extended.
0552      std::numeric_limits<T>::digits <= 113 ? 4  // for both 128-bit long double (3) and 128-bit quad suffix Q type (4).
0553 #endif
0554       :  5>;                                           // All Generic multiprecision types.
0555   // std::cout << "\ntag type = " << tag_type << std::endl; // error C2275: 'tag_type': illegal use of this type as an expression.
0556   return lambert_w0_small_z(x, pol, tag_type());
0557 } // template <typename T> T lambert_w0_small_z(T x)
0558 
0559   //! Specialization of float (32-bit) series expansion used for small z (abs(z) < 0.05).
0560   // Only 9 Coefficients are computed to 21 decimal digits precision, ample for 32-bit float used by most platforms.
0561   // Taylor series coefficients used are computed by Wolfram to 50 decimal digits using instruction
0562   // N[InverseSeries[Series[z Exp[z],{z,0,34}]],50],
0563   // as proposed by Tosio Fukushima and implemented by Darko Veberic.
0564 
0565 template <typename T, typename Policy>
0566 T lambert_w0_small_z(T z, const Policy&, std::integral_constant<int, 0> const&)
0567 {
0568 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0569   std::streamsize prec = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
0570   std::cout << "\ntag_type 0 float lambert_w0_small_z called with z = " << z << " using " << 9 << " terms of precision "
0571     << std::numeric_limits<float>::max_digits10 << " decimal digits. " << std::endl;
0572 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0573   T result =
0574     z * (1 - // j1 z^1 term = 1
0575       z * (1 -  // j2 z^2 term = -1
0576         z * (static_cast<float>(3uLL) / 2uLL - // 3/2 // j3 z^3 term = 1.5.
0577           z * (2.6666666666666666667F -  // 8/3 // j4
0578             z * (5.2083333333333333333F - // -125/24 // j5
0579               z * (10.8F - // j6
0580                 z * (23.343055555555555556F - // j7
0581                   z * (52.012698412698412698F - // j8
0582                     z * 118.62522321428571429F)))))))); // j9
0583 
0584 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0585   std::cout << "return w = " << result << std::endl;
0586   std::cout.precision(prec); // Restore.
0587 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0588 
0589   return result;
0590 } // template <typename T>   T lambert_w0_small_z(T x, std::integral_constant<int, 0> const&)
0591 
0592   //! Specialization of double (64-bit double) series expansion used for small z (abs(z) < 0.05).
0593   // 17 Coefficients are computed to 21 decimal digits precision suitable for 64-bit double used by most platforms.
0594   // Taylor series coefficients used are computed by Wolfram to 50 decimal digits using instruction
0595   // N[InverseSeries[Series[z Exp[z],{z,0,34}]],50], as proposed by Tosio Fukushima and implemented by Veberic.
0596 
0597 template <typename T, typename Policy>
0598 T lambert_w0_small_z(const T z, const Policy&, std::integral_constant<int, 1> const&)
0599 {
0600 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0601   std::streamsize prec = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
0602   std::cout << "\ntag_type 1 double lambert_w0_small_z called with z = " << z << " using " << 17 << " terms of precision, "
0603     << std::numeric_limits<double>::max_digits10 << " decimal digits. " << std::endl;
0604 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0605   T result =
0606     z * (1. - // j1 z^1
0607       z * (1. -  // j2 z^2
0608         z * (1.5 - // 3/2 // j3 z^3
0609           z * (2.6666666666666666667 -  // 8/3 // j4
0610             z * (5.2083333333333333333 - // -125/24 // j5
0611               z * (10.8 - // j6
0612                 z * (23.343055555555555556 - // j7
0613                   z * (52.012698412698412698 - // j8
0614                     z * (118.62522321428571429 - // j9
0615                       z * (275.57319223985890653 - // j10
0616                         z * (649.78717234347442681 - // j11
0617                           z * (1551.1605194805194805 - // j12
0618                             z * (3741.4497029592385495 - // j13
0619                               z * (9104.5002411580189358 - // j14
0620                                 z * (22324.308512706601434 - // j15
0621                                   z * (55103.621972903835338 - // j16
0622                                     z * 136808.86090394293563)))))))))))))))); // j17 z^17
0623 
0624 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0625   std::cout << "return w = " << result << std::endl;
0626   std::cout.precision(prec); // Restore.
0627 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0628 
0629   return result;
0630 } // T lambert_w0_small_z(const T z, std::integral_constant<int, 1> const&)
0631 
0632   //! Specialization of long double (80-bit double extended) series expansion used for small z (abs(z) < 0.05).
0633   // 21 Coefficients are computed to 21 decimal digits precision suitable for 80-bit long double used by some
0634   // platforms including GCC and Clang when generating for Intel X86 floating-point processors with 80-bit operations enabled (the default).
0635   // (This is NOT used by Microsoft Visual Studio where double and long always both use only 64-bit type.
0636   // Nor used for 128-bit float128.)
0637 template <typename T, typename Policy>
0638 T lambert_w0_small_z(const T z, const Policy&, std::integral_constant<int, 2> const&)
0639 {
0640 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0641   std::streamsize precision = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
0642   std::cout << "\ntag_type 2 long double (80-bit double extended) lambert_w0_small_z called with z = " << z << " using " << 21 << " terms of precision, "
0643     << std::numeric_limits<long double>::max_digits10 << " decimal digits. " << std::endl;
0644 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0645 //  T  result =
0646 //    z * (1.L - // j1 z^1
0647 //      z * (1.L -  // j2 z^2
0648 //        z * (1.5L - // 3/2 // j3
0649 //          z * (2.6666666666666666667L -  // 8/3 // j4
0650 //            z * (5.2083333333333333333L - // -125/24 // j5
0651 //              z * (10.800000000000000000L - // j6
0652 //                z * (23.343055555555555556L - // j7
0653 //                  z * (52.012698412698412698L - // j8
0654 //                    z * (118.62522321428571429L - // j9
0655 //                      z * (275.57319223985890653L - // j10
0656 //                        z * (649.78717234347442681L - // j11
0657 //                          z * (1551.1605194805194805L - // j12
0658 //                            z * (3741.4497029592385495L - // j13
0659 //                              z * (9104.5002411580189358L - // j14
0660 //                                z * (22324.308512706601434L - // j15
0661 //                                  z * (55103.621972903835338L - // j16
0662 //                                    z * (136808.86090394293563L - // j17 z^17  last term used by Fukushima double.
0663 //                                      z * (341422.050665838363317L - // z^18
0664 //                                        z * (855992.9659966075514633L - // z^19
0665 //                                          z * (2.154990206091088289321e6L - // z^20
0666 //                                            z * 5.4455529223144624316423e6L   // z^21
0667 //                                              ))))))))))))))))))));
0668 //
0669 
0670   T result =
0671 z * (1.L - // z j1
0672 z * (1.L - // z^2
0673 z * (1.500000000000000000000000000000000L - // z^3
0674 z * (2.666666666666666666666666666666666L - // z ^ 4
0675 z * (5.208333333333333333333333333333333L - // z ^ 5
0676 z * (10.80000000000000000000000000000000L - // z ^ 6
0677 z * (23.34305555555555555555555555555555L - //  z ^ 7
0678 z * (52.01269841269841269841269841269841L - // z ^ 8
0679 z * (118.6252232142857142857142857142857L - // z ^ 9
0680 z * (275.5731922398589065255731922398589L - // z ^ 10
0681 z * (649.7871723434744268077601410934744L - // z ^ 11
0682 z * (1551.160519480519480519480519480519L - // z ^ 12
0683 z * (3741.449702959238549516327294105071L - //z ^ 13
0684 z * (9104.500241158018935796713574491352L - //  z ^ 14
0685 z * (22324.308512706601434280005708577137L - //  z ^ 15
0686 z * (55103.621972903835337697771560205422L - //  z ^ 16
0687 z * (136808.86090394293563342215789305736L - // z ^ 17
0688 z * (341422.05066583836331735491399356945L - //  z^18
0689 z * (855992.9659966075514633630250633224L - // z^19
0690 z * (2.154990206091088289321708745358647e6L // z^20 distance -5 without term 20
0691 ))))))))))))))))))));
0692 
0693 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0694   std::cout << "return w = " << result << std::endl;
0695   std::cout.precision(precision); // Restore.
0696 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0697   return result;
0698 }  // long double lambert_w0_small_z(const T z, std::integral_constant<int, 1> const&)
0699 
0700 //! Specialization of 128-bit long double series expansion used for small z (abs(z) < 0.05).
0701 // 34 Taylor series coefficients used are computed by Wolfram to 50 decimal digits using instruction
0702 // N[InverseSeries[Series[z Exp[z],{z,0,34}]],50],
0703 // and are suffixed by L as they are assumed of type long double.
0704 // (This is NOT used for 128-bit quad boost::multiprecision::float128 type which required a suffix Q
0705 // nor multiprecision type cpp_bin_float_quad that can only be initialized at full precision of the type
0706 // constructed with a decimal digit string like "2.6666666666666666666666666666666666666666666666667".)
0707 
0708 template <typename T, typename Policy>
0709 T lambert_w0_small_z(const T z, const Policy&, std::integral_constant<int, 3> const&)
0710 {
0711 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0712   std::streamsize precision = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
0713   std::cout << "\ntag_type 3 long double (128-bit) lambert_w0_small_z called with z = " << z << " using " << 17 << " terms of precision,  "
0714     << std::numeric_limits<double>::max_digits10 << " decimal digits. " << std::endl;
0715 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0716   T  result =
0717     z * (1.L - // j1
0718       z * (1.L -  // j2
0719         z * (1.5L - // 3/2 // j3
0720           z * (2.6666666666666666666666666666666666L -  // 8/3 // j4
0721             z * (5.2052083333333333333333333333333333L - // -125/24 // j5
0722               z * (10.800000000000000000000000000000000L - // j6
0723                 z * (23.343055555555555555555555555555555L - // j7
0724                   z * (52.0126984126984126984126984126984126L - // j8
0725                     z * (118.625223214285714285714285714285714L - // j9
0726                       z * (275.57319223985890652557319223985890L - // * z ^ 10 - // j10
0727                         z * (649.78717234347442680776014109347442680776014109347L - // j11
0728                           z * (1551.1605194805194805194805194805194805194805194805L - // j12
0729                             z * (3741.4497029592385495163272941050718828496606274384L - // j13
0730                               z * (9104.5002411580189357967135744913522691300469078247L - // j14
0731                                 z * (22324.308512706601434280005708577137148565719994291L - // j15
0732                                   z * (55103.621972903835337697771560205422639285073147507L - // j16
0733                                     z * 136808.86090394293563342215789305736395683485630576L    // j17
0734                                       ))))))))))))))));
0735 
0736 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0737   std::cout << "return w = " << result << std::endl;
0738   std::cout.precision(precision); // Restore.
0739 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0740   return result;
0741 }  // T lambert_w0_small_z(const T z, std::integral_constant<int, 3> const&)
0742 
0743 //! Specialization of 128-bit quad series expansion used for small z (abs(z) < 0.05).
0744 // 34 Taylor series coefficients used were computed by Wolfram to 50 decimal digits using instruction
0745 //   N[InverseSeries[Series[z Exp[z],{z,0,34}]],50],
0746 // and are suffixed by Q as they are assumed of type quad.
0747 // This could be used for 128-bit quad (which requires a suffix Q for full precision).
0748 // But experiments with GCC 7.2.0 show that while this gives full 128-bit precision
0749 // when the -f-ext-numeric-literals option is in force and the libquadmath library available,
0750 // over the range -0.049 to +0.049,
0751 // it is slightly slower than getting a double approximation followed by a single Halley step.
0752 
0753 #ifdef BOOST_HAS_FLOAT128
0754 template <typename T, typename Policy>
0755 T lambert_w0_small_z(const T z, const Policy&, std::integral_constant<int, 4> const&)
0756 {
0757 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0758   std::streamsize precision = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
0759   std::cout << "\ntag_type 4 128-bit quad float128 lambert_w0_small_z called with z = " << z << " using " << 34 << " terms of precision, "
0760     << std::numeric_limits<float128>::max_digits10 << " max decimal digits." << std::endl;
0761 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0762   T  result =
0763     z * (1.Q - // z j1
0764       z * (1.Q - // z^2
0765         z * (1.500000000000000000000000000000000Q - // z^3
0766           z * (2.666666666666666666666666666666666Q - // z ^ 4
0767             z * (5.208333333333333333333333333333333Q - // z ^ 5
0768               z * (10.80000000000000000000000000000000Q - // z ^ 6
0769                 z * (23.34305555555555555555555555555555Q - //  z ^ 7
0770                   z * (52.01269841269841269841269841269841Q - // z ^ 8
0771                     z * (118.6252232142857142857142857142857Q - // z ^ 9
0772                       z * (275.5731922398589065255731922398589Q - // z ^ 10
0773                         z * (649.7871723434744268077601410934744Q - // z ^ 11
0774                           z * (1551.160519480519480519480519480519Q - // z ^ 12
0775                             z * (3741.449702959238549516327294105071Q - //z ^ 13
0776                               z * (9104.500241158018935796713574491352Q - //  z ^ 14
0777                                 z * (22324.308512706601434280005708577137Q - //  z ^ 15
0778                                   z * (55103.621972903835337697771560205422Q - //  z ^ 16
0779                                     z * (136808.86090394293563342215789305736Q - // z ^ 17
0780                                       z * (341422.05066583836331735491399356945Q - //  z^18
0781                                         z * (855992.9659966075514633630250633224Q - // z^19
0782                                           z * (2.154990206091088289321708745358647e6Q - //  20
0783                                             z * (5.445552922314462431642316420035073e6Q - // 21
0784                                               z * (1.380733000216662949061923813184508e7Q - // 22
0785                                                 z * (3.511704498513923292853869855945334e7Q - // 23
0786                                                   z * (8.956800256102797693072819557780090e7Q - // 24
0787                                                     z * (2.290416846187949813964782641734774e8Q - // 25
0788                                                       z * (5.871035041171798492020292225245235e8Q - // 26
0789                                                         z * (1.508256053857792919641317138812957e9Q - // 27
0790                                                           z * (3.882630161293188940385873468413841e9Q - // 28
0791                                                             z * (1.001394313665482968013913601565723e10Q - // 29
0792                                                               z * (2.587356736265760638992878359024929e10Q - // 30
0793                                                                 z * (6.696209709358073856946120522333454e10Q - // 31
0794                                                                   z * (1.735711659599198077777078238043644e11Q - // 32
0795                                                                     z * (4.505680465642353886756098108484670e11Q - // 33
0796                                                                       z * (1.171223178256487391904047636564823e12Q  //z^34
0797                                                                         ))))))))))))))))))))))))))))))))));
0798 
0799 
0800  #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0801   std::cout << "return w = " << result << std::endl;
0802   std::cout.precision(precision); // Restore.
0803 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0804 
0805   return result;
0806 }  // T lambert_w0_small_z(const T z, std::integral_constant<int, 4> const&) float128
0807 
0808 #else
0809 
0810 template <typename T, typename Policy>
0811 inline T lambert_w0_small_z(const T z, const Policy& pol, std::integral_constant<int, 4> const&)
0812 {
0813    return lambert_w0_small_z(z, pol, std::integral_constant<int, 5>());
0814 }
0815 
0816 #endif // BOOST_HAS_FLOAT128
0817 
0818 //! Series functor to compute series term using pow and factorial.
0819 //! \details Functor is called after evaluating polynomial with the coefficients as rationals below.
0820 template <typename T>
0821 struct lambert_w0_small_z_series_term
0822 {
0823   using result_type = T;
0824   //! \param _z Lambert W argument z.
0825   //! \param -term  -pow<18>(z) / 6402373705728000uLL
0826   //! \param _k number of terms == initially 18
0827 
0828   //  Note *after* evaluating N terms, its internal state has k = N and term = (-1)^N z^N.
0829 
0830   lambert_w0_small_z_series_term(T _z, T _term, int _k)
0831     : k(_k), z(_z), term(_term) { }
0832 
0833   T operator()()
0834   { // Called by sum_series until needs precision set by factor (policy::get_epsilon).
0835     using std::pow;
0836     ++k;
0837     term *= -z / k;
0838     //T t = pow(z, k) * pow(T(k), -1 + k) / factorial<T>(k); // (z^k * k(k-1)^k) / k!
0839     T result = term * pow(T(k), T(-1 + k)); // term * k^(k-1)
0840                                          // std::cout << " k = " << k << ", term = " << term << ", result = " << result << std::endl;
0841     return result; //
0842   }
0843 private:
0844   int k;
0845   T z;
0846   T term;
0847 }; // template <typename T> struct lambert_w0_small_z_series_term
0848 
0849    //! Generic variant for T a User-defined types like Boost.Multiprecision.
0850 template <typename T, typename Policy>
0851 inline T lambert_w0_small_z(T z, const Policy& pol, std::integral_constant<int, 5> const&)
0852 {
0853 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0854   std::streamsize precision = std::cout.precision(std::numeric_limits<T>::max_digits10); // Save.
0855   std::cout << "Generic lambert_w0_small_z called with z = " << z << " using as many terms needed for precision." << std::endl;
0856   std::cout << "Argument z is of type " << typeid(T).name() << std::endl;
0857 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0858 
0859   // First several terms of the series are tabulated and evaluated as a polynomial:
0860   // this will save us a bunch of expensive calls to pow.
0861   // Then our series functor is initialized "as if" it had already reached term 18,
0862   // enough evaluation of built-in 64-bit double and float (and 80-bit long double?) types.
0863 
0864   // Coefficients should be stored such that the coefficients for the x^i terms are in poly[i].
0865   static const T coeff[] =
0866   {
0867     0, // z^0  Care: zeroth term needed by tools::evaluate_polynomial, but not in the Wolfram equation, so indexes are one different!
0868     1, // z^1 term.
0869     -1, // z^2 term
0870     static_cast<T>(3uLL) / 2uLL, // z^3 term.
0871     -static_cast<T>(8uLL) / 3uLL, // z^4
0872     static_cast<T>(125uLL) / 24uLL, // z^5
0873     -static_cast<T>(54uLL) / 5uLL, // z^6
0874     static_cast<T>(16807uLL) / 720uLL, // z^7
0875     -static_cast<T>(16384uLL) / 315uLL, // z^8
0876     static_cast<T>(531441uLL) / 4480uLL, // z^9
0877     -static_cast<T>(156250uLL) / 567uLL, // z^10
0878     static_cast<T>(2357947691uLL) / 3628800uLL, // z^11
0879     -static_cast<T>(2985984uLL) / 1925uLL, // z^12
0880     static_cast<T>(1792160394037uLL) / 479001600uLL, // z^13
0881     -static_cast<T>(7909306972uLL) / 868725uLL, // z^14
0882     static_cast<T>(320361328125uLL) / 14350336uLL, // z^15
0883     -static_cast<T>(35184372088832uLL) / 638512875uLL, // z^16
0884     static_cast<T>(2862423051509815793uLL) / 20922789888000uLL, // z^17 term
0885     -static_cast<T>(5083731656658uLL) / 14889875uLL,
0886     // z^18 term. = 136808.86090394293563342215789305735851647769682393
0887 
0888     // z^18 is biggest that can be computed as rational using the largest possible uLL integers,
0889     // so higher terms cannot be potentially compiler-computed as uLL rationals.
0890     // Wolfram (5083731656658 z ^ 18) / 14889875 or
0891     // -341422.05066583836331735491399356945575432970390954 z^18
0892 
0893     // See note below calling the functor to compute another term,
0894     // sufficient for 80-bit long double precision.
0895     // Wolfram -341422.05066583836331735491399356945575432970390954 z^19 term.
0896     // (5480386857784802185939 z^19)/6402373705728000
0897     // But now this variant is not used to compute long double
0898     // as specializations are provided above.
0899   }; // static const T coeff[]
0900 
0901      /*
0902      Table of 19 computed coefficients:
0903 
0904      #0 0
0905      #1 1
0906      #2 -1
0907      #3 1.5
0908      #4 -2.6666666666666666666666666666666665382713370408509
0909      #5 5.2083333333333333333333333333333330765426740817019
0910      #6 -10.800000000000000000000000000000000616297582203915
0911      #7 23.343055555555555555555555555555555076212991619177
0912      #8 -52.012698412698412698412698412698412659282693193402
0913      #9 118.62522321428571428571428571428571146835390992496
0914      #10 -275.57319223985890652557319223985891400375196748314
0915      #11 649.7871723434744268077601410934743969785223845882
0916      #12 -1551.1605194805194805194805194805194947599566007429
0917      #13 3741.4497029592385495163272941050719510009019331763
0918      #14 -9104.5002411580189357967135744913524243896052869184
0919      #15 22324.308512706601434280005708577137322392070452582
0920      #16 -55103.621972903835337697771560205423203318720697224
0921      #17 136808.86090394293563342215789305735851647769682393
0922          136808.86090394293563342215789305735851647769682393   == Exactly same as Wolfram computed value.
0923      #18 -341422.05066583836331735491399356947486381600607416
0924           341422.05066583836331735491399356945575432970390954  z^19  Wolfram value differs at 36 decimal digit, as expected.
0925      */
0926 
0927   using boost::math::policies::get_epsilon; // for type T.
0928   using boost::math::tools::sum_series;
0929   using boost::math::tools::evaluate_polynomial;
0930   // http://www.boost.org/doc/libs/release/libs/math/doc/html/math_toolkit/roots/rational.html
0931 
0932   // std::streamsize prec = std::cout.precision(std::numeric_limits <T>::max_digits10);
0933 
0934   T result = evaluate_polynomial(coeff, z);
0935   //  template <std::size_t N, typename T, typename V>
0936   //  V evaluate_polynomial(const T(&poly)[N], const V& val);
0937   // Size of coeff found from N
0938   //std::cout << "evaluate_polynomial(coeff, z); == " << result << std::endl;
0939   //std::cout << "result = " << result << std::endl;
0940   // It's an artefact of the way I wrote the functor: *after* evaluating N
0941   // terms, its internal state has k = N and term = (-1)^N z^N.  So after
0942   // evaluating 18 terms, we initialize the functor to the term we've just
0943   // evaluated, and then when it's called, it increments itself to the next term.
0944   // So 18!is 6402373705728000, which is where that comes from.
0945 
0946   // The 19th coefficient of the polynomial is actually, 19 ^ 18 / 19!=
0947   // 104127350297911241532841 / 121645100408832000 which after removing GCDs
0948   // reduces down to Wolfram rational 5480386857784802185939 / 6402373705728000.
0949   // Wolfram z^19 term +(5480386857784802185939 z^19) /6402373705728000
0950   // +855992.96599660755146336302506332246623424823099755 z^19
0951 
0952   //! Evaluate Functor.
0953   lambert_w0_small_z_series_term<T> s(z, -pow<18>(z) / 6402373705728000uLL, 18);
0954 
0955   // Temporary to list the coefficients.
0956   //std::cout << " Table of coefficients" << std::endl;
0957   //std::streamsize saved_precision = std::cout.precision(50);
0958   //for (size_t i = 0; i != 19; i++)
0959   //{
0960   //  std::cout << "#" << i << " " << coeff[i] << std::endl;
0961   //}
0962   //std::cout.precision(saved_precision);
0963 
0964   std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>(); // Max iterations from policy.
0965 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0966   std::cout << "max iter from policy = " << max_iter << std::endl;
0967   // //   max iter from policy = 1000000 is default.
0968 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES
0969 
0970   result = sum_series(s, get_epsilon<T, Policy>(), max_iter, result);
0971   // result == evaluate_polynomial.
0972   //sum_series(Functor& func, int bits, std::uintmax_t& max_terms, const U& init_value)
0973   // std::cout << "sum_series(s, get_epsilon<T, Policy>(), max_iter, result); = " << result << std::endl;
0974 
0975   //T epsilon = get_epsilon<T, Policy>();
0976   //std::cout << "epsilon from policy = " << epsilon << std::endl;
0977   // epsilon from policy = 1.93e-34 for T == quad
0978   //  5.35e-51 for t = cpp_bin_float_50
0979 
0980   // std::cout << " get eps = " << get_epsilon<T, Policy>() << std::endl; // quad eps = 1.93e-34, bin_float_50 eps = 5.35e-51
0981   policies::check_series_iterations<T>("boost::math::lambert_w0_small_z<%1%>(%1%)", max_iter, pol);
0982 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES_ITERATIONS
0983   std::cout << "z = " << z << " needed  " << max_iter << " iterations." << std::endl;
0984   std::cout.precision(prec); // Restore.
0985 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_W_SMALL_Z_SERIES_ITERATIONS
0986   return result;
0987 } // template <typename T, typename Policy> inline T lambert_w0_small_z_series(T z, const Policy& pol)
0988 
0989 // Approximate lambert_w0 (used for z values that are outside range of lookup table or rational functions)
0990 // Corless equation 4.19, page 349, and Chapeau-Blondeau equation 20, page 2162.
0991 template <typename T>
0992 inline T lambert_w0_approx(T z)
0993 {
0994   BOOST_MATH_STD_USING
0995   T lz = log(z);
0996   T llz = log(lz);
0997   T w = lz - llz + (llz / lz); // Corless equation 4.19, page 349, and Chapeau-Blondeau equation 20, page 2162.
0998   return w;
0999   // std::cout << "w max " << max_w << std::endl; // double 703.227
1000 }
1001 
1002   //////////////////////////////////////////////////////////////////////////////////////////
1003 
1004 //! \brief Lambert_w0 implementations for float, double and higher precisions.
1005 //! 3rd parameter used to select which version is used.
1006 
1007 //! /details Rational polynomials are provided for several range of argument z.
1008 //! For very small values of z, and for z very near the branch singularity at -e^-1 (~= -0.367879),
1009 //! two other series functions are used.
1010 
1011 //! float precision polynomials are used for 32-bit (usually float) precision (for speed)
1012 //! double precision polynomials are used for 64-bit (usually double) precision.
1013 //! For higher precisions, a 64-bit double approximation is computed first,
1014 //! and then refined using Halley iterations.
1015 
1016 template <typename T>
1017 inline T do_get_near_singularity_param(T z)
1018 {
1019    BOOST_MATH_STD_USING
1020    const T p2 = 2 * (boost::math::constants::e<T>() * z + 1);
1021    const T p = sqrt(p2);
1022    return p;
1023 }
1024 template <typename T, typename Policy>
1025 inline T get_near_singularity_param(T z, const Policy)
1026 {
1027    using value_type = typename policies::evaluation<T, Policy>::type;
1028    return static_cast<T>(do_get_near_singularity_param(static_cast<value_type>(z)));
1029 }
1030 
1031 // Forward declarations:
1032 
1033 //template <typename T, typename Policy> T lambert_w0_small_z(T z, const Policy& pol);
1034 //template <typename T, typename Policy>
1035 //T lambert_w0_imp(T w, const Policy& pol, const std::integral_constant<int, 0>&); // 32 bit usually float.
1036 //template <typename T, typename Policy>
1037 //T lambert_w0_imp(T w, const Policy& pol, const std::integral_constant<int, 1>&); //  64 bit usually double.
1038 //template <typename T, typename Policy>
1039 //T lambert_w0_imp(T w, const Policy& pol, const std::integral_constant<int, 2>&); // 80-bit long double.
1040 
1041 template <typename T>
1042 T lambert_w_positive_rational_float(T z)
1043 {
1044    BOOST_MATH_STD_USING
1045    if (z < 2)
1046    {
1047       if (z < T(0.5))
1048       { // 0.05 < z < 0.5
1049         // Maximum Deviation Found:                     2.993e-08
1050         // Expected Error Term : 2.993e-08
1051         // Maximum Relative Change in Control Points : 7.555e-04 Y offset : -8.196592331e-01
1052          static const T Y = 8.196592331e-01f;
1053          static const T P[] = {
1054             1.803388345e-01f,
1055             -4.820256838e-01f,
1056             -1.068349741e+00f,
1057             -3.506624319e-02f,
1058          };
1059          static const T Q[] = {
1060             1.000000000e+00f,
1061             2.871703469e+00f,
1062             1.690949264e+00f,
1063          };
1064          return z * (Y + boost::math::tools::evaluate_polynomial(P, z) / boost::math::tools::evaluate_polynomial(Q, z));
1065       }
1066       else
1067       { // 0.5 < z < 2
1068         // Max error in interpolated form: 1.018e-08
1069          static const T Y = 5.503368378e-01f;
1070          static const T P[] = {
1071             4.493332766e-01f,
1072             2.543432707e-01f,
1073             -4.808788799e-01f,
1074             -1.244425316e-01f,
1075          };
1076          static const T Q[] = {
1077             1.000000000e+00f,
1078             2.780661241e+00f,
1079             1.830840318e+00f,
1080             2.407221031e-01f,
1081          };
1082          return z * (Y + boost::math::tools::evaluate_rational(P, Q, z));
1083       }
1084    }
1085    else if (z < 6)
1086    {
1087       // 2 < z < 6
1088       // Max error in interpolated form: 2.944e-08
1089       static const T Y = 1.162393570e+00f;
1090       static const T P[] = {
1091          -1.144183394e+00f,
1092          -4.712732855e-01f,
1093          1.563162512e-01f,
1094          1.434010911e-02f,
1095       };
1096       static const T Q[] = {
1097          1.000000000e+00f,
1098          1.192626340e+00f,
1099          2.295580708e-01f,
1100          5.477869455e-03f,
1101       };
1102       return Y + boost::math::tools::evaluate_rational(P, Q, z);
1103    }
1104    else if (z < 18)
1105    {
1106       // 6 < z < 18
1107       // Max error in interpolated form: 5.893e-08
1108       static const T Y = 1.809371948e+00f;
1109       static const T P[] = {
1110          -1.689291769e+00f,
1111          -3.337812742e-01f,
1112          3.151434873e-02f,
1113          1.134178734e-03f,
1114       };
1115       static const T Q[] = {
1116          1.000000000e+00f,
1117          5.716915685e-01f,
1118          4.489521292e-02f,
1119          4.076716763e-04f,
1120       };
1121       return Y + boost::math::tools::evaluate_rational(P, Q, z);
1122    }
1123    else if (z < T(9897.12905874))  // 2.8 < log(z) < 9.2
1124    {
1125       // Max error in interpolated form: 1.771e-08
1126       static const T Y = -1.402973175e+00f;
1127       static const T P[] = {
1128          1.966174312e+00f,
1129          2.350864728e-01f,
1130          -5.098074353e-02f,
1131          -1.054818339e-02f,
1132       };
1133       static const T Q[] = {
1134          1.000000000e+00f,
1135          4.388208264e-01f,
1136          8.316639634e-02f,
1137          3.397187918e-03f,
1138          -1.321489743e-05f,
1139       };
1140       T log_w = log(z);
1141       return log_w + Y + boost::math::tools::evaluate_polynomial(P, log_w) / boost::math::tools::evaluate_polynomial(Q, log_w);
1142    }
1143    else if (z < T(7.896296e+13))  // 9.2 < log(z) <= 32
1144    {
1145       // Max error in interpolated form: 5.821e-08
1146       static const T Y = -2.735729218e+00f;
1147       static const T P[] = {
1148          3.424903470e+00f,
1149          7.525631787e-02f,
1150          -1.427309584e-02f,
1151          -1.435974178e-05f,
1152       };
1153       static const T Q[] = {
1154          1.000000000e+00f,
1155          2.514005579e-01f,
1156          6.118994652e-03f,
1157          -1.357889535e-05f,
1158          7.312865624e-08f,
1159       };
1160       T log_w = log(z);
1161       return log_w + Y + boost::math::tools::evaluate_polynomial(P, log_w) / boost::math::tools::evaluate_polynomial(Q, log_w);
1162    }
1163 
1164     // Max error in interpolated form: 1.491e-08
1165     static const T Y = -4.012863159e+00f;
1166     static const T P[] = {
1167         4.431629226e+00f,
1168         2.756690487e-01f,
1169         -2.992956930e-03f,
1170         -4.912259384e-05f,
1171     };
1172     static const T Q[] = {
1173         1.000000000e+00f,
1174         2.015434591e-01f,
1175         4.949426142e-03f,
1176         1.609659944e-05f,
1177         -5.111523436e-09f,
1178     };
1179     T log_w = log(z);
1180     return log_w + Y + boost::math::tools::evaluate_polynomial(P, log_w) / boost::math::tools::evaluate_polynomial(Q, log_w);
1181 
1182 }
1183 
1184 template <typename T, typename Policy>
1185 T lambert_w_negative_rational_float(T z, const Policy& pol)
1186 {
1187    BOOST_MATH_STD_USING
1188    if (z > T(-0.27))
1189    {
1190       if (z < T(-0.051))
1191       {
1192          // -0.27 < z < -0.051
1193          // Max error in interpolated form: 5.080e-08
1194          static const T Y = 1.255809784e+00f;
1195          static const T P[] = {
1196             -2.558083412e-01f,
1197             -2.306524098e+00f,
1198             -5.630887033e+00f,
1199             -3.803974556e+00f,
1200          };
1201          static const T Q[] = {
1202             1.000000000e+00f,
1203             5.107680783e+00f,
1204             7.914062868e+00f,
1205             3.501498501e+00f,
1206          };
1207          return z * (Y + boost::math::tools::evaluate_rational(P, Q, z));
1208       }
1209       else
1210       {
1211          // Very small z so use a series function.
1212          return lambert_w0_small_z(z, pol);
1213       }
1214    }
1215    else if (z > T(-0.3578794411714423215955237701))
1216    { // Very close to branch singularity.
1217      // Max error in interpolated form: 5.269e-08
1218       static const T Y = 1.220928431e-01f;
1219       static const T P[] = {
1220          -1.221787446e-01f,
1221          -6.816155875e+00f,
1222          7.144582035e+01f,
1223          1.128444390e+03f,
1224       };
1225       static const T Q[] = {
1226          1.000000000e+00f,
1227          6.480326790e+01f,
1228          1.869145243e+02f,
1229          -1.361804274e+03f,
1230          1.117826726e+03f,
1231       };
1232       T d = z + 0.367879441171442321595523770161460867445811f;
1233       return -d / (Y + boost::math::tools::evaluate_polynomial(P, d) / boost::math::tools::evaluate_polynomial(Q, d));
1234    }
1235 
1236     return lambert_w_singularity_series(get_near_singularity_param(z, pol));
1237 }
1238 
1239 //! Lambert_w0 @b 'float' implementation, selected when T is 32-bit precision.
1240 template <typename T, typename Policy>
1241 inline T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 1>&)
1242 {
1243   static const char* function = "boost::math::lambert_w0<%1%>"; // For error messages.
1244   BOOST_MATH_STD_USING // Aid ADL of std functions.
1245 
1246   if ((boost::math::isnan)(z))
1247   {
1248     return boost::math::policies::raise_domain_error<T>(function, "Expected a value > -e^-1 (-0.367879...) but got %1%.", z, pol);
1249   }
1250   if ((boost::math::isinf)(z))
1251   {
1252     return boost::math::policies::raise_overflow_error<T>(function, "Expected a finite value but got %1%.", z, pol);
1253   }
1254 
1255    if (z >= T(0.05)) // Fukushima switch point.
1256    // if (z >= 0.045) // 34 terms makes 128-bit 'exact' below 0.045.
1257    { // Normal ranges using several rational polynomials.
1258       return lambert_w_positive_rational_float(z);
1259    }
1260    else if (z <= -0.3678794411714423215955237701614608674458111310f)
1261    {
1262       if (z < -0.3678794411714423215955237701614608674458111310f)
1263          return boost::math::policies::raise_domain_error<T>(function, "Expected z >= -e^-1 (-0.367879...) but got %1%.", z, pol);
1264       return -1;
1265    }
1266 
1267    return lambert_w_negative_rational_float(z, pol);
1268 } // T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 1>&) for 32-bit usually float.
1269 
1270 template <typename T>
1271 T lambert_w_positive_rational_double(T z)
1272 {
1273    BOOST_MATH_STD_USING
1274    if (z < 2)
1275    {
1276       if (z < 0.5)
1277       {
1278          // Max error in interpolated form: 2.255e-17
1279          static const T offset = 8.19659233093261719e-01;
1280          static const T P[] = {
1281             1.80340766906685177e-01,
1282             3.28178241493119307e-01,
1283             -2.19153620687139706e+00,
1284             -7.24750929074563990e+00,
1285             -7.28395876262524204e+00,
1286             -2.57417169492512916e+00,
1287             -2.31606948888704503e-01
1288          };
1289          static const T Q[] = {
1290             1.00000000000000000e+00,
1291             7.36482529307436604e+00,
1292             2.03686007856430677e+01,
1293             2.62864592096657307e+01,
1294             1.59742041380858333e+01,
1295             4.03760534788374589e+00,
1296             2.91327346750475362e-01
1297          };
1298          return z * (offset + boost::math::tools::evaluate_polynomial(P, z) / boost::math::tools::evaluate_polynomial(Q, z));
1299       }
1300       else
1301       {
1302          // Max error in interpolated form: 3.806e-18
1303          static const T offset = 5.50335884094238281e-01;
1304          static const T P[] = {
1305             4.49664083944098322e-01,
1306             1.90417666196776909e+00,
1307             1.99951368798255994e+00,
1308             -6.91217310299270265e-01,
1309             -1.88533935998617058e+00,
1310             -7.96743968047750836e-01,
1311             -1.02891726031055254e-01,
1312             -3.09156013592636568e-03
1313          };
1314          static const T Q[] = {
1315             1.00000000000000000e+00,
1316             6.45854489419584014e+00,
1317             1.54739232422116048e+01,
1318             1.72606164253337843e+01,
1319             9.29427055609544096e+00,
1320             2.29040824649748117e+00,
1321             2.21610620995418981e-01,
1322             5.70597669908194213e-03
1323          };
1324          return z * (offset + boost::math::tools::evaluate_rational(P, Q, z));
1325       }
1326    }
1327    else if (z < 6)
1328    {
1329       // 2 < z < 6
1330       // Max error in interpolated form: 1.216e-17
1331       static const T Y = 1.16239356994628906e+00;
1332       static const T P[] = {
1333          -1.16230494982099475e+00,
1334          -3.38528144432561136e+00,
1335          -2.55653717293161565e+00,
1336          -3.06755172989214189e-01,
1337          1.73149743765268289e-01,
1338          3.76906042860014206e-02,
1339          1.84552217624706666e-03,
1340          1.69434126904822116e-05,
1341       };
1342       static const T Q[] = {
1343          1.00000000000000000e+00,
1344          3.77187616711220819e+00,
1345          4.58799960260143701e+00,
1346          2.24101228462292447e+00,
1347          4.54794195426212385e-01,
1348          3.60761772095963982e-02,
1349          9.25176499518388571e-04,
1350          4.43611344705509378e-06,
1351       };
1352       return Y + boost::math::tools::evaluate_rational(P, Q, z);
1353    }
1354    else if (z < 18)
1355    {
1356       // 6 < z < 18
1357       // Max error in interpolated form: 1.985e-19
1358       static const T offset = 1.80937194824218750e+00;
1359       static const T P[] =
1360       {
1361          -1.80690935424793635e+00,
1362          -3.66995929380314602e+00,
1363          -1.93842957940149781e+00,
1364          -2.94269984375794040e-01,
1365          1.81224710627677778e-03,
1366          2.48166798603547447e-03,
1367          1.15806592415397245e-04,
1368          1.43105573216815533e-06,
1369          3.47281483428369604e-09
1370       };
1371       static const T Q[] = {
1372          1.00000000000000000e+00,
1373          2.57319080723908597e+00,
1374          1.96724528442680658e+00,
1375          5.84501352882650722e-01,
1376          7.37152837939206240e-02,
1377          3.97368430940416778e-03,
1378          8.54941838187085088e-05,
1379          6.05713225608426678e-07,
1380          8.17517283816615732e-10
1381       };
1382       return offset + boost::math::tools::evaluate_rational(P, Q, z);
1383    }
1384    else if (z < 9897.12905874)  // 2.8 < log(z) < 9.2
1385    {
1386       // Max error in interpolated form: 1.195e-18
1387       static const T Y = -1.40297317504882812e+00;
1388       static const T P[] = {
1389          1.97011826279311924e+00,
1390          1.05639945701546704e+00,
1391          3.33434529073196304e-01,
1392          3.34619153200386816e-02,
1393          -5.36238353781326675e-03,
1394          -2.43901294871308604e-03,
1395          -2.13762095619085404e-04,
1396          -4.85531936495542274e-06,
1397          -2.02473518491905386e-08,
1398       };
1399       static const T Q[] = {
1400          1.00000000000000000e+00,
1401          8.60107275833921618e-01,
1402          4.10420467985504373e-01,
1403          1.18444884081994841e-01,
1404          2.16966505556021046e-02,
1405          2.24529766630769097e-03,
1406          9.82045090226437614e-05,
1407          1.36363515125489502e-06,
1408          3.44200749053237945e-09,
1409       };
1410       T log_w = log(z);
1411       return log_w + Y + boost::math::tools::evaluate_rational(P, Q, log_w);
1412    }
1413    else if (z < 7.896296e+13)  // 9.2 < log(z) <= 32
1414    {
1415       // Max error in interpolated form: 6.529e-18
1416       static const T Y = -2.73572921752929688e+00;
1417       static const T P[] = {
1418          3.30547638424076217e+00,
1419          1.64050071277550167e+00,
1420          4.57149576470736039e-01,
1421          4.03821227745424840e-02,
1422          -4.99664976882514362e-04,
1423          -1.28527893803052956e-04,
1424          -2.95470325373338738e-06,
1425          -1.76662025550202762e-08,
1426          -1.98721972463709290e-11,
1427       };
1428       static const T Q[] = {
1429          1.00000000000000000e+00,
1430          6.91472559412458759e-01,
1431          2.48154578891676774e-01,
1432          4.60893578284335263e-02,
1433          3.60207838982301946e-03,
1434          1.13001153242430471e-04,
1435          1.33690948263488455e-06,
1436          4.97253225968548872e-09,
1437          3.39460723731970550e-12,
1438       };
1439       T log_w = log(z);
1440       return log_w + Y + boost::math::tools::evaluate_rational(P, Q, log_w);
1441    }
1442    else if (z < 2.6881171e+43) // 32 < log(z) < 100
1443    {
1444       // Max error in interpolated form: 2.015e-18
1445       static const T Y = -4.01286315917968750e+00;
1446       static const T P[] = {
1447          5.07714858354309672e+00,
1448          -3.32994414518701458e+00,
1449          -8.61170416909864451e-01,
1450          -4.01139705309486142e-02,
1451          -1.85374201771834585e-04,
1452          1.08824145844270666e-05,
1453          1.17216905810452396e-07,
1454          2.97998248101385990e-10,
1455          1.42294856434176682e-13,
1456       };
1457       static const T Q[] = {
1458          1.00000000000000000e+00,
1459          -4.85840770639861485e-01,
1460          -3.18714850604827580e-01,
1461          -3.20966129264610534e-02,
1462          -1.06276178044267895e-03,
1463          -1.33597828642644955e-05,
1464          -6.27900905346219472e-08,
1465          -9.35271498075378319e-11,
1466          -2.60648331090076845e-14,
1467       };
1468       T log_w = log(z);
1469       return log_w + Y + boost::math::tools::evaluate_rational(P, Q, log_w);
1470    }
1471    else // 100 < log(z) < 710
1472    {
1473       // Max error in interpolated form: 5.277e-18
1474       static const T Y = -5.70115661621093750e+00;
1475       static const T P[] = {
1476          6.42275660145116698e+00,
1477          1.33047964073367945e+00,
1478          6.72008923401652816e-02,
1479          1.16444069958125895e-03,
1480          7.06966760237470501e-06,
1481          5.48974896149039165e-09,
1482          -7.00379652018853621e-11,
1483          -1.89247635913659556e-13,
1484          -1.55898770790170598e-16,
1485          -4.06109208815303157e-20,
1486          -2.21552699006496737e-24,
1487       };
1488       static const T Q[] = {
1489          1.00000000000000000e+00,
1490          3.34498588416632854e-01,
1491          2.51519862456384983e-02,
1492          6.81223810622416254e-04,
1493          7.94450897106903537e-06,
1494          4.30675039872881342e-08,
1495          1.10667669458467617e-10,
1496          1.31012240694192289e-13,
1497          6.53282047177727125e-17,
1498          1.11775518708172009e-20,
1499          3.78250395617836059e-25,
1500       };
1501       T log_w = log(z);
1502       return log_w + Y + boost::math::tools::evaluate_rational(P, Q, log_w);
1503    }
1504 }
1505 
1506 template <typename T, typename Policy>
1507 T lambert_w_negative_rational_double(T z, const Policy& pol)
1508 {
1509    BOOST_MATH_STD_USING
1510    if (z > -0.1)
1511    {
1512       if (z < -0.051)
1513       {
1514          // -0.1 < z < -0.051
1515          // Maximum Deviation Found:                     4.402e-22
1516          // Expected Error Term : 4.240e-22
1517          // Maximum Relative Change in Control Points : 4.115e-03
1518          static const T Y = 1.08633995056152344e+00;
1519          static const T P[] = {
1520             -8.63399505615014331e-02,
1521             -1.64303871814816464e+00,
1522             -7.71247913918273738e+00,
1523             -1.41014495545382454e+01,
1524             -1.02269079949257616e+01,
1525             -2.17236002836306691e+00,
1526          };
1527          static const T Q[] = {
1528             1.00000000000000000e+00,
1529             7.44775406945739243e+00,
1530             2.04392643087266541e+01,
1531             2.51001961077774193e+01,
1532             1.31256080849023319e+01,
1533             2.11640324843601588e+00,
1534          };
1535          return z * (Y + boost::math::tools::evaluate_rational(P, Q, z));
1536       }
1537       else
1538       {
1539          // Very small z > 0.051:
1540          return lambert_w0_small_z(z, pol);
1541       }
1542    }
1543    else if (z > -0.2)
1544    {
1545       // -0.2 < z < -0.1
1546       // Maximum Deviation Found:                     2.898e-20
1547       // Expected Error Term : 2.873e-20
1548       // Maximum Relative Change in Control Points : 3.779e-04
1549       static const T Y = 1.20359611511230469e+00;
1550       static const T P[] = {
1551          -2.03596115108465635e-01,
1552          -2.95029082937201859e+00,
1553          -1.54287922188671648e+01,
1554          -3.81185809571116965e+01,
1555          -4.66384358235575985e+01,
1556          -2.59282069989642468e+01,
1557          -4.70140451266553279e+00,
1558       };
1559       static const T Q[] = {
1560          1.00000000000000000e+00,
1561          9.57921436074599929e+00,
1562          3.60988119290234377e+01,
1563          6.73977699505546007e+01,
1564          6.41104992068148823e+01,
1565          2.82060127225153607e+01,
1566          4.10677610657724330e+00,
1567       };
1568       return z * (Y + boost::math::tools::evaluate_rational(P, Q, z));
1569    }
1570    else if (z > -0.3178794411714423215955237)
1571    {
1572       // Max error in interpolated form: 6.996e-18
1573       static const T Y = 3.49680423736572266e-01;
1574       static const T P[] = {
1575          -3.49729841718749014e-01,
1576          -6.28207407760709028e+01,
1577          -2.57226178029669171e+03,
1578          -2.50271008623093747e+04,
1579          1.11949239154711388e+05,
1580          1.85684566607844318e+06,
1581          4.80802490427638643e+06,
1582          2.76624752134636406e+06,
1583       };
1584       static const T Q[] = {
1585          1.00000000000000000e+00,
1586          1.82717661215113000e+02,
1587          8.00121119810280100e+03,
1588          1.06073266717010129e+05,
1589          3.22848993926057721e+05,
1590          -8.05684814514171256e+05,
1591          -2.59223192927265737e+06,
1592          -5.61719645211570871e+05,
1593          6.27765369292636844e+04,
1594       };
1595       T d = z + 0.367879441171442321595523770161460867445811;
1596       return -d / (Y + boost::math::tools::evaluate_polynomial(P, d) / boost::math::tools::evaluate_polynomial(Q, d));
1597    }
1598    else if (z > -0.3578794411714423215955237701)
1599    {
1600       // Max error in interpolated form: 1.404e-17
1601       static const T Y = 5.00126481056213379e-02;
1602       static const T  P[] = {
1603          -5.00173570682372162e-02,
1604          -4.44242461870072044e+01,
1605          -9.51185533619946042e+03,
1606          -5.88605699015429386e+05,
1607          -1.90760843597427751e+06,
1608          5.79797663818311404e+08,
1609          1.11383352508459134e+10,
1610          5.67791253678716467e+10,
1611          6.32694500716584572e+10,
1612       };
1613       static const T Q[] = {
1614          1.00000000000000000e+00,
1615          9.08910517489981551e+02,
1616          2.10170163753340133e+05,
1617          1.67858612416470327e+07,
1618          4.90435561733227953e+08,
1619          4.54978142622939917e+09,
1620          2.87716585708739168e+09,
1621          -4.59414247951143131e+10,
1622          -1.72845216404874299e+10,
1623       };
1624       T d = z + 0.36787944117144232159552377016146086744581113103176804;
1625       return -d / (Y + boost::math::tools::evaluate_polynomial(P, d) / boost::math::tools::evaluate_polynomial(Q, d));
1626    }
1627    else
1628    {  // z is very close (within 0.01) of the singularity at -e^-1,
1629       // so use a series expansion from R. M. Corless et al.
1630       const T p2 = 2 * (boost::math::constants::e<T>() * z + 1);
1631       const T p = sqrt(p2);
1632       return lambert_w_detail::lambert_w_singularity_series(p);
1633    }
1634 }
1635 
1636 //! Lambert_w0 @b 'double' implementation, selected when T is 64-bit precision.
1637 template <typename T, typename Policy>
1638 inline T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 2>&)
1639 {
1640    static const char* function = "boost::math::lambert_w0<%1%>";
1641    BOOST_MATH_STD_USING // Aid ADL of std functions.
1642 
1643    // Detect unusual case of 32-bit double with a wider/64-bit long double
1644    static_assert(std::numeric_limits<double>::digits >= 53,
1645    "Our double precision coefficients will be truncated, "
1646    "please file a bug report with details of your platform's floating point types "
1647    "- or possibly edit the coefficients to have "
1648    "an appropriate size-suffix for 64-bit floats on your platform - L?");
1649 
1650     if ((boost::math::isnan)(z))
1651     {
1652       return boost::math::policies::raise_domain_error<T>(function, "Expected a value > -e^-1 (-0.367879...) but got %1%.", z, pol);
1653     }
1654     if ((boost::math::isinf)(z))
1655     {
1656       return boost::math::policies::raise_overflow_error<T>(function, "Expected a finite value but got %1%.", z, pol);
1657     }
1658 
1659    if (z >= 0.05)
1660    {
1661       return lambert_w_positive_rational_double(z);
1662    }
1663    else if (z <= -0.36787944117144232159552377016146086744581113103176804) // Precision is max_digits10(cpp_bin_float_50).
1664    {
1665       if (z < -0.36787944117144232159552377016146086744581113103176804)
1666       {
1667          return boost::math::policies::raise_domain_error<T>(function, "Expected z >= -e^-1 (-0.367879...) but got %1%.", z, pol);
1668       }
1669       return -1;
1670    }
1671    else
1672    {
1673       return lambert_w_negative_rational_double(z, pol);
1674    }
1675 } // T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 2>&) 64-bit precision, usually double.
1676 
1677 //! lambert_W0 implementation for extended precision types including
1678 //! long double (80-bit and 128-bit), ???
1679 //! quad float128, Boost.Multiprecision types like cpp_bin_float_quad, cpp_bin_float_50...
1680 
1681 template <typename T, typename Policy>
1682 inline T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 0>&)
1683 {
1684    static const char* function = "boost::math::lambert_w0<%1%>";
1685    BOOST_MATH_STD_USING // Aid ADL of std functions.
1686 
1687    // Filter out special cases first:
1688    if ((boost::math::isnan)(z))
1689    {
1690       return boost::math::policies::raise_domain_error<T>(function, "Expected z >= -e^-1 (-0.367879...) but got %1%.", z, pol);
1691    }
1692    if (fabs(z) <= 0.05f)
1693    {
1694       // Very small z:
1695       return lambert_w0_small_z(z, pol);
1696    }
1697    if (z > (std::numeric_limits<double>::max)())
1698    {
1699       if ((boost::math::isinf)(z))
1700       {
1701          return policies::raise_overflow_error<T>(function, nullptr, pol);
1702          // Or might return infinity if available else max_value,
1703          // but other Boost.Math special functions raise overflow.
1704       }
1705       // z is larger than the largest double, so cannot use the polynomial to get an approximation,
1706       // so use the asymptotic approximation and Halley iterate:
1707 
1708      T w = lambert_w0_approx(z);  // Make an inline function as also used elsewhere.
1709       //T lz = log(z);
1710       //T llz = log(lz);
1711       //T w = lz - llz + (llz / lz); // Corless equation 4.19, page 349, and Chapeau-Blondeau equation 20, page 2162.
1712       return lambert_w_halley_iterate(w, z);
1713    }
1714    if (z < -0.3578794411714423215955237701)
1715    { // Very close to branch point so rational polynomials are not usable.
1716       if (z <= -boost::math::constants::exp_minus_one<T>())
1717       {
1718          if (z == -boost::math::constants::exp_minus_one<T>())
1719          { // Exactly at the branch point singularity.
1720             return -1;
1721          }
1722          return boost::math::policies::raise_domain_error<T>(function, "Expected z >= -e^-1 (-0.367879...) but got %1%.", z, pol);
1723       }
1724       // z is very close (within 0.01) of the branch singularity at -e^-1
1725       // so use a series approximation proposed by Corless et al.
1726       const T p2 = 2 * (boost::math::constants::e<T>() * z + 1);
1727       const T p = sqrt(p2);
1728       T w = lambert_w_detail::lambert_w_singularity_series(p);
1729       return lambert_w_halley_iterate(w, z);
1730    }
1731 
1732    // Phew!  If we get here we are in the normal range of the function,
1733    // so get a double precision approximation first, then iterate to full precision of T.
1734    // We define a tag_type that is:
1735    // true_type if there are so many digits precision wanted that iteration is necessary.
1736    // false_type if a single Halley step is sufficient.
1737 
1738    using precision_type = typename policies::precision<T, Policy>::type;
1739    using tag_type = std::integral_constant<bool,
1740       (precision_type::value == 0) || (precision_type::value > 113) ?
1741       true // Unknown at compile-time, variable/arbitrary, or more than float128 or cpp_bin_quad 128-bit precision.
1742       : false // float, double, float128, cpp_bin_quad 128-bit, so single Halley step.
1743    >;
1744 
1745    // For speed, we also cast z to type double when that is possible
1746    //   if (std::is_constructible<double, T>() == true).
1747    T w = lambert_w0_imp(maybe_reduce_to_double(z, std::is_constructible<double, T>()), pol, std::integral_constant<int, 2>());
1748 
1749    return lambert_w_maybe_halley_iterate(w, z, tag_type());
1750 
1751 } // T lambert_w0_imp(T z, const Policy& pol, const std::integral_constant<int, 0>&)  all extended precision types.
1752 
1753   // Lambert w-1 implementation
1754 // ==============================================================================================
1755 
1756   //! Lambert W for W-1 branch, -max(z) < z <= -1/e.
1757   // TODO is -max(z) allowed?
1758 template<typename T, typename Policy>
1759 T lambert_wm1_imp(const T z, const Policy&  pol)
1760 {
1761   // Catch providing an integer value as parameter x to lambert_w, for example, lambert_w(1).
1762   // Need to ensure it is a floating-point type (of the desired type, float 1.F, double 1., or long double 1.L),
1763   // or static_casted integer, for example:  static_cast<float>(1) or static_cast<cpp_dec_float_50>(1).
1764   // Want to allow fixed_point types too, so do not just test for floating-point.
1765   // Integral types should be promoted to double by user Lambert w functions.
1766   // If integral type provided to user function lambert_w0 or lambert_wm1,
1767   // then should already have been promoted to double.
1768   static_assert(!std::is_integral<T>::value,
1769     "Must be floating-point or fixed type (not integer type), for example: lambert_wm1(1.), not lambert_wm1(1)!");
1770 
1771   BOOST_MATH_STD_USING // Aid argument dependent lookup (ADL) of abs.
1772 
1773   const char* function = "boost::math::lambert_wm1<RealType>(<RealType>)"; // Used for error messages.
1774 
1775   // Check for edge and corner cases first:
1776   if ((boost::math::isnan)(z))
1777   {
1778     return policies::raise_domain_error(function,
1779       "Argument z is NaN!",
1780       z, pol);
1781   } // isnan
1782 
1783   if ((boost::math::isinf)(z))
1784   {
1785     return policies::raise_domain_error(function,
1786       "Argument z is infinite!",
1787       z, pol);
1788   } // isinf
1789 
1790   if (z == static_cast<T>(0))
1791   { // z is exactly zero so return -std::numeric_limits<T>::infinity();
1792     if (std::numeric_limits<T>::has_infinity)
1793     {
1794       return -std::numeric_limits<T>::infinity();
1795     }
1796     else
1797     {
1798       return -tools::max_value<T>();
1799     }
1800   }
1801   if (boost::math::detail::has_denorm_now<T>())
1802   { // All real types except arbitrary precision.
1803     if (!(boost::math::isnormal)(z))
1804     { // Almost zero - might also just return infinity like z == 0 or max_value?
1805       return policies::raise_overflow_error(function,
1806         "Argument z =  %1% is denormalized! (must be z > (std::numeric_limits<RealType>::min)() or z == 0)",
1807         z, pol);
1808     }
1809   }
1810 
1811   if (z > static_cast<T>(0))
1812   { //
1813     return policies::raise_domain_error(function,
1814       "Argument z = %1% is out of range (z <= 0) for Lambert W-1 branch! (Try Lambert W0 branch?)",
1815       z, pol);
1816   }
1817   if (z > -boost::math::tools::min_value<T>())
1818   { // z is denormalized, so cannot be computed.
1819     // -std::numeric_limits<T>::min() is smallest for type T,
1820     // for example, for double: lambert_wm1(-2.2250738585072014e-308) = -714.96865723796634
1821     return policies::raise_overflow_error(function,
1822       "Argument z = %1% is too small (z < -std::numeric_limits<T>::min so denormalized) for Lambert W-1 branch!",
1823       z, pol);
1824   }
1825   if (z == -boost::math::constants::exp_minus_one<T>()) // == singularity/branch point z = -exp(-1) = -3.6787944.
1826   { // At singularity, so return exactly -1.
1827     return -static_cast<T>(1);
1828   }
1829   // z is too negative for the W-1 (or W0) branch.
1830   if (z < -boost::math::constants::exp_minus_one<T>()) // > singularity/branch point z = -exp(-1) = -3.6787944.
1831   {
1832     return policies::raise_domain_error(function,
1833       "Argument z = %1% is out of range (z < -exp(-1) = -3.6787944... <= 0) for Lambert W-1 (or W0) branch!",
1834       z, pol);
1835   }
1836   if (z < static_cast<T>(-0.35))
1837   { // Close to singularity/branch point z = -0.3678794411714423215955237701614608727 but on W-1 branch.
1838     const T p2 = 2 * (boost::math::constants::e<T>() * z + 1);
1839     if (p2 == 0)
1840     { // At the singularity at branch point.
1841       return -1;
1842     }
1843     if (p2 > 0)
1844     {
1845       T w_series = lambert_w_singularity_series(T(-sqrt(p2)));
1846       if (boost::math::tools::digits<T>() > 53)
1847       { // Multiprecision, so try a Halley refinement.
1848         w_series = lambert_w_detail::lambert_w_halley_iterate(w_series, z);
1849 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1_NOT_BUILTIN
1850         std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
1851         std::cout << "Lambert W-1 Halley updated to " << w_series << std::endl;
1852         std::cout.precision(saved_precision);
1853 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_WM1_NOT_BUILTIN
1854       }
1855       return w_series;
1856     }
1857     // Should not get here.
1858     return policies::raise_domain_error(function,
1859       "Argument z = %1% is out of range for Lambert W-1 branch. (Should not get here - please report!)",
1860       z, pol);
1861   } // if (z < -0.35)
1862 
1863   using lambert_w_lookup::wm1es;
1864   using lambert_w_lookup::wm1zs;
1865   using lambert_w_lookup::noof_wm1zs; // size == 64
1866 
1867   // std::cout <<" Wm1zs[63] (== G[64]) = " << " " << wm1zs[63] << std::endl; // Wm1zs[63] (== G[64]) =  -1.0264389699511283e-26
1868   // Check that z argument value is not smaller than lookup_table G[64]
1869   // std::cout << "(z > wm1zs[63]) = " << std::boolalpha << (z > wm1zs[63]) << std::endl;
1870 
1871   if (z >= T(wm1zs[63])) // wm1zs[63]  = -1.0264389699511282259046957018510946438e-26L  W = 64.00000000000000000
1872   {  // z >= -1.0264389699511303e-26 (but z != 0 and z >= std::numeric_limits<T>::min() and so NOT denormalized).
1873 
1874     // Some info on Lambert W-1 values for extreme values of z.
1875     // std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
1876     // std::cout << "-std::numeric_limits<float>::min() = " << -(std::numeric_limits<float>::min)() << std::endl;
1877     // std::cout << "-std::numeric_limits<double>::min() = " << -(std::numeric_limits<double>::min)() << std::endl;
1878     // -std::numeric_limits<float>::min() = -1.1754943508222875e-38
1879     // -std::numeric_limits<double>::min() = -2.2250738585072014e-308
1880     // N[productlog(-1, -1.1754943508222875 * 10^-38 ), 50] = -91.856775324595479509567756730093823993834155027858
1881     // N[productlog(-1, -2.2250738585072014e-308 * 10^-308 ), 50] = -1424.8544521230553853558132180518404363617968042942
1882     // N[productlog(-1, -1.4325445274604020119111357113179868158* 10^-27), 37] = -65.99999999999999999999999999999999955
1883 
1884     // R.M.Corless, G.H.Gonnet, D.E.G.Hare, D.J.Jeffrey, and D.E.Knuth,
1885     // On the Lambert W function, Adv.Comput.Math., vol. 5, pp. 329, 1996.
1886     // Francois Chapeau-Blondeau and Abdelilah Monir
1887     // Numerical Evaluation of the Lambert W Function
1888     // IEEE Transactions On Signal Processing, VOL. 50, NO. 9, Sep 2002
1889     // https://pdfs.semanticscholar.org/7a5a/76a9369586dd0dd34dda156d8f2779d1fd59.pdf
1890     // Estimate Lambert W using ln(-z)  ...
1891     // This is roughly the power of ten * ln(10) ~= 2.3.   n ~= 10^n
1892     //  and improve by adding a second term -ln(ln(-z))
1893     T guess; // bisect lowest possible Gk[=64] (for lookup_t type)
1894     T lz = log(-z);
1895     T llz = log(-lz);
1896     guess = lz - llz + (llz / lz); // Chapeau-Blondeau equation 20, page 2162.
1897 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1_TINY
1898     std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
1899     std::cout << "z = " << z << ", guess = " << guess << ", ln(-z) = " << lz << ", ln(-ln(-z) = " << llz << ", llz/lz = " << (llz / lz) << std::endl;
1900     // z = -1.0000000000000001e-30, guess = -73.312782616731482, ln(-z) = -69.077552789821368, ln(-ln(-z) = 4.2352298269101114, llz/lz = -0.061311231447304194
1901     // z = -9.9999999999999999e-91, guess = -212.56650048504233, ln(-z) = -207.23265836946410, ln(-ln(-z) = 5.3338421155782205, llz/lz = -0.025738424423764311
1902     // >z = -2.2250738585072014e-308, guess = -714.95942238244606, ln(-z) = -708.39641853226408, ln(-ln(-z) = 6.5630038501819854, llz/lz = -0.0092645920821846622
1903     int d10 = policies::digits_base10<T, Policy>(); // policy template parameter digits10
1904     int d2 = policies::digits<T, Policy>(); // digits base 2 from policy.
1905     std::cout << "digits10 = " << d10 << ", digits2 = " << d2 // For example: digits10 = 1, digits2 = 5
1906       << std::endl;
1907     std::cout.precision(saved_precision);
1908 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_WM1_TINY
1909     if (policies::digits<T, Policy>() < 12)
1910     { // 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.
1911       return guess;
1912     }
1913     T result = lambert_w_detail::lambert_w_halley_iterate(guess, z);
1914     return result;
1915 
1916     // Was Fukushima
1917     // G[k=64] == g[63] == -1.02643897e-26
1918     //return policies::raise_domain_error(function,
1919     //  "Argument z = %1% is too small (< -1.02643897e-26) ! (Should not occur, please report.",
1920     //  z, pol);
1921   } // Z too small so use approximation and Halley.
1922     // Else Use a lookup table to find the nearest integer part of Lambert W-1 as starting point for Bisection.
1923 
1924   if (boost::math::tools::digits<T>() > 53)
1925   { // T is more precise than 64-bit double (or long double, or ?),
1926     // so compute an approximate value using only one Schroeder refinement,
1927     // (avoiding any double-precision Halley refinement from policy double_digits2<50> 53 - 3 = 50
1928     // because are next going to use Halley refinement at full/high precision using this as an approximation).
1929     using boost::math::policies::precision;
1930     using boost::math::policies::digits10;
1931     using boost::math::policies::digits2;
1932     using boost::math::policies::policy;
1933     // Compute a 50-bit precision approximate W0 in a double (no Halley refinement).
1934     T double_approx(static_cast<T>(lambert_wm1_imp(must_reduce_to_double(z, std::is_constructible<double, T>()), policy<digits2<50>>())));
1935 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1_NOT_BUILTIN
1936     std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
1937     std::cout << "Lambert_wm1 Argument Type " << typeid(T).name() << " approximation double = " << double_approx << std::endl;
1938     std::cout.precision(saved_precision);
1939 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_WM1
1940     // Perform additional Halley refinement(s) to ensure that
1941     // get a near as possible to correct result (usually +/- one epsilon).
1942     T result = lambert_w_halley_iterate(double_approx, z);
1943 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1
1944     std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
1945     std::cout << "Result " << typeid(T).name() << " precision Halley refinement =    " << result << std::endl;
1946     std::cout.precision(saved_precision);
1947 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_WM1
1948     return result;
1949   } // digits > 53  - higher precision than double.
1950   else // T is double or less precision.
1951   { // Use a lookup table to find the nearest integer part of Lambert W as starting point for Bisection.
1952     using namespace boost::math::lambert_w_detail::lambert_w_lookup;
1953     // Bracketing sequence  n = (2, 4, 8, 16, 32, 64) for W-1 branch. (0 is -infinity)
1954     // Since z is probably quite small, start with lowest n (=2).
1955     int n = 2;
1956     if (T(wm1zs[n - 1]) > z)
1957     {
1958       goto bisect;
1959     }
1960     for (int j = 1; j <= 5; ++j)
1961     {
1962       n *= 2;
1963       if (T(wm1zs[n - 1]) > z)
1964       {
1965         goto overshot;
1966       }
1967     }
1968     // else z < g[63] == -1.0264389699511303e-26, so Lambert W-1 integer part > 64.
1969     // This should not now occur (should be caught by test and code above) so should be a logic_error?
1970     return policies::raise_domain_error(function,
1971       "Argument z = %1% is too small (< -1.026439e-26) (logic error - please report!)",
1972       z, pol);
1973   overshot:
1974     {
1975       int nh = n / 2;
1976       for (int j = 1; j <= 5; ++j)
1977       {
1978         nh /= 2; // halve step size.
1979         if (nh <= 0)
1980         {
1981           break; // goto bisect;
1982         }
1983         if (T(wm1zs[n - nh - 1]) > z)
1984         {
1985           n -= nh;
1986         }
1987       }
1988     }
1989   bisect:
1990     --n;
1991     // g[n] now holds lambert W of floor integer n and g[n+1] the ceil part;
1992     // these are used as initial values for bisection.
1993 #ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1_LOOKUP
1994     std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
1995     std::cout << "Result lookup W-1(" << z << ") bisection between wm1zs[" << n - 1 << "] = " << wm1zs[n - 1] << " and wm1zs[" << n << "] = " << wm1zs[n]
1996       << ", bisect mean = " << (wm1zs[n - 1] + wm1zs[n]) / 2 << std::endl;
1997     std::cout.precision(saved_precision);
1998 #endif // BOOST_MATH_INSTRUMENT_LAMBERT_WM1_LOOKUP
1999 
2000     // Compute bisections is the number of bisections computed from n,
2001     // such that a single application of the fifth-order Schroeder update formula
2002     // after the bisections is enough to evaluate Lambert W-1 with (near?) 53-bit accuracy.
2003     // Fukushima established these by trial and error?
2004     int bisections = 11; //  Assume maximum number of bisections will be needed (most common case).
2005     if (n >= 8)
2006     {
2007       bisections = 8;
2008     }
2009     else if (n >= 3)
2010     {
2011       bisections = 9;
2012     }
2013     else if (n >= 2)
2014     {
2015       bisections = 10;
2016     }
2017     // Bracketing, Fukushima section 2.3, page 82:
2018     // (Avoiding using exponential function for speed).
2019     // Only use @c lookup_t precision, default double, for bisection (again for speed),
2020     // and use later Halley refinement for higher precisions.
2021     using lambert_w_lookup::halves;
2022     using lambert_w_lookup::sqrtwm1s;
2023 
2024     using calc_type = typename std::conditional<std::is_constructible<lookup_t, T>::value, lookup_t, T>::type;
2025 
2026     calc_type w = -static_cast<calc_type>(n); // Equation 25,
2027     calc_type y = static_cast<calc_type>(z * T(wm1es[n - 1])); // Equation 26,
2028                                                           // Perform the bisections fractional bisections for necessary precision.
2029     for (int j = 0; j < bisections; ++j)
2030     { // Equation 27.
2031       calc_type wj = w - halves[j]; // Subtract 1/2, 1/4, 1/8 ...
2032       calc_type yj = y * sqrtwm1s[j]; // Multiply by sqrt(1/e), ...
2033       if (wj < yj)
2034       {
2035         w = wj;
2036         y = yj;
2037       }
2038     } // for j
2039     return static_cast<T>(schroeder_update(w, y)); // Schroeder 5th order method refinement.
2040 
2041 //      else // Perform additional Halley refinement(s) to ensure that
2042 //           // get a near as possible to correct result (usually +/- epsilon).
2043 //      {
2044 //       // result = lambert_w_halley_iterate(result, z);
2045 //        result = lambert_w_halley_step(result, z);  // Just one Halley step should be enough.
2046 //#ifdef BOOST_MATH_INSTRUMENT_LAMBERT_WM1_HALLEY
2047 //        std::streamsize saved_precision = std::cout.precision(std::numeric_limits<T>::max_digits10);
2048 //        std::cout << "Halley refinement estimate =    " << result << std::endl;
2049 //        std::cout.precision(saved_precision);
2050 //#endif // BOOST_MATH_INSTRUMENT_LAMBERT_W1_HALLEY
2051 //        return result; // Halley
2052 //      } // Schroeder or Schroeder and Halley.
2053     }
2054   } // template<typename T = double> T lambert_wm1_imp(const T z)
2055 } // namespace lambert_w_detail
2056 
2057 /////////////////////////////  User Lambert w functions. //////////////////////////////
2058 
2059 //! Lambert W0 using User-defined policy.
2060   template <typename T, typename Policy>
2061   inline
2062     typename boost::math::tools::promote_args<T>::type
2063     lambert_w0(T z, const Policy& pol)
2064   {
2065      // Promote integer or expression template arguments to double,
2066      // without doing any other internal promotion like float to double.
2067     using result_type = typename tools::promote_args<T>::type;
2068 
2069     // Work out what precision has been selected,
2070     // based on the Policy and the number type.
2071     using precision_type = typename policies::precision<result_type, Policy>::type;
2072     // and then select the correct implementation based on that precision (not the type T):
2073     using tag_type = std::integral_constant<int,
2074       (precision_type::value == 0) || (precision_type::value > 53) ?
2075         0  // either variable precision (0), or greater than 64-bit precision.
2076       : (precision_type::value <= 24) ? 1 // 32-bit (probably float) precision.
2077       : 2  // 64-bit (probably double) precision.
2078       >;
2079 
2080     return lambert_w_detail::lambert_w0_imp(result_type(z), pol, tag_type()); //
2081   } // lambert_w0(T z, const Policy& pol)
2082 
2083   //! Lambert W0 using default policy.
2084   template <typename T>
2085   inline
2086     typename tools::promote_args<T>::type
2087     lambert_w0(T z)
2088   {
2089     // Promote integer or expression template arguments to double,
2090     // without doing any other internal promotion like float to double.
2091     using result_type = typename tools::promote_args<T>::type;
2092 
2093     // Work out what precision has been selected, based on the Policy and the number type.
2094     // For the default policy version, we want the *default policy* precision for T.
2095     using precision_type = typename policies::precision<result_type, policies::policy<>>::type;
2096     // and then select the correct implementation based on that (not the type T):
2097     using tag_type = std::integral_constant<int,
2098       (precision_type::value == 0) || (precision_type::value > 53) ?
2099       0  // either variable precision (0), or greater than 64-bit precision.
2100       : (precision_type::value <= 24) ? 1 // 32-bit (probably float) precision.
2101       : 2  // 64-bit (probably double) precision.
2102     >;
2103     return lambert_w_detail::lambert_w0_imp(result_type(z),  policies::policy<>(), tag_type());
2104   } // lambert_w0(T z) using default policy.
2105 
2106     //! W-1 branch (-max(z) < z <= -1/e).
2107 
2108     //! Lambert W-1 using User-defined policy.
2109   template <typename T, typename Policy>
2110   inline
2111     typename tools::promote_args<T>::type
2112     lambert_wm1(T z, const Policy& pol)
2113   {
2114     // Promote integer or expression template arguments to double,
2115     // without doing any other internal promotion like float to double.
2116     using result_type = typename tools::promote_args<T>::type;
2117     return lambert_w_detail::lambert_wm1_imp(result_type(z), pol); //
2118   }
2119 
2120   //! Lambert W-1 using default policy.
2121   template <typename T>
2122   inline
2123     typename tools::promote_args<T>::type
2124     lambert_wm1(T z)
2125   {
2126     using result_type = typename tools::promote_args<T>::type;
2127     return lambert_w_detail::lambert_wm1_imp(result_type(z), policies::policy<>());
2128   } // lambert_wm1(T z)
2129 
2130   // First derivative of Lambert W0 and W-1.
2131   template <typename T, typename Policy>
2132   inline typename tools::promote_args<T>::type
2133   lambert_w0_prime(T z, const Policy& pol)
2134   {
2135     using result_type = typename tools::promote_args<T>::type;
2136     using std::numeric_limits;
2137     if (z == 0)
2138     {
2139         return static_cast<result_type>(1);
2140     }
2141     // This is the sensible choice if we regard the Lambert-W function as complex analytic.
2142     // Of course on the real line, it's just undefined.
2143     if (z == - boost::math::constants::exp_minus_one<result_type>())
2144     {
2145         return numeric_limits<result_type>::has_infinity ? numeric_limits<result_type>::infinity() : boost::math::tools::max_value<result_type>();
2146     }
2147     // if z < -1/e, we'll let lambert_w0 do the error handling:
2148     result_type w = lambert_w0(result_type(z), pol);
2149     // If w ~ -1, then presumably this can get inaccurate.
2150     // Is there an accurate way to evaluate 1 + W(-1/e + eps)?
2151     //  Yes: This is discussed in the Princeton Companion to Applied Mathematics,
2152     // 'The Lambert-W function', Section 1.3: Series and Generating Functions.
2153     // 1 + W(-1/e + x) ~ sqrt(2ex).
2154     // Nick is not convinced this formula is more accurate than the naive one.
2155     // However, for z != -1/e, we never get rounded to w = -1 in any precision I've tested (up to cpp_bin_float_100).
2156     return w / (z * (1 + w));
2157   } // lambert_w0_prime(T z)
2158 
2159   template <typename T>
2160   inline typename tools::promote_args<T>::type
2161      lambert_w0_prime(T z)
2162   {
2163      return lambert_w0_prime(z, policies::policy<>());
2164   }
2165 
2166   template <typename T, typename Policy>
2167   inline typename tools::promote_args<T>::type
2168   lambert_wm1_prime(T z, const Policy& pol)
2169   {
2170     using std::numeric_limits;
2171     using result_type = typename tools::promote_args<T>::type;
2172     //if (z == 0)
2173     //{
2174     //      return static_cast<result_type>(1);
2175     //}
2176     //if (z == - boost::math::constants::exp_minus_one<result_type>())
2177     if (z == 0 || z == - boost::math::constants::exp_minus_one<result_type>())
2178     {
2179         return numeric_limits<result_type>::has_infinity ? -numeric_limits<result_type>::infinity() : -boost::math::tools::max_value<result_type>();
2180     }
2181 
2182     result_type w = lambert_wm1(z, pol);
2183     return w/(z*(1+w));
2184   } // lambert_wm1_prime(T z)
2185 
2186   template <typename T>
2187   inline typename tools::promote_args<T>::type
2188      lambert_wm1_prime(T z)
2189   {
2190      return lambert_wm1_prime(z, policies::policy<>());
2191   }
2192 
2193 }} //boost::math namespaces
2194 
2195 #endif // #ifdef BOOST_MATH_SF_LAMBERT_W_HPP
2196