bernoulli.h – support for Bernoulli numbers¶
This module provides helper functions for exact or approximate calculation of the Bernoulli numbers, which are defined by the exponential generating function
Efficient algorithms are implemented for both multievaluation and calculation of isolated Bernoulli numbers. A global (or threadlocal) cache is also provided, to support fast repeated evaluation of various special functions that depend on the Bernoulli numbers (including the gamma function and the Riemann zeta function).
Generation of Bernoulli numbers¶

bernoulli_rev_t
¶ An iterator object for generating a range of evenindexed Bernoulli numbers exactly in reverse order, i.e. computing the exact fractions \(B_n, B_{n2}, B_{n4}, \ldots, B_0\). The Bernoulli numbers are generated from scratch, i.e. no caching is performed.
The Bernoulli numbers are computed by direct summation of the zeta series. This is made fast by storing a table of powers (as done by [Blo2009]). As an optimization, we only include the odd powers, and use fixedpoint arithmetic.
The reverse iteration order is preferred for performance reasons, as the powers can be updated using multiplications instead of divisions, and we avoid having to periodically recompute terms to higher precision. To generate Bernoulli numbers in the forward direction without having to store all of them, one can split the desired range into smaller blocks and compute each block with a single reverse pass.

void
bernoulli_rev_init
(bernoulli_rev_t iter, ulong n)¶ Initializes the iterator iter. The first Bernoulli number to be generated by calling
bernoulli_rev_next()
is \(B_n\). It is assumed that \(n\) is even.

void
bernoulli_rev_next
(fmpz_t numer, fmpz_t denom, bernoulli_rev_t iter)¶ Sets numer and denom to the exact, reduced numerator and denominator of the Bernoulli number \(B_k\) and advances the state of iter so that the next invocation generates \(B_{k2}\).

void
bernoulli_rev_clear
(bernoulli_rev_t iter)¶ Frees all memory allocated internally by iter.
Caching¶

fmpq *
bernoulli_cache
¶ Cache of Bernoulli numbers. Uses threadlocal storage if enabled in FLINT.
Bounding¶

slong
bernoulli_bound_2exp_si
(ulong n)¶ Returns an integer \(b\) such that \(B_n \le 2^b\). Uses a lookup table for small \(n\), and for larger \(n\) uses the inequality \(B_n < 4 n! / (2 \pi)^n < 4 (n+1)^{n+1} e^{n} / (2 \pi)^n\). Uses integer arithmetic throughout, with the bound for the logarithm being looked up from a table. If \(B_n = 0\), returns LONG_MIN. Otherwise, the returned exponent \(b\) is never more than one percent larger than the true magnitude.
This function is intended for use when \(n\) small enough that one might comfortably compute \(B_n\) exactly. It aborts if \(n\) is so large that internal overflow occurs.

void
_bernoulli_fmpq_ui_zeta
(fmpz_t num, fmpz_t den, ulong n)¶ Sets num and den to the reduced numerator and denominator of the Bernoulli number \(B_n\).
This function computes the denominator \(d\) using von StaudtClausen theorem, numerically approximates \(B_n\) using
arb_bernoulli_ui_zeta()
, and then rounds \(d B_n\) to the correct numerator. If the working precision is insufficient to determine the numerator, the function prints a warning message and retries with increased precision (this should not be expected to happen).