Evaluate the Riemann zeta function by series for all s != 1. Convergence is great until largish negative numbers. Then the convergence of the > 0 sum gets better.
Havil 2003, p. 206.
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
71 template<
typename _Tp>
73 __riemann_zeta_sum(_Tp __s)
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())
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);
150 template<
typename _Tp>
152 __riemann_zeta_glob(_Tp __s)
156 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
158 const _Tp __max_bincoeff = std::numeric_limits<_Tp>::max_exponent10
159 * std::log(_Tp(10)) - _Tp(1);
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)
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);
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;
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))
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);
358 template<
typename _Tp>
360 __hurwitz_zeta_glob(_Tp __a, _Tp __s)
364 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
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)
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);
423 template<
typename _Tp>
425 __hurwitz_zeta(_Tp __a, _Tp __s)
426 {
return __hurwitz_zeta_glob(__a, __s); }
428 _GLIBCXX_END_NAMESPACE_VERSION