1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006, 2007, 2008, 2009
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file tr1/gamma.tcc
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
32 // ISO C++ 14882 TR1: 5.2 Special functions
35 // Written by Edward Smith-Rowland based on:
36 // (1) Handbook of Mathematical Functions,
37 // ed. Milton Abramowitz and Irene A. Stegun,
38 // Dover Publications,
39 // Section 6, pp. 253-266
40 // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
41 // (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky,
42 // W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992),
43 // 2nd ed, pp. 213-216
44 // (4) Gamma, Exploring Euler's Constant, Julian Havil,
47 #ifndef _GLIBCXX_TR1_GAMMA_TCC
48 #define _GLIBCXX_TR1_GAMMA_TCC 1
50 #include "special_function_util.h"
56 // Implementation-space details.
61 * @brief This returns Bernoulli numbers from a table or by summation
64 * Recursion is unstable.
66 * @param __n the order n of the Bernoulli number.
67 * @return The Bernoulli number of order n.
69 template <typename _Tp>
70 _Tp __bernoulli_series(unsigned int __n)
73 static const _Tp __num[28] = {
74 _Tp(1UL), -_Tp(1UL) / _Tp(2UL),
75 _Tp(1UL) / _Tp(6UL), _Tp(0UL),
76 -_Tp(1UL) / _Tp(30UL), _Tp(0UL),
77 _Tp(1UL) / _Tp(42UL), _Tp(0UL),
78 -_Tp(1UL) / _Tp(30UL), _Tp(0UL),
79 _Tp(5UL) / _Tp(66UL), _Tp(0UL),
80 -_Tp(691UL) / _Tp(2730UL), _Tp(0UL),
81 _Tp(7UL) / _Tp(6UL), _Tp(0UL),
82 -_Tp(3617UL) / _Tp(510UL), _Tp(0UL),
83 _Tp(43867UL) / _Tp(798UL), _Tp(0UL),
84 -_Tp(174611) / _Tp(330UL), _Tp(0UL),
85 _Tp(854513UL) / _Tp(138UL), _Tp(0UL),
86 -_Tp(236364091UL) / _Tp(2730UL), _Tp(0UL),
87 _Tp(8553103UL) / _Tp(6UL), _Tp(0UL)
94 return -_Tp(1) / _Tp(2);
96 // Take care of the rest of the odd ones.
100 // Take care of some small evens that are painful for the series.
106 if ((__n / 2) % 2 == 0)
108 for (unsigned int __k = 1; __k <= __n; ++__k)
109 __fact *= __k / (_Tp(2) * __numeric_constants<_Tp>::__pi());
113 for (unsigned int __i = 1; __i < 1000; ++__i)
115 _Tp __term = std::pow(_Tp(__i), -_Tp(__n));
116 if (__term < std::numeric_limits<_Tp>::epsilon())
121 return __fact * __sum;
126 * @brief This returns Bernoulli number \f$B_n\f$.
128 * @param __n the order n of the Bernoulli number.
129 * @return The Bernoulli number of order n.
131 template<typename _Tp>
133 __bernoulli(const int __n)
135 return __bernoulli_series<_Tp>(__n);
140 * @brief Return \f$log(\Gamma(x))\f$ by asymptotic expansion
141 * with Bernoulli number coefficients. This is like
142 * Sterling's approximation.
144 * @param __x The argument of the log of the gamma function.
145 * @return The logarithm of the gamma function.
147 template<typename _Tp>
149 __log_gamma_bernoulli(const _Tp __x)
151 _Tp __lg = (__x - _Tp(0.5L)) * std::log(__x) - __x
152 + _Tp(0.5L) * std::log(_Tp(2)
153 * __numeric_constants<_Tp>::__pi());
155 const _Tp __xx = __x * __x;
156 _Tp __help = _Tp(1) / __x;
157 for ( unsigned int __i = 1; __i < 20; ++__i )
159 const _Tp __2i = _Tp(2 * __i);
160 __help /= __2i * (__2i - _Tp(1)) * __xx;
161 __lg += __bernoulli<_Tp>(2 * __i) * __help;
169 * @brief Return \f$log(\Gamma(x))\f$ by the Lanczos method.
170 * This method dominates all others on the positive axis I think.
172 * @param __x The argument of the log of the gamma function.
173 * @return The logarithm of the gamma function.
175 template<typename _Tp>
177 __log_gamma_lanczos(const _Tp __x)
179 const _Tp __xm1 = __x - _Tp(1);
181 static const _Tp __lanczos_cheb_7[9] = {
182 _Tp( 0.99999999999980993227684700473478L),
183 _Tp( 676.520368121885098567009190444019L),
184 _Tp(-1259.13921672240287047156078755283L),
185 _Tp( 771.3234287776530788486528258894L),
186 _Tp(-176.61502916214059906584551354L),
187 _Tp( 12.507343278686904814458936853L),
188 _Tp(-0.13857109526572011689554707L),
189 _Tp( 9.984369578019570859563e-6L),
190 _Tp( 1.50563273514931155834e-7L)
193 static const _Tp __LOGROOT2PI
194 = _Tp(0.9189385332046727417803297364056176L);
196 _Tp __sum = __lanczos_cheb_7[0];
197 for(unsigned int __k = 1; __k < 9; ++__k)
198 __sum += __lanczos_cheb_7[__k] / (__xm1 + __k);
200 const _Tp __term1 = (__xm1 + _Tp(0.5L))
201 * std::log((__xm1 + _Tp(7.5L))
202 / __numeric_constants<_Tp>::__euler());
203 const _Tp __term2 = __LOGROOT2PI + std::log(__sum);
204 const _Tp __result = __term1 + (__term2 - _Tp(7));
211 * @brief Return \f$ log(|\Gamma(x)|) \f$.
212 * This will return values even for \f$ x < 0 \f$.
213 * To recover the sign of \f$ \Gamma(x) \f$ for
214 * any argument use @a __log_gamma_sign.
216 * @param __x The argument of the log of the gamma function.
217 * @return The logarithm of the gamma function.
219 template<typename _Tp>
221 __log_gamma(const _Tp __x)
224 return __log_gamma_lanczos(__x);
228 = std::abs(std::sin(__numeric_constants<_Tp>::__pi() * __x));
229 if (__sin_fact == _Tp(0))
230 std::__throw_domain_error(__N("Argument is nonpositive integer "
232 return __numeric_constants<_Tp>::__lnpi()
233 - std::log(__sin_fact)
234 - __log_gamma_lanczos(_Tp(1) - __x);
240 * @brief Return the sign of \f$ \Gamma(x) \f$.
241 * At nonpositive integers zero is returned.
243 * @param __x The argument of the gamma function.
244 * @return The sign of the gamma function.
246 template<typename _Tp>
248 __log_gamma_sign(const _Tp __x)
255 = std::sin(__numeric_constants<_Tp>::__pi() * __x);
256 if (__sin_fact > _Tp(0))
258 else if (__sin_fact < _Tp(0))
267 * @brief Return the logarithm of the binomial coefficient.
268 * The binomial coefficient is given by:
270 * \left( \right) = \frac{n!}{(n-k)! k!}
273 * @param __n The first argument of the binomial coefficient.
274 * @param __k The second argument of the binomial coefficient.
275 * @return The binomial coefficient.
277 template<typename _Tp>
279 __log_bincoef(const unsigned int __n, const unsigned int __k)
281 // Max e exponent before overflow.
282 static const _Tp __max_bincoeff
283 = std::numeric_limits<_Tp>::max_exponent10
284 * std::log(_Tp(10)) - _Tp(1);
285 #if _GLIBCXX_USE_C99_MATH_TR1
286 _Tp __coeff = std::tr1::lgamma(_Tp(1 + __n))
287 - std::tr1::lgamma(_Tp(1 + __k))
288 - std::tr1::lgamma(_Tp(1 + __n - __k));
290 _Tp __coeff = __log_gamma(_Tp(1 + __n))
291 - __log_gamma(_Tp(1 + __k))
292 - __log_gamma(_Tp(1 + __n - __k));
298 * @brief Return the binomial coefficient.
299 * The binomial coefficient is given by:
301 * \left( \right) = \frac{n!}{(n-k)! k!}
304 * @param __n The first argument of the binomial coefficient.
305 * @param __k The second argument of the binomial coefficient.
306 * @return The binomial coefficient.
308 template<typename _Tp>
310 __bincoef(const unsigned int __n, const unsigned int __k)
312 // Max e exponent before overflow.
313 static const _Tp __max_bincoeff
314 = std::numeric_limits<_Tp>::max_exponent10
315 * std::log(_Tp(10)) - _Tp(1);
317 const _Tp __log_coeff = __log_bincoef<_Tp>(__n, __k);
318 if (__log_coeff > __max_bincoeff)
319 return std::numeric_limits<_Tp>::quiet_NaN();
321 return std::exp(__log_coeff);
326 * @brief Return \f$ \Gamma(x) \f$.
328 * @param __x The argument of the gamma function.
329 * @return The gamma function.
331 template<typename _Tp>
333 __gamma(const _Tp __x)
335 return std::exp(__log_gamma(__x));
340 * @brief Return the digamma function by series expansion.
341 * The digamma or @f$ \psi(x) @f$ function is defined by
343 * \psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}
346 * The series is given by:
348 * \psi(x) = -\gamma_E - \frac{1}{x}
349 * \sum_{k=1}^{\infty} \frac{x}{k(x + k)}
352 template<typename _Tp>
354 __psi_series(const _Tp __x)
356 _Tp __sum = -__numeric_constants<_Tp>::__gamma_e() - _Tp(1) / __x;
357 const unsigned int __max_iter = 100000;
358 for (unsigned int __k = 1; __k < __max_iter; ++__k)
360 const _Tp __term = __x / (__k * (__k + __x));
362 if (std::abs(__term / __sum) < std::numeric_limits<_Tp>::epsilon())
370 * @brief Return the digamma function for large argument.
371 * The digamma or @f$ \psi(x) @f$ function is defined by
373 * \psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}
376 * The asymptotic series is given by:
378 * \psi(x) = \ln(x) - \frac{1}{2x}
379 * - \sum_{n=1}^{\infty} \frac{B_{2n}}{2 n x^{2n}}
382 template<typename _Tp>
384 __psi_asymp(const _Tp __x)
386 _Tp __sum = std::log(__x) - _Tp(0.5L) / __x;
387 const _Tp __xx = __x * __x;
389 const unsigned int __max_iter = 100;
390 for (unsigned int __k = 1; __k < __max_iter; ++__k)
392 const _Tp __term = __bernoulli<_Tp>(2 * __k) / (2 * __k * __xp);
394 if (std::abs(__term / __sum) < std::numeric_limits<_Tp>::epsilon())
403 * @brief Return the digamma function.
404 * The digamma or @f$ \psi(x) @f$ function is defined by
406 * \psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}
408 * For negative argument the reflection formula is used:
410 * \psi(x) = \psi(1-x) - \pi \cot(\pi x)
413 template<typename _Tp>
417 const int __n = static_cast<int>(__x + 0.5L);
418 const _Tp __eps = _Tp(4) * std::numeric_limits<_Tp>::epsilon();
419 if (__n <= 0 && std::abs(__x - _Tp(__n)) < __eps)
420 return std::numeric_limits<_Tp>::quiet_NaN();
421 else if (__x < _Tp(0))
423 const _Tp __pi = __numeric_constants<_Tp>::__pi();
424 return __psi(_Tp(1) - __x)
425 - __pi * std::cos(__pi * __x) / std::sin(__pi * __x);
427 else if (__x > _Tp(100))
428 return __psi_asymp(__x);
430 return __psi_series(__x);
435 * @brief Return the polygamma function @f$ \psi^{(n)}(x) @f$.
437 * The polygamma function is related to the Hurwitz zeta function:
439 * \psi^{(n)}(x) = (-1)^{n+1} m! \zeta(m+1,x)
442 template<typename _Tp>
444 __psi(const unsigned int __n, const _Tp __x)
447 std::__throw_domain_error(__N("Argument out of range "
453 const _Tp __hzeta = __hurwitz_zeta(_Tp(__n + 1), __x);
454 #if _GLIBCXX_USE_C99_MATH_TR1
455 const _Tp __ln_nfact = std::tr1::lgamma(_Tp(__n + 1));
457 const _Tp __ln_nfact = __log_gamma(_Tp(__n + 1));
459 _Tp __result = std::exp(__ln_nfact) * __hzeta;
461 __result = -__result;
466 } // namespace std::tr1::__detail
470 #endif // _GLIBCXX_TR1_GAMMA_TCC