1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006-2013 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)
51 // [5.2] Special functions
53 // Implementation-space details.
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
60 * by summation for s > 1.
62 * The Riemann zeta function is defined by:
64 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
66 * For s < 1 use the reflection formula:
68 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
71 template<typename _Tp>
73 __riemann_zeta_sum(_Tp __s)
75 // A user shouldn't get to this.
77 std::__throw_domain_error(__N("Bad argument in zeta sum."));
79 const unsigned int max_iter = 10000;
81 for (unsigned int __k = 1; __k < max_iter; ++__k)
83 _Tp __term = std::pow(static_cast<_Tp>(__k), -__s);
84 if (__term < std::numeric_limits<_Tp>::epsilon())
96 * @brief Evaluate the Riemann zeta function @f$ \zeta(s) @f$
97 * by an alternate series for s > 0.
99 * The Riemann zeta function is defined by:
101 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
103 * For s < 1 use the reflection formula:
105 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
108 template<typename _Tp>
110 __riemann_zeta_alt(_Tp __s)
114 for (unsigned int __i = 1; __i < 10000000; ++__i)
116 _Tp __term = __sgn / std::pow(__i, __s);
117 if (std::abs(__term) < std::numeric_limits<_Tp>::epsilon())
122 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
129 * @brief Evaluate the Riemann zeta function by series for all s != 1.
130 * Convergence is great until largish negative numbers.
131 * Then the convergence of the > 0 sum gets better.
135 * \zeta(s) = \frac{1}{1-2^{1-s}}
136 * \sum_{n=0}^{\infty} \frac{1}{2^{n+1}}
137 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (k+1)^{-s}
139 * Havil 2003, p. 206.
141 * The Riemann zeta function is defined by:
143 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
145 * For s < 1 use the reflection formula:
147 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
150 template<typename _Tp>
152 __riemann_zeta_glob(_Tp __s)
156 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
157 // Max e exponent before overflow.
158 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
159 * std::log(_Tp(10)) - _Tp(1);
161 // This series works until the binomial coefficient blows up
162 // so use reflection.
165 #if _GLIBCXX_USE_C99_MATH_TR1
166 if (std::tr1::fmod(__s,_Tp(2)) == _Tp(0))
171 _Tp __zeta = __riemann_zeta_glob(_Tp(1) - __s);
172 __zeta *= std::pow(_Tp(2)
173 * __numeric_constants<_Tp>::__pi(), __s)
174 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
175 #if _GLIBCXX_USE_C99_MATH_TR1
176 * std::exp(std::tr1::lgamma(_Tp(1) - __s))
178 * std::exp(__log_gamma(_Tp(1) - __s))
180 / __numeric_constants<_Tp>::__pi();
185 _Tp __num = _Tp(0.5L);
186 const unsigned int __maxit = 10000;
187 for (unsigned int __i = 0; __i < __maxit; ++__i)
192 for (unsigned int __j = 0; __j <= __i; ++__j)
194 #if _GLIBCXX_USE_C99_MATH_TR1
195 _Tp __bincoeff = std::tr1::lgamma(_Tp(1 + __i))
196 - std::tr1::lgamma(_Tp(1 + __j))
197 - std::tr1::lgamma(_Tp(1 + __i - __j));
199 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
200 - __log_gamma(_Tp(1 + __j))
201 - __log_gamma(_Tp(1 + __i - __j));
203 if (__bincoeff > __max_bincoeff)
205 // This only gets hit for x << 0.
209 __bincoeff = std::exp(__bincoeff);
210 __term += __sgn * __bincoeff * std::pow(_Tp(1 + __j), -__s);
217 if (std::abs(__term/__zeta) < __eps)
222 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
229 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
230 * using the product over prime factors.
232 * \zeta(s) = \Pi_{i=1}^\infty \frac{1}{1 - p_i^{-s}}
234 * where @f$ {p_i} @f$ are the prime numbers.
236 * The Riemann zeta function is defined by:
238 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
240 * For s < 1 use the reflection formula:
242 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
245 template<typename _Tp>
247 __riemann_zeta_product(_Tp __s)
249 static const _Tp __prime[] = {
250 _Tp(2), _Tp(3), _Tp(5), _Tp(7), _Tp(11), _Tp(13), _Tp(17), _Tp(19),
251 _Tp(23), _Tp(29), _Tp(31), _Tp(37), _Tp(41), _Tp(43), _Tp(47),
252 _Tp(53), _Tp(59), _Tp(61), _Tp(67), _Tp(71), _Tp(73), _Tp(79),
253 _Tp(83), _Tp(89), _Tp(97), _Tp(101), _Tp(103), _Tp(107), _Tp(109)
255 static const unsigned int __num_primes = sizeof(__prime) / sizeof(_Tp);
258 for (unsigned int __i = 0; __i < __num_primes; ++__i)
260 const _Tp __fact = _Tp(1) - std::pow(__prime[__i], -__s);
262 if (_Tp(1) - __fact < std::numeric_limits<_Tp>::epsilon())
266 __zeta = _Tp(1) / __zeta;
273 * @brief Return the Riemann zeta function @f$ \zeta(s) @f$.
275 * The Riemann zeta function is defined by:
277 * \zeta(s) = \sum_{k=1}^{\infty} k^{-s} for s > 1
278 * \frac{(2\pi)^s}{pi} sin(\frac{\pi s}{2})
279 * \Gamma (1 - s) \zeta (1 - s) for s < 1
281 * For s < 1 use the reflection formula:
283 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
286 template<typename _Tp>
288 __riemann_zeta(_Tp __s)
291 return std::numeric_limits<_Tp>::quiet_NaN();
292 else if (__s == _Tp(1))
293 return std::numeric_limits<_Tp>::infinity();
294 else if (__s < -_Tp(19))
296 _Tp __zeta = __riemann_zeta_product(_Tp(1) - __s);
297 __zeta *= std::pow(_Tp(2) * __numeric_constants<_Tp>::__pi(), __s)
298 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
299 #if _GLIBCXX_USE_C99_MATH_TR1
300 * std::exp(std::tr1::lgamma(_Tp(1) - __s))
302 * std::exp(__log_gamma(_Tp(1) - __s))
304 / __numeric_constants<_Tp>::__pi();
307 else if (__s < _Tp(20))
309 // Global double sum or McLaurin?
312 return __riemann_zeta_glob(__s);
316 return __riemann_zeta_sum(__s);
319 _Tp __zeta = std::pow(_Tp(2)
320 * __numeric_constants<_Tp>::__pi(), __s)
321 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
322 #if _GLIBCXX_USE_C99_MATH_TR1
323 * std::tr1::tgamma(_Tp(1) - __s)
325 * std::exp(__log_gamma(_Tp(1) - __s))
327 * __riemann_zeta_sum(_Tp(1) - __s);
333 return __riemann_zeta_product(__s);
338 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
339 * for all s != 1 and x > -1.
341 * The Hurwitz zeta function is defined by:
343 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
345 * The Riemann zeta function is a special case:
347 * \zeta(s) = \zeta(1,s)
350 * This functions uses the double sum that converges for s != 1
353 * \zeta(x,s) = \frac{1}{s-1}
354 * \sum_{n=0}^{\infty} \frac{1}{n + 1}
355 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (x+k)^{-s}
358 template<typename _Tp>
360 __hurwitz_zeta_glob(_Tp __a, _Tp __s)
364 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
365 // Max e exponent before overflow.
366 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
367 * std::log(_Tp(10)) - _Tp(1);
369 const unsigned int __maxit = 10000;
370 for (unsigned int __i = 0; __i < __maxit; ++__i)
375 for (unsigned int __j = 0; __j <= __i; ++__j)
377 #if _GLIBCXX_USE_C99_MATH_TR1
378 _Tp __bincoeff = std::tr1::lgamma(_Tp(1 + __i))
379 - std::tr1::lgamma(_Tp(1 + __j))
380 - std::tr1::lgamma(_Tp(1 + __i - __j));
382 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
383 - __log_gamma(_Tp(1 + __j))
384 - __log_gamma(_Tp(1 + __i - __j));
386 if (__bincoeff > __max_bincoeff)
388 // This only gets hit for x << 0.
392 __bincoeff = std::exp(__bincoeff);
393 __term += __sgn * __bincoeff * std::pow(_Tp(__a + __j), -__s);
398 __term /= _Tp(__i + 1);
399 if (std::abs(__term / __zeta) < __eps)
404 __zeta /= __s - _Tp(1);
411 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
412 * for all s != 1 and x > -1.
414 * The Hurwitz zeta function is defined by:
416 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
418 * The Riemann zeta function is a special case:
420 * \zeta(s) = \zeta(1,s)
423 template<typename _Tp>
425 __hurwitz_zeta(_Tp __a, _Tp __s)
426 { return __hurwitz_zeta_glob(__a, __s); }
428 _GLIBCXX_END_NAMESPACE_VERSION
429 } // namespace std::tr1::__detail
433 #endif // _GLIBCXX_TR1_RIEMANN_ZETA_TCC