1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006, 2007, 2008
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
60 // Implementation-space details.
65 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
66 * by summation for s > 1.
68 * The Riemann zeta function is defined by:
70 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
72 * For s < 1 use the reflection formula:
74 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
77 template<typename _Tp>
79 __riemann_zeta_sum(const _Tp __s)
81 // A user shouldn't get to this.
83 std::__throw_domain_error(__N("Bad argument in zeta sum."));
85 const unsigned int max_iter = 10000;
87 for (unsigned int __k = 1; __k < max_iter; ++__k)
89 _Tp __term = std::pow(static_cast<_Tp>(__k), -__s);
90 if (__term < std::numeric_limits<_Tp>::epsilon())
102 * @brief Evaluate the Riemann zeta function @f$ \zeta(s) @f$
103 * by an alternate series for s > 0.
105 * The Riemann zeta function is defined by:
107 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
109 * For s < 1 use the reflection formula:
111 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
114 template<typename _Tp>
116 __riemann_zeta_alt(const _Tp __s)
120 for (unsigned int __i = 1; __i < 10000000; ++__i)
122 _Tp __term = __sgn / std::pow(__i, __s);
123 if (std::abs(__term) < std::numeric_limits<_Tp>::epsilon())
128 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
135 * @brief Evaluate the Riemann zeta function by series for all s != 1.
136 * Convergence is great until largish negative numbers.
137 * Then the convergence of the > 0 sum gets better.
141 * \zeta(s) = \frac{1}{1-2^{1-s}}
142 * \sum_{n=0}^{\infty} \frac{1}{2^{n+1}}
143 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (k+1)^{-s}
145 * Havil 2003, p. 206.
147 * The Riemann zeta function is defined by:
149 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
151 * For s < 1 use the reflection formula:
153 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
156 template<typename _Tp>
158 __riemann_zeta_glob(const _Tp __s)
162 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
163 // Max e exponent before overflow.
164 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
165 * std::log(_Tp(10)) - _Tp(1);
167 // This series works until the binomial coefficient blows up
168 // so use reflection.
171 #if _GLIBCXX_USE_C99_MATH_TR1
172 if (std::tr1::fmod(__s,_Tp(2)) == _Tp(0))
177 _Tp __zeta = __riemann_zeta_glob(_Tp(1) - __s);
178 __zeta *= std::pow(_Tp(2)
179 * __numeric_constants<_Tp>::__pi(), __s)
180 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
181 #if _GLIBCXX_USE_C99_MATH_TR1
182 * std::exp(std::tr1::lgamma(_Tp(1) - __s))
184 * std::exp(__log_gamma(_Tp(1) - __s))
186 / __numeric_constants<_Tp>::__pi();
191 _Tp __num = _Tp(0.5L);
192 const unsigned int __maxit = 10000;
193 for (unsigned int __i = 0; __i < __maxit; ++__i)
198 for (unsigned int __j = 0; __j <= __i; ++__j)
200 #if _GLIBCXX_USE_C99_MATH_TR1
201 _Tp __bincoeff = std::tr1::lgamma(_Tp(1 + __i))
202 - std::tr1::lgamma(_Tp(1 + __j))
203 - std::tr1::lgamma(_Tp(1 + __i - __j));
205 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
206 - __log_gamma(_Tp(1 + __j))
207 - __log_gamma(_Tp(1 + __i - __j));
209 if (__bincoeff > __max_bincoeff)
211 // This only gets hit for x << 0.
215 __bincoeff = std::exp(__bincoeff);
216 __term += __sgn * __bincoeff * std::pow(_Tp(1 + __j), -__s);
223 if (std::abs(__term/__zeta) < __eps)
228 __zeta /= _Tp(1) - std::pow(_Tp(2), _Tp(1) - __s);
235 * @brief Compute the Riemann zeta function @f$ \zeta(s) @f$
236 * using the product over prime factors.
238 * \zeta(s) = \Pi_{i=1}^\infty \frac{1}{1 - p_i^{-s}}
240 * where @f$ {p_i} @f$ are the prime numbers.
242 * The Riemann zeta function is defined by:
244 * \zeta(s) = \sum_{k=1}^{\infty} \frac{1}{k^{s}} for s > 1
246 * For s < 1 use the reflection formula:
248 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
251 template<typename _Tp>
253 __riemann_zeta_product(const _Tp __s)
255 static const _Tp __prime[] = {
256 _Tp(2), _Tp(3), _Tp(5), _Tp(7), _Tp(11), _Tp(13), _Tp(17), _Tp(19),
257 _Tp(23), _Tp(29), _Tp(31), _Tp(37), _Tp(41), _Tp(43), _Tp(47),
258 _Tp(53), _Tp(59), _Tp(61), _Tp(67), _Tp(71), _Tp(73), _Tp(79),
259 _Tp(83), _Tp(89), _Tp(97), _Tp(101), _Tp(103), _Tp(107), _Tp(109)
261 static const unsigned int __num_primes = sizeof(__prime) / sizeof(_Tp);
264 for (unsigned int __i = 0; __i < __num_primes; ++__i)
266 const _Tp __fact = _Tp(1) - std::pow(__prime[__i], -__s);
268 if (_Tp(1) - __fact < std::numeric_limits<_Tp>::epsilon())
272 __zeta = _Tp(1) / __zeta;
279 * @brief Return the Riemann zeta function @f$ \zeta(s) @f$.
281 * The Riemann zeta function is defined by:
283 * \zeta(s) = \sum_{k=1}^{\infty} k^{-s} for s > 1
284 * \frac{(2\pi)^s}{pi} sin(\frac{\pi s}{2})
285 * \Gamma (1 - s) \zeta (1 - s) for s < 1
287 * For s < 1 use the reflection formula:
289 * \zeta(s) = 2^s \pi^{s-1} \Gamma(1-s) \zeta(1-s)
292 template<typename _Tp>
294 __riemann_zeta(const _Tp __s)
297 return std::numeric_limits<_Tp>::quiet_NaN();
298 else if (__s == _Tp(1))
299 return std::numeric_limits<_Tp>::infinity();
300 else if (__s < -_Tp(19))
302 _Tp __zeta = __riemann_zeta_product(_Tp(1) - __s);
303 __zeta *= std::pow(_Tp(2) * __numeric_constants<_Tp>::__pi(), __s)
304 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
305 #if _GLIBCXX_USE_C99_MATH_TR1
306 * std::exp(std::tr1::lgamma(_Tp(1) - __s))
308 * std::exp(__log_gamma(_Tp(1) - __s))
310 / __numeric_constants<_Tp>::__pi();
313 else if (__s < _Tp(20))
315 // Global double sum or McLaurin?
318 return __riemann_zeta_glob(__s);
322 return __riemann_zeta_sum(__s);
325 _Tp __zeta = std::pow(_Tp(2)
326 * __numeric_constants<_Tp>::__pi(), __s)
327 * std::sin(__numeric_constants<_Tp>::__pi_2() * __s)
328 #if _GLIBCXX_USE_C99_MATH_TR1
329 * std::tr1::tgamma(_Tp(1) - __s)
331 * std::exp(__log_gamma(_Tp(1) - __s))
333 * __riemann_zeta_sum(_Tp(1) - __s);
339 return __riemann_zeta_product(__s);
344 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
345 * for all s != 1 and x > -1.
347 * The Hurwitz zeta function is defined by:
349 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
351 * The Riemann zeta function is a special case:
353 * \zeta(s) = \zeta(1,s)
356 * This functions uses the double sum that converges for s != 1
359 * \zeta(x,s) = \frac{1}{s-1}
360 * \sum_{n=0}^{\infty} \frac{1}{n + 1}
361 * \sum_{k=0}^{n} (-1)^k \frac{n!}{(n-k)!k!} (x+k)^{-s}
364 template<typename _Tp>
366 __hurwitz_zeta_glob(const _Tp __a, const _Tp __s)
370 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
371 // Max e exponent before overflow.
372 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
373 * std::log(_Tp(10)) - _Tp(1);
375 const unsigned int __maxit = 10000;
376 for (unsigned int __i = 0; __i < __maxit; ++__i)
381 for (unsigned int __j = 0; __j <= __i; ++__j)
383 #if _GLIBCXX_USE_C99_MATH_TR1
384 _Tp __bincoeff = std::tr1::lgamma(_Tp(1 + __i))
385 - std::tr1::lgamma(_Tp(1 + __j))
386 - std::tr1::lgamma(_Tp(1 + __i - __j));
388 _Tp __bincoeff = __log_gamma(_Tp(1 + __i))
389 - __log_gamma(_Tp(1 + __j))
390 - __log_gamma(_Tp(1 + __i - __j));
392 if (__bincoeff > __max_bincoeff)
394 // This only gets hit for x << 0.
398 __bincoeff = std::exp(__bincoeff);
399 __term += __sgn * __bincoeff * std::pow(_Tp(__a + __j), -__s);
404 __term /= _Tp(__i + 1);
405 if (std::abs(__term / __zeta) < __eps)
410 __zeta /= __s - _Tp(1);
417 * @brief Return the Hurwitz zeta function @f$ \zeta(x,s) @f$
418 * for all s != 1 and x > -1.
420 * The Hurwitz zeta function is defined by:
422 * \zeta(x,s) = \sum_{n=0}^{\infty} \frac{1}{(n + x)^s}
424 * The Riemann zeta function is a special case:
426 * \zeta(s) = \zeta(1,s)
429 template<typename _Tp>
431 __hurwitz_zeta(const _Tp __a, const _Tp __s)
433 return __hurwitz_zeta_glob(__a, __s);
436 } // namespace std::tr1::__detail
440 #endif // _GLIBCXX_TR1_RIEMANN_ZETA_TCC