-
Notifications
You must be signed in to change notification settings - Fork 1
/
compute_const.sage
417 lines (345 loc) · 14.4 KB
/
compute_const.sage
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import argh
from tqdm import tqdm
import unittest
import itertools
MAX_Q = 1000
class computeConstantForD:
abs_const = 1/(gamma(1/2)*zeta(2)**(1/2))
def __init__(self, d, max_q = MAX_Q):
"""
ARGUMENTS
d: The value of d for which we want the constant
max_q: When we compute (an upper or lower bound on)
the product over all q s.t. chi_d(q) = 1 during the
computation of c'_d, we take the terms up to this value of q.
1000 by default, it doesn't seem to change much if it's
increased.
INSTANCE VARIABLES
disc: The fundamental discriminant associated to this d. It will be
d*eps_d if d is odd (this is always 1 mod 4) or 4deps_d if
d is even (this is fine since d is squarefree).
kronecker_d: The Kronecker character of self.disc.
modulus: The modulus of the associated Kronecker character.
d_factors: A list of the factors of d.
"""
self.d = d
self.max_q = max_q
eps_d = 1 if d%4 == 1 else -1
self.disc = d*eps_d if d%2 == 1 else 4*d*eps_d
self.kronecker_d = kronecker_character(self.disc)
self.modulus = abs(self.disc)
self.d_factors = prime_factors(d)
def computeConstant(self):
"""
Returns a list of
[lower bound on c_R, upper bound on c_R,
lower bound on c_I, upper bound on c_I]
"""
prod_of_constants = [tup[0] * tup[1] for tup in itertools.product(self.computeSecondConstant(), self.computeFirstConstant())]
return [x / (abs(self.d)*2**(len(self.d_factors))) for x in prod_of_constants]
def computeFirstConstant(self):
"""
Returns a lower and upper bound on c'_d for this value of d.
Comes from Corollary 4.9 of the paper.
"""
x = computeConstantForD.abs_const * self.computeLocalConstant()
annoying_product_lb, annoying_product_ub = self.computeAnnoyingProduct()
return x * annoying_product_lb, x * annoying_product_ub
def computeSecondConstant(self):
"""
Returns c''_{d,R} and c''_{d,I})
Comes from Lemma 5.1 of the paper.
"""
return self.computeSecondRealConstant(), self.computeSecondImaginaryConstant()
def computeSecondRealConstant(self):
"""
Computes c''_{d,R}
"""
if self.d % 8 == 1 or self.d % 8 == 7:
return 1
elif self.d % 8 == 3 or self.d % 8 == 5:
return 2/3
elif self.d % 8 == 6:
return 1/4
elif self.d % 8 == 2 and self.d > 0:
return 1/4
else:
return 0
def computeSecondImaginaryConstant(self):
"""
Computes c''_{d,I}
"""
if self.d % 8 == 1 and self.d > 0:
return 1
elif self.d % 8 == 7 and self.d < 0:
return 1
elif self.d % 8 == 3 and self.d < 0:
return 2/3
elif self.d % 8 == 5 and self.d > 0:
return 2/3
elif (self.d % 8 == 2 or self.d % 8 == 6) and self.d < 0:
return 1/4
else:
return 0
def computeLocalConstant(self):
"""
Compute the value of L(chi_d, 1)^{1/2}\prod_{q | m} (1+1/q)^{-1/2}
"""
return sqrt(self.computeLocalProduct() * abs(self.evaluateLSeriesOfChiAtOne()))
def computeLocalProduct(self):
"""
Computes \prod_{q | m} (1+1/q)^{-1/2}
"""
local_prod = 1.0
for fac in self.d_factors:
local_prod *= (fac/(fac+1))
return local_prod
def evaluateLSeriesOfChiAtOne(self):
"""
Computes the value of L(1, chi_d), using a formula
of Dirichlet for evaluating the L-series of a
real quadratic character at 1. We adjust if chi_d
is not a Kronecker symbol (i.e. when d is \pm 3 mod 8)
as per Lemma 4.4(ii).
"""
def evaluateLSeriesOfKroneckerAtOne():
"""
Computes the value at 1 of the L series of the Kronecker symbol
with top value eps_d * d
Comes from Theorem 7.5 of the paper.
"""
if self.disc < 0:
ans = -pi*pow(self.modulus, -3/2)
int_sum = 0
for j in range(1,self.modulus):
int_sum += self.kronecker_d(j)*j
return ans * int_sum
else:
ans = -pow(self.modulus, -1/2)
int_sum = 0
for j in range(1,self.modulus):
int_sum += self.kronecker_d(j)*log(sin(j*pi/self.modulus))
return ans * int_sum
if (self.d % 8 == 3 or self.d % 8 == 5):
return 3*evaluateLSeriesOfKroneckerAtOne()
else:
return evaluateLSeriesOfKroneckerAtOne()
def computeAnnoyingProduct(self):
"""
Computes a bound on the product of (1-q^{-2})
over all primes q for which chi_d(q) = 1.
Follows Equation 28 of the paper.
"""
return self.computeAnnoyingProductLB(), self.computeAnnoyingProductUB()
def computeAnnoyingProductUB(self):
"""
For an upper bound, it takes all the terms for
primes up to q_max, since each term is at most 1.
"""
prod = 1
for p in primes(self.max_q):
if self.kronecker_d(p) == 1:
prod *= (1-1/(p**2))
if self.d % 8 == 3 or self.d % 8 == 5:
prod *= (3/4)
return sqrt(prod)
def computeAnnoyingProductLB(self):
"""
For a lower bound, it starts with 1/zeta(2)
(as if all primes were in the product) and then
removes primes up to q_max for which chi_d(q) != 1
"""
prod = 1/zeta(2)
for p in primes(self.max_q):
if self.kronecker_d(p) != 1:
prod *= (p**2)/(p**2-1)
if self.d % 8 == 3 or self.d % 8 == 5:
prod *= (3/4)
return sqrt(prod)
def returnTestDict(self):
"""
Returns a dictionary of information which the test can check
This is probably bad software practice but whatever, seems
cleaner than having a bunch of getters.
"""
return {
"disc": self.disc,
"modulus": self.modulus,
"local": self.computeLocalProduct(),
"l_series_at_one": self.evaluateLSeriesOfChiAtOne(),
"annoying": self.computeAnnoyingProduct(),
"second_const": self.computeSecondConstant(),
}
def computeTail(neg_max_d, pos_max_d, a, b):
"""
neg_max_d: The good d in input file include all the negative good
values of d which are at least neg_max_d.
pos_max_d: The good d in input file include all the pos good
values of d which are at most pos_max_d.
Compute an upper bound on the sum of c'_dc''_d/(|d|2^{w(d)}).
c'_d is at most 1/(Gamma(1/2)zeta(2)^{1/2}) * |L(1,chi_d)|^{1/2}.
We upper bound the latter using P\'olya-Vinogradov (we could do
better with work of Pintz, but we don't need to for now).
The formula comes from Corollary 7.7 of the paper, and a proof
is in the comments of the LaTeX source.
2^{w(d)} is at least 2, hence the 2 in the denominator of f(x)
"""
a = QQ(a)
b = QQ(b)
f(x) = sqrt((1/2)*log(4*x)+log(log(4*x))+1/(2*sqrt(4*x)*log(4*x))+2+euler_gamma)/(2*x)
#These next two lines carry out integration by parts of the Stieljes integral
#associated to the tail.
g(x,A,B) = (1/2)*A*x^B*f.diff(x)
pos_tail = f(pos_max_d)*a*(pos_max_d)^b
pos_integral = numerical_integral(g(x,a,b),pos_max_d,Infinity)
pos_tail += (-1)*pos_integral[0] + abs(pos_integral[1])
neg_max_d *= -1
neg_tail = f(neg_max_d)*a*(neg_max_d)^b
neg_integral = numerical_integral(g(x,a,b),neg_max_d,Infinity)
neg_tail += (-1)*neg_integral[0] + abs(neg_integral[1])
tail = pos_tail + neg_tail
tail *= computeConstantForD.abs_const
tail = N(tail)
print("Contribution from d <", neg_max_d, "and d >", pos_max_d, "is at most", tail)
return tail
def computeConstant(good_file = "data/good-d.txt", neg_max_d = -10000, pos_max_d = 50000, max_q = MAX_Q, a = 5, b = 0.35):
"""
good_file: Where to fetch data from
max_d: If bound is 'u', how far up to compute before we bound the tail.
max_q: When we compute (an upper or lower bound on)
the product of (1-q^{-2}) over all q s.t. chi_d(q) = 1 during the
computation of c'_d, we take the terms up to this value of q.
It doesn't seem to change much if it's increased beyond 1000.
a: The leading coefficient in our power law upper bound on number of good d up to D
b: The exponent in our power law upper bound on number of good d up to D
Computes an upper or lower bound on c_R or c_I, based on the values
of signature and bound.
"""
with open(good_file, 'r') as goodfile:
num_d = 0
for line in goodfile:
num_d += 1
print("The number of good d in this file is", num_d)
print("I will take d in between", neg_max_d, "and", pos_max_d)
print("Computing constant")
total = [0,0,0,0]
with open(good_file, 'r') as goodfile:
for line in tqdm(goodfile, total=int(num_d)):
d = int(line)
d_contributions = computeConstantForD(d, max_q).computeConstant()
for i in range(4):
total[i] += d_contributions[i]
tail = computeTail(neg_max_d, pos_max_d, a, b)
print("c_R is at least", N(total[0]), "and is at most", N(total[1] + tail))
print("c_I is at least", N(total[2]), "and is at most", N(total[3] + tail))
"""
===========================================================================
TESTS
===========================================================================
"""
def runTests():
suite = unittest.defaultTestLoader.loadTestsFromTestCase(
TestComputeConstantForD)
unittest.TextTestRunner().run(suite)
class TestComputeConstantForD(unittest.TestCase):
"""
For now, just has a bunch of examples for which to compute the
various things
The proper way to do this is probably to have a different test for each
key in the expected dict.
"""
l_idx = 0
u_idx = 1
r_idx = 0
i_idx = 1
def test_2ru12(self):
test_input = (2,12)
expected = {
"disc": -8,
"modulus": 8,
"local": 2/3,
"l_series_at_one": -(pi/pow(8,3/2))*(1 + 3 - 5 - 7),
"annoying": sqrt((8/9)*(120/121)),
"second_const": 1/4,
}
actual = computeConstantForD(*test_input).returnTestDict()
actual["annoying"] = actual["annoying"][self.u_idx]
actual["second_const"] = actual["second_const"][self.r_idx]
for key in expected.keys():
self.assertEqual(actual[key], expected[key], key)
def test_minus7rl14(self):
test_input = (-7,14)
expected = {
"disc": -7,
"modulus": 7,
"local": 7/8,
"l_series_at_one": -(pi/pow(7,3/2))*(1 + 2 - 3 + 4 - 5 - 6),
"annoying": sqrt((1/zeta(2))*(9/8)*(25/24)*(49/48)*(169/168)),
"second_const": 1
}
actual = computeConstantForD(*test_input).returnTestDict()
actual["annoying"] = actual["annoying"][self.l_idx]
actual["second_const"] = actual["second_const"][self.r_idx]
for key in expected.keys():
self.assertEqual(actual[key], expected[key], key)
def test_7iu5(self):
test_input = (7,5)
expected = {
"disc": -7,
"modulus": 7,
"local": 7/8,
"l_series_at_one": -(pi/pow(7,3/2))*(1 + 2 - 3 + 4 - 5 - 6),
"annoying": sqrt(3/4),
"second_const": 0
}
actual = computeConstantForD(*test_input).returnTestDict()
actual["annoying"] = actual["annoying"][self.u_idx]
actual["second_const"] = actual["second_const"][self.i_idx]
for key in expected.keys():
self.assertEqual(actual[key], expected[key], key)
def test_5ru10(self):
test_input = (5,10)
expected = {
"disc": 5,
"modulus": 5,
"local": 5/6,
"l_series_at_one": -3*(1/pow(5,1/2))*(log(sin(pi*1/5)) - log(sin(pi*2/5)) - log(sin(pi*3/5)) + log(sin(pi*4/5))),
"annoying": sqrt(3/4),
"second_const": 2/3
}
actual = computeConstantForD(*test_input).returnTestDict()
actual["annoying"] = actual["annoying"][self.u_idx]
actual["second_const"] = actual["second_const"][self.r_idx]
for key in expected.keys():
self.assertEqual(actual[key], expected[key], key)
def test_15il15(self):
test_input = (15,15)
expected = {
"disc": -15,
"modulus": 15,
"local": 15/24,
"l_series_at_one": -(pi/pow(15,3/2))*(1 + 2 + 4 - 7 + 8 - 11 - 13 - 14),
"annoying": sqrt((1/zeta(2))*(9/8)*(25/24)*(49/48)*(121/120)*(169/168)),
"second_const": 0
}
actual = computeConstantForD(*test_input).returnTestDict()
actual["annoying"] = actual["annoying"][self.l_idx]
actual["second_const"] = actual["second_const"][self.i_idx]
for key in expected.keys():
self.assertEqual(actual[key], expected[key], key)
def test_minus6ru3(self):
test_input = (-6,3)
expected = {
"disc": 24,
"modulus": 24,
"local": 1/2,
"l_series_at_one": -(1/pow(24,1/2))*(log(sin(pi/24)) + log(sin(5*pi/24)) - log(sin(7*pi/24)) - log(sin(11*pi/24)) - log(sin(13*pi/24)) - log(sin(17*pi/24)) + log(sin(19*pi/24)) + log(sin(23*pi/24))),
"annoying": 1,
"second_const": 0
}
actual = computeConstantForD(*test_input).returnTestDict()
actual["annoying"] = actual["annoying"][self.u_idx]
actual["second_const"] = actual["second_const"][self.r_idx]
for key in expected.keys():
self.assertEqual(actual[key], expected[key], key)
argh.dispatch_commands([computeConstant, runTests])