lambert_w_lookup_table_generator.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // Lambert W lookup table generator lambert_w_lookup_table_generator.cpp
  2. //! \file
  3. //! Output a table of precomputed array values for Lambert W0 and W-1,
  4. //! and square roots and halves, and powers of e,
  5. // as a .ipp file for use by lambert_w.hpp.
  6. //! \details Output as long double precision (suffix L) using Boost.Multiprecision
  7. //! to 34 decimal digits precision to cater for platforms that have 128-bit long double.
  8. //! The function bisection can then use any built-in floating-point type,
  9. //! which may have different precision and speed on different platforms.
  10. //! The actual builtin floating-point type of the arrays is chosen by a
  11. //! typedef in \modular-boost\libs\math\include\boost\math\special_functions\lambert_w.hpp
  12. //! by default, for example: typedef double lookup_t;
  13. // This includes lookup tables for both branches W0 and W-1.
  14. // Only W-1 is needed by current code that uses JM rational Polynomials,
  15. // but W0 is kept (for now) to allow comparison with the previous FKDVPB version
  16. // that uses lookup for W0 branch as well as W-1.
  17. #include <boost/config.hpp>
  18. #include <boost/math/constants/constants.hpp> // For exp_minus_one == 3.67879441171442321595523770161460867e-01.
  19. using boost::math::constants::exp_minus_one; // 0.36787944
  20. using boost::math::constants::root_e; // 1.64872
  21. #include <boost/multiprecision/cpp_bin_float.hpp>
  22. using boost::multiprecision::cpp_bin_float_quad;
  23. using boost::multiprecision::cpp_bin_float_50;
  24. #include <iostream>
  25. #include <fstream>
  26. #include <typeinfo>
  27. /*
  28. typedef double lookup_t; // Type for lookup table (double or float?)
  29. BOOST_STATIC_CONSTEXPR std::size_t noof_sqrts = 12;
  30. BOOST_STATIC_CONSTEXPR lookup_t a[noof_sqrts] = // 0.6065 0.7788, 0.8824 ... 0.9997, sqrt of previous elements.
  31. {
  32. 0.60653065971263342, 0.77880078307140487, 0.8824969025845954, 0.93941306281347579, 0.96923323447634408, 0.98449643700540841,
  33. 0.99221793826024351, 0.99610136947011749, 0.99804878110747547, 0.99902391418197566, 0.99951183793988937, 0.99975588917489722
  34. };
  35. BOOST_STATIC_CONSTEXPR lookup_t b[noof_sqrts] = // 0.5 0.25 0.125, 0.0625 ... 0.0002441, halves of previous elements.
  36. { 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, 0.00390625, 0.001953125, 0.0009765625, 0.00048828125, 0.000244140625 };
  37. BOOST_STATIC_CONSTEXPR size_t noof_w0zs = 65;
  38. BOOST_STATIC_CONSTEXPR lookup_t g[noof_w0zs] = // lambert_w[k] for W0 branch. 0 2.7182 14.77 60.2566 ... 1.445e+29 3.990e+29.
  39. { 0., 2.7182818284590452, 14.7781121978613, 60.256610769563003, 218.39260013257696, 742.06579551288302, 2420.5727609564107, 7676.4321089992102,
  40. 23847.663896333826, 72927.755348178456, 220264.65794806717, 658615.558867176, 1953057.4970280471, 5751374.0961159665, 16836459.978306875, 49035260.58708166,
  41. 142177768.32812596, 410634196.81078007, 1181879444.4719492, 3391163718.300558, 9703303908.1958056, 27695130424.147509, 78868082614.895014, 224130479263.72476,
  42. 635738931116.24334, 1800122483434.6468, 5088969845149.8079, 14365302496248.563, 40495197800161.305, 114008694617177.22, 320594237445733.86,
  43. 900514339622670.18, 2526814725845782.2, 7083238132935230.1, 19837699245933466., 55510470830970076., 1.5520433569614703e+17, 4.3360826779369662e+17,
  44. 1.2105254067703227e+18, 3.3771426165357561e+18, 9.4154106734807994e+18, 2.6233583234732253e+19, 7.3049547543861044e+19, 2.032970971338619e+20,
  45. 5.6547040503180956e+20, 1.5720421975868293e+21, 4.3682149334771265e+21, 1.2132170565093317e+22, 3.3680332378068632e+22, 9.3459982052259885e+22,
  46. 2.5923527642935362e+23, 7.1876803203773879e+23, 1.99212416037262e+24, 5.5192924995054165e+24, 1.5286067837683347e+25, 4.2321318958281094e+25,
  47. 1.1713293177672778e+26, 3.2408603996214814e+26, 8.9641258264226028e+26, 2.4787141382364034e+27, 6.8520443388941057e+27, 1.8936217407781711e+28,
  48. 5.2317811346197018e+28, 1.4450833904658542e+29, 3.9904954117194348e+29
  49. };
  50. BOOST_STATIC_CONSTEXPR std::size_t noof_wm1zs = 66;
  51. BOOST_STATIC_CONSTEXPR lookup_t e[noof_wm1zs] = // lambert_w[k] for W-1 branch. 2.7182 1. 0.3678 0.1353 0.04978 ... 4.359e-28 1.603e-28
  52. {
  53. 2.7182818284590452, 1., 0.36787944117144232, 0.13533528323661269, 0.049787068367863943, 0.01831563888873418, 0.0067379469990854671,
  54. 0.0024787521766663584, 0.00091188196555451621, 0.00033546262790251184, 0.00012340980408667955, 4.5399929762484852e-05, 1.6701700790245659e-05,
  55. 6.1442123533282098e-06, 2.2603294069810543e-06, 8.3152871910356788e-07, 3.0590232050182579e-07, 1.1253517471925911e-07, 4.1399377187851667e-08,
  56. 1.5229979744712628e-08, 5.6027964375372675e-09, 2.0611536224385578e-09, 7.5825604279119067e-10, 2.7894680928689248e-10, 1.026187963170189e-10,
  57. 3.7751345442790977e-11, 1.3887943864964021e-11, 5.1090890280633247e-12, 1.8795288165390833e-12, 6.914400106940203e-13, 2.5436656473769229e-13,
  58. 9.3576229688401746e-14, 3.4424771084699765e-14, 1.2664165549094176e-14, 4.6588861451033974e-15, 1.713908431542013e-15, 6.3051167601469894e-16,
  59. 2.3195228302435694e-16, 8.5330476257440658e-17, 3.1391327920480296e-17, 1.1548224173015786e-17, 4.248354255291589e-18, 1.5628821893349888e-18,
  60. 5.7495222642935598e-19, 2.1151310375910805e-19, 7.7811322411337965e-20, 2.8625185805493936e-20, 1.0530617357553812e-20, 3.8739976286871871e-21,
  61. 1.4251640827409351e-21, 5.2428856633634639e-22, 1.9287498479639178e-22, 7.0954741622847041e-23, 2.6102790696677048e-23, 9.602680054508676e-24,
  62. 3.532628572200807e-24, 1.2995814250075031e-24, 4.7808928838854691e-25, 1.7587922024243116e-25, 6.4702349256454603e-26, 2.3802664086944006e-26,
  63. 8.7565107626965203e-27, 3.2213402859925161e-27, 1.185064864233981e-27, 4.359610000063081e-28, 1.6038108905486378e-28
  64. };
  65. lambert_w0 version of array from Fukushima
  66. // lambert_w[k] for W-1 branch. 2.7182 1. 0.3678 0.1353 0.04978 ... 4.359e-28 1.603e-28
  67. e: 2.7182818284590452, 1., 0.36787944117144232, 0.13533528323661269, 0.049787068367863943, 0.01831563888873418, 0.0067379469990854671,
  68. 0.0024787521766663584, 0.00091188196555451621, 0.00033546262790251184, 0.00012340980408667955, 4.5399929762484852e-05, 1.6701700790245659e-05,
  69. 6.1442123533282098e-06, 2.2603294069810543e-06, 8.3152871910356788e-07, 3.0590232050182579e-07, 1.1253517471925911e-07, 4.1399377187851667e-08,
  70. 1.5229979744712628e-08, 5.6027964375372675e-09, 2.0611536224385578e-09, 7.5825604279119067e-10, 2.7894680928689248e-10, 1.026187963170189e-10,
  71. 3.7751345442790977e-11, 1.3887943864964021e-11, 5.1090890280633247e-12, 1.8795288165390833e-12, 6.914400106940203e-13, 2.5436656473769229e-13,
  72. 9.3576229688401746e-14, 3.4424771084699765e-14, 1.2664165549094176e-14, 4.6588861451033974e-15, 1.713908431542013e-15, 6.3051167601469894e-16,
  73. 2.3195228302435694e-16, 8.5330476257440658e-17, 3.1391327920480296e-17, 1.1548224173015786e-17, 4.248354255291589e-18, 1.5628821893349888e-18,
  74. 5.7495222642935598e-19, 2.1151310375910805e-19, 7.7811322411337965e-20, 2.8625185805493936e-20, 1.0530617357553812e-20, 3.8739976286871871e-21,
  75. 1.4251640827409351e-21, 5.2428856633634639e-22, 1.9287498479639178e-22, 7.0954741622847041e-23, 2.6102790696677048e-23, 9.602680054508676e-24,
  76. 3.532628572200807e-24, 1.2995814250075031e-24, 4.7808928838854691e-25, 1.7587922024243116e-25, 6.4702349256454603e-26, 2.3802664086944006e-26,
  77. 8.7565107626965203e-27, 3.2213402859925161e-27, 1.185064864233981e-27, 4.359610000063081e-28, 1.6038108905486378e-28
  78. // lambert_w[k] for W0 branch. 0 2.7182 14.77 60.2566 ... 1.445e+29 3.990e+29.
  79. g: 0, 2.7182818284590452, 14.7781121978613, 60.256610769563003, 218.39260013257696, 742.06579551288302, 2420.5727609564107, 7676.4321089992102,
  80. 23847.663896333826, 72927.755348178456, 220264.65794806717, 658615.558867176, 1953057.4970280471, 5751374.0961159665, 16836459.978306875, 49035260.58708166,
  81. 142177768.32812596, 410634196.81078007, 1181879444.4719492, 3391163718.300558, 9703303908.1958056, 27695130424.147509, 78868082614.895014, 224130479263.72476,
  82. 635738931116.24334, 1800122483434.6468, 5088969845149.8079, 14365302496248.563, 40495197800161.305, 114008694617177.22, 320594237445733.86,
  83. 900514339622670.18, 2526814725845782.2, 7083238132935230.1, 19837699245933466, 55510470830970076, 1.5520433569614703e+17, 4.3360826779369662e+17,
  84. 1.2105254067703227e+18, 3.3771426165357561e+18, 9.4154106734807994e+18, 2.6233583234732253e+19, 7.3049547543861044e+19, 2.032970971338619e+20,
  85. 5.6547040503180956e+20, 1.5720421975868293e+21, 4.3682149334771265e+21, 1.2132170565093317e+22, 3.3680332378068632e+22, 9.3459982052259885e+22,
  86. 2.5923527642935362e+23, 7.1876803203773879e+23, 1.99212416037262e+24, 5.5192924995054165e+24, 1.5286067837683347e+25, 4.2321318958281094e+25,
  87. 1.1713293177672778e+26, 3.2408603996214814e+26, 8.9641258264226028e+26, 2.4787141382364034e+27, 6.8520443388941057e+27, 1.8936217407781711e+28,
  88. 5.2317811346197018e+28, 1.4450833904658542e+29, 3.9904954117194348e+29
  89. lambert_wm1 version of arrays from Fukushima
  90. e: 2.7182817459106445 7.3890557289123535 20.085535049438477 54.59814453125 148.41314697265625 403.42874145507813 1096.6329345703125 2980.957275390625 8103.08154296875 22026.458984375 59874.12109375 162754.734375 442413.21875 1202603.75 3269015.75 8886106 24154940 65659932 178482192 485164896 1318814848 3584910336 9744796672 26489102336 72004845568 195729457152 532047822848 1446255919104 3931331100672 10686465835008 29048824659968 78962889850880 214643389759488 583461240832000 1586012102852608 4311227773747200 11719131799748608 31855901283450880 86593318145753088 2.3538502982225101e+17 6.398428560008151e+17 1.7392731886358364e+18 4.7278345784949473e+18 1.2851586685678387e+19 3.493423319351296e+19 9.4961089747571704e+19 2.581309902546461e+20 7.0167278463083348e+20 1.9073443887231177e+21 5.1846992652160593e+21 1.4093473476000776e+22 3.831003235981371e+22 1.0413746376682761e+23 2.8307496154307266e+23 7.6947746628514896e+23 2.0916565667371597e+24 5.6857119515524837e+24 1.5455367020327599e+25 4.2012039964445827e+25 1.1420056438012293e+26 3.1042929865047826e+26 8.4383428037470738e+26 2.2937792813113457e+27 6.2351382164292627e+27
  91. g: -0.36787945032119751 -0.27067059278488159 -0.14936122298240662 -0.073262564837932587 -0.033689741045236588 -0.014872515574097633 -0.0063831745646893978 -0.0026837014593183994 -0.0011106884339824319 -0.00045399941154755652 -0.00018371877376921475 -7.3730567237362266e-05 -2.9384291337919421e-05 -1.1641405762929935e-05 -4.5885362851549871e-06 -1.8005634956352878e-06 -7.0378973759943619e-07 -2.7413975089984888e-07 -1.0645318582191976e-07 -4.122309249510181e-08 -1.5923385277005764e-08 -6.1368328196920174e-09 -2.3602335641470518e-09 -9.0603280433754207e-10 -3.471987974901225e-10 -1.3283640853956058e-10 -5.0747316071575455e-11 -1.9360334516105304e-11 -7.3766357605586919e-12 -2.8072891233854591e-12 -1.0671687058344537e-12 -4.0525363013271809e-13 -1.5374336461045079e-13 -5.8272932648966574e-14 -2.206792725173521e-14 -8.3502896573240185e-15 -3.1572303958374423e-15 -1.192871523299666e-15 -4.5038112940094517e-16 -1.699343306816689e-16 -6.4078234365689933e-17 -2.4148019279880996e-17 -9.095073346605316e-18 -3.4237017961279004e-18 -1.2881348671140216e-18 -4.8440896082993503e-19 -1.8207810463107454e-19 -6.8407959442757565e-20 -2.569017156788846e-20 -9.6437611040447661e-21 -3.6186962678628536e-21 -1.357346940624028e-21 -5.0894276378983633e-22 -1.9076220526102576e-22 -7.1477077345829229e-23 -2.6773039821769189e-23 -1.0025130740057213e-23 -3.7527418826161672e-24 -1.4043593713279384e-24 -5.2539147015754201e-25 -1.9650207139502987e-25 -7.3474141096711539e-26 -2.7465588329293218e-26 -1.0264406957471058e-26
  92. a: 1.6487212181091309 1.2840254306793213 1.1331484317779541 1.0644944906234741 1.0317434072494507 1.0157476663589478 1.007843017578125 1.0039138793945313 1.0019550323486328 1.0009770393371582 1.0004884004592896 1.000244140625
  93. // These are common to both W0 and W-1
  94. b: 0.5 0.25 0.125 0.0625 0.03125 0.015625 0.0078125 0.00390625 0.001953125 0.0009765625 0.00048828125 0.000244140625
  95. */
  96. // Creates if no file exists, & uses default overwrite/ ios::replace.
  97. //const char filename[] = // "lambert_w_lookup_table.ipp"; // Write to same folder as generator:
  98. //"I:/modular-boost/libs/math/include/boost/math/special_functions/lambert_w_lookup_table.ipp";
  99. const char filename[] = "lambert_w_lookup_table.ipp";
  100. std::ofstream fout(filename, std::ios::out); // File output stream.
  101. // 128-bit precision type (so that full precision if long double type uses 128-bit).
  102. // typedef cpp_bin_float_quad table_lookup_t; // Output using max_digits10 for 37 decimal digit precision.
  103. // (This is the precision for the tables output as a C++ program,
  104. // not the precision used by the lambert_w.hpp, that defines another typedef lookup_t, default double.
  105. typedef cpp_bin_float_50 table_lookup_t; // Compute tables to 50 decimla digit precision to avoid slight inaccuracy from repeated multiply.
  106. // But Output using max_digits10 for 37 decimal digit precision.
  107. int main()
  108. {
  109. std::cout << "Lambert W table lookup values." << std::endl;
  110. if (!fout.is_open())
  111. { // File failed to open OK.
  112. std::cerr << "Open file " << filename << " failed!" << std::endl;
  113. std::cerr << "errno " << errno << std::endl;
  114. return -1;
  115. }
  116. try
  117. {
  118. std::cout << "Lambert W test values writing to file " << filename << std::endl;
  119. int output_precision = std::numeric_limits<cpp_bin_float_quad>::max_digits10; // 37 decimal digits.
  120. fout.precision(output_precision);
  121. fout <<
  122. "// Copyright Paul A. Bristow 2017." "\n"
  123. "// Distributed under the Boost Software License, Version 1.0." "\n"
  124. "// (See accompanying file LICENSE_1_0.txt" "\n"
  125. "// or copy at http://www.boost.org/LICENSE_1_0.txt)" "\n"
  126. "\n"
  127. "// " << filename << "\n\n"
  128. "// A collection of 128-bit precision integral z argument Lambert W values computed using "
  129. << output_precision << " decimal digits precision.\n"
  130. "// C++ floating-point precision is 128-bit long double.\n"
  131. "// Output as "
  132. << std::numeric_limits<table_lookup_t>::max_digits10
  133. << " decimal digits, suffixed L.\n"
  134. "\n"
  135. "// C++ floating-point type is provided by lambert_w.hpp typedef." "\n"
  136. "// For example: typedef lookup_t double; (or float or long double)" "\n"
  137. "\n"
  138. "// Written by " << __FILE__ << " " << __TIMESTAMP__ << "\n"
  139. << std::endl;
  140. fout << "// Sizes of arrays of z values for Lambert W[0], W[1] ... W[64]"
  141. "\"n""and W[-1], W[-2] ... W[-64]." << std::endl;
  142. fout << "\nnamespace boost {\nnamespace math {\nnamespace lambert_w_detail {\nnamespace lambert_w_lookup\n{ \n";
  143. BOOST_STATIC_CONSTEXPR std::size_t noof_sqrts = 12;
  144. BOOST_STATIC_CONSTEXPR std::size_t noof_halves = 12;
  145. fout << "BOOST_STATIC_CONSTEXPR std::size_t noof_sqrts = " << noof_sqrts << ";" << std::endl;
  146. fout << "BOOST_STATIC_CONSTEXPR std::size_t noof_halves = " << noof_halves << ";" << std::endl; // Common to both branches.
  147. BOOST_STATIC_CONSTEXPR std::size_t noof_w0zs = 65; // F[k] 0 <= k <= 64. f[0] = F[0], f[64] = F[64]
  148. BOOST_STATIC_CONSTEXPR std::size_t noof_w0es = 66; // noof_w0zs +1 for gratuitous extra power.
  149. BOOST_STATIC_CONSTEXPR std::size_t noof_wm1zs = 64; // G[k] 1 <= k <= 64. (W-1 = 0 would be z = -infinity, so not stored in array) g[0] == G[1], g[63] = G[64]
  150. BOOST_STATIC_CONSTEXPR std::size_t noof_wm1es = 64; //
  151. fout << "BOOST_STATIC_CONSTEXPR std::size_t noof_w0es = " << noof_w0zs << ";" << std::endl;
  152. fout << "BOOST_STATIC_CONSTEXPR std::size_t noof_w0zs = " << noof_w0zs << ";" << std::endl;
  153. fout << "BOOST_STATIC_CONSTEXPR std::size_t noof_wm1es = " << noof_wm1zs << ";" << std::endl;
  154. fout << "BOOST_STATIC_CONSTEXPR std::size_t noof_wm1zs = " << noof_wm1zs << ";" << std::endl;
  155. // Defining actual lookup table sqrts of e^k, e^-k = 1/e, etc.
  156. table_lookup_t halves[noof_halves]; // 0.5 0.25 0.125, 0.0625 ... 0.0002441, halves of previous elements.
  157. table_lookup_t sqrtw0s[noof_sqrts]; // 0.6065 0.7788, 0.8824 ... 0.9997, sqrt of previous elements.
  158. table_lookup_t sqrtwm1s[noof_sqrts]; // 1.6487, 1.2840 1.1331 ... 1.00024 , sqrt of previous elements.
  159. table_lookup_t w0es[noof_w0es]; // lambert_w[k] for W0 branch. 2.7182, 1, 0.3678, 0.1353, ... 1.6038e-28
  160. table_lookup_t w0zs[noof_w0zs]; // lambert_w[k] for W0 branch. 0. , 2.7182, 14.77, 60.2566 ... 1.445e+29, 3.990e+29.
  161. table_lookup_t wm1es[noof_wm1es]; // lambert_w[k] for W-1 branch. 2.7182 7.38905 20.085 ... 6.235e+27
  162. table_lookup_t wm1zs[noof_wm1zs]; // lambert_w[k] for W-1 branch. -0.3678 ... -1.0264e-26
  163. // e values lambert_w[k] for W-1 branch. 2.7182 1. 0.3678 0.1353 0.04978 ... 4.359e-28 1.603e-28
  164. using boost::math::constants::e;
  165. using boost::math::constants::exp_minus_one;
  166. { // z values for integral W F[k] and powers for W0 branch.
  167. table_lookup_t ej = 1; //
  168. w0es[0] = e<table_lookup_t>(); // e = 2.7182 exp(-1) - 1/e exp_minus_one = 0.36787944.
  169. w0es[1] = 1; // e^0
  170. w0zs[0] = 0; // F[0] = 0 or W0 branch.
  171. for (int j = 1, jj = 2; jj != noof_w0es; ++jj)
  172. {
  173. w0es[jj] = w0es[j] * exp_minus_one<table_lookup_t>(); // previous * 0.36787944.
  174. ej *= e<table_lookup_t>(); // * 2.7182
  175. w0zs[j] = j * ej; // For W0 branch.
  176. j = jj; // Previous.
  177. } // for
  178. }
  179. // Checks on accuracy of W0 exponents.
  180. // Checks on e power w0es
  181. // w0es[64] = 4.3596100000630809736231248158884615452e-28
  182. // N[e ^ -63, 37] = 4.359610000063080973623124815888459643*10^-28
  183. // So slight loss at last decimal place.
  184. // Checks on accuracy of z for integral W0 w0zs
  185. // w0zs[0] = 0, z = -infinity expected? but = zero
  186. // w0zs[1] = 2.7182818284590452353602874713526623144
  187. // w0[2] z = 14.778112197861300454460854921150012956
  188. // w0zs[64] = 3.9904954117194348050619127737142022705e+29
  189. // N[productlog(0, 3.9904954117194348050619127737142022705 10^+29), 37]
  190. // = 63.99999999999999999999999999999999547
  191. // = 64.0 to 34 decimal digits, so exact. :-)
  192. { // z values for integral powers G[k] and e^-k for W-1 branch.
  193. // Fukushima indexing of G (k-1) differs by 1 from(k).
  194. // G[0] = -infinity, so his first item in array g[0] is -0.3678 which is G[1]
  195. // and last is g[63] = G[64] = 1.026e-26
  196. table_lookup_t e1 = 1. / e<table_lookup_t>(); // 1/e = 0.36787944117144233
  197. table_lookup_t ej = e1;
  198. wm1es[0] = e<table_lookup_t>(); // e = 2.7182
  199. wm1zs[0] = -e1; // -1/e = - 0.3678
  200. for (int j = 0, jj = 1; jj != noof_wm1zs; ++jj)
  201. {
  202. ej *= e1; // * 0.3678..
  203. wm1es[jj] = wm1es[j] * e<table_lookup_t>();
  204. wm1zs[jj] = -(jj + 1) * ej;
  205. j = jj; // Previous.
  206. } // for
  207. }
  208. // Checks on W-1 branch accuracy wm1es by comparing with Wolfram.
  209. // exp powers:
  210. // N[e ^ 1, 37] 2.718281828459045235360287471352662498
  211. // wm1es[0] = 2.7182818284590452353602874713526623144 - close enough.
  212. // N[e ^ 3, 37] 20.08553692318766774092852965458171790
  213. // computed wm1es[2] 2.0085536923187667740928529654581712847e+01L OK
  214. // e ^ 66 = 4.6071866343312915426773184428060086893349003037096040 * 10^28
  215. // N[e ^ 66, 34] = 4.607186634331291542677318442806009 10^28
  216. // computed 4.6071866343312915426773184428059867859e+28L
  217. // N[e ^ 66, 37] = 4.607186634331291542677318442806008689*10^28
  218. // so suffering some loss of precision by repeated multiplication computation.
  219. // :-(
  220. // Repeat with cpp_bin_float_50 and correct to 37th decimal digit.
  221. // 4.60718663433129154267731844280600868933490030370929982
  222. // output std::cout.precision(std::numeric_limits<cpp_bin_float_quad>::max_digits10) as 37 decimal digits.
  223. // 4.6071866343312915426773184428060086893e+28L
  224. // N[e ^ 66, 37] = 4.607186634331291542677318442806008689*10^28
  225. // Agrees exactly for 37th place, so should be read in to nearest representable value.
  226. // Checks W-1 branch z values wm1zs
  227. // W-1[0] = -2.7067056647322538378799898994496883858e-01
  228. // w-1[1] = -1.4936120510359182893802724695018536337e-01
  229. // wm1zs[65] -1.4325445274604020119111357113179868158e-27
  230. // N[productlog(-1, -1.4325445274604020119111357113179868158* 10^-27), 37]
  231. // = -65.99999999999999999999999999999999955
  232. // = -66 accurately, so this is OK.
  233. // z = 66 * e^66 =
  234. // =N[-66*e ^ -66, 37]
  235. // -1.432544527460402011911135711317986177*10^-27
  236. // wm1zs[65] -1.4325445274604020119111357113179868158e-27
  237. // which agrees well enough to 34 decimal digits.
  238. // last wm1zs[65] = 0 is unused.
  239. // Halves, common to both W0 and W-1.
  240. halves[0] = static_cast<table_lookup_t>(0.5); // Exactly representable.
  241. for (int j = 0; j != noof_sqrts -1; ++j)
  242. {
  243. halves[j+1] = halves[j] / 2; // Half previous element (/2 will be optimised better?).
  244. } // for j
  245. // W0 sqrts
  246. sqrtw0s[0] = static_cast<table_lookup_t>(0.606530659712633423603799534991180453441918135487186955682L);
  247. for (int j = 0; j != noof_sqrts -1; ++j)
  248. {
  249. sqrtw0s[j+1] = sqrt(sqrtw0s[j]); // Sqrt of previous element. sqrt(1/e), sqrt(sqrt(1/e)) ...
  250. } // for j
  251. // W-1 sqrts
  252. sqrtwm1s[0] = root_e<table_lookup_t>();
  253. for (int j = 0; j != noof_sqrts -1; ++j)
  254. {
  255. sqrtwm1s[j+1] = sqrt(sqrtwm1s[j]); // Sqrt of previous element. sqrt(1/e), sqrt(sqrt(1/e)) ...
  256. } // for j
  257. // Output values as C++ arrays,
  258. // using BOOST_STATIC_CONSTEXPR as static and constexpr as possible for platform.
  259. fout << std::noshowpoint; // Do show NOT trailing zeros for halves and sqrts values.
  260. fout <<
  261. "\n" "BOOST_STATIC_CONSTEXPR lookup_t halves[noof_halves] = " //
  262. "\n" "{ // Common to Lambert W0 and W-1 (and exactly representable)." << "\n ";
  263. for (int i = 0; i != noof_halves; i++)
  264. {
  265. fout << halves[i] << 'L';
  266. if (i != noof_halves - 1)
  267. { // Omit trailing comma on last element.
  268. fout << ", ";
  269. }
  270. else
  271. {
  272. fout << std::endl;
  273. }
  274. }
  275. fout << "}; // halves, 0.5, 0.25, ... 0.000244140625, common to W0 and W-1." << std::endl;
  276. fout <<
  277. "\n" "BOOST_STATIC_CONSTEXPR lookup_t sqrtw0s[noof_sqrts] = " //
  278. "\n" "{ // For Lambert W0 only." << "\n ";
  279. for (int i = 0; i != noof_sqrts; i++)
  280. {
  281. fout << sqrtw0s[i] << 'L';
  282. if (i != noof_sqrts - 1)
  283. { // Omit trailing comma on last element.
  284. fout << ", ";
  285. }
  286. else
  287. {
  288. fout << std::endl;
  289. }
  290. }
  291. fout << "}; // sqrtw0s" << std::endl;
  292. fout <<
  293. "\n" "BOOST_STATIC_CONSTEXPR lookup_t sqrtwm1s[noof_sqrts] = " //
  294. "\n" "{ // For Lambert W-1 only." << "\n ";
  295. for (int i = 0; i != noof_sqrts; i++)
  296. {
  297. fout << sqrtwm1s[i] << 'L';
  298. if (i != noof_sqrts - 1)
  299. { // Omit trailing comma on last element.
  300. fout << ", ";
  301. }
  302. else
  303. {
  304. fout << std::endl;
  305. }
  306. }
  307. fout << "}; // sqrtwm1s" << std::endl;
  308. fout << std::scientific // May be needed to avoid very large dddddddddddddddd.ddddddddddddddd output?
  309. << std::showpoint; // Do show trailing zeros for sqrts and halves.
  310. // Two W0 arrays
  311. fout << // W0 e values.
  312. // Fukushima code generates an extra unused power, but it is not output by using noof_w0zs instead of noof_w0es.
  313. "\n" "BOOST_STATIC_CONSTEXPR lookup_t w0es[noof_w0zs] = " //
  314. "\n" "{ // Fukushima e powers array e[0] = 2.718, 1., e[2] = e^-1 = 0.135, e[3] = e^-2 = 0.133 ... e[64] = 4.3596100000630809736231248158884615452e-28." << "\n ";
  315. for (int i = 0; i != noof_w0zs; i++)
  316. {
  317. fout << w0es[i] << 'L';
  318. if (i != noof_w0es - 1)
  319. { // Omit trailing comma on last element.
  320. fout << ", ";
  321. }
  322. if (i % 4 == 0)
  323. {
  324. fout << "\n ";
  325. }
  326. }
  327. fout << "\n}; // w0es" << std::endl;
  328. fout << // W0 z values for W[1], .
  329. "\n" "BOOST_STATIC_CONSTEXPR lookup_t w0zs[noof_w0zs] = " //
  330. "\n" "{ // z values for W[0], W[1], W[2] ... W[64] (Fukushima array Fk). " << "\n ";
  331. for (int i = 0; i != noof_w0zs; i++)
  332. {
  333. fout << w0zs[i] << 'L';
  334. if (i != noof_w0zs - 1)
  335. { // Omit trailing comma on last element.
  336. fout << ", ";
  337. }
  338. if (i % 4 == 0)
  339. {
  340. fout << "\n ";
  341. }
  342. }
  343. fout << "\n}; // w0zs" << std::endl;
  344. // Two arrays for w-1
  345. fout << // W-1 e values.
  346. "\n" "BOOST_STATIC_CONSTEXPR lookup_t wm1es[noof_wm1es] = " //
  347. "\n" "{ // Fukushima e array e[0] = e^1 = 2.718, e[1] = e^2 = 7.39 ... e[64] = 4.60718e+28." << "\n ";
  348. for (int i = 0; i != noof_wm1es; i++)
  349. {
  350. fout << wm1es[i] << 'L';
  351. if (i != noof_wm1es - 1)
  352. { // Omit trailing comma on last element.
  353. fout << ", ";
  354. }
  355. if (i % 4 == 0)
  356. {
  357. fout << "\n ";
  358. }
  359. }
  360. fout << "\n}; // wm1es" << std::endl;
  361. fout << // Wm1 z values for integral K.
  362. "\n" "BOOST_STATIC_CONSTEXPR lookup_t wm1zs[noof_wm1zs] = " //
  363. "\n" "{ // Fukushima G array of z values for integral K, (Fukushima Gk) g[0] (k = -1) = 1 ... g[64] = -1.0264389699511303e-26." << "\n ";
  364. for (int i = 0; i != noof_wm1zs; i++)
  365. {
  366. fout << wm1zs[i] << 'L';
  367. if (i != noof_wm1zs - 1)
  368. { // Omit trailing comma on last element.
  369. fout << ", ";
  370. }
  371. if (i % 4 == 0)
  372. { // 4 values per line.
  373. fout << "\n ";
  374. }
  375. }
  376. fout << "\n}; // wm1zs" << std::endl;
  377. fout << "} // namespace lambert_w_lookup\n} // namespace lambert_w_detail\n} // namespace math\n} // namespace boost" << std::endl;
  378. }
  379. catch (std::exception& ex)
  380. {
  381. std::cout << "Exception " << ex.what() << std::endl;
  382. }
  383. fout.close();
  384. return 0;
  385. } // int main()
  386. /*
  387. Original arrays as output by Veberic/Fukushima code:
  388. w0 branch
  389. W-1 branch
  390. e: 2.7182818284590451 7.3890560989306495 20.085536923187664 54.598150033144229 148.41315910257657 403.42879349273500 1096.6331584284583 2980.9579870417274 8103.0839275753815 22026.465794806707 59874.141715197788 162754.79141900383 442413.39200892020 1202604.2841647759 3269017.3724721079 8886110.5205078647 24154952.753575277 65659969.137330450 178482300.96318710 485165195.40978980 1318815734.4832134 3584912846.1315880 9744803446.2488918 26489122129.843441 72004899337.385788 195729609428.83853 532048240601.79797 1446257064291.4734 3931334297144.0371 10686474581524.447 29048849665247.383 78962960182680.578 214643579785915.75 583461742527454.00 1586013452313428.3 4311231547115188.5 11719142372802592. 31855931757113704. 86593400423993600. 2.3538526683701958e+17 6.3984349353005389e+17 1.7392749415204982e+18 4.7278394682293381e+18 1.2851600114359284e+19 3.4934271057485025e+19 9.4961194206024286e+19 2.5813128861900616e+20 7.0167359120976157e+20 1.9073465724950953e+21 5.1847055285870605e+21 1.4093490824269355e+22 3.8310080007165677e+22 1.0413759433029062e+23 2.8307533032746866e+23 7.6947852651419974e+23 2.0916594960129907e+24 5.6857199993359170e+24 1.5455389355900996e+25 4.2012104037905024e+25 1.1420073898156810e+26 3.1042979357019109e+26 8.4383566687414291e+26 2.2937831594696028e+27 6.2351490808115970e+27
  391. g: -0.36787944117144233 -0.27067056647322540 -0.14936120510359185 -0.073262555554936742 -0.033689734995427351 -0.014872513059998156 -0.0063831737588816162 -0.0026837010232200957 -0.0011106882367801162 -0.00045399929762484866 -0.00018371870869270232 -7.3730548239938541e-05 -2.9384282290753722e-05 -1.1641402067449956e-05 -4.5885348075273889e-06 -1.8005627955081467e-06 -7.0378941219347870e-07 -2.7413963540482742e-07 -1.0645313231320814e-07 -4.1223072448771179e-08 -1.5923376898615014e-08 -6.1368298043116385e-09 -2.3602323152914367e-09 -9.0603229062698418e-10 -3.4719859662410078e-10 -1.3283631472964657e-10 -5.0747278046555293e-11 -1.9360320299432585e-11 -7.3766303773930841e-12 -2.8072868906520550e-12 -1.0671679036256938e-12 -4.0525329757101402e-13 -1.5374324278841227e-13 -5.8272886672428505e-14 -2.2067908660514491e-14 -8.3502821888768594e-15 -3.1572276215253082e-15 -1.1928704609782527e-15 -4.5038074274761624e-16 -1.6993417021166378e-16 -6.4078169762734621e-17 -2.4147993510032983e-17 -9.0950634616416589e-18 -3.4236981860988753e-18 -1.2881333612472291e-18 -4.8440839844747606e-19 -1.8207788854829806e-19 -6.8407875971564987e-20 -2.5690139750481013e-20 -9.6437492398196038e-21 -3.6186918227652047e-21 -1.3573451162272088e-21 -5.0894204288896066e-22 -1.9076194289884390e-22 -7.1476978375412793e-23 -2.6773000149758669e-23 -1.0025115553818592e-23 -3.7527362568743735e-24 -1.4043571811296988e-24 -5.2539064576179218e-25 -1.9650175744554385e-25 -7.3474021582506962e-26 -2.7465543000397468e-26 -1.0264389699511303e-26
  392. a: 1.6487212707001282 1.2840254166877414 1.1331484530668263 1.0644944589178595 1.0317434074991028 1.0157477085866857 1.0078430972064480 1.0039138893383477 1.0019550335910028 1.0009770394924165 1.0004884004786945 1.0002441704297478
  393. b: 0.50000000000000000 0.25000000000000000 0.12500000000000000 0.062500000000000000 0.031250000000000000 0.015625000000000000 0.0078125000000000000 0.0039062500000000000 0.0019531250000000000 0.00097656250000000000 0.00048828125000000000 0.00024414062500000000
  394. */