]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/tr1/gamma.tcc
[multiple changes]
[thirdparty/gcc.git] / libstdc++-v3 / include / tr1 / gamma.tcc
CommitLineData
7c62b943
BK
1// Special functions -*- C++ -*-
2
3// Copyright (C) 2006-2007
4// Free Software Foundation, Inc.
5//
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)
10// any later version.
11//
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.
16//
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,
20// USA.
21//
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.
30
31/** @file tr1/gamma.tcc
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
34 */
35
36//
37// ISO C++ 14882 TR1: 5.2 Special functions
38//
39
40// Written by Edward Smith-Rowland based on:
41// (1) Handbook of Mathematical Functions,
42// ed. Milton Abramowitz and Irene A. Stegun,
43// Dover Publications,
44// Section 6, pp. 253-266
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. 213-216
49// (4) Gamma, Exploring Euler's Constant, Julian Havil,
50// Princeton, 2003.
51
52#ifndef _TR1_GAMMA_TCC
53#define _TR1_GAMMA_TCC 1
54
55#include "special_function_util.h"
56
57namespace std
58{
59_GLIBCXX_BEGIN_NAMESPACE(_GLIBCXX_TR1)
60
61 /**
62 * @ingroup tr1_math_spec_func
63 * @{
64 */
65
66 //
67 // Implementation-space details.
68 //
69 namespace __detail
70 {
71
72 /**
73 * @brief This returns Bernoulli numbers from a table or by summation
74 * for larger values.
75 *
76 * Recursion is unstable.
77 *
78 * @param __n the order n of the Bernoulli number.
79 * @return The Bernoulli number of order n.
80 */
81 template <typename _Tp>
82 _Tp __bernoulli_series(unsigned int __n)
83 {
84
85 static const _Tp __num[28] = {
86 _Tp(1UL), -_Tp(1UL) / _Tp(2UL),
87 _Tp(1UL) / _Tp(6UL), _Tp(0UL),
88 -_Tp(1UL) / _Tp(30UL), _Tp(0UL),
89 _Tp(1UL) / _Tp(42UL), _Tp(0UL),
90 -_Tp(1UL) / _Tp(30UL), _Tp(0UL),
91 _Tp(5UL) / _Tp(66UL), _Tp(0UL),
92 -_Tp(691UL) / _Tp(2730UL), _Tp(0UL),
93 _Tp(7UL) / _Tp(6UL), _Tp(0UL),
94 -_Tp(3617UL) / _Tp(510UL), _Tp(0UL),
95 _Tp(43867UL) / _Tp(798UL), _Tp(0UL),
96 -_Tp(174611) / _Tp(330UL), _Tp(0UL),
97 _Tp(854513UL) / _Tp(138UL), _Tp(0UL),
98 -_Tp(236364091UL) / _Tp(2730UL), _Tp(0UL),
99 _Tp(8553103UL) / _Tp(6UL), _Tp(0UL)
100 };
101
102 if (__n == 0)
103 return _Tp(1);
104
105 if (__n == 1)
106 return -_Tp(1) / _Tp(2);
107
108 // Take care of the rest of the odd ones.
109 if (__n % 2 == 1)
110 return _Tp(0);
111
112 // Take care of some small evens that are painful for the series.
113 if (__n < 28)
114 return __num[__n];
115
116
117 _Tp __fact = _Tp(1);
118 if ((__n / 2) % 2 == 0)
119 __fact *= _Tp(-1);
120 for (unsigned int __k = 1; __k <= __n; ++__k)
121 __fact *= __k / (_Tp(2) * __numeric_constants<_Tp>::__pi());
122 __fact *= _Tp(2);
123
124 _Tp __sum = _Tp(0);
125 for (unsigned int __i = 1; __i < 1000; ++__i)
126 {
127 _Tp __term = std::pow(_Tp(__i), -_Tp(__n));
128 if (__term < std::numeric_limits<_Tp>::epsilon())
129 break;
130 __sum += __term;
131 }
132
133 return __fact * __sum;
134 }
135
136
137 /**
138 * @brief This returns Bernoulli number \f$B_n\f$.
139 *
140 * @param __n the order n of the Bernoulli number.
141 * @return The Bernoulli number of order n.
142 */
143 template<typename _Tp>
144 inline _Tp
145 __bernoulli(const int __n)
146 {
147 return __bernoulli_series<_Tp>(__n);
148 }
149
150
151 /**
152 * @brief Return \f$log(\Gamma(x))\f$ by asymptotic expansion
153 * with Bernoulli number coefficients. This is like
154 * Sterling's approximation.
155 *
156 * @param __x The argument of the log of the gamma function.
157 * @return The logarithm of the gamma function.
158 */
159 template<typename _Tp>
160 _Tp
161 __log_gamma_bernoulli(const _Tp __x)
162 {
163 _Tp __lg = (__x - _Tp(0.5L)) * std::log(__x) - __x
164 + _Tp(0.5L) * std::log(_Tp(2)
165 * __numeric_constants<_Tp>::__pi());
166
167 const _Tp __xx = __x * __x;
168 _Tp __help = _Tp(1) / __x;
169 for ( unsigned int __i = 1; __i < 20; ++__i )
170 {
171 const _Tp __2i = _Tp(2 * __i);
172 __help /= __2i * (__2i - _Tp(1)) * __xx;
173 __lg += __bernoulli<_Tp>(2 * __i) * __help;
174 }
175
176 return __lg;
177 }
178
179
180 /**
181 * @brief Return \f$log(\Gamma(x))\f$ by the Lanczos method.
182 * This method dominates all others on the positive axis I think.
183 *
184 * @param __x The argument of the log of the gamma function.
185 * @return The logarithm of the gamma function.
186 */
187 template<typename _Tp>
188 _Tp
189 __log_gamma_lanczos(const _Tp __x)
190 {
191 const _Tp __xm1 = __x - _Tp(1);
192
193 static const _Tp __lanczos_cheb_7[9] = {
194 _Tp( 0.99999999999980993227684700473478L),
195 _Tp( 676.520368121885098567009190444019L),
196 _Tp(-1259.13921672240287047156078755283L),
197 _Tp( 771.3234287776530788486528258894L),
198 _Tp(-176.61502916214059906584551354L),
199 _Tp( 12.507343278686904814458936853L),
200 _Tp(-0.13857109526572011689554707L),
201 _Tp( 9.984369578019570859563e-6L),
202 _Tp( 1.50563273514931155834e-7L)
203 };
204
205 static const _Tp __LOGROOT2PI
206 = _Tp(0.9189385332046727417803297364056176L);
207
208 _Tp __sum = __lanczos_cheb_7[0];
209 for(unsigned int __k = 1; __k < 9; ++__k)
210 __sum += __lanczos_cheb_7[__k] / (__xm1 + __k);
211
212 const _Tp __term1 = (__xm1 + _Tp(0.5L))
213 * std::log((__xm1 + _Tp(7.5L))
214 / __numeric_constants<_Tp>::__euler());
215 const _Tp __term2 = __LOGROOT2PI + std::log(__sum);
216 const _Tp __result = __term1 + (__term2 - _Tp(7));
217
218 return __result;
219 }
220
221
222 /**
223 * @brief Return \f$ log(|\Gamma(x)|) \f$.
224 * This will return values even for \f$ x < 0 \f$.
225 * To recover the sign of \f$ \Gamma(x) \f$ for
226 * any argument use @a __log_gamma_sign.
227 *
228 * @param __x The argument of the log of the gamma function.
229 * @return The logarithm of the gamma function.
230 */
231 template<typename _Tp>
232 _Tp
233 __log_gamma(const _Tp __x)
234 {
235 if (__x > _Tp(0.5L))
236 return __log_gamma_lanczos(__x);
237 else
238 {
239 const _Tp __sin_fact
240 = std::abs(std::sin(__numeric_constants<_Tp>::__pi() * __x));
241 if (__sin_fact == _Tp(0))
242 std::__throw_domain_error(__N("Argument is nonpositive integer "
243 "in __log_gamma"));
244 return __numeric_constants<_Tp>::__lnpi()
245 - std::log(__sin_fact)
246 - __log_gamma_lanczos(_Tp(1) - __x);
247 }
248 }
249
250
251 /**
252 * @brief Return the sign of \f$ \Gamma(x) \f$.
253 * At nonpositive integers zero is returned.
254 *
255 * @param __x The argument of the gamma function.
256 * @return The sign of the gamma function.
257 */
258 template<typename _Tp>
259 _Tp
260 __log_gamma_sign(const _Tp __x)
261 {
262 if (__x > _Tp(0))
263 return _Tp(1);
264 else
265 {
266 const _Tp __sin_fact
267 = std::sin(__numeric_constants<_Tp>::__pi() * __x);
268 if (__sin_fact > _Tp(0))
269 return (1);
270 else if (__sin_fact < _Tp(0))
271 return -_Tp(1);
272 else
273 return _Tp(0);
274 }
275 }
276
277
278 /**
279 * @brief Return the logarithm of the binomial coefficient.
280 * The binomial coefficient is given by:
281 * @f[
282 * \left( \right) = \frac{n!}{(n-k)! k!}
283 * @f]
284 *
285 * @param __n The first argument of the binomial coefficient.
286 * @param __k The second argument of the binomial coefficient.
287 * @return The binomial coefficient.
288 */
289 template<typename _Tp>
290 _Tp
291 __log_bincoef(const unsigned int __n, const unsigned int __k)
292 {
293 // Max e exponent before overflow.
294 static const _Tp __max_bincoeff
295 = std::numeric_limits<_Tp>::max_exponent10
296 * std::log(_Tp(10)) - _Tp(1);
297#if _GLIBCXX_USE_C99_MATH_TR1
298 _Tp __coeff = std::_GLIBCXX_TR1::lgamma(_Tp(1 + __n))
299 - std::_GLIBCXX_TR1::lgamma(_Tp(1 + __k))
300 - std::_GLIBCXX_TR1::lgamma(_Tp(1 + __n - __k));
301#else
302 _Tp __coeff = __log_gamma(_Tp(1 + __n))
303 - __log_gamma(_Tp(1 + __k))
304 - __log_gamma(_Tp(1 + __n - __k));
305#endif
306 }
307
308
309 /**
310 * @brief Return the binomial coefficient.
311 * The binomial coefficient is given by:
312 * @f[
313 * \left( \right) = \frac{n!}{(n-k)! k!}
314 * @f]
315 *
316 * @param __n The first argument of the binomial coefficient.
317 * @param __k The second argument of the binomial coefficient.
318 * @return The binomial coefficient.
319 */
320 template<typename _Tp>
321 _Tp
322 __bincoef(const unsigned int __n, const unsigned int __k)
323 {
324 // Max e exponent before overflow.
325 static const _Tp __max_bincoeff
326 = std::numeric_limits<_Tp>::max_exponent10
327 * std::log(_Tp(10)) - _Tp(1);
328
329 const _Tp __log_coeff = __log_bincoef<_Tp>(__n, __k);
330 if (__log_coeff > __max_bincoeff)
331 return std::numeric_limits<_Tp>::quiet_NaN();
332 else
333 return std::exp(__log_coeff);
334 }
335
336
337 /**
338 * @brief Return \f$ \Gamma(x) \f$.
339 *
340 * @param __x The argument of the gamma function.
341 * @return The gamma function.
342 */
343 template<typename _Tp>
344 inline _Tp
345 __gamma(const _Tp __x)
346 {
347 return std::exp(__log_gamma(__x));
348 }
349
350
351 /**
352 * @brief Return the digamma function by series expansion.
353 * The digamma or @f$ \psi(x) @f$ function is defined by
354 * @f[
355 * \psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}
356 * @f]
357 *
358 * The series is given by:
359 * @f[
360 * \psi(x) = -\gamma_E - \frac{1}{x}
361 * \sum_{k=1}^{\infty} \frac{x}{k(x + k)}
362 * @f]
363 */
364 template<typename _Tp>
365 _Tp
366 __psi_series(const _Tp __x)
367 {
368 _Tp __sum = -__numeric_constants<_Tp>::__gamma_e() - _Tp(1) / __x;
369 const unsigned int __max_iter = 100000;
370 for (unsigned int __k = 1; __k < __max_iter; ++__k)
371 {
372 const _Tp __term = __x / (__k * (__k + __x));
373 __sum += __term;
374 if (std::abs(__term / __sum) < std::numeric_limits<_Tp>::epsilon())
375 break;
376 }
377 return __sum;
378 }
379
380
381 /**
382 * @brief Return the digamma function for large argument.
383 * The digamma or @f$ \psi(x) @f$ function is defined by
384 * @f[
385 * \psi(x) = \frac{Gamma'(x)}{\Gamma(x)}
386 * @f]
387 *
388 * The asymptotic series is given by:
389 * @f[
390 * \psi(x) = \ln(x) - \frac{1}{2x}
391 * - \sum_{n=1}^{\infty} \frac{B_{2n}}{2 n x^{2n}}
392 * @f]
393 */
394 template<typename _Tp>
395 _Tp
396 __psi_asymp(const _Tp __x)
397 {
398 _Tp __sum = std::log(__x) - _Tp(0.5L) / __x;
399 const _Tp __xx = __x * __x;
400 _Tp __xp = __xx;
401 const unsigned int __max_iter = 100;
402 for (unsigned int __k = 1; __k < __max_iter; ++__k)
403 {
404 const _Tp __term = __bernoulli<_Tp>(2 * __k) / (2 * __k * __xp);
405 __sum -= __term;
406 if (std::abs(__term / __sum) < std::numeric_limits<_Tp>::epsilon())
407 break;
408 __xp *= __xx;
409 }
410 return __sum;
411 }
412
413
414 /**
415 * @brief Return the digamma function.
416 * The digamma or @f$ \psi(x) @f$ function is defined by
417 * @f[
418 * \psi(x) = \frac{Gamma'(x)}{\Gamma(x)}
419 * @f]
420 * For negative argument the reflection formula is used:
421 * @f[
422 * \psi(x) = \psi(1-x) - \pi \cot(\pi x)
423 * @f]
424 */
425 template<typename _Tp>
426 _Tp
427 __psi(const _Tp __x)
428 {
429 const int __n = static_cast<int>(__x + 0.5L);
430 const _Tp __eps = _Tp(4) * std::numeric_limits<_Tp>::epsilon();
431 if (__n <= 0 && std::abs(__x - _Tp(__n)) < __eps)
432 return std::numeric_limits<_Tp>::quiet_NaN();
433 else if (__x < _Tp(0))
434 {
435 const _Tp __pi = __numeric_constants<_Tp>::__pi();
436 return __psi(_Tp(1) - __x)
437 - __pi * std::cos(__pi * __x) / std::sin(__pi * __x);
438 }
439 else if (__x > _Tp(100))
440 return __psi_asymp(__x);
441 else
442 return __psi_series(__x);
443 }
444
445
446 /**
447 * @brief Return the polygamma function @f$ \psi^{(n)}(x) @f$.
448 *
449 * The polygamma function is related to the Hurwitz zeta function:
450 * @f[
451 * \psi^{(n)}(x) = (-1)^{n+1} m! \zeta(m+1,x)
452 * @f]
453 */
454 template<typename _Tp>
455 _Tp
456 __psi(const unsigned int __n, const _Tp __x)
457 {
458 if (__x <= _Tp(0))
459 std::__throw_domain_error(__N("Argument out of range "
460 "in __psi"));
461 else if (__n == 0)
462 return __psi(__x);
463 else
464 {
465 const _Tp __hzeta = __hurwitz_zeta(_Tp(__n + 1), __x);
466#if _GLIBCXX_USE_C99_MATH_TR1
467 const _Tp __ln_nfact = std::_GLIBCXX_TR1::lgamma(_Tp(__n + 1));
468#else
469 const _Tp __ln_nfact = __log_gamma(_Tp(__n + 1));
470#endif
471 _Tp __result = std::exp(__ln_nfact) * __hzeta;
472 if (__n % 2 == 1)
473 __result = -__result;
474 return __result;
475 }
476 }
477
478 } // namespace std::tr1::__detail
479
480 /* @} */ // group tr1_math_spec_func
481
482_GLIBCXX_END_NAMESPACE
483}
484
485#endif // _TR1_GAMMA_TCC
486