expint.qbk 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. [section:expint Exponential Integrals]
  2. [section:expint_n Exponential Integral En]
  3. [h4 Synopsis]
  4. ``
  5. #include <boost/math/special_functions/expint.hpp>
  6. ``
  7. namespace boost{ namespace math{
  8. template <class T>
  9. ``__sf_result`` expint(unsigned n, T z);
  10. template <class T, class ``__Policy``>
  11. ``__sf_result`` expint(unsigned n, T z, const ``__Policy``&);
  12. }} // namespaces
  13. The return type of these functions is computed using the __arg_promotion_rules:
  14. the return type is `double` if T is an integer type, and T otherwise.
  15. [optional_policy]
  16. [h4 Description]
  17. template <class T>
  18. ``__sf_result`` expint(unsigned n, T z);
  19. template <class T, class ``__Policy``>
  20. ``__sf_result`` expint(unsigned n, T z, const ``__Policy``&);
  21. Returns the [@http://mathworld.wolfram.com/En-Function.html exponential integral En]
  22. of z:
  23. [equation expint_n_1]
  24. [graph expint2]
  25. [h4 Accuracy]
  26. The following table shows the peak errors (in units of epsilon)
  27. found on various platforms with various floating point types,
  28. along with comparisons to other libraries.
  29. Unless otherwise specified any floating point type that is narrower
  30. than the one shown will have __zero_error.
  31. [table_expint_En_]
  32. [h4 Testing]
  33. The tests for these functions come in two parts:
  34. basic sanity checks use spot values calculated using
  35. [@http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp?name=ExpIntegralE Mathworld's online evaluator],
  36. while accuracy checks use high-precision test values calculated at 1000-bit precision with
  37. [@http://shoup.net/ntl/doc/RR.txt NTL::RR] and this implementation.
  38. Note that the generic and type-specific
  39. versions of these functions use differing implementations internally, so this
  40. gives us reasonably independent test data. Using our test data to test other
  41. "known good" implementations also provides an additional sanity check.
  42. [h4 Implementation]
  43. The generic version of this function uses the continued fraction:
  44. [equation expint_n_3]
  45. for large /x/ and the infinite series:
  46. [equation expint_n_2]
  47. for small /x/.
  48. Where the precision of /x/ is known at compile time and is 113 bits or fewer
  49. in precision, then rational approximations [jm_rationals] are used for the
  50. `n == 1` case.
  51. For `x < 1` the approximating form is a minimax approximation:
  52. [equation expint_n_4]
  53. and for `x > 1` a Chebyshev interpolated approximation of the form:
  54. [equation expint_n_5]
  55. is used.
  56. [endsect] [/section:expint_n Exponential Integral En]
  57. [section:expint_i Exponential Integral Ei]
  58. [h4 Synopsis]
  59. ``
  60. #include <boost/math/special_functions/expint.hpp>
  61. ``
  62. namespace boost{ namespace math{
  63. template <class T>
  64. ``__sf_result`` expint(T z);
  65. template <class T, class ``__Policy``>
  66. ``__sf_result`` expint(T z, const ``__Policy``&);
  67. }} // namespaces
  68. The return type of these functions is computed using the __arg_promotion_rules:
  69. the return type is `double` if T is an integer type, and T otherwise.
  70. [optional_policy]
  71. [h4 Description]
  72. template <class T>
  73. ``__sf_result`` expint(T z);
  74. template <class T, class ``__Policy``>
  75. ``__sf_result`` expint(T z, const ``__Policy``&);
  76. Returns the [@http://mathworld.wolfram.com/ExponentialIntegral.html exponential integral]
  77. of z:
  78. [equation expint_i_1]
  79. [graph expint_i]
  80. [h4 Accuracy]
  81. The following table shows the peak errors (in units of epsilon)
  82. found on various platforms with various floating point types,
  83. along with comparisons to Cody's SPECFUN implementation and the __gsl library.
  84. Unless otherwise specified any floating point type that is narrower
  85. than the one shown will have __zero_error.
  86. [table_expint_Ei_]
  87. It should be noted that all three libraries tested above
  88. offer sub-epsilon precision over most of their range.
  89. GSL has the greatest difficulty near the positive root of En, while
  90. Cody's SPECFUN along with this implementation increase their
  91. error rates very slightly over the range \[4,6\].
  92. The following error plot are based on an exhaustive search of the functions domain, MSVC-15.5 at `double` precision,
  93. and GCC-7.1/Ubuntu for `long double` and `__float128`.
  94. [graph exponential_integral_ei__double]
  95. [graph exponential_integral_ei__80_bit_long_double]
  96. [graph exponential_integral_ei____float128]
  97. [h4 Testing]
  98. The tests for these functions come in two parts:
  99. basic sanity checks use spot values calculated using
  100. [@http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp?name=ExpIntegralEi Mathworld's online evaluator],
  101. while accuracy checks use high-precision test values calculated at 1000-bit precision with
  102. [@http://shoup.net/ntl/doc/RR.txt NTL::RR] and this implementation.
  103. Note that the generic and type-specific
  104. versions of these functions use differing implementations internally, so this
  105. gives us reasonably independent test data. Using our test data to test other
  106. "known good" implementations also provides an additional sanity check.
  107. [h4 Implementation]
  108. For x < 0 this function just calls __expint_n(1, -x): which in turn is implemented
  109. in terms of rational approximations when the type of x has 113 or fewer bits of
  110. precision.
  111. For x > 0 the generic version is implemented using the infinte series:
  112. [equation expint_i_2]
  113. However, when the precision of the argument type is known at compile time
  114. and is 113 bits or less, then rational approximations [jm_rationals] are used.
  115. For 0 < z < 6 a root-preserving approximation of the form:
  116. [equation expint_i_3]
  117. is used, where z[sub 0] is the positive root of the function, and
  118. R(z/3 - 1) is a minimax rational approximation rescaled so that
  119. it is evaluated over \[-1,1\]. Note that while the rational approximation
  120. over \[0,6\] converges rapidly to the minimax solution it is rather
  121. ill-conditioned in practice. Cody and Thacher
  122. [footnote W. J. Cody and H. C. Thacher, Jr.,
  123. Rational Chebyshev approximations for the exponential integral E[sub 1](x),
  124. Math. Comp. 22 (1968), 641-649,
  125. and W. J. Cody and H. C. Thacher, Jr., Chebyshev approximations for the
  126. exponential integral Ei(x), Math. Comp. 23 (1969), 289-303.]
  127. experienced the same issue and
  128. converted the polynomials into Chebeshev form to ensure stable
  129. computation. By experiment we found that the polynomials are just as stable
  130. in polynomial as Chebyshev form, /provided/ they are computed
  131. over the interval \[-1,1\].
  132. Over the a series of intervals ['[a, b]] and ['[b, INF]] the rational approximation
  133. takes the form:
  134. [equation expint_i_4]
  135. where /c/ is a constant, and ['R(t)] is a minimax solution optimised for low
  136. absolute error compared to /c/. Variable /t/ is `1/z` when the range in infinite
  137. and `2z/(b-a) - (2a/(b-a) + 1)` otherwise: this has the effect of scaling z to the
  138. interval \[-1,1\]. As before rational approximations over arbitrary intervals
  139. were found to be ill-conditioned: Cody and Thacher solved this issue by
  140. converting the polynomials to their J-Fraction equivalent. However, as long
  141. as the interval of evaluation was \[-1,1\] and the number of terms carefully chosen,
  142. it was found that the polynomials /could/ be evaluated to suitable precision:
  143. error rates are typically 2 to 3 epsilon which is comparible to the error
  144. rate that Cody and Thacher achieved using J-Fractions, but marginally more
  145. efficient given that fewer divisions are involved.
  146. [endsect] [/section:expint_n Exponential Integral En]
  147. [endsect] [/section:expint Exponential Integrals]
  148. [/
  149. Copyright 2006 John Maddock and Paul A. Bristow.
  150. Distributed under the Boost Software License, Version 1.0.
  151. (See accompanying file LICENSE_1_0.txt or copy at
  152. http://www.boost.org/LICENSE_1_0.txt).
  153. ]