-
Notifications
You must be signed in to change notification settings - Fork 141
/
bernoulli.h
119 lines (91 loc) · 2.88 KB
/
bernoulli.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Copyright (C) 2012 Fredrik Johansson
This file is part of Arb.
Arb is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version. See <http://www.gnu.org/licenses/>.
*/
#ifndef BERNOULLI_H
#define BERNOULLI_H
#include <math.h>
#include "flint/flint.h"
#include "flint/fmpz.h"
#include "flint/fmpz_vec.h"
#include "flint/fmpq.h"
#include "flint/arith.h"
#include "arb.h"
#ifdef __cplusplus
extern "C" {
#endif
extern slong TLS_PREFIX bernoulli_cache_num;
extern TLS_PREFIX fmpq * bernoulli_cache;
void bernoulli_cache_compute(slong n);
/*
Crude bound for the bits in d(n) = denom(B_n).
By von Staudt-Clausen, d(n) = prod_{p-1 | n} p
<= prod_{k | n} 2k
<= n^{sigma_0(n)}.
We get a more accurate estimate taking the square root of this.
Further, at least for sufficiently large n,
sigma_0(n) < exp(1.066 log(n) / log(log(n))).
*/
static __inline__ slong bernoulli_denom_size(slong n)
{
return 0.5 * 1.4427 * log(n) * pow(n, 1.066 / log(log(n)));
}
static __inline__ slong bernoulli_zeta_terms(ulong s, slong prec)
{
slong N;
N = pow(2.0, (prec + 1.0) / (s - 1.0));
N += ((N % 2) == 0);
return N;
}
static __inline__ slong bernoulli_power_prec(slong i, ulong s1, slong wp)
{
slong p = wp - s1 * log(i) * 1.44269504088896341;
return FLINT_MAX(p, 10);
}
/* we should technically add O(log(n)) guard bits, but this is unnecessary
in practice since the denominator estimate is quite a bit larger
than the true denominators
*/
static __inline__ slong bernoulli_global_prec(ulong nmax)
{
return arith_bernoulli_number_size(nmax) + bernoulli_denom_size(nmax);
}
/* avoid potential numerical problems for very small n */
#define BERNOULLI_REV_MIN 32
typedef struct
{
slong alloc;
slong prec;
slong max_power;
fmpz * powers;
fmpz_t pow_error;
arb_t prefactor;
arb_t two_pi_squared;
ulong n;
}
bernoulli_rev_struct;
typedef bernoulli_rev_struct bernoulli_rev_t[1];
void bernoulli_rev_init(bernoulli_rev_t iter, ulong nmax);
void bernoulli_rev_next(fmpz_t numer, fmpz_t denom, bernoulli_rev_t iter);
void bernoulli_rev_clear(bernoulli_rev_t iter);
void bernoulli_fmpq_vec_no_cache(fmpq * res, ulong a, slong num);
#define BERNOULLI_ENSURE_CACHED(n) \
do { \
slong __n = (n); \
if (__n >= bernoulli_cache_num) \
bernoulli_cache_compute(__n + 1); \
} while (0); \
slong bernoulli_bound_2exp_si(ulong n);
ulong bernoulli_mod_p_harvey(ulong k, ulong p);
void _bernoulli_fmpq_ui_multi_mod(fmpz_t num, fmpz_t den, ulong n, double alpha);
void _bernoulli_fmpq_ui_zeta(fmpz_t num, fmpz_t den, ulong n);
void _bernoulli_fmpq_ui(fmpz_t num, fmpz_t den, ulong n);
void bernoulli_fmpq_ui(fmpq_t b, ulong n);
#ifdef __cplusplus
}
#endif
#endif