1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006, 2007, 2008, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file tr1/riemann_zeta.tcc
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{tr1/cmath}
32 // ISO C++ 14882 TR1: 5.2 Special functions
35 // Written by Edward Smith-Rowland based on:
36 // (1) Handbook of Mathematical Functions,
37 // Ed. by Milton Abramowitz and Irene A. Stegun,
38 // Dover Publications, New-York, Section 5, pp. 807-808.
39 // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
40 // (3) Gamma, Exploring Euler's Constant, Julian Havil,
43 #ifndef _GLIBCXX_TR1_RIEMANN_ZETA_TCC
44 #define _GLIBCXX_TR1_RIEMANN_ZETA_TCC 1
46 #include "special_function_util.h"
48 namespace std _GLIBCXX_VISIBILITY(default)
52 // [5.2] Special functions
54 // Implementation-space details.
57 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
61 * by summation for s > 1.
63 * The Riemann zeta function is defined by:
65 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
67 * For s < 1 use the reflection formula:
69 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
72 template<typename _Tp>
74 __riemann_zeta_sum(const _Tp __s)
76 // A user shouldn't get to this.
78 std::__throw_domain_error(__N("Bad argument in zeta sum."));
80 const unsigned int max_iter = 10000;
82 for (unsigned int __k = 1; __k < max_iter; ++__k)
84 _Tp __term = std::pow(static_cast<_Tp>(__k), -__s);
85 if (__term < std::numeric_limits<_Tp>::epsilon())
97 * @brief Evaluate the Riemann zeta function @f$ \zeta(s) @f$
98 * by an alternate series for s > 0.
100 * The Riemann zeta function is defined by:
102 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
104 * For s < 1 use the reflection formula:
106 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
109 template<typename _Tp>
111 __riemann_zeta_alt(const _Tp __s)
115 for (unsigned int __i = 1; __i < 10000000; ++__i)
117 _Tp __term = __sgn / std::pow(__i, __s);
118 if (std::abs(__term) < std::numeric_limits<_Tp>::epsilon())
123 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
130 * @brief Evaluate the Riemann zeta function by series for all s != 1.
131 * Convergence is great until largish negative numbers.
132 * Then the convergence of the > 0 sum gets better.
136 * \zeta(s) = \frac{1}{1-2^{1-s}}
137 * \sum_{n=0}^{\infty} \frac{1}{2^{n+1}}
138 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (k+1)^{-s}
140 * Havil 2003, p. 206.
142 * The Riemann zeta function is defined by:
144 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
146 * For s < 1 use the reflection formula:
148 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
151 template<typename _Tp>
153 __riemann_zeta_glob(const _Tp __s)
157 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
158 // Max e exponent before overflow.
159 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
160 * std::log(_Tp(10)) - _Tp(1);
162 // This series works until the binomial coefficient blows up
163 // so use reflection.
166 #if _GLIBCXX_USE_C99_MATH_TR1
167 if (std::tr1::fmod(__s,_Tp(2)) == _Tp(0))
172 _Tp __zeta = __riemann_zeta_glob(_Tp(1) - __s);
173 __zeta *= std::pow(_Tp(2)
174 * __numeric_constants<_Tp>::__pi(), __s)
175 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
176 #if _GLIBCXX_USE_C99_MATH_TR1
177 * std::exp(std::tr1::lgamma(_Tp(1) - __s))
179 * std::exp(__log_gamma(_Tp(1) - __s))
181 / __numeric_constants<_Tp>::__pi();
186 _Tp __num = _Tp(0.5L);
187 const unsigned int __maxit = 10000;
188 for (unsigned int __i = 0; __i < __maxit; ++__i)
193 for (unsigned int __j = 0; __j <= __i; ++__j)
195 #if _GLIBCXX_USE_C99_MATH_TR1
196 _Tp __bincoeff = std::tr1::lgamma(_Tp(1 + __i))
197 - std::tr1::lgamma(_Tp(1 + __j))
198 - std::tr1::lgamma(_Tp(1 + __i - __j));
200 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
201 - __log_gamma(_Tp(1 + __j))
202 - __log_gamma(_Tp(1 + __i - __j));
204 if (__bincoeff > __max_bincoeff)
206 // This only gets hit for x << 0.
210 __bincoeff = std::exp(__bincoeff);
211 __term += __sgn * __bincoeff * std::pow(_Tp(1 + __j), -__s);
218 if (std::abs(__term/__zeta) < __eps)
223 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
230 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
231 * using the product over prime factors.
233 * \zeta(s) = \Pi_{i=1}^\infty \frac{1}{1 - p_i^{-s}}
235 * where @f$ {p_i} @f$ are the prime numbers.
237 * The Riemann zeta function is defined by:
239 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
241 * For s < 1 use the reflection formula:
243 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
246 template<typename _Tp>
248 __riemann_zeta_product(const _Tp __s)
250 static const _Tp __prime[] = {
251 _Tp(2), _Tp(3), _Tp(5), _Tp(7), _Tp(11), _Tp(13), _Tp(17), _Tp(19),
252 _Tp(23), _Tp(29), _Tp(31), _Tp(37), _Tp(41), _Tp(43), _Tp(47),
253 _Tp(53), _Tp(59), _Tp(61), _Tp(67), _Tp(71), _Tp(73), _Tp(79),
254 _Tp(83), _Tp(89), _Tp(97), _Tp(101), _Tp(103), _Tp(107), _Tp(109)
256 static const unsigned int __num_primes = sizeof(__prime) / sizeof(_Tp);
259 for (unsigned int __i = 0; __i < __num_primes; ++__i)
261 const _Tp __fact = _Tp(1) - std::pow(__prime[__i], -__s);
263 if (_Tp(1) - __fact < std::numeric_limits<_Tp>::epsilon())
267 __zeta = _Tp(1) / __zeta;
274 * @brief Return the Riemann zeta function @f$ \zeta(s) @f$.
276 * The Riemann zeta function is defined by:
278 * \zeta(s) = \sum_{k=1}^{\infty} k^{-s} for s > 1
279 * \frac{(2\pi)^s}{pi} sin(\frac{\pi s}{2})
280 * \Gamma (1 - s) \zeta (1 - s) for s < 1
282 * For s < 1 use the reflection formula:
284 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
287 template<typename _Tp>
289 __riemann_zeta(const _Tp __s)
292 return std::numeric_limits<_Tp>::quiet_NaN();
293 else if (__s == _Tp(1))
294 return std::numeric_limits<_Tp>::infinity();
295 else if (__s < -_Tp(19))
297 _Tp __zeta = __riemann_zeta_product(_Tp(1) - __s);
298 __zeta *= std::pow(_Tp(2) * __numeric_constants<_Tp>::__pi(), __s)
299 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
300 #if _GLIBCXX_USE_C99_MATH_TR1
301 * std::exp(std::tr1::lgamma(_Tp(1) - __s))
303 * std::exp(__log_gamma(_Tp(1) - __s))
305 / __numeric_constants<_Tp>::__pi();
308 else if (__s < _Tp(20))
310 // Global double sum or McLaurin?
313 return __riemann_zeta_glob(__s);
317 return __riemann_zeta_sum(__s);
320 _Tp __zeta = std::pow(_Tp(2)
321 * __numeric_constants<_Tp>::__pi(), __s)
322 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
323 #if _GLIBCXX_USE_C99_MATH_TR1
324 * std::tr1::tgamma(_Tp(1) - __s)
326 * std::exp(__log_gamma(_Tp(1) - __s))
328 * __riemann_zeta_sum(_Tp(1) - __s);
334 return __riemann_zeta_product(__s);
339 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
340 * for all s != 1 and x > -1.
342 * The Hurwitz zeta function is defined by:
344 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
346 * The Riemann zeta function is a special case:
348 * \zeta(s) = \zeta(1,s)
351 * This functions uses the double sum that converges for s != 1
354 * \zeta(x,s) = \frac{1}{s-1}
355 * \sum_{n=0}^{\infty} \frac{1}{n + 1}
356 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (x+k)^{-s}
359 template<typename _Tp>
361 __hurwitz_zeta_glob(const _Tp __a, const _Tp __s)
365 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
366 // Max e exponent before overflow.
367 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
368 * std::log(_Tp(10)) - _Tp(1);
370 const unsigned int __maxit = 10000;
371 for (unsigned int __i = 0; __i < __maxit; ++__i)
376 for (unsigned int __j = 0; __j <= __i; ++__j)
378 #if _GLIBCXX_USE_C99_MATH_TR1
379 _Tp __bincoeff = std::tr1::lgamma(_Tp(1 + __i))
380 - std::tr1::lgamma(_Tp(1 + __j))
381 - std::tr1::lgamma(_Tp(1 + __i - __j));
383 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
384 - __log_gamma(_Tp(1 + __j))
385 - __log_gamma(_Tp(1 + __i - __j));
387 if (__bincoeff > __max_bincoeff)
389 // This only gets hit for x << 0.
393 __bincoeff = std::exp(__bincoeff);
394 __term += __sgn * __bincoeff * std::pow(_Tp(__a + __j), -__s);
399 __term /= _Tp(__i + 1);
400 if (std::abs(__term / __zeta) < __eps)
405 __zeta /= __s - _Tp(1);
412 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
413 * for all s != 1 and x > -1.
415 * The Hurwitz zeta function is defined by:
417 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
419 * The Riemann zeta function is a special case:
421 * \zeta(s) = \zeta(1,s)
424 template<typename _Tp>
426 __hurwitz_zeta(const _Tp __a, const _Tp __s)
428 return __hurwitz_zeta_glob(__a, __s);
431 _GLIBCXX_END_NAMESPACE_VERSION
432 } // namespace std::tr1::__detail
436 #endif // _GLIBCXX_TR1_RIEMANN_ZETA_TCC