1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006-2018 Free Software Foundation, Inc.
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)
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.
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.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file tr1/riemann_zeta.tcc
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{tr1/cmath}
31 // ISO C++ 14882 TR1: 5.2 Special functions
34 // Written by Edward Smith-Rowland based on:
35 // (1) Handbook of Mathematical Functions,
36 // Ed. by Milton Abramowitz and Irene A. Stegun,
37 // Dover Publications, New-York, Section 5, pp. 807-808.
38 // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
39 // (3) Gamma, Exploring Euler's Constant, Julian Havil,
42 #ifndef _GLIBCXX_TR1_RIEMANN_ZETA_TCC
43 #define _GLIBCXX_TR1_RIEMANN_ZETA_TCC 1
45 #include "special_function_util.h"
47 namespace std _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 #if _GLIBCXX_USE_STD_SPEC_FUNCS
52 # define _GLIBCXX_MATH_NS ::std
53 #elif defined(_GLIBCXX_TR1_CMATH)
56 # define _GLIBCXX_MATH_NS ::std::tr1
58 # error do not include this header directly, use <cmath> or <tr1/cmath>
60 // [5.2] Special functions
62 // Implementation-space details.
66 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
67 * by summation for s > 1.
69 * The Riemann zeta function is defined by:
71 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
73 * For s < 1 use the reflection formula:
75 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
78 template<typename _Tp>
80 __riemann_zeta_sum(_Tp __s)
82 // A user shouldn't get to this.
84 std::__throw_domain_error(__N("Bad argument in zeta sum."));
86 const unsigned int max_iter = 10000;
88 for (unsigned int __k = 1; __k < max_iter; ++__k)
90 _Tp __term = std::pow(static_cast<_Tp>(__k), -__s);
91 if (__term < std::numeric_limits<_Tp>::epsilon())
103 * @brief Evaluate the Riemann zeta function @f$ \zeta(s) @f$
104 * by an alternate series for s > 0.
106 * The Riemann zeta function is defined by:
108 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
110 * For s < 1 use the reflection formula:
112 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
115 template<typename _Tp>
117 __riemann_zeta_alt(_Tp __s)
121 for (unsigned int __i = 1; __i < 10000000; ++__i)
123 _Tp __term = __sgn / std::pow(__i, __s);
124 if (std::abs(__term) < std::numeric_limits<_Tp>::epsilon())
129 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
136 * @brief Evaluate the Riemann zeta function by series for all s != 1.
137 * Convergence is great until largish negative numbers.
138 * Then the convergence of the > 0 sum gets better.
142 * \zeta(s) = \frac{1}{1-2^{1-s}}
143 * \sum_{n=0}^{\infty} \frac{1}{2^{n+1}}
144 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (k+1)^{-s}
146 * Havil 2003, p. 206.
148 * The Riemann zeta function is defined by:
150 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
152 * For s < 1 use the reflection formula:
154 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
157 template<typename _Tp>
159 __riemann_zeta_glob(_Tp __s)
163 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
164 // Max e exponent before overflow.
165 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
166 * std::log(_Tp(10)) - _Tp(1);
168 // This series works until the binomial coefficient blows up
169 // so use reflection.
172 #if _GLIBCXX_USE_C99_MATH_TR1
173 if (_GLIBCXX_MATH_NS::fmod(__s,_Tp(2)) == _Tp(0))
178 _Tp __zeta = __riemann_zeta_glob(_Tp(1) - __s);
179 __zeta *= std::pow(_Tp(2)
180 * __numeric_constants<_Tp>::__pi(), __s)
181 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
182 #if _GLIBCXX_USE_C99_MATH_TR1
183 * std::exp(_GLIBCXX_MATH_NS::lgamma(_Tp(1) - __s))
185 * std::exp(__log_gamma(_Tp(1) - __s))
187 / __numeric_constants<_Tp>::__pi();
192 _Tp __num = _Tp(0.5L);
193 const unsigned int __maxit = 10000;
194 for (unsigned int __i = 0; __i < __maxit; ++__i)
199 for (unsigned int __j = 0; __j <= __i; ++__j)
201 #if _GLIBCXX_USE_C99_MATH_TR1
202 _Tp __bincoeff = _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __i))
203 - _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __j))
204 - _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __i - __j));
206 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
207 - __log_gamma(_Tp(1 + __j))
208 - __log_gamma(_Tp(1 + __i - __j));
210 if (__bincoeff > __max_bincoeff)
212 // This only gets hit for x << 0.
216 __bincoeff = std::exp(__bincoeff);
217 __term += __sgn * __bincoeff * std::pow(_Tp(1 + __j), -__s);
224 if (std::abs(__term/__zeta) < __eps)
229 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
236 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
237 * using the product over prime factors.
239 * \zeta(s) = \Pi_{i=1}^\infty \frac{1}{1 - p_i^{-s}}
241 * where @f$ {p_i} @f$ are the prime numbers.
243 * The Riemann zeta function is defined by:
245 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
247 * For s < 1 use the reflection formula:
249 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
252 template<typename _Tp>
254 __riemann_zeta_product(_Tp __s)
256 static const _Tp __prime[] = {
257 _Tp(2), _Tp(3), _Tp(5), _Tp(7), _Tp(11), _Tp(13), _Tp(17), _Tp(19),
258 _Tp(23), _Tp(29), _Tp(31), _Tp(37), _Tp(41), _Tp(43), _Tp(47),
259 _Tp(53), _Tp(59), _Tp(61), _Tp(67), _Tp(71), _Tp(73), _Tp(79),
260 _Tp(83), _Tp(89), _Tp(97), _Tp(101), _Tp(103), _Tp(107), _Tp(109)
262 static const unsigned int __num_primes = sizeof(__prime) / sizeof(_Tp);
265 for (unsigned int __i = 0; __i < __num_primes; ++__i)
267 const _Tp __fact = _Tp(1) - std::pow(__prime[__i], -__s);
269 if (_Tp(1) - __fact < std::numeric_limits<_Tp>::epsilon())
273 __zeta = _Tp(1) / __zeta;
280 * @brief Return the Riemann zeta function @f$ \zeta(s) @f$.
282 * The Riemann zeta function is defined by:
284 * \zeta(s) = \sum_{k=1}^{\infty} k^{-s} for s > 1
285 * \frac{(2\pi)^s}{pi} sin(\frac{\pi s}{2})
286 * \Gamma (1 - s) \zeta (1 - s) for s < 1
288 * For s < 1 use the reflection formula:
290 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
293 template<typename _Tp>
295 __riemann_zeta(_Tp __s)
298 return std::numeric_limits<_Tp>::quiet_NaN();
299 else if (__s == _Tp(1))
300 return std::numeric_limits<_Tp>::infinity();
301 else if (__s < -_Tp(19))
303 _Tp __zeta = __riemann_zeta_product(_Tp(1) - __s);
304 __zeta *= std::pow(_Tp(2) * __numeric_constants<_Tp>::__pi(), __s)
305 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
306 #if _GLIBCXX_USE_C99_MATH_TR1
307 * std::exp(_GLIBCXX_MATH_NS::lgamma(_Tp(1) - __s))
309 * std::exp(__log_gamma(_Tp(1) - __s))
311 / __numeric_constants<_Tp>::__pi();
314 else if (__s < _Tp(20))
316 // Global double sum or McLaurin?
319 return __riemann_zeta_glob(__s);
323 return __riemann_zeta_sum(__s);
326 _Tp __zeta = std::pow(_Tp(2)
327 * __numeric_constants<_Tp>::__pi(), __s)
328 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
329 #if _GLIBCXX_USE_C99_MATH_TR1
330 * _GLIBCXX_MATH_NS::tgamma(_Tp(1) - __s)
332 * std::exp(__log_gamma(_Tp(1) - __s))
334 * __riemann_zeta_sum(_Tp(1) - __s);
340 return __riemann_zeta_product(__s);
345 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
346 * for all s != 1 and x > -1.
348 * The Hurwitz zeta function is defined by:
350 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
352 * The Riemann zeta function is a special case:
354 * \zeta(s) = \zeta(1,s)
357 * This functions uses the double sum that converges for s != 1
360 * \zeta(x,s) = \frac{1}{s-1}
361 * \sum_{n=0}^{\infty} \frac{1}{n + 1}
362 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (x+k)^{-s}
365 template<typename _Tp>
367 __hurwitz_zeta_glob(_Tp __a, _Tp __s)
371 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
372 // Max e exponent before overflow.
373 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
374 * std::log(_Tp(10)) - _Tp(1);
376 const unsigned int __maxit = 10000;
377 for (unsigned int __i = 0; __i < __maxit; ++__i)
382 for (unsigned int __j = 0; __j <= __i; ++__j)
384 #if _GLIBCXX_USE_C99_MATH_TR1
385 _Tp __bincoeff = _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __i))
386 - _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __j))
387 - _GLIBCXX_MATH_NS::lgamma(_Tp(1 + __i - __j));
389 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
390 - __log_gamma(_Tp(1 + __j))
391 - __log_gamma(_Tp(1 + __i - __j));
393 if (__bincoeff > __max_bincoeff)
395 // This only gets hit for x << 0.
399 __bincoeff = std::exp(__bincoeff);
400 __term += __sgn * __bincoeff * std::pow(_Tp(__a + __j), -__s);
405 __term /= _Tp(__i + 1);
406 if (std::abs(__term / __zeta) < __eps)
411 __zeta /= __s - _Tp(1);
418 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
419 * for all s != 1 and x > -1.
421 * The Hurwitz zeta function is defined by:
423 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
425 * The Riemann zeta function is a special case:
427 * \zeta(s) = \zeta(1,s)
430 template<typename _Tp>
432 __hurwitz_zeta(_Tp __a, _Tp __s)
433 { return __hurwitz_zeta_glob(__a, __s); }
434 } // namespace __detail
435 #undef _GLIBCXX_MATH_NS
436 #if ! _GLIBCXX_USE_STD_SPEC_FUNCS && defined(_GLIBCXX_TR1_CMATH)
440 _GLIBCXX_END_NAMESPACE_VERSION
443 #endif // _GLIBCXX_TR1_RIEMANN_ZETA_TCC