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