1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006-2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file tr1/riemann_zeta.tcc
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
37 // ISO C++ 14882 TR1: 5.2 Special functions
40 // Written by Edward Smith-Rowland based on:
41 // (1) Handbook of Mathematical Functions,
42 // Ed. by Milton Abramowitz and Irene A. Stegun,
43 // Dover Publications, New-York, Section 5, pp. 807-808.
44 // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
45 // (3) Gamma, Exploring Euler's Constant, Julian Havil,
48 #ifndef _GLIBCXX_TR1_RIEMANN_ZETA_TCC
49 #define _GLIBCXX_TR1_RIEMANN_ZETA_TCC 1
51 #include "special_function_util.h"
58 // [5.2] Special functions
61 * @ingroup tr1_math_spec_func
66 // Implementation-space details.
72 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
73 * by summation for s > 1.
75 * The Riemann zeta function is defined by:
77 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
79 * For s < 1 use the reflection formula:
81 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
84 template<typename _Tp>
86 __riemann_zeta_sum(const _Tp __s)
88 // A user shouldn't get to this.
90 std::__throw_domain_error(__N("Bad argument in zeta sum."));
92 const unsigned int max_iter = 10000;
94 for (unsigned int __k = 1; __k < max_iter; ++__k)
96 _Tp __term = std::pow(static_cast<_Tp>(__k), -__s);
97 if (__term < std::numeric_limits<_Tp>::epsilon())
109 * @brief Evaluate the Riemann zeta function @f$ \zeta(s) @f$
110 * by an alternate series for s > 0.
112 * The Riemann zeta function is defined by:
114 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
116 * For s < 1 use the reflection formula:
118 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
121 template<typename _Tp>
123 __riemann_zeta_alt(const _Tp __s)
127 for (unsigned int __i = 1; __i < 10000000; ++__i)
129 _Tp __term = __sgn / std::pow(__i, __s);
130 if (std::abs(__term) < std::numeric_limits<_Tp>::epsilon())
135 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
142 * @brief Evaluate the Riemann zeta function by series for all s != 1.
143 * Convergence is great until largish negative numbers.
144 * Then the convergence of the > 0 sum gets better.
148 * \zeta(s) = \frac{1}{1-2^{1-s}}
149 * \sum_{n=0}^{\infty} \frac{1}{2^{n+1}}
150 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (k+1)^{-s}
152 * Havil 2003, p. 206.
154 * The Riemann zeta function is defined by:
156 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
158 * For s < 1 use the reflection formula:
160 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
163 template<typename _Tp>
165 __riemann_zeta_glob(const _Tp __s)
169 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
170 // Max e exponent before overflow.
171 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
172 * std::log(_Tp(10)) - _Tp(1);
174 // This series works until the binomial coefficient blows up
175 // so use reflection.
178 #if _GLIBCXX_USE_C99_MATH_TR1
179 if (std::tr1::fmod(__s,_Tp(2)) == _Tp(0))
184 _Tp __zeta = __riemann_zeta_glob(_Tp(1) - __s);
185 __zeta *= std::pow(_Tp(2)
186 * __numeric_constants<_Tp>::__pi(), __s)
187 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
188 #if _GLIBCXX_USE_C99_MATH_TR1
189 * std::exp(std::tr1::lgamma(_Tp(1) - __s))
191 * std::exp(__log_gamma(_Tp(1) - __s))
193 / __numeric_constants<_Tp>::__pi();
198 _Tp __num = _Tp(0.5L);
199 const unsigned int __maxit = 10000;
200 for (unsigned int __i = 0; __i < __maxit; ++__i)
205 for (unsigned int __j = 0; __j <= __i; ++__j)
207 #if _GLIBCXX_USE_C99_MATH_TR1
208 _Tp __bincoeff = std::tr1::lgamma(_Tp(1 + __i))
209 - std::tr1::lgamma(_Tp(1 + __j))
210 - std::tr1::lgamma(_Tp(1 + __i - __j));
212 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
213 - __log_gamma(_Tp(1 + __j))
214 - __log_gamma(_Tp(1 + __i - __j));
216 if (__bincoeff > __max_bincoeff)
218 // This only gets hit for x << 0.
222 __bincoeff = std::exp(__bincoeff);
223 __term += __sgn * __bincoeff * std::pow(_Tp(1 + __j), -__s);
230 if (std::abs(__term/__zeta) < __eps)
235 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
242 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
243 * using the product over prime factors.
245 * \zeta(s) = \Pi_{i=1}^\infty \frac{1}{1 - p_i^{-s}}
247 * where @f$ {p_i} @f$ are the prime numbers.
249 * The Riemann zeta function is defined by:
251 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
253 * For s < 1 use the reflection formula:
255 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
258 template<typename _Tp>
260 __riemann_zeta_product(const _Tp __s)
262 static const _Tp __prime[] = {
263 _Tp(2), _Tp(3), _Tp(5), _Tp(7), _Tp(11), _Tp(13), _Tp(17), _Tp(19),
264 _Tp(23), _Tp(29), _Tp(31), _Tp(37), _Tp(41), _Tp(43), _Tp(47),
265 _Tp(53), _Tp(59), _Tp(61), _Tp(67), _Tp(71), _Tp(73), _Tp(79),
266 _Tp(83), _Tp(89), _Tp(97), _Tp(101), _Tp(103), _Tp(107), _Tp(109)
268 static const unsigned int __num_primes = sizeof(__prime) / sizeof(_Tp);
271 for (unsigned int __i = 0; __i < __num_primes; ++__i)
273 const _Tp __fact = _Tp(1) - std::pow(__prime[__i], -__s);
275 if (_Tp(1) - __fact < std::numeric_limits<_Tp>::epsilon())
279 __zeta = _Tp(1) / __zeta;
286 * @brief Return the Riemann zeta function @f$ \zeta(s) @f$.
288 * The Riemann zeta function is defined by:
290 * \zeta(s) = \sum_{k=1}^{\infty} k^{-s} for s > 1
291 * \frac{(2\pi)^s}{pi} sin(\frac{\pi s}{2})
292 * \Gamma (1 - s) \zeta (1 - s) for s < 1
294 * For s < 1 use the reflection formula:
296 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
299 template<typename _Tp>
301 __riemann_zeta(const _Tp __s)
304 return std::numeric_limits<_Tp>::quiet_NaN();
305 else if (__s == _Tp(1))
306 return std::numeric_limits<_Tp>::infinity();
307 else if (__s < -_Tp(19))
309 _Tp __zeta = __riemann_zeta_product(_Tp(1) - __s);
310 __zeta *= std::pow(_Tp(2) * __numeric_constants<_Tp>::__pi(), __s)
311 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
312 #if _GLIBCXX_USE_C99_MATH_TR1
313 * std::exp(std::tr1::lgamma(_Tp(1) - __s))
315 * std::exp(__log_gamma(_Tp(1) - __s))
317 / __numeric_constants<_Tp>::__pi();
320 else if (__s < _Tp(20))
322 // Global double sum or McLaurin?
325 return __riemann_zeta_glob(__s);
329 return __riemann_zeta_sum(__s);
332 _Tp __zeta = std::pow(_Tp(2)
333 * __numeric_constants<_Tp>::__pi(), __s)
334 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
335 #if _GLIBCXX_USE_C99_MATH_TR1
336 * std::tr1::tgamma(_Tp(1) - __s)
338 * std::exp(__log_gamma(_Tp(1) - __s))
340 * __riemann_zeta_sum(_Tp(1) - __s);
346 return __riemann_zeta_product(__s);
351 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
352 * for all s != 1 and x > -1.
354 * The Hurwitz zeta function is defined by:
356 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
358 * The Riemann zeta function is a special case:
360 * \zeta(s) = \zeta(1,s)
363 * This functions uses the double sum that converges for s != 1
366 * \zeta(x,s) = \frac{1}{s-1}
367 * \sum_{n=0}^{\infty} \frac{1}{n + 1}
368 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (x+k)^{-s}
371 template<typename _Tp>
373 __hurwitz_zeta_glob(const _Tp __a, const _Tp __s)
377 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
378 // Max e exponent before overflow.
379 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
380 * std::log(_Tp(10)) - _Tp(1);
382 const unsigned int __maxit = 10000;
383 for (unsigned int __i = 0; __i < __maxit; ++__i)
388 for (unsigned int __j = 0; __j <= __i; ++__j)
390 #if _GLIBCXX_USE_C99_MATH_TR1
391 _Tp __bincoeff = std::tr1::lgamma(_Tp(1 + __i))
392 - std::tr1::lgamma(_Tp(1 + __j))
393 - std::tr1::lgamma(_Tp(1 + __i - __j));
395 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
396 - __log_gamma(_Tp(1 + __j))
397 - __log_gamma(_Tp(1 + __i - __j));
399 if (__bincoeff > __max_bincoeff)
401 // This only gets hit for x << 0.
405 __bincoeff = std::exp(__bincoeff);
406 __term += __sgn * __bincoeff * std::pow(_Tp(__a + __j), -__s);
411 __term /= _Tp(__i + 1);
412 if (std::abs(__term / __zeta) < __eps)
417 __zeta /= __s - _Tp(1);
424 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
425 * for all s != 1 and x > -1.
427 * The Hurwitz zeta function is defined by:
429 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
431 * The Riemann zeta function is a special case:
433 * \zeta(s) = \zeta(1,s)
436 template<typename _Tp>
438 __hurwitz_zeta(const _Tp __a, const _Tp __s)
440 return __hurwitz_zeta_glob(__a, __s);
443 } // namespace std::tr1::__detail
445 /* @} */ // group tr1_math_spec_func
450 #endif // _GLIBCXX_TR1_RIEMANN_ZETA_TCC