arith.h – arithmetic and special functions¶
This module implements arithmetic functions, number-theoretic and combinatorial special number sequences and polynomials.
Harmonic numbers¶
Stirling numbers¶
-
void arith_stirling_number_2(fmpz_t s, ulong n, ulong k)¶
Sets
to where denotes an unsigned Stirling number of the first kind , a signed Stirling number of the first kind , or a Stirling number of the second kind . The Stirling numbers are defined using the generating functionswhere
is a falling factorial and is a rising factorial. is taken to be zero if or .These three functions are useful for computing isolated Stirling numbers efficiently. To compute a range of numbers, the vector or matrix versions should generally be used.
-
void arith_stirling_number_2_vec(fmpz *row, ulong n, slong klen)¶
Computes the row of Stirling numbers
S(n,0), S(n,1), S(n,2), ..., S(n,klen-1)
.To compute a full row, this function can be called with
klen = n+1
. It is assumed thatklen
is at most .
-
void arith_stirling_number_2_vec_next(fmpz *row, const fmpz *prev, slong n, slong klen)¶
Given the vector
prev
containing a row of Stirling numbersS(n-1,0), S(n-1,1), S(n-1,2), ..., S(n-1,klen-1)
, computes and stores in the row argumentS(n,0), S(n,1), S(n,2), ..., S(n,klen-1)
.If
klen
is greater thann
, the output ends withS(n,n) = 1
followed byS(n,n+1) = S(n,n+2) = ... = 0
. In this case, the input only needs to have lengthn-1
; only the input entries up toS(n-1,n-2)
are read.The
row
andprev
arguments are permitted to be the same, meaning that the row will be updated in-place.
-
void arith_stirling_matrix_1u(fmpz_mat_t mat)¶
-
void arith_stirling_matrix_1(fmpz_mat_t mat)¶
-
void arith_stirling_matrix_2(fmpz_mat_t mat)¶
For an arbitrary
-by- matrix, writes the truncation of the infinite Stirling number matrix:row 0 : S(0,0) row 1 : S(1,0), S(1,1) row 2 : S(2,0), S(2,1), S(2,2) row 3 : S(3,0), S(3,1), S(3,2), S(3,3)
up to row
and column inclusive. The upper triangular part of the matrix is zeroed.For any
, the and matrices thus obtained are inverses of each other.
Bell numbers¶
-
void arith_bell_number(fmpz_t b, ulong n)¶
-
void arith_bell_number_dobinski(fmpz_t res, ulong n)¶
-
void arith_bell_number_multi_mod(fmpz_t res, ulong n)¶
Sets
to the Bell number , defined as the number of partitions of a set with members. Equivalently, where denotes a Stirling number of the second kind.The default version automatically selects between table lookup, Dobinski’s formula, and the multimodular algorithm.
The
dobinski
version evaluates a precise truncation of the series (Dobinski’s formula). In fact, we compute and and evaluate , avoiding the use of floating-point arithmetic.The
multi_mod
version computes the result modulo several limb-size primes and reconstructs the integer value using the fast Chinese remainder algorithm. A bound for the number of needed primes is computed usingarith_bell_number_size
.
-
void arith_bell_number_vec(fmpz *b, slong n)¶
-
void arith_bell_number_vec_recursive(fmpz *b, slong n)¶
-
void arith_bell_number_vec_multi_mod(fmpz *b, slong n)¶
Sets
to the vector of Bell numbers inclusive. Therecursive
version uses the triangular recurrence, while themulti_mod
version implements multimodular evaluation of the exponential generating function, running in time . The default version chooses an algorithm automatically.
-
ulong arith_bell_number_nmod(ulong n, nmod_t mod)¶
Computes the Bell number
modulo an integer given bymod
.After handling special cases, we use the formula
We arrange the operations in such a way that we only have to multiply (and not divide) in the main loop. As a further optimisation, we use sieving to reduce the number of powers that need to be evaluated. This results in
memory usage.If the divisions by factorials are impossible, we fall back to calling
arith_bell_number_nmod_vec
and reading the last coefficient.
-
void arith_bell_number_nmod_vec(nn_ptr b, slong n, nmod_t mod)¶
-
void arith_bell_number_nmod_vec_recursive(nn_ptr b, slong n, nmod_t mod)¶
-
void arith_bell_number_nmod_vec_ogf(nn_ptr b, slong n, nmod_t mod)¶
-
int arith_bell_number_nmod_vec_series(nn_ptr b, slong n, nmod_t mod)¶
Sets
to the vector of Bell numbers inclusive modulo an integer given bymod
.The recursive version uses the
triangular recurrence. The ogf version expands the ordinary generating function using binary splitting, which is .The series version uses the exponential generating function
, running in . This only works if division by is possible, and the function returns whether it is successful. All other versions support any modulus.The default version of this function selects an algorithm automatically.
-
double arith_bell_number_size(ulong n)¶
Returns
such that . A previous version of this function used the inequality which is given in [BerTas2010]; we now use a slightly better bound based on an asymptotic expansion.
Bernoulli numbers and polynomials¶
-
void _arith_bernoulli_number(fmpz_t num, fmpz_t den, ulong n)¶
Sets
(num, den)
to the reduced numerator and denominator of the -th Bernoulli number.
-
void arith_bernoulli_number(fmpq_t x, ulong n)¶
Sets
x
to the -th Bernoulli number. This function is equivalent to_arith_bernoulli_number
apart from the output being a singlefmpq_t
variable.
-
void _arith_bernoulli_number_vec(fmpz *num, fmpz *den, slong n)¶
Sets the elements of
num
andden
to the reduced numerators and denominators of the Bernoulli numbers inclusive. This function automatically chooses between therecursive
,zeta
andmulti_mod
algorithms according to the size of .
-
void arith_bernoulli_number_vec(fmpq *x, slong n)¶
Sets the
x
to the vector of Bernoulli numbers inclusive. This function is equivalent to_arith_bernoulli_number_vec
apart from the output being a singlefmpq
vector.
-
void arith_bernoulli_number_denom(fmpz_t den, ulong n)¶
Sets
den
to the reduced denominator of the -th Bernoulli number . For even , the denominator is computed as the product of all primes for which divides ; this property is a consequence of the von Staudt-Clausen theorem. For odd , the denominator is trivial (den
is set to 1 whenever ). The initial sequence of values smaller than are looked up directly from a table.
-
double arith_bernoulli_number_size(ulong n)¶
Returns
such that , using the inequality and . No special treatment is given to odd . Accuracy is not guaranteed if .
-
void arith_bernoulli_polynomial(fmpq_poly_t poly, ulong n)¶
Sets
poly
to the Bernoulli polynomial of degree , where is a Bernoulli number. This function basically callsarith_bernoulli_number_vec
and then rescales the coefficients efficiently.
-
void _arith_bernoulli_number_vec_recursive(fmpz *num, fmpz *den, slong n)¶
Sets the elements of
num
andden
to the reduced numerators and denominators of inclusive.The first few entries are computed using
arith_bernoulli_number
, and then Ramanujan’s recursive formula expressing as a sum over for congruent to modulo 6 is applied repeatedly.To avoid costly GCDs, the numerators are transformed internally to a common denominator and all operations are performed using integer arithmetic. This makes the algorithm fast for small
, say . The common denominator is calculated directly as the primorial of .%[1] https://en.wikipedia.org/w/index.php?title=Bernoulli_number&oldid=405938876
-
void _arith_bernoulli_number_vec_multi_mod(fmpz *num, fmpz *den, slong n)¶
Sets the elements of
num
andden
to the reduced numerators and denominators of inclusive. Uses the generating functionwhich is evaluated modulo several limb-size primes using
nmod_poly
arithmetic to yield the numerators of the Bernoulli numbers after multiplication by the denominators and CRT reconstruction. This formula, given (incorrectly) in [BuhlerCrandallSompolski1992], saves about half of the time compared to the usual generating function since the odd terms vanish.
Euler numbers and polynomials¶
Euler numbers are the integers
-
void arith_euler_number_vec(fmpz *res, slong n)¶
Computes the Euler numbers
for and stores the result inres
, which must be an initialisedfmpz
vector of sufficient size.This function evaluates the even-index
modulo several limb-size primes using the generating function andnmod_poly
arithmetic. A tight bound for the number of needed primes is computed usingarith_euler_number_size
, and the final integer values are recovered using balanced CRT reconstruction.
-
double arith_euler_number_size(ulong n)¶
Returns
such that , using the inequality|E_n| < \frac{2^{n+2} n!}{\pi^{n+1}}
and . No special treatment is given to odd . Accuracy is not guaranteed if .
-
void arith_euler_polynomial(fmpq_poly_t poly, ulong n)¶
Sets
poly
to the Euler polynomial . Uses the formulawith the Bernoulli polynomial
evaluated once usingbernoulli_polynomial
and then rescaled.
Multiplicative functions¶
-
void arith_divisors(fmpz_poly_t res, const fmpz_t n)¶
Set the coefficients of the polynomial
res
to the divisors of , including and itself, in ascending order.
-
void arith_ramanujan_tau(fmpz_t res, const fmpz_t n)¶
Sets
res
to the Ramanujan tau function which is the coefficient of in the series expansion of .We factor
and use the identity along with the recursion for prime powers.The base values
are obtained using the functionarith_ramanujan_tau_series()
. Thus the speed ofarith_ramanujan_tau()
depends on the largest prime factor of .Future improvement: optimise this function for small
, which could be accomplished using a lookup table or by callingarith_ramanujan_tau_series()
directly.
-
void arith_ramanujan_tau_series(fmpz_poly_t res, slong n)¶
Sets
res
to the polynomial with coefficients , giving the initial terms in the series expansion of .We use the theta function identity
which is evaluated using three squarings. The first squaring is done directly since the polynomial is very sparse at this point.
Landau’s function¶
-
void arith_landau_function_vec(fmpz *res, slong len)¶
Computes the first
len
values of Landau’s function starting with . Landau’s function gives the largest order of an element of the symmetric group .Implements the “basic algorithm” given in [DelegliseNicolasZimmermann2009]. The running time is
.
Number of partitions¶
-
void arith_number_of_partitions_vec(fmpz *res, slong len)¶
Computes first
len
values of the partition function starting with . Uses inversion of Euler’s pentagonal series.
-
void arith_number_of_partitions_nmod_vec(nn_ptr res, slong len, nmod_t mod)¶
Computes first
len
values of the partition function starting with , modulo the modulus defined bymod
. Uses inversion of Euler’s pentagonal series.
-
void trig_prod_init(trig_prod_t prod)¶
Initializes
prod
. This is an inline function only.
-
void arith_hrr_expsum_factored(trig_prod_t prod, ulong k, ulong n)¶
Symbolically evaluates the exponential sum
appearing in the Hardy-Ramanujan-Rademacher formula, where
is a Dedekind sum.Rather than evaluating the sum naively, we factor
into a product of cosines based on the prime factorisation of . This process is based on the identities given in [Whiteman1956].The special
trig_prod_t
structureprod
represents a product of cosines of rational arguments, multiplied by an algebraic prefactor. It must be pre-initialised withtrig_prod_init
.This function assumes that
and do not overflow a single limb. If is larger, it can be pre-reduced modulo , since only depends on the value of .
-
void arith_number_of_partitions_mpfr(mpfr_t x, ulong n)¶
Sets the pre-initialised MPFR variable
to the exact value of . The value is computed using the Hardy-Ramanujan-Rademacher formula.The precision of
will be changed to allow to be represented exactly. The interface of this function may be updated in the future to allow computing an approximation of to smaller precision.The Hardy-Ramanujan-Rademacher formula is given with error bounds in [Rademacher1937]. We evaluate it in the form
where
and where
is a certain exponential sum. The remainder satisfiesWe choose
such that , and a working precision at term such that the absolute error of the term is expected to be less than . We also use a summation variable with increased precision, essentially making additions exact. Thus the sum of errors adds up to less than 0.5, giving the correct value of when rounding to the nearest integer.The remainder estimate at step
provides an upper bound for the size of the -th term. We add bits to get low bits in the terms below in magnitude.Using
arith_hrr_expsum_factored
, each evaluation is broken down to a product of cosines of exact rational multiples of . We transform all angles to for optimal accuracy.Since the evaluation of each term involves only
multiplications and evaluations of trigonometric functions of small angles, the relative rounding error is at most a few bits. We therefore just add an additional bits for the when is large. The cancellation of terms in is of no concern, since Rademacher’s bound allows us to terminate before becomes small.This analysis should be performed in more detail to give a rigorous error bound, but the precision currently implemented is almost certainly sufficient, not least considering that Rademacher’s remainder bound significantly overshoots the actual values.
To improve performance, we switch to doubles when the working precision becomes small enough. We also use a separate accumulator variable which gets added to the main sum periodically, in order to avoid costly updates of the full-precision result when
is large.
Sums of squares¶
-
void arith_sum_of_squares(fmpz_t r, ulong k, const fmpz_t n)¶
Sets
to the number of ways in which can be represented as a sum of squares.If
or , we write as a divisor sum.Otherwise, we either recurse on
or compute the theta function expansion up to and read off the last coefficient. This is generally optimal.