gamma.tcc 14 KB

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