1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006, 2007, 2008
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file tr1/exp_integral.tcc
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
37 // ISO C++ 14882 TR1: 5.2 Special functions
40 // Written by Edward Smith-Rowland based on:
42 // (1) Handbook of Mathematical Functions,
43 // Ed. by Milton Abramowitz and Irene A. Stegun,
44 // Dover Publications, New-York, Section 5, pp. 228-251.
45 // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
46 // (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky,
47 // W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992),
48 // 2nd ed, pp. 222-225.
51 #ifndef _GLIBCXX_TR1_EXP_INTEGRAL_TCC
52 #define _GLIBCXX_TR1_EXP_INTEGRAL_TCC 1
54 #include "special_function_util.h"
61 // [5.2] Special functions
63 // Implementation-space details.
68 * @brief Return the exponential integral @f$ E_1(x) @f$
69 * by series summation. This should be good
72 * The exponential integral is given by
74 * E_1(x) = \int_{1}^{\infty} \frac{e^{-xt}}{t} dt
77 * @param __x The argument of the exponential integral function.
78 * @return The exponential integral.
80 template<typename _Tp>
82 __expint_E1_series(const _Tp __x)
84 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
88 const unsigned int __max_iter = 100;
89 for (unsigned int __i = 1; __i < __max_iter; ++__i)
91 __term *= - __x / __i;
92 if (std::abs(__term) < __eps)
95 __esum += __term / __i;
97 __osum += __term / __i;
100 return - __esum - __osum
101 - __numeric_constants<_Tp>::__gamma_e() - std::log(__x);
106 * @brief Return the exponential integral @f$ E_1(x) @f$
107 * by asymptotic expansion.
109 * The exponential integral is given by
111 * E_1(x) = \int_{1}^\infty \frac{e^{-xt}}{t} dt
114 * @param __x The argument of the exponential integral function.
115 * @return The exponential integral.
117 template<typename _Tp>
119 __expint_E1_asymp(const _Tp __x)
124 const unsigned int __max_iter = 1000;
125 for (unsigned int __i = 1; __i < __max_iter; ++__i)
128 __term *= - __i / __x;
129 if (std::abs(__term) > std::abs(__prev))
131 if (__term >= _Tp(0))
137 return std::exp(- __x) * (__esum + __osum) / __x;
142 * @brief Return the exponential integral @f$ E_n(x) @f$
143 * by series summation.
145 * The exponential integral is given by
147 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
150 * @param __n The order of the exponential integral function.
151 * @param __x The argument of the exponential integral function.
152 * @return The exponential integral.
154 template<typename _Tp>
156 __expint_En_series(const unsigned int __n, const _Tp __x)
158 const unsigned int __max_iter = 100;
159 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
160 const int __nm1 = __n - 1;
161 _Tp __ans = (__nm1 != 0
162 ? _Tp(1) / __nm1 : -std::log(__x)
163 - __numeric_constants<_Tp>::__gamma_e());
165 for (int __i = 1; __i <= __max_iter; ++__i)
167 __fact *= -__x / _Tp(__i);
170 __del = -__fact / _Tp(__i - __nm1);
173 _Tp __psi = -_TR1_GAMMA_TCC;
174 for (int __ii = 1; __ii <= __nm1; ++__ii)
175 __psi += _Tp(1) / _Tp(__ii);
176 __del = __fact * (__psi - std::log(__x));
179 if (std::abs(__del) < __eps * std::abs(__ans))
182 std::__throw_runtime_error(__N("Series summation failed "
183 "in __expint_En_series."));
188 * @brief Return the exponential integral @f$ E_n(x) @f$
189 * by continued fractions.
191 * The exponential integral is given by
193 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
196 * @param __n The order of the exponential integral function.
197 * @param __x The argument of the exponential integral function.
198 * @return The exponential integral.
200 template<typename _Tp>
202 __expint_En_cont_frac(const unsigned int __n, const _Tp __x)
204 const unsigned int __max_iter = 100;
205 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
206 const _Tp __fp_min = std::numeric_limits<_Tp>::min();
207 const int __nm1 = __n - 1;
208 _Tp __b = __x + _Tp(__n);
209 _Tp __c = _Tp(1) / __fp_min;
210 _Tp __d = _Tp(1) / __b;
212 for ( unsigned int __i = 1; __i <= __max_iter; ++__i )
214 _Tp __a = -_Tp(__i * (__nm1 + __i));
216 __d = _Tp(1) / (__a * __d + __b);
217 __c = __b + __a / __c;
218 const _Tp __del = __c * __d;
220 if (std::abs(__del - _Tp(1)) < __eps)
222 const _Tp __ans = __h * std::exp(-__x);
226 std::__throw_runtime_error(__N("Continued fraction failed "
227 "in __expint_En_cont_frac."));
232 * @brief Return the exponential integral @f$ E_n(x) @f$
233 * by recursion. Use upward recursion for @f$ x < n @f$
234 * and downward recursion (Miller's algorithm) otherwise.
236 * The exponential integral is given by
238 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
241 * @param __n The order of the exponential integral function.
242 * @param __x The argument of the exponential integral function.
243 * @return The exponential integral.
245 template<typename _Tp>
247 __expint_En_recursion(const unsigned int __n, const _Tp __x)
250 _Tp __E1 = __expint_E1(__x);
253 // Forward recursion is stable only for n < x.
255 for (unsigned int __j = 2; __j < __n; ++__j)
256 __En = (std::exp(-__x) - __x * __En) / _Tp(__j - 1);
260 // Backward recursion is stable only for n >= x.
262 const int __N = __n + 20; // TODO: Check this starting number.
264 for (int __j = __N; __j > 0; --__j)
266 __En = (std::exp(-__x) - __j * __En) / __x;
270 _Tp __norm = __En / __E1;
278 * @brief Return the exponential integral @f$ Ei(x) @f$
279 * by series summation.
281 * The exponential integral is given by
283 * Ei(x) = -\int_{-x}^\infty \frac{e^t}{t} dt
286 * @param __x The argument of the exponential integral function.
287 * @return The exponential integral.
289 template<typename _Tp>
291 __expint_Ei_series(const _Tp __x)
295 const unsigned int __max_iter = 1000;
296 for (unsigned int __i = 1; __i < __max_iter; ++__i)
299 __sum += __term / __i;
300 if (__term < std::numeric_limits<_Tp>::epsilon() * __sum)
304 return __numeric_constants<_Tp>::__gamma_e() + __sum + std::log(__x);
309 * @brief Return the exponential integral @f$ Ei(x) @f$
310 * by asymptotic expansion.
312 * The exponential integral is given by
314 * Ei(x) = -\int_{-x}^\infty \frac{e^t}{t} dt
317 * @param __x The argument of the exponential integral function.
318 * @return The exponential integral.
320 template<typename _Tp>
322 __expint_Ei_asymp(const _Tp __x)
326 const unsigned int __max_iter = 1000;
327 for (unsigned int __i = 1; __i < __max_iter; ++__i)
331 if (__term < std::numeric_limits<_Tp>::epsilon())
333 if (__term >= __prev)
338 return std::exp(__x) * __sum / __x;
343 * @brief Return the exponential integral @f$ Ei(x) @f$.
345 * The exponential integral is given by
347 * Ei(x) = -\int_{-x}^\infty \frac{e^t}{t} dt
350 * @param __x The argument of the exponential integral function.
351 * @return The exponential integral.
353 template<typename _Tp>
355 __expint_Ei(const _Tp __x)
358 return -__expint_E1(-__x);
359 else if (__x < -std::log(std::numeric_limits<_Tp>::epsilon()))
360 return __expint_Ei_series(__x);
362 return __expint_Ei_asymp(__x);
367 * @brief Return the exponential integral @f$ E_1(x) @f$.
369 * The exponential integral is given by
371 * E_1(x) = \int_{1}^\infty \frac{e^{-xt}}{t} dt
374 * @param __x The argument of the exponential integral function.
375 * @return The exponential integral.
377 template<typename _Tp>
379 __expint_E1(const _Tp __x)
382 return -__expint_Ei(-__x);
383 else if (__x < _Tp(1))
384 return __expint_E1_series(__x);
385 else if (__x < _Tp(100)) // TODO: Find a good asymptotic switch point.
386 return __expint_En_cont_frac(1, __x);
388 return __expint_E1_asymp(__x);
393 * @brief Return the exponential integral @f$ E_n(x) @f$
394 * for large argument.
396 * The exponential integral is given by
398 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
401 * This is something of an extension.
403 * @param __n The order of the exponential integral function.
404 * @param __x The argument of the exponential integral function.
405 * @return The exponential integral.
407 template<typename _Tp>
409 __expint_asymp(const unsigned int __n, const _Tp __x)
413 for (unsigned int __i = 1; __i <= __n; ++__i)
416 __term *= -(__n - __i + 1) / __x;
417 if (std::abs(__term) > std::abs(__prev))
422 return std::exp(-__x) * __sum / __x;
427 * @brief Return the exponential integral @f$ E_n(x) @f$
430 * The exponential integral is given by
432 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
435 * This is something of an extension.
437 * @param __n The order of the exponential integral function.
438 * @param __x The argument of the exponential integral function.
439 * @return The exponential integral.
441 template<typename _Tp>
443 __expint_large_n(const unsigned int __n, const _Tp __x)
445 const _Tp __xpn = __x + __n;
446 const _Tp __xpn2 = __xpn * __xpn;
449 for (unsigned int __i = 1; __i <= __n; ++__i)
452 __term *= (__n - 2 * (__i - 1) * __x) / __xpn2;
453 if (std::abs(__term) < std::numeric_limits<_Tp>::epsilon())
458 return std::exp(-__x) * __sum / __xpn;
463 * @brief Return the exponential integral @f$ E_n(x) @f$.
465 * The exponential integral is given by
467 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
469 * This is something of an extension.
471 * @param __n The order of the exponential integral function.
472 * @param __x The argument of the exponential integral function.
473 * @return The exponential integral.
475 template<typename _Tp>
477 __expint(const unsigned int __n, const _Tp __x)
479 // Return NaN on NaN input.
481 return std::numeric_limits<_Tp>::quiet_NaN();
482 else if (__n <= 1 && __x == _Tp(0))
483 return std::numeric_limits<_Tp>::infinity();
486 _Tp __E0 = std::exp(__x) / __x;
490 _Tp __E1 = __expint_E1(__x);
495 return _Tp(1) / static_cast<_Tp>(__n - 1);
497 _Tp __En = __expint_En_recursion(__n, __x);
505 * @brief Return the exponential integral @f$ Ei(x) @f$.
507 * The exponential integral is given by
509 * Ei(x) = -\int_{-x}^\infty \frac{e^t}{t} dt
512 * @param __x The argument of the exponential integral function.
513 * @return The exponential integral.
515 template<typename _Tp>
517 __expint(const _Tp __x)
520 return std::numeric_limits<_Tp>::quiet_NaN();
522 return __expint_Ei(__x);
525 } // namespace std::tr1::__detail
529 #endif // _GLIBCXX_TR1_EXP_INTEGRAL_TCC