CoCalc Public Fileswww / cgi-bin / mfd / misc.pyOpen with one click!
Author: William A. Stein
Compute Environment: Ubuntu 18.04 (Deprecated)
1
import constants
2
import os, string, random
3
4
def print_array(a):
5
if a==constants.not_computed:
6
return a
7
return str(a)
8
9
def parse_sqlarray(s):
10
if s==constants.not_computed or s=='':
11
return constants.not_computed
12
s = s.replace('{','[')
13
s = s.replace('}',']')
14
return eval(s)
15
16
def parse_sqlarray_int(s):
17
return parse_sql_array(s)
18
19
def array_togroup(a):
20
# convert to pretty group-looking format
21
if len(a) == 0:
22
return '{0}'
23
if len(a) == 1:
24
return 'Z/'+str(a[0])+'Z'
25
return array_togroup(a[:1]) + ' x ' + array_togroup(a[1:])
26
27
ALPHABET = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",\
28
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
29
30
def ToIsogenyCode(n):
31
# Returns the n-th isogeny coding. The coding goes A,B,C,...,Z,AA,BB,...
32
if n == 0:
33
return ""
34
c = ""
35
for i in range(0,(n-1)/26+1):
36
c = c + ALPHABET[(n-1)%26]
37
return c
38
39
def IsogenyCodeToInteger(s):
40
return 26*(len(s)-1) + ALPHABET.index(s[0])+1
41
42
def newtempfilename():
43
n = int(random.random()*1000000)
44
while os.access(constants.home+'/tmp/tmp'+str(n),os.F_OK):
45
n = n+1
46
return constants.home+'/tmp/tmp'+str(n)
47
48
def newtempfile():
49
return open(newtempfilename(),"w")
50
51
52
def list_to_sqlstring(a):
53
return string.replace(string.replace(str(a),'[','{'),']','}')
54
55
def is_nonnegative_number(s):
56
for i in range(len(s)):
57
if s[i] < '0' or s[i] > '9':
58
return 0
59
return 1
60
61
########################################################
62
# convert between cremona and stein labeling.
63
64
ctos = range(451) # cremona to stein
65
ctos[56] = "BA"
66
ctos[77] = "ACB"
67
ctos[84] = "BA"
68
ctos[99] = "ACBD"
69
ctos[102] = "ACB"
70
ctos[106] = "DACB"
71
ctos[110] = "CBA"
72
ctos[114] = "CAB"
73
ctos[116] = "CAB"
74
ctos[118] = "ACDB"
75
ctos[120] = "BA"
76
ctos[121] = "CABD"
77
ctos[123] = "BA"
78
ctos[124] = "BA"
79
ctos[126] = "BA"
80
ctos[128] = "ADCB"
81
ctos[130] = "ACB"
82
ctos[132] = "BA"
83
ctos[136] = "BA"
84
ctos[140] = "BA"
85
ctos[141] = "EBCAD"
86
ctos[142] = "EABDC"
87
ctos[144] = "BA"
88
ctos[147] = "ACB"
89
ctos[150] = "CAB"
90
ctos[153] = "ADCB"
91
ctos[154] = "ACB"
92
ctos[155] = "CBA"
93
ctos[158] = "EACBD"
94
ctos[162] = "ACBD"
95
ctos[168] = "BA"
96
ctos[170] = "DAECB"
97
ctos[171] = "ADBC"
98
ctos[174] = "CEDBA"
99
ctos[175] = "CAB"
100
ctos[178] = "BA"
101
ctos[182] = "CEADB"
102
ctos[184] = "DABC"
103
ctos[185] = "BCA"
104
ctos[186] = "ACB"
105
ctos[187] = "BA"
106
ctos[189] = "ADBC"
107
ctos[190] = "BAC"
108
ctos[192] = "ACBD"
109
ctos[195] = "CDBA"
110
ctos[196] = "BA"
111
ctos[198] = "CEDAB"
112
ctos[200] = "BECAD"
113
ctos[201] = "BCA"
114
ctos[203] = "BCA"
115
ctos[205] = "CAB"
116
ctos[208] = "DABC"
117
ctos[210] = "DBCAE"
118
ctos[212] = "BA"
119
ctos[214] = "DBAC"
120
ctos[219] = "BCA"
121
ctos[221] = "BA"
122
ctos[222] = "EDACB"
123
ctos[234] = "BCADE"
124
ctos[235] = "CAB"
125
ctos[236] = "BA"
126
ctos[238] = "DAECB"
127
ctos[240] = "BCAD"
128
ctos[242] = "BA"
129
ctos[245] = "CAB"
130
ctos[246] = "EFCAGDB"
131
ctos[249] = "BA"
132
ctos[252] = "BA"
133
ctos[254] = "DCAB"
134
ctos[256] = "BACD"
135
ctos[262] = "BA"
136
ctos[264] = "DABC"
137
ctos[267] = "BA"
138
ctos[270] = "BCDA"
139
ctos[272] = "ADBC"
140
ctos[274] = "CBA"
141
ctos[278] = "BA"
142
ctos[285] = "CAB"
143
ctos[286] = "BDAECF"
144
ctos[288] = "AECBD"
145
ctos[291] = "DBAC"
146
ctos[294] = "EFGCABD"
147
ctos[297] = "DACB"
148
ctos[298] = "BA"
149
ctos[300] = "ACDB"
150
ctos[302] = "BAC"
151
ctos[304] = "ECABDF"
152
ctos[306] = "CBAD"
153
ctos[312] = "CAFBDE"
154
ctos[315] = "BA"
155
ctos[318] = "DCAEB"
156
ctos[320] = "EABCDF"
157
ctos[322] = "BADC"
158
ctos[324] = "ABDC"
159
ctos[325] = "EABDC"
160
ctos[326] = "ACB"
161
ctos[330] = "AEDCB"
162
ctos[333] = "DABC"
163
ctos[336] = "CABEDF"
164
ctos[338] = "ADECFB"
165
ctos[339] = "BAC"
166
ctos[342] = "FGDEABC"
167
ctos[345] = "AECDBF"
168
ctos[348] = "BDAC"
169
ctos[350] = "BFADCE"
170
ctos[352] = "CEABDF"
171
ctos[354] = "DCABEF"
172
ctos[360] = "BCAED"
173
ctos[364] = "BA"
174
ctos[366] = "FGBDACE"
175
ctos[368] = "AEDBGFC"
176
ctos[369] = "BA"
177
ctos[370] = "ACBD"
178
ctos[372] = "ACDB"
179
ctos[378] = "GBEAHDFC"
180
ctos[380] = "BA"
181
ctos[381] = "BA"
182
ctos[384] = "BECADFGH"
183
ctos[387] = "CABDE"
184
ctos[390] = "AFGDEBC"
185
ctos[392] = "EBACDF"
186
ctos[396] = "BAC"
187
ctos[400] = "AFHCGDEB"
188
ctos[402] = "ABDC"
189
ctos[404] = "BA"
190
ctos[405] = "DABFEC"
191
ctos[406] = "ACDB"
192
ctos[408] = "ADCB"
193
ctos[410] = "BDAC"
194
ctos[414] = "CDAB"
195
ctos[418] = "BAC"
196
ctos[423] = "ECFBDGA"
197
ctos[425] = "ADBC"
198
ctos[426] = "CAB"
199
ctos[427] = "BCA"
200
ctos[432] = "EGBAFHCD"
201
ctos[434] = "AEBDC"
202
ctos[435] = "BACD"
203
ctos[437] = "BA"
204
ctos[438] = "FGACBED"
205
ctos[440] = "ADBC"
206
ctos[441] = "BADECF"
207
ctos[442] = "BEACD"
208
ctos[446] = "ADCB"
209
ctos[448] = "AGBDEFHC"
210
ctos[450] = "FGDBEAC"
211
212
stoc = range(451) # stein to cremona
213
stoc[56] = "BA"
214
stoc[77] = "ACB"
215
stoc[84] = "BA"
216
stoc[99] = "ACBD"
217
stoc[102] = "ACB"
218
stoc[106] = "BDCA"
219
stoc[110] = "CBA"
220
stoc[114] = "BCA"
221
stoc[116] = "BCA"
222
stoc[118] = "ADBC"
223
stoc[120] = "BA"
224
stoc[121] = "BCAD"
225
stoc[123] = "BA"
226
stoc[124] = "BA"
227
stoc[126] = "BA"
228
stoc[128] = "ADCB"
229
stoc[130] = "ACB"
230
stoc[132] = "BA"
231
stoc[136] = "BA"
232
stoc[140] = "BA"
233
stoc[141] = "DBCEA"
234
stoc[142] = "BCEDA"
235
stoc[144] = "BA"
236
stoc[147] = "ACB"
237
stoc[150] = "BCA"
238
stoc[153] = "ADCB"
239
stoc[154] = "ACB"
240
stoc[155] = "CBA"
241
stoc[158] = "BDCEA"
242
stoc[162] = "ACBD"
243
stoc[168] = "BA"
244
stoc[170] = "BEDAC"
245
stoc[171] = "ACDB"
246
stoc[174] = "EDACB"
247
stoc[175] = "BCA"
248
stoc[178] = "BA"
249
stoc[182] = "CEADB"
250
stoc[184] = "BCDA"
251
stoc[185] = "CAB"
252
stoc[186] = "ACB"
253
stoc[187] = "BA"
254
stoc[189] = "ACDB"
255
stoc[190] = "BAC"
256
stoc[192] = "ACBD"
257
stoc[195] = "DCAB"
258
stoc[196] = "BA"
259
stoc[198] = "DEACB"
260
stoc[200] = "DACEB"
261
stoc[201] = "CAB"
262
stoc[203] = "CAB"
263
stoc[205] = "BCA"
264
stoc[208] = "BCDA"
265
stoc[210] = "DBCAE"
266
stoc[212] = "BA"
267
stoc[214] = "CBDA"
268
stoc[219] = "CAB"
269
stoc[221] = "BA"
270
stoc[222] = "CEDBA"
271
stoc[234] = "CABDE"
272
stoc[235] = "BCA"
273
stoc[236] = "BA"
274
stoc[238] = "BEDAC"
275
stoc[240] = "CABD"
276
stoc[242] = "BA"
277
stoc[245] = "BCA"
278
stoc[246] = "DGCFABE"
279
stoc[249] = "BA"
280
stoc[252] = "BA"
281
stoc[254] = "CDBA"
282
stoc[256] = "BACD"
283
stoc[262] = "BA"
284
stoc[264] = "BCDA"
285
stoc[267] = "BA"
286
stoc[270] = "DABC"
287
stoc[272] = "ACDB"
288
stoc[274] = "CBA"
289
stoc[278] = "BA"
290
stoc[285] = "BCA"
291
stoc[286] = "CAEBDF"
292
stoc[288] = "ADCEB"
293
stoc[291] = "CBDA"
294
stoc[294] = "EFDGABC"
295
stoc[297] = "BDCA"
296
stoc[298] = "BA"
297
stoc[300] = "ADBC"
298
stoc[302] = "BAC"
299
stoc[304] = "CDBEAF"
300
stoc[306] = "CBAD"
301
stoc[312] = "BDAEFC"
302
stoc[315] = "BA"
303
stoc[318] = "CEBAD"
304
stoc[320] = "BCDEAF"
305
stoc[322] = "BADC"
306
stoc[324] = "ABDC"
307
stoc[325] = "BCEDA"
308
stoc[326] = "ACB"
309
stoc[330] = "AEDCB"
310
stoc[333] = "BCDA"
311
stoc[336] = "BCAEDF"
312
stoc[338] = "AFDBCE"
313
stoc[339] = "BAC"
314
stoc[342] = "EFGCDAB"
315
stoc[345] = "AECDBF"
316
stoc[348] = "CADB"
317
stoc[350] = "CAEDFB"
318
stoc[352] = "CDAEBF"
319
stoc[354] = "CDBAEF"
320
stoc[360] = "CABED"
321
stoc[364] = "BA"
322
stoc[366] = "ECFDGAB"
323
stoc[368] = "ADGCBFE"
324
stoc[369] = "BA"
325
stoc[370] = "ACBD"
326
stoc[372] = "ADBC"
327
stoc[378] = "DBHFCGAE"
328
stoc[380] = "BA"
329
stoc[381] = "BA"
330
stoc[384] = "DACEBFGH"
331
stoc[387] = "BCADE"
332
stoc[390] = "AFGDEBC"
333
stoc[392] = "CBDEAF"
334
stoc[396] = "BAC"
335
stoc[400] = "AHDFGBEC"
336
stoc[402] = "ABDC"
337
stoc[404] = "BA"
338
stoc[405] = "BCFAED"
339
stoc[406] = "ADBC"
340
stoc[408] = "ADCB"
341
stoc[410] = "CADB"
342
stoc[414] = "CDAB"
343
stoc[418] = "BAC"
344
stoc[423] = "GDBEACF"
345
stoc[425] = "ACDB"
346
stoc[426] = "BCA"
347
stoc[427] = "CAB"
348
stoc[432] = "DCGHAEBF"
349
stoc[434] = "ACEDB"
350
stoc[435] = "BACD"
351
stoc[437] = "BA"
352
stoc[438] = "CEDGFAB"
353
stoc[440] = "ACDB"
354
stoc[441] = "BAECDF"
355
stoc[442] = "CADEB"
356
stoc[446] = "ADCB"
357
stoc[448] = "ACHDEFBG"
358
stoc[450] = "FDGCEAB"
359
360
361
# N is an integer and iso is a string or number
362
# the returned value is (N,iso), where iso is an integer
363
def cremona_to_stein(N, iso):
364
if type(iso) == type("A"):
365
iso = IsogenyCodeToInteger(iso)
366
if N >= len(ctos) or ctos[N] == N or iso > len(ctos[N]):
367
return (N,iso)
368
iso = IsogenyCodeToInteger(ctos[N][iso-1])
369
return (N,iso)
370
371
372
def stein_to_cremona(N, iso):
373
if N >= len(stoc) or stoc[N] == N or iso > len(stoc[N]):
374
if type(iso) == type("A"):
375
iso = IsogenyCodeToInteger(iso)
376
return (N,iso)
377
if type(iso) == type("A"):
378
iso = IsogenyCodeToInteger(iso)
379
iso = IsogenyCodeToInteger(stoc[N][iso-1])
380
return (N,iso)
381
382
def quotient_string(numerator, denominator):
383
# num and denom should be strings
384
numerator=str(numerator)
385
denominator=str(denominator)
386
if numerator == '0':
387
return '0'
388
if denominator == '1':
389
return numerator
390
return numerator + '/' + denominator
391