Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
241852 views
1
import sqrt5, sqrt5_fast
2
from sage.misc.all import cputime
3
from sage.rings.all import Integer, ZZ
4
5
F = sqrt5.F
6
7
def ideals_of_bounded_norm(B):
8
return sum([v for n, v in F.ideals_of_bdd_norm(B).iteritems() if n != 1], [])
9
10
def ideals_of_norm(v):
11
try:
12
v = list(v)
13
except TypeError:
14
v = [Integer(v)]
15
z = F.ideals_of_bdd_norm(max(v))
16
return sum([z[n] for n in v if n>1],[])
17
18
def canonical_gen(I):
19
"""
20
Return a canonical choice of generator for this ideal I (of a
21
PID).
22
23
The implementation computes the Hermite normal form (HNF) basis of
24
the ideal, which is canonical, then finds a generator for the
25
ideal it defines.
26
27
EXAMPLES::
28
29
sage: import psage.modform.hilbert.sqrt5.tables as tables
30
sage: a = tables.F.gen()
31
sage: z = a^30 * (-45*a+28); z
32
-37284985*a - 23043388
33
sage: tables.canonical_gen(tables.F.ideal(z))
34
-45*a + 28
35
"""
36
try:
37
g = I.ring().ideal(I.basis()).gens_reduced()
38
assert len(g) == 1
39
return g[0]
40
except AttributeError:
41
# sometimes I is just a usuaul integer
42
return Integer(I)
43
44
45
def canonical_rep(z):
46
"""
47
Return canonical generator for the ideal generated by z. See the
48
documentation for the canonical_gen function.
49
"""
50
return canonical_gen(z.parent().ideal(z))
51
52
def test_canonical_gen(B=50):
53
a = F.gen()
54
z = -45*a + 28
55
v = [canonical_rep(a**i * z) for i in range(-B,B)]
56
assert len(set(v)) == 1
57
58
59
def no_space(s):
60
return str(s).replace(' ', '')
61
62
def dimensions(v, filename=None):
63
"""
64
Compute dimensions of spaces of Hilbert modular forms for all the levels in v.
65
The format is:
66
67
Norm dimension generator time
68
"""
69
F = open(filename,'a') if filename else None
70
for N in ideals_of_norm(v):
71
t = cputime()
72
H = sqrt5_fast.IcosiansModP1ModN(N)
73
tm = cputime(t)
74
s = '%s %s %s %s'%(N.norm(), H.cardinality(), no_space(canonical_gen(N)), tm)
75
print s
76
if F:
77
F.write(s+'\n')
78
F.flush()
79
80
def charpolys(v, B, filename=None):
81
"""
82
Compute characteristic polynomials of T_P for primes P with norm <= B
83
for spaces of Hilbert modular forms for all the levels in v.
84
"""
85
F = open(filename,'a') if filename else None
86
P = [p for p in ideals_of_bounded_norm(B) if p.is_prime()]
87
for N in ideals_of_norm(v):
88
t = cputime()
89
H = sqrt5_fast.IcosiansModP1ModN(N)
90
T = [(p.smallest_integer(),H.hecke_matrix(p).fcp()) for p in P if
91
gcd(Integer(p.norm()), Integer(N.norm())) == 1]
92
tm = cputime(t)
93
s = '%s %s %s %s'%(N.norm(), no_space(canonical_gen(N)), tm, no_space(T))
94
print s
95
if F:
96
F.write(s+'\n')
97
F.flush()
98
99
def one_charpoly(v, filename=None):
100
"""
101
Compute and factor one characteristic polynomials for all the
102
levels in v. Always compute the charpoly of T_P where P is the
103
smallest prime not dividing the level.
104
"""
105
F = open(filename,'a') if filename else None
106
P = [p for p in ideals_of_bounded_norm(100) if p.is_prime()]
107
for N in ideals_of_norm(v):
108
NN = Integer(N.norm())
109
t = cputime()
110
H = sqrt5_fast.IcosiansModP1ModN(N)
111
t0 = cputime(t)
112
for p in P:
113
if Integer(p.norm()).gcd(NN) == 1:
114
break
115
t = cputime()
116
T = H.hecke_matrix(p)
117
t1 = cputime(t)
118
t = cputime()
119
f = T.fcp()
120
t2 = cputime(t)
121
s = '%s\t%s\t%s\t%s\t%s\t(%.1f,%.1f,%.1f)'%(N.norm(), no_space(canonical_gen(N)),
122
p.smallest_integer(), no_space(canonical_gen(p)), no_space(f),
123
t0, t1, t2,)
124
print s
125
if F:
126
F.write(s+'\n')
127
F.flush()
128
129
130
def elliptic_curves(v, B=100, filename=None):
131
from hmf import HilbertModularForms
132
F = open(filename,'a') if filename else None
133
for N in ideals_of_norm(v):
134
H = HilbertModularForms(N)
135
for i, E in enumerate(H.elliptic_curve_factors()):
136
v = E.aplist(B)
137
s = '%s\t%s\t%s\t%s'%(N.norm(), no_space(canonical_gen(N)), i, ' '.join([no_space(x) for x in v]))
138
print s
139
if F:
140
F.write(s+'\n')
141
F.flush()
142
143
def elliptic_curves_parallel(v, B, dir, ncpu=16):
144
from hmf import HilbertModularForms
145
from sage.all import parallel, set_random_seed
146
import os
147
@parallel(ncpu)
148
def f(N):
149
set_random_seed(0) # to replicate any linear algebra issues?
150
level = no_space(canonical_gen(N)).replace('*','').replace('-','_')
151
F = open(os.path.join(dir,'%s.txt'%level),'w')
152
H = HilbertModularForms(N)
153
level = no_space(canonical_gen(N))
154
try:
155
D = H.elliptic_curve_factors()
156
F.write('count %s %s %s\n'%(N.norm(), level, len(D)))
157
F.flush()
158
for i, E in enumerate(D):
159
v = E.aplist(B)
160
s = '%s\t%s\t%s\t%s'%(N.norm(), level, i, ' '.join([no_space(x) for x in v]))
161
print s
162
F.write(s+'\n')
163
F.flush()
164
except Exception, msg:
165
F.write('exception %s %s "%s"\n'%(N.norm(), level, msg))
166
F.close()
167
168
for X in f(ideals_of_norm(v)):
169
print X
170
171
#################################################################
172
173
from sage.libs.all import pari
174
from sage.rings.all import primes
175
176
def primes_of_bounded_norm(B):
177
"""
178
Return the prime ideals of the integers of the field Q(sqrt(5)) of
179
norm at most B, ordered first by norm, then by the image of the
180
golden ratio mod the prime in GF(p)={0,1,...,p-1}.
181
182
INPUT:
183
184
- B -- integer
185
186
OUTPUT:
187
188
- list of prime ideals
189
190
EXAMPLES::
191
192
sage: import psage
193
sage: psage.modform.hilbert.sqrt5.primes_of_bounded_norm(4)
194
[Fractional ideal (2)]
195
sage: len(psage.modform.hilbert.sqrt5.primes_of_bounded_norm(10^4))
196
1233
197
sage: v = psage.modform.hilbert.sqrt5.primes_of_bounded_norm(11); v
198
[Fractional ideal (2), Fractional ideal (2*a - 1), Fractional ideal (3), Fractional ideal (3*a - 1), Fractional ideal (3*a - 2)]
199
200
Check that the sort order is as claimed::
201
202
sage: P0 = v[-2]; P1 = v[-1]
203
sage: K = P0.number_field(); K
204
Number Field in a with defining polynomial x^2 - x - 1
205
sage: P0.residue_field()(K.gen())
206
4
207
sage: P1.residue_field()(K.gen()) # yep, 4 < 8
208
8
209
"""
210
v = []
211
g = F.gen()
212
for p in primes(B+1):
213
if p == 5:
214
v.append((5, F.ideal(2*g-1)))
215
elif p%5 in [2,3]:
216
Norm = p*p
217
if Norm <= B:
218
v.append((Norm, F.ideal(p)))
219
else:
220
s = pari(5).Mod(p).sqrt()
221
a = int(((1 + s)/2).lift()); b = int(((1 - s)/2).lift())
222
v.append((p, a, F.ideal([p, g - a])))
223
v.append((p, b, F.ideal([p, g - b])))
224
v.sort()
225
return [z[-1] for z in v]
226
227