-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_ec_dataset.sage
More file actions
226 lines (192 loc) · 6.49 KB
/
Copy pathbuild_ec_dataset.sage
File metadata and controls
226 lines (192 loc) · 6.49 KB
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
import sys, os, csv, json, argparse, time
from sageall import EllipticCurve, pari, ZZ
# ----------------------------------------------------------------------
# PARI configuration
# ----------------------------------------------------------------------
pari.default('two_seconds', 10**9)
# ----------------------------------------------------------------------
# Paths to submodules
# ----------------------------------------------------------------------
REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
ECDATA_ROOT = os.path.join(REPO_ROOT, "data", "ecdata")
ECLIB_ROOT = os.path.join(REPO_ROOT, "data", "eclib")
LMFDB_ROOT = os.path.join(REPO_ROOT, "data", "lmfdb")
# ----------------------------------------------------------------------
# Load Cremona ecdata
# ----------------------------------------------------------------------
def load_cremona_from_ecdata(label):
"""
Load a curve from data/ecdata/<conductor>/<isogeny>/<label>
Returns dict with keys:
a_invariants, conductor, torsion, ...
or None if not found.
"""
try:
N, iso, num = label.split()
except:
# Cremona labels are like "11a1"
# Parse manually
import re
m = re.match(r"(\d+)([a-z]+)(\d+)", label)
if not m:
return None
N, iso, num = m.groups()
path = os.path.join(ECDATA_ROOT, N, iso, f"{N}{iso}{num}")
if not os.path.exists(path):
return None
data = {}
with open(path) as f:
for line in f:
if line.startswith("a-invariants"):
parts = line.split(":")[1].strip().split()
data["a_invariants"] = list(map(int, parts))
elif line.startswith("conductor"):
data["conductor"] = int(line.split(":")[1])
elif line.startswith("torsion"):
data["torsion"] = int(line.split(":")[1])
elif line.startswith("tamagawa"):
data["tamagawa"] = int(line.split(":")[1])
return data if "a_invariants" in data else None
# ----------------------------------------------------------------------
# Load LMFDB JSON
# ----------------------------------------------------------------------
def load_lmfdb(label):
"""
Load curve from data/lmfdb/elliptic_curves/<N>/<iso>/<label>.json
"""
try:
import re
m = re.match(r"(\d+)([a-z]+)(\d+)", label)
if not m:
return None
N, iso, num = m.groups()
except:
return None
json_path = os.path.join(
LMFDB_ROOT, "elliptic_curves", N, iso, f"{label}.json"
)
if not os.path.exists(json_path):
return None
with open(json_path) as f:
j = json.load(f)
return {
"a_invariants": j["ainvs"],
"conductor": j["conductor"],
"torsion": j["torsion_order"],
"tamagawa": j.get("tamagawa_product"),
"rank_lmfdb": j.get("rank"),
}
# ----------------------------------------------------------------------
# Compute invariants
# ----------------------------------------------------------------------
def compute_invariants(label):
row = {"label": label}
# Try Cremona ecdata
data = load_cremona_from_ecdata(label)
# If not found, try LMFDB
if data is None:
data = load_lmfdb(label)
if data is None:
row["error"] = "Curve not found in ecdata or lmfdb"
return row
# Build curve
try:
E = EllipticCurve(data["a_invariants"])
except Exception as e:
row["error"] = f"Failed to construct curve: {e}"
return row
# Basic invariants
try:
row["conductor"] = int(E.conductor())
row["discriminant"] = int(E.discriminant())
row["j_invariant"] = int(E.j_invariant())
row["torsion_order"] = int(E.torsion_order())
row["omega_real"] = float(E.period_lattice().real_period())
row["tamagawa_product"] = int(E.tamagawa_product())
except Exception as e:
row["error"] = f"Invariant error: {e}"
# Algebraic rank
try:
row["rank_algebraic"] = int(E.rank())
except Exception as e:
row["error"] = f"rank error: {e}"
# Analytic rank via PARI
try:
gE = pari(E)
rdata = gE.ellrankinit()
row["rank_analytic_pari"] = int(gE.ellrank(rdata))
except Exception as e:
row["rank_analytic_pari_error"] = str(e)
# 2-Selmer rank
try:
row["selmer2_rank_pari"] = int(E.selmer_rank(2))
except Exception as e:
row["selmer2_rank_pari_error"] = str(e)
# Sha
try:
sha = E.sha()
if sha is not None:
row["sha_order"] = int(sha.order())
except:
pass
# Heegner height (placeholder)
try:
gens = E.gens()
if gens:
row["heegner_height"] = float(E.height(gens[0]))
except:
pass
return row
# ----------------------------------------------------------------------
# Main
# ----------------------------------------------------------------------
def main():
p = argparse.ArgumentParser()
p.add_argument("--labels-file", required=True)
p.add_argument("--output", required=True)
p.add_argument("--batch-size", type=int, default=500)
args = p.parse_args()
labels = []
with open(args.labels_file) as f:
reader = csv.DictReader(f)
for r in reader:
labels.append(r["label"])
out_dir = os.path.dirname(args.output)
if out_dir and not os.path.exists(out_dir):
os.makedirs(out_dir)
fieldnames = [
"label",
"conductor",
"discriminant",
"j_invariant",
"omega_real",
"torsion_order",
"tamagawa_product",
"rank_algebraic",
"rank_analytic_pari",
"selmer2_rank_pari",
"sha_order",
"heegner_height",
"error",
"rank_analytic_pari_error",
"selmer2_rank_pari_error",
]
write_header = not os.path.exists(args.output)
with open(args.output, "a") as f_out:
writer = csv.DictWriter(f_out, fieldnames=fieldnames)
if write_header:
writer.writeheader()
batch = []
t0 = time.time()
for i, lab in enumerate(labels, 1):
row = compute_invariants(lab)
batch.append(row)
if len(batch) >= args.batch_size:
writer.writerows(batch)
batch = []
print(f"[{i}/{len(labels)}] processed")
if batch:
writer.writerows(batch)
print("Done.")
if __name__ == "__main__":
main()