You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

480 lines
14KB

  1. // Special functions -*- C++ -*-
  2. // Copyright (C) 2006-2020 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 <tr1/special_function_util.h>
  43. namespace std _GLIBCXX_VISIBILITY(default)
  44. {
  45. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  46. #if _GLIBCXX_USE_STD_SPEC_FUNCS
  47. # define _GLIBCXX_MATH_NS ::std
  48. #elif defined(_GLIBCXX_TR1_CMATH)
  49. namespace tr1
  50. {
  51. # define _GLIBCXX_MATH_NS ::std::tr1
  52. #else
  53. # error do not include this header directly, use <cmath> or <tr1/cmath>
  54. #endif
  55. // Implementation-space details.
  56. namespace __detail
  57. {
  58. /**
  59. * @brief This returns Bernoulli numbers from a table or by summation
  60. * for larger values.
  61. *
  62. * Recursion is unstable.
  63. *
  64. * @param __n the order n of the Bernoulli number.
  65. * @return The Bernoulli number of order n.
  66. */
  67. template <typename _Tp>
  68. _Tp
  69. __bernoulli_series(unsigned int __n)
  70. {
  71. static const _Tp __num[28] = {
  72. _Tp(1UL), -_Tp(1UL) / _Tp(2UL),
  73. _Tp(1UL) / _Tp(6UL), _Tp(0UL),
  74. -_Tp(1UL) / _Tp(30UL), _Tp(0UL),
  75. _Tp(1UL) / _Tp(42UL), _Tp(0UL),
  76. -_Tp(1UL) / _Tp(30UL), _Tp(0UL),
  77. _Tp(5UL) / _Tp(66UL), _Tp(0UL),
  78. -_Tp(691UL) / _Tp(2730UL), _Tp(0UL),
  79. _Tp(7UL) / _Tp(6UL), _Tp(0UL),
  80. -_Tp(3617UL) / _Tp(510UL), _Tp(0UL),
  81. _Tp(43867UL) / _Tp(798UL), _Tp(0UL),
  82. -_Tp(174611) / _Tp(330UL), _Tp(0UL),
  83. _Tp(854513UL) / _Tp(138UL), _Tp(0UL),
  84. -_Tp(236364091UL) / _Tp(2730UL), _Tp(0UL),
  85. _Tp(8553103UL) / _Tp(6UL), _Tp(0UL)
  86. };
  87. if (__n == 0)
  88. return _Tp(1);
  89. if (__n == 1)
  90. return -_Tp(1) / _Tp(2);
  91. // Take care of the rest of the odd ones.
  92. if (__n % 2 == 1)
  93. return _Tp(0);
  94. // Take care of some small evens that are painful for the series.
  95. if (__n < 28)
  96. return __num[__n];
  97. _Tp __fact = _Tp(1);
  98. if ((__n / 2) % 2 == 0)
  99. __fact *= _Tp(-1);
  100. for (unsigned int __k = 1; __k <= __n; ++__k)
  101. __fact *= __k / (_Tp(2) * __numeric_constants<_Tp>::__pi());
  102. __fact *= _Tp(2);
  103. _Tp __sum = _Tp(0);
  104. for (unsigned int __i = 1; __i < 1000; ++__i)
  105. {
  106. _Tp __term = std::pow(_Tp(__i), -_Tp(__n));
  107. if (__term < std::numeric_limits<_Tp>::epsilon())
  108. break;
  109. __sum += __term;
  110. }
  111. return __fact * __sum;
  112. }
  113. /**
  114. * @brief This returns Bernoulli number \f$B_n\f$.
  115. *
  116. * @param __n the order n of the Bernoulli number.
  117. * @return The Bernoulli number of order n.
  118. */
  119. template<typename _Tp>
  120. inline _Tp
  121. __bernoulli(int __n)
  122. { return __bernoulli_series<_Tp>(__n); }
  123. /**
  124. * @brief Return \f$log(\Gamma(x))\f$ by asymptotic expansion
  125. * with Bernoulli number coefficients. This is like
  126. * Sterling's approximation.
  127. *
  128. * @param __x The argument of the log of the gamma function.
  129. * @return The logarithm of the gamma function.
  130. */
  131. template<typename _Tp>
  132. _Tp
  133. __log_gamma_bernoulli(_Tp __x)
  134. {
  135. _Tp __lg = (__x - _Tp(0.5L)) * std::log(__x) - __x
  136. + _Tp(0.5L) * std::log(_Tp(2)
  137. * __numeric_constants<_Tp>::__pi());
  138. const _Tp __xx = __x * __x;
  139. _Tp __help = _Tp(1) / __x;
  140. for ( unsigned int __i = 1; __i < 20; ++__i )
  141. {
  142. const _Tp __2i = _Tp(2 * __i);
  143. __help /= __2i * (__2i - _Tp(1)) * __xx;
  144. __lg += __bernoulli<_Tp>(2 * __i) * __help;
  145. }
  146. return __lg;
  147. }
  148. /**
  149. * @brief Return \f$log(\Gamma(x))\f$ by the Lanczos method.
  150. * This method dominates all others on the positive axis I think.
  151. *
  152. * @param __x The argument of the log of the gamma function.
  153. * @return The logarithm of the gamma function.
  154. */
  155. template<typename _Tp>
  156. _Tp
  157. __log_gamma_lanczos(_Tp __x)
  158. {
  159. const _Tp __xm1 = __x - _Tp(1);
  160. static const _Tp __lanczos_cheb_7[9] = {
  161. _Tp( 0.99999999999980993227684700473478L),
  162. _Tp( 676.520368121885098567009190444019L),
  163. _Tp(-1259.13921672240287047156078755283L),
  164. _Tp( 771.3234287776530788486528258894L),
  165. _Tp(-176.61502916214059906584551354L),
  166. _Tp( 12.507343278686904814458936853L),
  167. _Tp(-0.13857109526572011689554707L),
  168. _Tp( 9.984369578019570859563e-6L),
  169. _Tp( 1.50563273514931155834e-7L)
  170. };
  171. static const _Tp __LOGROOT2PI
  172. = _Tp(0.9189385332046727417803297364056176L);
  173. _Tp __sum = __lanczos_cheb_7[0];
  174. for(unsigned int __k = 1; __k < 9; ++__k)
  175. __sum += __lanczos_cheb_7[__k] / (__xm1 + __k);
  176. const _Tp __term1 = (__xm1 + _Tp(0.5L))
  177. * std::log((__xm1 + _Tp(7.5L))
  178. / __numeric_constants<_Tp>::__euler());
  179. const _Tp __term2 = __LOGROOT2PI + std::log(__sum);
  180. const _Tp __result = __term1 + (__term2 - _Tp(7));
  181. return __result;
  182. }
  183. /**
  184. * @brief Return \f$ log(|\Gamma(x)|) \f$.
  185. * This will return values even for \f$ x < 0 \f$.
  186. * To recover the sign of \f$ \Gamma(x) \f$ for
  187. * any argument use @a __log_gamma_sign.
  188. *
  189. * @param __x The argument of the log of the gamma function.
  190. * @return The logarithm of the gamma function.
  191. */
  192. template<typename _Tp>
  193. _Tp
  194. __log_gamma(_Tp __x)
  195. {
  196. if (__x > _Tp(0.5L))
  197. return __log_gamma_lanczos(__x);
  198. else
  199. {
  200. const _Tp __sin_fact
  201. = std::abs(std::sin(__numeric_constants<_Tp>::__pi() * __x));
  202. if (__sin_fact == _Tp(0))
  203. std::__throw_domain_error(__N("Argument is nonpositive integer "
  204. "in __log_gamma"));
  205. return __numeric_constants<_Tp>::__lnpi()
  206. - std::log(__sin_fact)
  207. - __log_gamma_lanczos(_Tp(1) - __x);
  208. }
  209. }
  210. /**
  211. * @brief Return the sign of \f$ \Gamma(x) \f$.
  212. * At nonpositive integers zero is returned.
  213. *
  214. * @param __x The argument of the gamma function.
  215. * @return The sign of the gamma function.
  216. */
  217. template<typename _Tp>
  218. _Tp
  219. __log_gamma_sign(_Tp __x)
  220. {
  221. if (__x > _Tp(0))
  222. return _Tp(1);
  223. else
  224. {
  225. const _Tp __sin_fact
  226. = std::sin(__numeric_constants<_Tp>::__pi() * __x);
  227. if (__sin_fact > _Tp(0))
  228. return (1);
  229. else if (__sin_fact < _Tp(0))
  230. return -_Tp(1);
  231. else
  232. return _Tp(0);
  233. }
  234. }
  235. /**
  236. * @brief Return the logarithm of the binomial coefficient.
  237. * The binomial coefficient is given by:
  238. * @f[
  239. * \left( \right) = \frac{n!}{(n-k)! k!}
  240. * @f]
  241. *
  242. * @param __n The first argument of the binomial coefficient.
  243. * @param __k The second argument of the binomial coefficient.
  244. * @return The binomial coefficient.
  245. */
  246. template<typename _Tp>
  247. _Tp
  248. __log_bincoef(unsigned int __n, unsigned int __k)
  249. {
  250. // Max e exponent before overflow.
  251. static const _Tp __max_bincoeff
  252. = std::numeric_limits<_Tp>::max_exponent10
  253. * std::log(_Tp(10)) - _Tp(1);
  254. #if _GLIBCXX_USE_C99_MATH_TR1
  255. _Tp __coeff = _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __n))
  256. - _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __k))
  257. - _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __n - __k));
  258. #else
  259. _Tp __coeff = __log_gamma(_Tp(1 + __n))
  260. - __log_gamma(_Tp(1 + __k))
  261. - __log_gamma(_Tp(1 + __n - __k));
  262. #endif
  263. }
  264. /**
  265. * @brief Return the binomial coefficient.
  266. * The binomial coefficient is given by:
  267. * @f[
  268. * \left( \right) = \frac{n!}{(n-k)! k!}
  269. * @f]
  270. *
  271. * @param __n The first argument of the binomial coefficient.
  272. * @param __k The second argument of the binomial coefficient.
  273. * @return The binomial coefficient.
  274. */
  275. template<typename _Tp>
  276. _Tp
  277. __bincoef(unsigned int __n, unsigned int __k)
  278. {
  279. // Max e exponent before overflow.
  280. static const _Tp __max_bincoeff
  281. = std::numeric_limits<_Tp>::max_exponent10
  282. * std::log(_Tp(10)) - _Tp(1);
  283. const _Tp __log_coeff = __log_bincoef<_Tp>(__n, __k);
  284. if (__log_coeff > __max_bincoeff)
  285. return std::numeric_limits<_Tp>::quiet_NaN();
  286. else
  287. return std::exp(__log_coeff);
  288. }
  289. /**
  290. * @brief Return \f$ \Gamma(x) \f$.
  291. *
  292. * @param __x The argument of the gamma function.
  293. * @return The gamma function.
  294. */
  295. template<typename _Tp>
  296. inline _Tp
  297. __gamma(_Tp __x)
  298. { return std::exp(__log_gamma(__x)); }
  299. /**
  300. * @brief Return the digamma function by series expansion.
  301. * The digamma or @f$ \psi(x) @f$ function is defined by
  302. * @f[
  303. * \psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}
  304. * @f]
  305. *
  306. * The series is given by:
  307. * @f[
  308. * \psi(x) = -\gamma_E - \frac{1}{x}
  309. * \sum_{k=1}^{\infty} \frac{x}{k(x + k)}
  310. * @f]
  311. */
  312. template<typename _Tp>
  313. _Tp
  314. __psi_series(_Tp __x)
  315. {
  316. _Tp __sum = -__numeric_constants<_Tp>::__gamma_e() - _Tp(1) / __x;
  317. const unsigned int __max_iter = 100000;
  318. for (unsigned int __k = 1; __k < __max_iter; ++__k)
  319. {
  320. const _Tp __term = __x / (__k * (__k + __x));
  321. __sum += __term;
  322. if (std::abs(__term / __sum) < std::numeric_limits<_Tp>::epsilon())
  323. break;
  324. }
  325. return __sum;
  326. }
  327. /**
  328. * @brief Return the digamma function for large argument.
  329. * The digamma or @f$ \psi(x) @f$ function is defined by
  330. * @f[
  331. * \psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}
  332. * @f]
  333. *
  334. * The asymptotic series is given by:
  335. * @f[
  336. * \psi(x) = \ln(x) - \frac{1}{2x}
  337. * - \sum_{n=1}^{\infty} \frac{B_{2n}}{2 n x^{2n}}
  338. * @f]
  339. */
  340. template<typename _Tp>
  341. _Tp
  342. __psi_asymp(_Tp __x)
  343. {
  344. _Tp __sum = std::log(__x) - _Tp(0.5L) / __x;
  345. const _Tp __xx = __x * __x;
  346. _Tp __xp = __xx;
  347. const unsigned int __max_iter = 100;
  348. for (unsigned int __k = 1; __k < __max_iter; ++__k)
  349. {
  350. const _Tp __term = __bernoulli<_Tp>(2 * __k) / (2 * __k * __xp);
  351. __sum -= __term;
  352. if (std::abs(__term / __sum) < std::numeric_limits<_Tp>::epsilon())
  353. break;
  354. __xp *= __xx;
  355. }
  356. return __sum;
  357. }
  358. /**
  359. * @brief Return the digamma function.
  360. * The digamma or @f$ \psi(x) @f$ function is defined by
  361. * @f[
  362. * \psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}
  363. * @f]
  364. * For negative argument the reflection formula is used:
  365. * @f[
  366. * \psi(x) = \psi(1-x) - \pi \cot(\pi x)
  367. * @f]
  368. */
  369. template<typename _Tp>
  370. _Tp
  371. __psi(_Tp __x)
  372. {
  373. const int __n = static_cast<int>(__x + 0.5L);
  374. const _Tp __eps = _Tp(4) * std::numeric_limits<_Tp>::epsilon();
  375. if (__n <= 0 && std::abs(__x - _Tp(__n)) < __eps)
  376. return std::numeric_limits<_Tp>::quiet_NaN();
  377. else if (__x < _Tp(0))
  378. {
  379. const _Tp __pi = __numeric_constants<_Tp>::__pi();
  380. return __psi(_Tp(1) - __x)
  381. - __pi * std::cos(__pi * __x) / std::sin(__pi * __x);
  382. }
  383. else if (__x > _Tp(100))
  384. return __psi_asymp(__x);
  385. else
  386. return __psi_series(__x);
  387. }
  388. /**
  389. * @brief Return the polygamma function @f$ \psi^{(n)}(x) @f$.
  390. *
  391. * The polygamma function is related to the Hurwitz zeta function:
  392. * @f[
  393. * \psi^{(n)}(x) = (-1)^{n+1} m! \zeta(m+1,x)
  394. * @f]
  395. */
  396. template<typename _Tp>
  397. _Tp
  398. __psi(unsigned int __n, _Tp __x)
  399. {
  400. if (__x <= _Tp(0))
  401. std::__throw_domain_error(__N("Argument out of range "
  402. "in __psi"));
  403. else if (__n == 0)
  404. return __psi(__x);
  405. else
  406. {
  407. const _Tp __hzeta = __hurwitz_zeta(_Tp(__n + 1), __x);
  408. #if _GLIBCXX_USE_C99_MATH_TR1
  409. const _Tp __ln_nfact = _GLIBCXX_MATH_NS::lgamma(_Tp(__n + 1));
  410. #else
  411. const _Tp __ln_nfact = __log_gamma(_Tp(__n + 1));
  412. #endif
  413. _Tp __result = std::exp(__ln_nfact) * __hzeta;
  414. if (__n % 2 == 1)
  415. __result = -__result;
  416. return __result;
  417. }
  418. }
  419. } // namespace __detail
  420. #undef _GLIBCXX_MATH_NS
  421. #if ! _GLIBCXX_USE_STD_SPEC_FUNCS && defined(_GLIBCXX_TR1_CMATH)
  422. } // namespace tr1
  423. #endif
  424. _GLIBCXX_END_NAMESPACE_VERSION
  425. } // namespace std
  426. #endif // _GLIBCXX_TR1_GAMMA_TCC