lambert_w_graph.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright Paul A. Bristow 2017
  2. // Copyright John Z. Maddock 2017
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or
  5. // copy at http ://www.boost.org/LICENSE_1_0.txt).
  6. /*! \brief Graph showing use of Lambert W function.
  7. \details
  8. Both Lambert W0 and W-1 branches can be shown on one graph.
  9. But useful to have another graph for larger values of argument z.
  10. Need two separate graphs for Lambert W0 and -1 prime because
  11. the sensible ranges and axes are too different.
  12. One would get too small LambertW0 in top right and W-1 in bottom left.
  13. */
  14. #include <boost/math/special_functions/lambert_w.hpp>
  15. using boost::math::lambert_w0;
  16. using boost::math::lambert_wm1;
  17. using boost::math::lambert_w0_prime;
  18. using boost::math::lambert_wm1_prime;
  19. #include <boost/math/special_functions.hpp>
  20. using boost::math::isfinite;
  21. #include <boost/svg_plot/svg_2d_plot.hpp>
  22. using namespace boost::svg;
  23. #include <boost/svg_plot/show_2d_settings.hpp>
  24. using boost::svg::show_2d_plot_settings;
  25. #include <iostream>
  26. // using std::cout;
  27. // using std::endl;
  28. #include <exception>
  29. #include <stdexcept>
  30. #include <string>
  31. #include <array>
  32. #include <vector>
  33. #include <utility>
  34. using std::pair;
  35. #include <map>
  36. using std::map;
  37. #include <set>
  38. using std::multiset;
  39. #include <limits>
  40. using std::numeric_limits;
  41. #include <cmath> //
  42. /*!
  43. */
  44. int main()
  45. {
  46. try
  47. {
  48. std::cout << "Lambert W graph example." << std::endl;
  49. //[lambert_w_graph_1
  50. //] [/lambert_w_graph_1]
  51. {
  52. std::map<const double, double> wm1s; // Lambert W-1 branch values.
  53. std::map<const double, double> w0s; // Lambert W0 branch values.
  54. std::cout.precision(std::numeric_limits<double>::max_digits10);
  55. int count = 0;
  56. for (double z = -0.36787944117144232159552377016146086744581113103176804; z < 2.8; z += 0.001)
  57. {
  58. double w0 = lambert_w0(z);
  59. w0s[z] = w0;
  60. // std::cout << "z " << z << ", w = " << w0 << std::endl;
  61. count++;
  62. }
  63. std::cout << "points " << count << std::endl;
  64. count = 0;
  65. for (double z = -0.3678794411714423215955237701614608727; z < -0.001; z += 0.001)
  66. {
  67. double wm1 = lambert_wm1(z);
  68. wm1s[z] = wm1;
  69. count++;
  70. }
  71. std::cout << "points " << count << std::endl;
  72. svg_2d_plot data_plot;
  73. data_plot.title("Lambert W function.")
  74. .x_size(400)
  75. .y_size(300)
  76. .legend_on(true)
  77. .legend_lines(true)
  78. .x_label("z")
  79. .y_label("W")
  80. .x_range(-1, 3.)
  81. .y_range(-4., +1.)
  82. .x_major_interval(1.)
  83. .y_major_interval(1.)
  84. .x_major_grid_on(true)
  85. .y_major_grid_on(true)
  86. //.x_values_on(true)
  87. //.y_values_on(true)
  88. .y_values_rotation(horizontal)
  89. //.plot_window_on(true)
  90. .x_values_precision(3)
  91. .y_values_precision(3)
  92. .coord_precision(4) // Needed to avoid stepping on curves.
  93. .copyright_holder("Paul A. Bristow")
  94. .copyright_date("2018")
  95. //.background_border_color(black);
  96. ;
  97. data_plot.plot(w0s, "W0 branch").line_color(red).shape(none).line_on(true).bezier_on(false).line_width(1);
  98. data_plot.plot(wm1s, "W-1 branch").line_color(blue).shape(none).line_on(true).bezier_on(false).line_width(1);
  99. data_plot.write("./lambert_w_graph");
  100. show_2d_plot_settings(data_plot); // For plot diagnosis only.
  101. } // small z Lambert W
  102. { // bigger argument z Lambert W
  103. std::map<const double, double> w0s_big; // Lambert W0 branch values for large z and W.
  104. std::map<const double, double> wm1s_big; // Lambert W-1 branch values for small z and large -W.
  105. int count = 0;
  106. for (double z = -0.3678794411714423215955237701614608727; z < 10000.; z += 50.)
  107. {
  108. double w0 = lambert_w0(z);
  109. w0s_big[z] = w0;
  110. count++;
  111. }
  112. std::cout << "points " << count << std::endl;
  113. count = 0;
  114. for (double z = -0.3678794411714423215955237701614608727; z < -0.001; z += 0.001)
  115. {
  116. double wm1 = lambert_wm1(z);
  117. wm1s_big[z] = wm1;
  118. count++;
  119. }
  120. std::cout << "Lambert W0 large z argument points = " << count << std::endl;
  121. svg_2d_plot data_plot2;
  122. data_plot2.title("Lambert W0 function for larger z.")
  123. .x_size(400)
  124. .y_size(300)
  125. .legend_on(false)
  126. .x_label("z")
  127. .y_label("W")
  128. //.x_label_on(true)
  129. //.y_label_on(true)
  130. //.xy_values_on(false)
  131. .x_range(-1, 10000.)
  132. .y_range(-1., +8.)
  133. .x_major_interval(2000.)
  134. .y_major_interval(1.)
  135. .x_major_grid_on(true)
  136. .y_major_grid_on(true)
  137. //.x_values_on(true)
  138. //.y_values_on(true)
  139. .y_values_rotation(horizontal)
  140. //.plot_window_on(true)
  141. .x_values_precision(3)
  142. .y_values_precision(3)
  143. .coord_precision(4) // Needed to avoid stepping on curves.
  144. .copyright_holder("Paul A. Bristow")
  145. .copyright_date("2018")
  146. //.background_border_color(black);
  147. ;
  148. data_plot2.plot(w0s_big, "W0 branch").line_color(red).shape(none).line_on(true).bezier_on(false).line_width(1);
  149. // data_plot2.plot(wm1s_big, "W-1 branch").line_color(blue).shape(none).line_on(true).bezier_on(false).line_width(1);
  150. // This wouldn't show anything useful.
  151. data_plot2.write("./lambert_w_graph_big_w");
  152. } // Big argument z Lambert W
  153. { // Lambert W0 Derivative plots
  154. // std::map<const double, double> wm1ps; // Lambert W-1 prime branch values.
  155. std::map<const double, double> w0ps; // Lambert W0 prime branch values.
  156. std::cout.precision(std::numeric_limits<double>::max_digits10);
  157. int count = 0;
  158. for (double z = -0.36; z < 3.; z += 0.001)
  159. {
  160. double w0p = lambert_w0_prime(z);
  161. w0ps[z] = w0p;
  162. // std::cout << "z " << z << ", w0 = " << w0 << std::endl;
  163. count++;
  164. }
  165. std::cout << "points " << count << std::endl;
  166. //count = 0;
  167. //for (double z = -0.36; z < -0.1; z += 0.001)
  168. //{
  169. // double wm1p = lambert_wm1_prime(z);
  170. // std::cout << "z " << z << ", w-1 = " << wm1p << std::endl;
  171. // wm1ps[z] = wm1p;
  172. // count++;
  173. //}
  174. //std::cout << "points " << count << std::endl;
  175. svg_2d_plot data_plotp;
  176. data_plotp.title("Lambert W0 prime function.")
  177. .x_size(400)
  178. .y_size(300)
  179. .legend_on(false)
  180. .x_label("z")
  181. .y_label("W0'")
  182. .x_range(-0.3, +1.)
  183. .y_range(0., +5.)
  184. .x_major_interval(0.2)
  185. .y_major_interval(2.)
  186. .x_major_grid_on(true)
  187. .y_major_grid_on(true)
  188. .y_values_rotation(horizontal)
  189. .x_values_precision(3)
  190. .y_values_precision(3)
  191. .coord_precision(4) // Needed to avoid stepping on curves.
  192. .copyright_holder("Paul A. Bristow")
  193. .copyright_date("2018")
  194. ;
  195. // derivative of N[productlog(0, x), 55] at x=0 to 10
  196. // Plot[D[N[ProductLog[0, x], 55], x], {x, 0, 10}]
  197. // Plot[ProductLog[x]/(x + x ProductLog[x]), {x, 0, 10}]
  198. data_plotp.plot(w0ps, "W0 prime branch").line_color(red).shape(none).line_on(true).bezier_on(false).line_width(1);
  199. data_plotp.write("./lambert_w0_prime_graph");
  200. } // Lambert W0 Derivative plots
  201. { // Lambert Wm1 Derivative plots
  202. std::map<const double, double> wm1ps; // Lambert W-1 prime branch values.
  203. std::cout.precision(std::numeric_limits<double>::max_digits10);
  204. int count = 0;
  205. for (double z = -0.3678; z < -0.00001; z += 0.001)
  206. {
  207. double wm1p = lambert_wm1_prime(z);
  208. // std::cout << "z " << z << ", w-1 = " << wm1p << std::endl;
  209. wm1ps[z] = wm1p;
  210. count++;
  211. }
  212. std::cout << "Lambert W-1 prime points = " << count << std::endl;
  213. svg_2d_plot data_plotp;
  214. data_plotp.title("Lambert W-1 prime function.")
  215. .x_size(400)
  216. .y_size(300)
  217. .legend_on(false)
  218. .x_label("z")
  219. .y_label("W-1'")
  220. .x_range(-0.4, +0.01)
  221. .x_major_interval(0.1)
  222. .y_range(-20., -5.)
  223. .y_major_interval(5.)
  224. .x_major_grid_on(true)
  225. .y_major_grid_on(true)
  226. .y_values_rotation(horizontal)
  227. .x_values_precision(3)
  228. .y_values_precision(3)
  229. .coord_precision(4) // Needed to avoid stepping on curves.
  230. .copyright_holder("Paul A. Bristow")
  231. .copyright_date("2018")
  232. ;
  233. // derivative of N[productlog(0, x), 55] at x=0 to 10
  234. // Plot[D[N[ProductLog[0, x], 55], x], {x, 0, 10}]
  235. // Plot[ProductLog[x]/(x + x ProductLog[x]), {x, 0, 10}]
  236. data_plotp.plot(wm1ps, "W-1 prime branch").line_color(blue).shape(none).line_on(true).bezier_on(false).line_width(1);
  237. data_plotp.write("./lambert_wm1_prime_graph");
  238. } // Lambert W-1 prime graph
  239. } // try
  240. catch (std::exception& ex)
  241. {
  242. std::cout << ex.what() << std::endl;
  243. }
  244. } // int main()
  245. /*
  246. //[lambert_w_graph_1_output
  247. //] [/lambert_w_graph_1_output]
  248. */