Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

444 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/riemann_zeta.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. by Milton Abramowitz and Irene A. Stegun,
  32. // Dover Publications, New-York, Section 5, pp. 807-808.
  33. // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
  34. // (3) Gamma, Exploring Euler's Constant, Julian Havil,
  35. // Princeton, 2003.
  36. #ifndef _GLIBCXX_TR1_RIEMANN_ZETA_TCC
  37. #define _GLIBCXX_TR1_RIEMANN_ZETA_TCC 1
  38. #include <tr1/special_function_util.h>
  39. namespace std _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. #if _GLIBCXX_USE_STD_SPEC_FUNCS
  43. # define _GLIBCXX_MATH_NS ::std
  44. #elif defined(_GLIBCXX_TR1_CMATH)
  45. namespace tr1
  46. {
  47. # define _GLIBCXX_MATH_NS ::std::tr1
  48. #else
  49. # error do not include this header directly, use <cmath> or <tr1/cmath>
  50. #endif
  51. // [5.2] Special functions
  52. // Implementation-space details.
  53. namespace __detail
  54. {
  55. /**
  56. * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
  57. * by summation for s > 1.
  58. *
  59. * The Riemann zeta function is defined by:
  60. * \f[
  61. * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
  62. * \f]
  63. * For s < 1 use the reflection formula:
  64. * \f[
  65. * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
  66. * \f]
  67. */
  68. template<typename _Tp>
  69. _Tp
  70. __riemann_zeta_sum(_Tp __s)
  71. {
  72. // A user shouldn't get to this.
  73. if (__s < _Tp(1))
  74. std::__throw_domain_error(__N("Bad argument in zeta sum."));
  75. const unsigned int max_iter = 10000;
  76. _Tp __zeta = _Tp(0);
  77. for (unsigned int __k = 1; __k < max_iter; ++__k)
  78. {
  79. _Tp __term = std::pow(static_cast<_Tp>(__k), -__s);
  80. if (__term < std::numeric_limits<_Tp>::epsilon())
  81. {
  82. break;
  83. }
  84. __zeta += __term;
  85. }
  86. return __zeta;
  87. }
  88. /**
  89. * @brief Evaluate the Riemann zeta function @f$ \zeta(s) @f$
  90. * by an alternate series for s > 0.
  91. *
  92. * The Riemann zeta function is defined by:
  93. * \f[
  94. * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
  95. * \f]
  96. * For s < 1 use the reflection formula:
  97. * \f[
  98. * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
  99. * \f]
  100. */
  101. template<typename _Tp>
  102. _Tp
  103. __riemann_zeta_alt(_Tp __s)
  104. {
  105. _Tp __sgn = _Tp(1);
  106. _Tp __zeta = _Tp(0);
  107. for (unsigned int __i = 1; __i < 10000000; ++__i)
  108. {
  109. _Tp __term = __sgn / std::pow(__i, __s);
  110. if (std::abs(__term) < std::numeric_limits<_Tp>::epsilon())
  111. break;
  112. __zeta += __term;
  113. __sgn *= _Tp(-1);
  114. }
  115. __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
  116. return __zeta;
  117. }
  118. /**
  119. * @brief Evaluate the Riemann zeta function by series for all s != 1.
  120. * Convergence is great until largish negative numbers.
  121. * Then the convergence of the > 0 sum gets better.
  122. *
  123. * The series is:
  124. * \f[
  125. * \zeta(s) = \frac{1}{1-2^{1-s}}
  126. * \sum_{n=0}^{\infty} \frac{1}{2^{n+1}}
  127. * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (k+1)^{-s}
  128. * \f]
  129. * Havil 2003, p. 206.
  130. *
  131. * The Riemann zeta function is defined by:
  132. * \f[
  133. * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
  134. * \f]
  135. * For s < 1 use the reflection formula:
  136. * \f[
  137. * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
  138. * \f]
  139. */
  140. template<typename _Tp>
  141. _Tp
  142. __riemann_zeta_glob(_Tp __s)
  143. {
  144. _Tp __zeta = _Tp(0);
  145. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  146. // Max e exponent before overflow.
  147. const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
  148. * std::log(_Tp(10)) - _Tp(1);
  149. // This series works until the binomial coefficient blows up
  150. // so use reflection.
  151. if (__s < _Tp(0))
  152. {
  153. #if _GLIBCXX_USE_C99_MATH_TR1
  154. if (_GLIBCXX_MATH_NS::fmod(__s,_Tp(2)) == _Tp(0))
  155. return _Tp(0);
  156. else
  157. #endif
  158. {
  159. _Tp __zeta = __riemann_zeta_glob(_Tp(1) - __s);
  160. __zeta *= std::pow(_Tp(2)
  161. * __numeric_constants<_Tp>::__pi(), __s)
  162. * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
  163. #if _GLIBCXX_USE_C99_MATH_TR1
  164. * std::exp(_GLIBCXX_MATH_NS::lgamma(_Tp(1) - __s))
  165. #else
  166. * std::exp(__log_gamma(_Tp(1) - __s))
  167. #endif
  168. / __numeric_constants<_Tp>::__pi();
  169. return __zeta;
  170. }
  171. }
  172. _Tp __num = _Tp(0.5L);
  173. const unsigned int __maxit = 10000;
  174. for (unsigned int __i = 0; __i < __maxit; ++__i)
  175. {
  176. bool __punt = false;
  177. _Tp __sgn = _Tp(1);
  178. _Tp __term = _Tp(0);
  179. for (unsigned int __j = 0; __j <= __i; ++__j)
  180. {
  181. #if _GLIBCXX_USE_C99_MATH_TR1
  182. _Tp __bincoeff = _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __i))
  183. - _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __j))
  184. - _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __i - __j));
  185. #else
  186. _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
  187. - __log_gamma(_Tp(1 + __j))
  188. - __log_gamma(_Tp(1 + __i - __j));
  189. #endif
  190. if (__bincoeff > __max_bincoeff)
  191. {
  192. // This only gets hit for x << 0.
  193. __punt = true;
  194. break;
  195. }
  196. __bincoeff = std::exp(__bincoeff);
  197. __term += __sgn * __bincoeff * std::pow(_Tp(1 + __j), -__s);
  198. __sgn *= _Tp(-1);
  199. }
  200. if (__punt)
  201. break;
  202. __term *= __num;
  203. __zeta += __term;
  204. if (std::abs(__term/__zeta) < __eps)
  205. break;
  206. __num *= _Tp(0.5L);
  207. }
  208. __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
  209. return __zeta;
  210. }
  211. /**
  212. * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
  213. * using the product over prime factors.
  214. * \f[
  215. * \zeta(s) = \Pi_{i=1}^\infty \frac{1}{1 - p_i^{-s}}
  216. * \f]
  217. * where @f$ {p_i} @f$ are the prime numbers.
  218. *
  219. * The Riemann zeta function is defined by:
  220. * \f[
  221. * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
  222. * \f]
  223. * For s < 1 use the reflection formula:
  224. * \f[
  225. * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
  226. * \f]
  227. */
  228. template<typename _Tp>
  229. _Tp
  230. __riemann_zeta_product(_Tp __s)
  231. {
  232. static const _Tp __prime[] = {
  233. _Tp(2), _Tp(3), _Tp(5), _Tp(7), _Tp(11), _Tp(13), _Tp(17), _Tp(19),
  234. _Tp(23), _Tp(29), _Tp(31), _Tp(37), _Tp(41), _Tp(43), _Tp(47),
  235. _Tp(53), _Tp(59), _Tp(61), _Tp(67), _Tp(71), _Tp(73), _Tp(79),
  236. _Tp(83), _Tp(89), _Tp(97), _Tp(101), _Tp(103), _Tp(107), _Tp(109)
  237. };
  238. static const unsigned int __num_primes = sizeof(__prime) / sizeof(_Tp);
  239. _Tp __zeta = _Tp(1);
  240. for (unsigned int __i = 0; __i < __num_primes; ++__i)
  241. {
  242. const _Tp __fact = _Tp(1) - std::pow(__prime[__i], -__s);
  243. __zeta *= __fact;
  244. if (_Tp(1) - __fact < std::numeric_limits<_Tp>::epsilon())
  245. break;
  246. }
  247. __zeta = _Tp(1) / __zeta;
  248. return __zeta;
  249. }
  250. /**
  251. * @brief Return the Riemann zeta function @f$ \zeta(s) @f$.
  252. *
  253. * The Riemann zeta function is defined by:
  254. * \f[
  255. * \zeta(s) = \sum_{k=1}^{\infty} k^{-s} for s > 1
  256. * \frac{(2\pi)^s}{pi} sin(\frac{\pi s}{2})
  257. * \Gamma (1 - s) \zeta (1 - s) for s < 1
  258. * \f]
  259. * For s < 1 use the reflection formula:
  260. * \f[
  261. * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
  262. * \f]
  263. */
  264. template<typename _Tp>
  265. _Tp
  266. __riemann_zeta(_Tp __s)
  267. {
  268. if (__isnan(__s))
  269. return std::numeric_limits<_Tp>::quiet_NaN();
  270. else if (__s == _Tp(1))
  271. return std::numeric_limits<_Tp>::infinity();
  272. else if (__s < -_Tp(19))
  273. {
  274. _Tp __zeta = __riemann_zeta_product(_Tp(1) - __s);
  275. __zeta *= std::pow(_Tp(2) * __numeric_constants<_Tp>::__pi(), __s)
  276. * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
  277. #if _GLIBCXX_USE_C99_MATH_TR1
  278. * std::exp(_GLIBCXX_MATH_NS::lgamma(_Tp(1) - __s))
  279. #else
  280. * std::exp(__log_gamma(_Tp(1) - __s))
  281. #endif
  282. / __numeric_constants<_Tp>::__pi();
  283. return __zeta;
  284. }
  285. else if (__s < _Tp(20))
  286. {
  287. // Global double sum or McLaurin?
  288. bool __glob = true;
  289. if (__glob)
  290. return __riemann_zeta_glob(__s);
  291. else
  292. {
  293. if (__s > _Tp(1))
  294. return __riemann_zeta_sum(__s);
  295. else
  296. {
  297. _Tp __zeta = std::pow(_Tp(2)
  298. * __numeric_constants<_Tp>::__pi(), __s)
  299. * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
  300. #if _GLIBCXX_USE_C99_MATH_TR1
  301. * _GLIBCXX_MATH_NS::tgamma(_Tp(1) - __s)
  302. #else
  303. * std::exp(__log_gamma(_Tp(1) - __s))
  304. #endif
  305. * __riemann_zeta_sum(_Tp(1) - __s);
  306. return __zeta;
  307. }
  308. }
  309. }
  310. else
  311. return __riemann_zeta_product(__s);
  312. }
  313. /**
  314. * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
  315. * for all s != 1 and x > -1.
  316. *
  317. * The Hurwitz zeta function is defined by:
  318. * @f[
  319. * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
  320. * @f]
  321. * The Riemann zeta function is a special case:
  322. * @f[
  323. * \zeta(s) = \zeta(1,s)
  324. * @f]
  325. *
  326. * This functions uses the double sum that converges for s != 1
  327. * and x > -1:
  328. * @f[
  329. * \zeta(x,s) = \frac{1}{s-1}
  330. * \sum_{n=0}^{\infty} \frac{1}{n + 1}
  331. * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (x+k)^{-s}
  332. * @f]
  333. */
  334. template<typename _Tp>
  335. _Tp
  336. __hurwitz_zeta_glob(_Tp __a, _Tp __s)
  337. {
  338. _Tp __zeta = _Tp(0);
  339. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  340. // Max e exponent before overflow.
  341. const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
  342. * std::log(_Tp(10)) - _Tp(1);
  343. const unsigned int __maxit = 10000;
  344. for (unsigned int __i = 0; __i < __maxit; ++__i)
  345. {
  346. bool __punt = false;
  347. _Tp __sgn = _Tp(1);
  348. _Tp __term = _Tp(0);
  349. for (unsigned int __j = 0; __j <= __i; ++__j)
  350. {
  351. #if _GLIBCXX_USE_C99_MATH_TR1
  352. _Tp __bincoeff = _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __i))
  353. - _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __j))
  354. - _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __i - __j));
  355. #else
  356. _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
  357. - __log_gamma(_Tp(1 + __j))
  358. - __log_gamma(_Tp(1 + __i - __j));
  359. #endif
  360. if (__bincoeff > __max_bincoeff)
  361. {
  362. // This only gets hit for x << 0.
  363. __punt = true;
  364. break;
  365. }
  366. __bincoeff = std::exp(__bincoeff);
  367. __term += __sgn * __bincoeff * std::pow(_Tp(__a + __j), -__s);
  368. __sgn *= _Tp(-1);
  369. }
  370. if (__punt)
  371. break;
  372. __term /= _Tp(__i + 1);
  373. if (std::abs(__term / __zeta) < __eps)
  374. break;
  375. __zeta += __term;
  376. }
  377. __zeta /= __s - _Tp(1);
  378. return __zeta;
  379. }
  380. /**
  381. * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
  382. * for all s != 1 and x > -1.
  383. *
  384. * The Hurwitz zeta function is defined by:
  385. * @f[
  386. * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
  387. * @f]
  388. * The Riemann zeta function is a special case:
  389. * @f[
  390. * \zeta(s) = \zeta(1,s)
  391. * @f]
  392. */
  393. template<typename _Tp>
  394. inline _Tp
  395. __hurwitz_zeta(_Tp __a, _Tp __s)
  396. { return __hurwitz_zeta_glob(__a, __s); }
  397. } // namespace __detail
  398. #undef _GLIBCXX_MATH_NS
  399. #if ! _GLIBCXX_USE_STD_SPEC_FUNCS && defined(_GLIBCXX_TR1_CMATH)
  400. } // namespace tr1
  401. #endif
  402. _GLIBCXX_END_NAMESPACE_VERSION
  403. }
  404. #endif // _GLIBCXX_TR1_RIEMANN_ZETA_TCC