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 _TR1_RIEMANN_ZETA_TCC
49 #define _TR1_RIEMANN_ZETA_TCC 1
51 #include "special_function_util.h"
55 _GLIBCXX_BEGIN_NAMESPACE(_GLIBCXX_TR1)
57 // [5.2] Special functions
60 * @ingroup tr1_math_spec_func
65 // Implementation-space details.
71 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
72 * by summation for s > 1.
74 * The Riemann zeta function is defined by:
76 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
78 * For s < 1 use the reflection formula:
80 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
83 template<typename _Tp>
85 __riemann_zeta_sum(const _Tp __s)
87 // A user shouldn't get to this.
89 std::__throw_domain_error(__N("Bad argument in zeta sum."));
91 const unsigned int max_iter = 10000;
93 for (unsigned int __k = 1; __k < max_iter; ++__k)
95 _Tp __term = std::pow(static_cast<_Tp>(__k), -__s);
96 if (__term < std::numeric_limits<_Tp>::epsilon())
108 * @brief Evaluate the Riemann zeta function @f$ \zeta(s) @f$
109 * by an alternate series for s > 0.
111 * The Riemann zeta function is defined by:
113 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
115 * For s < 1 use the reflection formula:
117 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
120 template<typename _Tp>
122 __riemann_zeta_alt(const _Tp __s)
126 for (unsigned int __i = 1; __i < 10000000; ++__i)
128 _Tp __term = __sgn / std::pow(__i, __s);
129 if (std::abs(__term) < std::numeric_limits<_Tp>::epsilon())
134 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
141 * @brief Evaluate the Riemann zeta function by series for all s != 1.
142 * Convergence is great until largish negative numbers.
143 * Then the convergence of the > 0 sum gets better.
147 * \zeta(s) = \frac{1}{1-2^{1-s}}
148 * \sum_{n=0}^{\infty} \frac{1}{2^{n+1}}
149 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (k+1)^{-s}
151 * Havil 2003, p. 206.
153 * The Riemann zeta function is defined by:
155 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
157 * For s < 1 use the reflection formula:
159 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
162 template<typename _Tp>
164 __riemann_zeta_glob(const _Tp __s)
168 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
169 // Max e exponent before overflow.
170 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
171 * std::log(_Tp(10)) - _Tp(1);
173 // This series works until the binomial coefficient blows up
174 // so use reflection.
177 #if _GLIBCXX_USE_C99_MATH_TR1
178 if (std::_GLIBCXX_TR1::fmod(__s,_Tp(2)) == _Tp(0))
183 _Tp __zeta = __riemann_zeta_glob(_Tp(1) - __s);
184 __zeta *= std::pow(_Tp(2)
185 * __numeric_constants<_Tp>::__pi(), __s)
186 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
187 #if _GLIBCXX_USE_C99_MATH_TR1
188 * std::exp(std::_GLIBCXX_TR1::lgamma(_Tp(1) - __s))
190 * std::exp(__log_gamma(_Tp(1) - __s))
192 / __numeric_constants<_Tp>::__pi();
197 _Tp __num = _Tp(0.5L);
198 const unsigned int __maxit = 10000;
199 for (unsigned int __i = 0; __i < __maxit; ++__i)
204 for (unsigned int __j = 0; __j <= __i; ++__j)
206 #if _GLIBCXX_USE_C99_MATH_TR1
207 _Tp __bincoeff = std::_GLIBCXX_TR1::lgamma(_Tp(1 + __i))
208 - std::_GLIBCXX_TR1::lgamma(_Tp(1 + __j))
209 - std::_GLIBCXX_TR1::lgamma(_Tp(1 + __i - __j));
211 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
212 - __log_gamma(_Tp(1 + __j))
213 - __log_gamma(_Tp(1 + __i - __j));
215 if (__bincoeff > __max_bincoeff)
217 // This only gets hit for x << 0.
221 __bincoeff = std::exp(__bincoeff);
222 __term += __sgn * __bincoeff * std::pow(_Tp(1 + __j), -__s);
229 if (std::abs(__term/__zeta) < __eps)
234 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
241 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
242 * using the product over prime factors.
244 * \zeta(s) = \Pi_{i=1}^\infty \frac{1}{1 - p_i^{-s}}
246 * where @f$ {p_i} @f$ are the prime numbers.
248 * The Riemann zeta function is defined by:
250 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
252 * For s < 1 use the reflection formula:
254 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
257 template<typename _Tp>
259 __riemann_zeta_product(const _Tp __s)
261 static const _Tp __prime[] = {
262 _Tp(2), _Tp(3), _Tp(5), _Tp(7), _Tp(11), _Tp(13), _Tp(17), _Tp(19),
263 _Tp(23), _Tp(29), _Tp(31), _Tp(37), _Tp(41), _Tp(43), _Tp(47),
264 _Tp(53), _Tp(59), _Tp(61), _Tp(67), _Tp(71), _Tp(73), _Tp(79),
265 _Tp(83), _Tp(89), _Tp(97), _Tp(101), _Tp(103), _Tp(107), _Tp(109)
267 static const unsigned int __num_primes = sizeof(__prime) / sizeof(_Tp);
270 for (unsigned int __i = 0; __i < __num_primes; ++__i)
272 const _Tp __fact = _Tp(1) - std::pow(__prime[__i], -__s);
274 if (_Tp(1) - __fact < std::numeric_limits<_Tp>::epsilon())
278 __zeta = _Tp(1) / __zeta;
285 * @brief Return the Riemann zeta function @f$ \zeta(s) @f$.
287 * The Riemann zeta function is defined by:
289 * \zeta(s) = \sum_{k=1}^{\infty} k^{-s} for s > 1
290 * \frac{(2\pi)^s}{pi} sin(\frac{\pi s}{2})
291 * \Gamma (1 - s) \zeta (1 - s) for s < 1
293 * For s < 1 use the reflection formula:
295 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
298 template<typename _Tp>
300 __riemann_zeta(const _Tp __s)
303 return std::numeric_limits<_Tp>::quiet_NaN();
304 else if (__s == _Tp(1))
305 return std::numeric_limits<_Tp>::infinity();
306 else if (__s < -_Tp(19))
308 _Tp __zeta = __riemann_zeta_product(_Tp(1) - __s);
309 __zeta *= std::pow(_Tp(2) * __numeric_constants<_Tp>::__pi(), __s)
310 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
311 #if _GLIBCXX_USE_C99_MATH_TR1
312 * std::exp(std::_GLIBCXX_TR1::lgamma(_Tp(1) - __s))
314 * std::exp(__log_gamma(_Tp(1) - __s))
316 / __numeric_constants<_Tp>::__pi();
319 else if (__s < _Tp(20))
321 // Global double sum or McLaurin?
324 return __riemann_zeta_glob(__s);
328 return __riemann_zeta_sum(__s);
331 _Tp __zeta = std::pow(_Tp(2)
332 * __numeric_constants<_Tp>::__pi(), __s)
333 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
334 #if _GLIBCXX_USE_C99_MATH_TR1
335 * std::_GLIBCXX_TR1::tgamma(_Tp(1) - __s)
337 * std::exp(__log_gamma(_Tp(1) - __s))
339 * __riemann_zeta_sum(_Tp(1) - __s);
345 return __riemann_zeta_product(__s);
350 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
351 * for all s != 1 and x > -1.
353 * The Hurwitz zeta function is defined by:
355 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
357 * The Riemann zeta function is a special case:
359 * \zeta(s) = \zeta(1,s)
362 * This functions uses the double sum that converges for s != 1
365 * \zeta(x,s) = \frac{1}{s-1}
366 * \sum_{n=0}^{\infty} \frac{1}{n + 1}
367 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (x+k)^{-s}
370 template<typename _Tp>
372 __hurwitz_zeta_glob(const _Tp __a, const _Tp __s)
376 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
377 // Max e exponent before overflow.
378 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
379 * std::log(_Tp(10)) - _Tp(1);
381 const unsigned int __maxit = 10000;
382 for (unsigned int __i = 0; __i < __maxit; ++__i)
387 for (unsigned int __j = 0; __j <= __i; ++__j)
389 #if _GLIBCXX_USE_C99_MATH_TR1
390 _Tp __bincoeff = std::_GLIBCXX_TR1::lgamma(_Tp(1 + __i))
391 - std::_GLIBCXX_TR1::lgamma(_Tp(1 + __j))
392 - std::_GLIBCXX_TR1::lgamma(_Tp(1 + __i - __j));
394 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
395 - __log_gamma(_Tp(1 + __j))
396 - __log_gamma(_Tp(1 + __i - __j));
398 if (__bincoeff > __max_bincoeff)
400 // This only gets hit for x << 0.
404 __bincoeff = std::exp(__bincoeff);
405 __term += __sgn * __bincoeff * std::pow(_Tp(__a + __j), -__s);
410 __term /= _Tp(__i + 1);
411 if (std::abs(__term / __zeta) < __eps)
416 __zeta /= __s - _Tp(1);
423 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
424 * for all s != 1 and x > -1.
426 * The Hurwitz zeta function is defined by:
428 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
430 * The Riemann zeta function is a special case:
432 * \zeta(s) = \zeta(1,s)
435 template<typename _Tp>
437 __hurwitz_zeta(const _Tp __a, const _Tp __s)
439 return __hurwitz_zeta_glob(__a, __s);
442 } // namespace std::tr1::__detail
444 /* @} */ // group tr1_math_spec_func
446 _GLIBCXX_END_NAMESPACE
449 #endif // _TR1_RIEMANN_ZETA_TCC