-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy paththeta_const_sum_rs.c
210 lines (170 loc) · 5.99 KB
/
theta_const_sum_rs.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Copyright (C) 2015 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/>.
*/
#include "acb_modular.h"
static const int square_best_m[] = {
2, 3, 4, 8, 12, 16, 32, 48, 80, 96,
112, 144, 240, 288, 336, 480, 560, 576, 720, 1008,
1440, 1680, 2016, 2640, 2880, 3600, 4032, 5040, 7920, 9360,
10080, 15840, 18480, 20160, 25200, 31680, 37440, 39600, 44352, 50400,
55440, 65520, 85680, 95760, 102960, 110880, 131040, 171360, 191520, 205920,
221760, 262080, 277200, 327600, 383040, 411840, 514800, 554400, 655200, 720720,
942480, 0
};
static const int square_best_m_residues[] = {
2, 2, 2, 3, 4, 4, 7, 8, 12, 14,
16, 16, 24, 28, 32, 42, 48, 48, 48, 64,
84, 96, 112, 144, 144, 176, 192, 192, 288, 336,
336, 504, 576, 576, 704, 864, 1008, 1056, 1152, 1232,
1152, 1344, 1728, 1920, 2016, 2016, 2352, 3024, 3360, 3528,
3456, 4032, 4224, 4928, 5760, 6048, 7392, 7392, 8624, 8064,
10368, 0
};
static const int trigonal_best_m[] = {
2, 6, 10, 14, 18, 30, 42, 66, 70, 90,
126, 198, 210, 330, 390, 450, 630, 990, 1170, 1386,
1638, 2142, 2310, 2730, 3150, 4950, 5850, 6930, 8190, 10710,
11970, 12870, 16830, 18018, 23562, 26334, 27846, 30030, 34650, 40950,
53550, 59850, 64350, 84150, 90090, 117810, 131670, 139230, 155610, 188370,
203490, 218790, 244530, 270270, 296010, 306306, 342342, 414414, 447678, 450450,
589050, 658350, 696150, 778050, 941850, 0
};
static const int trigonal_best_m_residues[] = {
1, 2, 3, 4, 4, 6, 8, 12, 12, 12,
16, 24, 24, 36, 42, 44, 48, 72, 84, 96,
112, 144, 144, 168, 176, 264, 308, 288, 336, 432,
480, 504, 648, 672, 864, 960, 1008, 1008, 1056, 1232,
1584, 1760, 1848, 2376, 2016, 2592, 2880, 3024, 3360, 4032,
4320, 4536, 5040, 5544, 6048, 6048, 6720, 8064, 8640, 7392,
9504, 10560, 11088, 12320, 14784, 0,
};
slong
acb_modular_rs_optimal_m(const int * best_ms, const int * num_residues, slong N)
{
slong i, m, cost, best_i, best_m, best_cost;
best_i = 0;
best_m = best_ms[0];
best_cost = WORD_MAX;
for (i = 0; (m = best_ms[i]) != 0; i++)
{
cost = N / m + num_residues[i];
if (i == 0 || cost < best_cost)
{
best_i = i;
best_cost = cost;
best_m = m;
}
}
/* flint_printf("N = %wd, best_m = %wd, best_cost = %wd, s(m) = %d\n",
N, best_m, best_cost, num_residues[best_i]); */
i = best_i;
return best_m;
}
void
acb_modular_theta_const_sum_rs(acb_t theta2, acb_t theta3, acb_t theta4,
const acb_t q, slong N, slong prec)
{
slong * tab;
slong k, term_prec, i, e, eprev;
slong M, m2, m3, num_square, num_trigonal;
double log2q_approx, log2term_approx;
acb_ptr qpow;
acb_t tmp1, tmp2;
mag_t qmag;
mag_init(qmag);
acb_get_mag(qmag, q);
log2q_approx = mag_get_d_log2_approx(qmag);
mag_clear(qmag);
acb_init(tmp1);
acb_init(tmp2);
/* choose rectangular splitting parameters */
m2 = acb_modular_rs_optimal_m(trigonal_best_m, trigonal_best_m_residues, N);
m3 = acb_modular_rs_optimal_m(square_best_m, square_best_m_residues, N);
M = FLINT_MAX(m2, m3) + 1;
/* build addition sequence */
tab = flint_calloc(M, sizeof(slong));
for (k = 0; k*(k+1) < N; k++)
tab[(k*(k+1)) % m2] = -1;
num_trigonal = k;
for (k = 0; k*k < N; k++)
tab[(k*k) % m3] = -1;
num_square = k;
tab[m2] = -1;
tab[m3] = -1;
/* compute powers in addition sequence */
qpow = _acb_vec_init(M);
acb_modular_fill_addseq(tab, M);
for (k = 0; k < M; k++)
{
if (k == 0)
{
acb_one(qpow + k);
}
else if (k == 1)
{
acb_set_round(qpow + k, q, prec);
}
else if (tab[k] != 0)
{
log2term_approx = k * log2q_approx;
term_prec = FLINT_MIN(FLINT_MAX(prec + log2term_approx + 16.0, 16.0), prec);
_acb_modular_mul(qpow + k, tmp1, tmp2, qpow + tab[k], qpow + k - tab[k], term_prec, prec);
}
}
/* compute theta2 */
acb_zero(theta2);
term_prec = prec;
for (k = num_trigonal - 1; k >= 0; k--)
{
e = k * (k + 1); /* exponent */
eprev = (k + 1) * (k + 2);
log2term_approx = e * log2q_approx;
term_prec = FLINT_MIN(FLINT_MAX(prec + log2term_approx + 16.0, 16.0), prec);
/* giant steps */
for (i = e / m2; i < eprev / m2; i++)
{
if (!acb_is_zero(theta2))
_acb_modular_mul(theta2, tmp1, tmp2, theta2, qpow + m2, term_prec, prec);
}
acb_add(theta2, theta2, qpow + (e % m2), prec);
}
acb_mul_2exp_si(theta2, theta2, 1);
/* compute theta3, theta4 */
acb_zero(theta3);
acb_zero(theta4);
term_prec = prec;
for (k = num_square - 1; k >= 0; k--)
{
e = k * k; /* exponent */
eprev = (k + 1) * (k + 1);
log2term_approx = e * log2q_approx;
term_prec = FLINT_MIN(FLINT_MAX(prec + log2term_approx + 16.0, 16.0), prec);
/* giant steps */
for (i = e / m3; i < eprev / m3; i++)
{
if (!acb_is_zero(theta3))
_acb_modular_mul(theta3, tmp1, tmp2, theta3, qpow + m3, term_prec, prec);
if (!acb_is_zero(theta4))
_acb_modular_mul(theta4, tmp1, tmp2, theta4, qpow + m3, term_prec, prec);
}
if (k == 0)
{
acb_mul_2exp_si(theta3, theta3, 1);
acb_mul_2exp_si(theta4, theta4, 1);
}
acb_add(theta3, theta3, qpow + (e % m3), prec);
if (k % 2 == 0)
acb_add(theta4, theta4, qpow + (e % m3), prec);
else
acb_sub(theta4, theta4, qpow + (e % m3), prec);
}
acb_clear(tmp1);
acb_clear(tmp2);
_acb_vec_clear(qpow, M);
flint_free(tab);
}