This returns Bernoulli numbers from a table or by summation for larger values.
Recursion is unstable.
Return the logarithm of the binomial coefficient. The binomial coefficient is given by:
Return the binomial coefficient. The binomial coefficient is given by:
Return the digamma function by series expansion. The digamma or
function is defined by
Return the digamma function for large argument. The digamma or
function is defined by
Return the digamma function. The digamma or
function is defined by
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
69 template <
typename _Tp>
71 __bernoulli_series(
unsigned int __n)
74 static const _Tp __num[28] = {
75 _Tp(1UL), -_Tp(1UL) / _Tp(2UL),
76 _Tp(1UL) / _Tp(6UL), _Tp(0UL),
77 -_Tp(1UL) / _Tp(30UL), _Tp(0UL),
78 _Tp(1UL) / _Tp(42UL), _Tp(0UL),
79 -_Tp(1UL) / _Tp(30UL), _Tp(0UL),
80 _Tp(5UL) / _Tp(66UL), _Tp(0UL),
81 -_Tp(691UL) / _Tp(2730UL), _Tp(0UL),
82 _Tp(7UL) / _Tp(6UL), _Tp(0UL),
83 -_Tp(3617UL) / _Tp(510UL), _Tp(0UL),
84 _Tp(43867UL) / _Tp(798UL), _Tp(0UL),
85 -_Tp(174611) / _Tp(330UL), _Tp(0UL),
86 _Tp(854513UL) / _Tp(138UL), _Tp(0UL),
87 -_Tp(236364091UL) / _Tp(2730UL), _Tp(0UL),
88 _Tp(8553103UL) / _Tp(6UL), _Tp(0UL)
95 return -_Tp(1) / _Tp(2);
107 if ((__n / 2) % 2 == 0)
109 for (
unsigned int __k = 1; __k <= __n; ++__k)
110 __fact *= __k / (_Tp(2) * __numeric_constants<_Tp>::__pi());
114 for (
unsigned int __i = 1; __i < 1000; ++__i)
116 _Tp __term = std::pow(_Tp(__i), -_Tp(__n));
117 if (__term < std::numeric_limits<_Tp>::epsilon())
122 return __fact * __sum;
132 template<
typename _Tp>
135 {
return __bernoulli_series<_Tp>(__n); }
146 template<
typename _Tp>
148 __log_gamma_bernoulli(_Tp __x)
150 _Tp __lg = (__x - _Tp(0.5L)) * std::log(__x) - __x
151 + _Tp(0.5L) * std::log(_Tp(2)
152 * __numeric_constants<_Tp>::__pi());
154 const _Tp __xx = __x * __x;
155 _Tp __help = _Tp(1) / __x;
156 for (
unsigned int __i = 1; __i < 20; ++__i )
158 const _Tp __2i = _Tp(2 * __i);
159 __help /= __2i * (__2i - _Tp(1)) * __xx;
160 __lg += __bernoulli<_Tp>(2 * __i) * __help;
174 template<
typename _Tp>
176 __log_gamma_lanczos(_Tp __x)
178 const _Tp __xm1 = __x - _Tp(1);
180 static const _Tp __lanczos_cheb_7[9] = {
181 _Tp( 0.99999999999980993227684700473478L),
182 _Tp( 676.520368121885098567009190444019L),
183 _Tp(-1259.13921672240287047156078755283L),
184 _Tp( 771.3234287776530788486528258894L),
185 _Tp(-176.61502916214059906584551354L),
186 _Tp( 12.507343278686904814458936853L),
187 _Tp(-0.13857109526572011689554707L),
188 _Tp( 9.984369578019570859563e-6L),
189 _Tp( 1.50563273514931155834e-7L)
192 static const _Tp __LOGROOT2PI
193 = _Tp(0.9189385332046727417803297364056176L);
195 _Tp __sum = __lanczos_cheb_7[0];
196 for(
unsigned int __k = 1; __k < 9; ++__k)
197 __sum += __lanczos_cheb_7[__k] / (__xm1 + __k);
199 const _Tp __term1 = (__xm1 + _Tp(0.5L))
200 * std::log((__xm1 + _Tp(7.5L))
201 / __numeric_constants<_Tp>::__euler());
202 const _Tp __term2 = __LOGROOT2PI + std::log(__sum);
203 const _Tp __result = __term1 + (__term2 - _Tp(7));
218 template<
typename _Tp>
223 return __log_gamma_lanczos(__x);
227 = std::abs(std::sin(__numeric_constants<_Tp>::__pi() * __x));
228 if (__sin_fact == _Tp(0))
229 std::__throw_domain_error(__N(
"Argument is nonpositive integer "
231 return __numeric_constants<_Tp>::__lnpi()
232 - std::log(__sin_fact)
233 - __log_gamma_lanczos(_Tp(1) - __x);
245 template<
typename _Tp>
247 __log_gamma_sign(_Tp __x)
254 = std::sin(__numeric_constants<_Tp>::__pi() * __x);
255 if (__sin_fact > _Tp(0))
257 else if (__sin_fact < _Tp(0))
276 template<
typename _Tp>
278 __log_bincoef(
unsigned int __n,
unsigned int __k)
281 static const _Tp __max_bincoeff
282 = std::numeric_limits<_Tp>::max_exponent10
283 * std::log(_Tp(10)) - _Tp(1);
284 #if _GLIBCXX_USE_C99_MATH_TR1
285 _Tp __coeff = std::tr1::lgamma(_Tp(1 + __n))
286 - std::tr1::lgamma(_Tp(1 + __k))
287 - std::tr1::lgamma(_Tp(1 + __n - __k));
289 _Tp __coeff = __log_gamma(_Tp(1 + __n))
290 - __log_gamma(_Tp(1 + __k))
291 - __log_gamma(_Tp(1 + __n - __k));
307 template<
typename _Tp>
309 __bincoef(
unsigned int __n,
unsigned int __k)
312 static const _Tp __max_bincoeff
313 = std::numeric_limits<_Tp>::max_exponent10
314 * std::log(_Tp(10)) - _Tp(1);
316 const _Tp __log_coeff = __log_bincoef<_Tp>(__n, __k);
317 if (__log_coeff > __max_bincoeff)
318 return std::numeric_limits<_Tp>::quiet_NaN();
320 return std::exp(__log_coeff);
330 template<
typename _Tp>
333 {
return std::exp(__log_gamma(__x)); }
349 template<
typename _Tp>
351 __psi_series(_Tp __x)
353 _Tp __sum = -__numeric_constants<_Tp>::__gamma_e() - _Tp(1) / __x;
354 const unsigned int __max_iter = 100000;
355 for (
unsigned int __k = 1; __k < __max_iter; ++__k)
357 const _Tp __term = __x / (__k * (__k + __x));
359 if (std::abs(__term / __sum) < std::numeric_limits<_Tp>::epsilon())
379 template<
typename _Tp>
383 _Tp __sum = std::log(__x) - _Tp(0.5L) / __x;
384 const _Tp __xx = __x * __x;
386 const unsigned int __max_iter = 100;
387 for (
unsigned int __k = 1; __k < __max_iter; ++__k)
389 const _Tp __term = __bernoulli<_Tp>(2 * __k) / (2 * __k * __xp);
391 if (std::abs(__term / __sum) < std::numeric_limits<_Tp>::epsilon())
410 template<
typename _Tp>
414 const int __n =
static_cast<int>(__x + 0.5L);
415 const _Tp __eps = _Tp(4) * std::numeric_limits<_Tp>::epsilon();
416 if (__n <= 0 && std::abs(__x - _Tp(__n)) < __eps)
417 return std::numeric_limits<_Tp>::quiet_NaN();
418 else if (__x < _Tp(0))
420 const _Tp __pi = __numeric_constants<_Tp>::__pi();
421 return __psi(_Tp(1) - __x)
422 - __pi * std::cos(__pi * __x) / std::sin(__pi * __x);
424 else if (__x > _Tp(100))
425 return __psi_asymp(__x);
427 return __psi_series(__x);
439 template<
typename _Tp>
441 __psi(
unsigned int __n, _Tp __x)
444 std::__throw_domain_error(__N(
"Argument out of range "
450 const _Tp __hzeta = __hurwitz_zeta(_Tp(__n + 1), __x);
451 #if _GLIBCXX_USE_C99_MATH_TR1
452 const _Tp __ln_nfact = std::tr1::lgamma(_Tp(__n + 1));
454 const _Tp __ln_nfact = __log_gamma(_Tp(__n + 1));
456 _Tp __result = std::exp(__ln_nfact) * __hzeta;
458 __result = -__result;
463 _GLIBCXX_END_NAMESPACE_VERSION