gamma.tcc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // Special functions -*- C++ -*-
  2. // Copyright (C) 2006, 2007, 2008, 2009
  3. // Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library. This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. //
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // Under Section 7 of GPL version 3, you are granted additional
  17. // permissions described in the GCC Runtime Library Exception, version
  18. // 3.1, as published by the Free Software Foundation.
  19. // You should have received a copy of the GNU General Public License and
  20. // a copy of the GCC Runtime Library Exception along with this program;
  21. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  22. // <http://www.gnu.org/licenses/>.
  23. /** @file tr1/gamma.tcc
  24. * This is an internal header file, included by other library headers.
  25. * You should not attempt to use it directly.
  26. */
  27. //
  28. // ISO C++ 14882 TR1: 5.2 Special functions
  29. //
  30. // Written by Edward Smith-Rowland based on:
  31. // (1) Handbook of Mathematical Functions,
  32. // ed. Milton Abramowitz and Irene A. Stegun,
  33. // Dover Publications,
  34. // Section 6, pp. 253-266
  35. // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
  36. // (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky,
  37. // W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992),
  38. // 2nd ed, pp. 213-216
  39. // (4) Gamma, Exploring Euler's Constant, Julian Havil,
  40. // Princeton, 2003.
  41. #ifndef _TR1_GAMMA_TCC
  42. #define _TR1_GAMMA_TCC 1
  43. #include "special_function_util.h"
  44. namespace std
  45. {
  46. namespace tr1
  47. {
  48. // Implementation-space details.
  49. namespace __detail
  50. {
  51. /**
  52. * @brief This returns Bernoulli numbers from a table or by summation
  53. * for larger values.
  54. *
  55. * Recursion is unstable.
  56. *
  57. * @param __n the order n of the Bernoulli number.
  58. * @return The Bernoulli number of order n.
  59. */
  60. template <typename _Tp>
  61. _Tp __bernoulli_series(unsigned int __n)
  62. {
  63. static const _Tp __num[28] = {
  64. _Tp(1UL), -_Tp(1UL) / _Tp(2UL),
  65. _Tp(1UL) / _Tp(6UL), _Tp(0UL),
  66. -_Tp(1UL) / _Tp(30UL), _Tp(0UL),
  67. _Tp(1UL) / _Tp(42UL), _Tp(0UL),
  68. -_Tp(1UL) / _Tp(30UL), _Tp(0UL),
  69. _Tp(5UL) / _Tp(66UL), _Tp(0UL),
  70. -_Tp(691UL) / _Tp(2730UL), _Tp(0UL),
  71. _Tp(7UL) / _Tp(6UL), _Tp(0UL),
  72. -_Tp(3617UL) / _Tp(510UL), _Tp(0UL),
  73. _Tp(43867UL) / _Tp(798UL), _Tp(0UL),
  74. -_Tp(174611) / _Tp(330UL), _Tp(0UL),
  75. _Tp(854513UL) / _Tp(138UL), _Tp(0UL),
  76. -_Tp(236364091UL) / _Tp(2730UL), _Tp(0UL),
  77. _Tp(8553103UL) / _Tp(6UL), _Tp(0UL)
  78. };
  79. if (__n == 0)
  80. return _Tp(1);
  81. if (__n == 1)
  82. return -_Tp(1) / _Tp(2);
  83. // Take care of the rest of the odd ones.
  84. if (__n % 2 == 1)
  85. return _Tp(0);
  86. // Take care of some small evens that are painful for the series.
  87. if (__n < 28)
  88. return __num[__n];
  89. _Tp __fact = _Tp(1);
  90. if ((__n / 2) % 2 == 0)
  91. __fact *= _Tp(-1);
  92. for (unsigned int __k = 1; __k <= __n; ++__k)
  93. __fact *= __k / (_Tp(2) * __numeric_constants<_Tp>::__pi());
  94. __fact *= _Tp(2);
  95. _Tp __sum = _Tp(0);
  96. for (unsigned int __i = 1; __i < 1000; ++__i)
  97. {
  98. _Tp __term = std::pow(_Tp(__i), -_Tp(__n));
  99. if (__term < std::numeric_limits<_Tp>::epsilon())
  100. break;
  101. __sum += __term;
  102. }
  103. return __fact * __sum;
  104. }
  105. /**
  106. * @brief This returns Bernoulli number \f$B_n\f$.
  107. *
  108. * @param __n the order n of the Bernoulli number.
  109. * @return The Bernoulli number of order n.
  110. */
  111. template<typename _Tp>
  112. inline _Tp
  113. __bernoulli(const int __n)
  114. {
  115. return __bernoulli_series<_Tp>(__n);
  116. }
  117. /**
  118. * @brief Return \f$log(\Gamma(x))\f$ by asymptotic expansion
  119. * with Bernoulli number coefficients. This is like
  120. * Sterling's approximation.
  121. *
  122. * @param __x The argument of the log of the gamma function.
  123. * @return The logarithm of the gamma function.
  124. */
  125. template<typename _Tp>
  126. _Tp
  127. __log_gamma_bernoulli(const _Tp __x)
  128. {
  129. _Tp __lg = (__x - _Tp(0.5L)) * std::log(__x) - __x
  130. + _Tp(0.5L) * std::log(_Tp(2)
  131. * __numeric_constants<_Tp>::__pi());
  132. const _Tp __xx = __x * __x;
  133. _Tp __help = _Tp(1) / __x;
  134. for ( unsigned int __i = 1; __i < 20; ++__i )
  135. {
  136. const _Tp __2i = _Tp(2 * __i);
  137. __help /= __2i * (__2i - _Tp(1)) * __xx;
  138. __lg += __bernoulli<_Tp>(2 * __i) * __help;
  139. }
  140. return __lg;
  141. }
  142. /**
  143. * @brief Return \f$log(\Gamma(x))\f$ by the Lanczos method.
  144. * This method dominates all others on the positive axis I think.
  145. *
  146. * @param __x The argument of the log of the gamma function.
  147. * @return The logarithm of the gamma function.
  148. */
  149. template<typename _Tp>
  150. _Tp
  151. __log_gamma_lanczos(const _Tp __x)
  152. {
  153. const _Tp __xm1 = __x - _Tp(1);
  154. static const _Tp __lanczos_cheb_7[9] = {
  155. _Tp( 0.99999999999980993227684700473478L),
  156. _Tp( 676.520368121885098567009190444019L),
  157. _Tp(-1259.13921672240287047156078755283L),
  158. _Tp( 771.3234287776530788486528258894L),
  159. _Tp(-176.61502916214059906584551354L),
  160. _Tp( 12.507343278686904814458936853L),
  161. _Tp(-0.13857109526572011689554707L),
  162. _Tp( 9.984369578019570859563e-6L),
  163. _Tp( 1.50563273514931155834e-7L)
  164. };
  165. static const _Tp __LOGROOT2PI
  166. = _Tp(0.9189385332046727417803297364056176L);
  167. _Tp __sum = __lanczos_cheb_7[0];
  168. for(unsigned int __k = 1; __k < 9; ++__k)
  169. __sum += __lanczos_cheb_7[__k] / (__xm1 + __k);
  170. const _Tp __term1 = (__xm1 + _Tp(0.5L))
  171. * std::log((__xm1 + _Tp(7.5L))
  172. / __numeric_constants<_Tp>::__euler());
  173. const _Tp __term2 = __LOGROOT2PI + std::log(__sum);
  174. const _Tp __result = __term1 + (__term2 - _Tp(7));
  175. return __result;
  176. }
  177. /**
  178. * @brief Return \f$ log(|\Gamma(x)|) \f$.
  179. * This will return values even for \f$ x < 0 \f$.
  180. * To recover the sign of \f$ \Gamma(x) \f$ for
  181. * any argument use @a __log_gamma_sign.
  182. *
  183. * @param __x The argument of the log of the gamma function.
  184. * @return The logarithm of the gamma function.
  185. */
  186. template<typename _Tp>
  187. _Tp
  188. __log_gamma(const _Tp __x)
  189. {
  190. if (__x > _Tp(0.5L))
  191. return __log_gamma_lanczos(__x);
  192. else
  193. {
  194. const _Tp __sin_fact
  195. = std::abs(std::sin(__numeric_constants<_Tp>::__pi() * __x));
  196. if (__sin_fact == _Tp(0))
  197. std::__throw_domain_error(__N("Argument is nonpositive integer "
  198. "in __log_gamma"));
  199. return __numeric_constants<_Tp>::__lnpi()
  200. - std::log(__sin_fact)
  201. - __log_gamma_lanczos(_Tp(1) - __x);
  202. }
  203. }
  204. /**
  205. * @brief Return the sign of \f$ \Gamma(x) \f$.
  206. * At nonpositive integers zero is returned.
  207. *
  208. * @param __x The argument of the gamma function.
  209. * @return The sign of the gamma function.
  210. */
  211. template<typename _Tp>
  212. _Tp
  213. __log_gamma_sign(const _Tp __x)
  214. {
  215. if (__x > _Tp(0))
  216. return _Tp(1);
  217. else
  218. {
  219. const _Tp __sin_fact
  220. = std::sin(__numeric_constants<_Tp>::__pi() * __x);
  221. if (__sin_fact > _Tp(0))
  222. return (1);
  223. else if (__sin_fact < _Tp(0))
  224. return -_Tp(1);
  225. else
  226. return _Tp(0);
  227. }
  228. }
  229. /**
  230. * @brief Return the logarithm of the binomial coefficient.
  231. * The binomial coefficient is given by:
  232. * @f[
  233. * \left( \right) = \frac{n!}{(n-k)! k!}
  234. * @f]
  235. *
  236. * @param __n The first argument of the binomial coefficient.
  237. * @param __k The second argument of the binomial coefficient.
  238. * @return The binomial coefficient.
  239. */
  240. template<typename _Tp>
  241. _Tp
  242. __log_bincoef(const unsigned int __n, const unsigned int __k)
  243. {
  244. // Max e exponent before overflow.
  245. static const _Tp __max_bincoeff
  246. = std::numeric_limits<_Tp>::max_exponent10
  247. * std::log(_Tp(10)) - _Tp(1);
  248. #if _GLIBCXX_USE_C99_MATH_TR1
  249. _Tp __coeff = std::tr1::lgamma(_Tp(1 + __n))
  250. - std::tr1::lgamma(_Tp(1 + __k))
  251. - std::tr1::lgamma(_Tp(1 + __n - __k));
  252. #else
  253. _Tp __coeff = __log_gamma(_Tp(1 + __n))
  254. - __log_gamma(_Tp(1 + __k))
  255. - __log_gamma(_Tp(1 + __n - __k));
  256. #endif
  257. }
  258. /**
  259. * @brief Return the binomial coefficient.
  260. * The binomial coefficient is given by:
  261. * @f[
  262. * \left( \right) = \frac{n!}{(n-k)! k!}
  263. * @f]
  264. *
  265. * @param __n The first argument of the binomial coefficient.
  266. * @param __k The second argument of the binomial coefficient.
  267. * @return The binomial coefficient.
  268. */
  269. template<typename _Tp>
  270. _Tp
  271. __bincoef(const unsigned int __n, const unsigned int __k)
  272. {
  273. // Max e exponent before overflow.
  274. static const _Tp __max_bincoeff
  275. = std::numeric_limits<_Tp>::max_exponent10
  276. * std::log(_Tp(10)) - _Tp(1);
  277. const _Tp __log_coeff = __log_bincoef<_Tp>(__n, __k);
  278. if (__log_coeff > __max_bincoeff)
  279. return std::numeric_limits<_Tp>::quiet_NaN();
  280. else
  281. return std::exp(__log_coeff);
  282. }
  283. /**
  284. * @brief Return \f$ \Gamma(x) \f$.
  285. *
  286. * @param __x The argument of the gamma function.
  287. * @return The gamma function.
  288. */
  289. template<typename _Tp>
  290. inline _Tp
  291. __gamma(const _Tp __x)
  292. {
  293. return std::exp(__log_gamma(__x));
  294. }
  295. /**
  296. * @brief Return the digamma function by series expansion.
  297. * The digamma or @f$ \psi(x) @f$ function is defined by
  298. * @f[
  299. * \psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}
  300. * @f]
  301. *
  302. * The series is given by:
  303. * @f[
  304. * \psi(x) = -\gamma_E - \frac{1}{x}
  305. * \sum_{k=1}^{\infty} \frac{x}{k(x + k)}
  306. * @f]
  307. */
  308. template<typename _Tp>
  309. _Tp
  310. __psi_series(const _Tp __x)
  311. {
  312. _Tp __sum = -__numeric_constants<_Tp>::__gamma_e() - _Tp(1) / __x;
  313. const unsigned int __max_iter = 100000;
  314. for (unsigned int __k = 1; __k < __max_iter; ++__k)
  315. {
  316. const _Tp __term = __x / (__k * (__k + __x));
  317. __sum += __term;
  318. if (std::abs(__term / __sum) < std::numeric_limits<_Tp>::epsilon())
  319. break;
  320. }
  321. return __sum;
  322. }
  323. /**
  324. * @brief Return the digamma function for large argument.
  325. * The digamma or @f$ \psi(x) @f$ function is defined by
  326. * @f[
  327. * \psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}
  328. * @f]
  329. *
  330. * The asymptotic series is given by:
  331. * @f[
  332. * \psi(x) = \ln(x) - \frac{1}{2x}
  333. * - \sum_{n=1}^{\infty} \frac{B_{2n}}{2 n x^{2n}}
  334. * @f]
  335. */
  336. template<typename _Tp>
  337. _Tp
  338. __psi_asymp(const _Tp __x)
  339. {
  340. _Tp __sum = std::log(__x) - _Tp(0.5L) / __x;
  341. const _Tp __xx = __x * __x;
  342. _Tp __xp = __xx;
  343. const unsigned int __max_iter = 100;
  344. for (unsigned int __k = 1; __k < __max_iter; ++__k)
  345. {
  346. const _Tp __term = __bernoulli<_Tp>(2 * __k) / (2 * __k * __xp);
  347. __sum -= __term;
  348. if (std::abs(__term / __sum) < std::numeric_limits<_Tp>::epsilon())
  349. break;
  350. __xp *= __xx;
  351. }
  352. return __sum;
  353. }
  354. /**
  355. * @brief Return the digamma function.
  356. * The digamma or @f$ \psi(x) @f$ function is defined by
  357. * @f[
  358. * \psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}
  359. * @f]
  360. * For negative argument the reflection formula is used:
  361. * @f[
  362. * \psi(x) = \psi(1-x) - \pi \cot(\pi x)
  363. * @f]
  364. */
  365. template<typename _Tp>
  366. _Tp
  367. __psi(const _Tp __x)
  368. {
  369. const int __n = static_cast<int>(__x + 0.5L);
  370. const _Tp __eps = _Tp(4) * std::numeric_limits<_Tp>::epsilon();
  371. if (__n <= 0 && std::abs(__x - _Tp(__n)) < __eps)
  372. return std::numeric_limits<_Tp>::quiet_NaN();
  373. else if (__x < _Tp(0))
  374. {
  375. const _Tp __pi = __numeric_constants<_Tp>::__pi();
  376. return __psi(_Tp(1) - __x)
  377. - __pi * std::cos(__pi * __x) / std::sin(__pi * __x);
  378. }
  379. else if (__x > _Tp(100))
  380. return __psi_asymp(__x);
  381. else
  382. return __psi_series(__x);
  383. }
  384. /**
  385. * @brief Return the polygamma function @f$ \psi^{(n)}(x) @f$.
  386. *
  387. * The polygamma function is related to the Hurwitz zeta function:
  388. * @f[
  389. * \psi^{(n)}(x) = (-1)^{n+1} m! \zeta(m+1,x)
  390. * @f]
  391. */
  392. template<typename _Tp>
  393. _Tp
  394. __psi(const unsigned int __n, const _Tp __x)
  395. {
  396. if (__x <= _Tp(0))
  397. std::__throw_domain_error(__N("Argument out of range "
  398. "in __psi"));
  399. else if (__n == 0)
  400. return __psi(__x);
  401. else
  402. {
  403. const _Tp __hzeta = __hurwitz_zeta(_Tp(__n + 1), __x);
  404. #if _GLIBCXX_USE_C99_MATH_TR1
  405. const _Tp __ln_nfact = std::tr1::lgamma(_Tp(__n + 1));
  406. #else
  407. const _Tp __ln_nfact = __log_gamma(_Tp(__n + 1));
  408. #endif
  409. _Tp __result = std::exp(__ln_nfact) * __hzeta;
  410. if (__n % 2 == 1)
  411. __result = -__result;
  412. return __result;
  413. }
  414. }
  415. } // namespace std::tr1::__detail
  416. }
  417. }
  418. #endif // _TR1_GAMMA_TCC