CoCalc Shared Fileswww / cgi-bin / mfd / misc.py
Author: William A. Stein
1import constants
2import os, string, random
3
4def print_array(a):
5    if a==constants.not_computed:
6        return a
7    return str(a)
8
9def 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
16def parse_sqlarray_int(s):
17    return parse_sql_array(s)
18
19def 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
27ALPHABET = ["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
30def 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
39def IsogenyCodeToInteger(s):
40    return 26*(len(s)-1) + ALPHABET.index(s[0])+1
41
42def 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
48def newtempfile():
49    return open(newtempfilename(),"w")
50
51
52def list_to_sqlstring(a):
53    return string.replace(string.replace(str(a),'[','{'),']','}')
54
55def 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
64ctos = range(451)  # cremona to stein
65ctos[56] = "BA"
66ctos[77] = "ACB"
67ctos[84] = "BA"
68ctos[99] = "ACBD"
69ctos[102] = "ACB"
70ctos[106] = "DACB"
71ctos[110] = "CBA"
72ctos[114] = "CAB"
73ctos[116] = "CAB"
74ctos[118] = "ACDB"
75ctos[120] = "BA"
76ctos[121] = "CABD"
77ctos[123] = "BA"
78ctos[124] = "BA"
79ctos[126] = "BA"
81ctos[130] = "ACB"
82ctos[132] = "BA"
83ctos[136] = "BA"
84ctos[140] = "BA"
86ctos[142] = "EABDC"
87ctos[144] = "BA"
88ctos[147] = "ACB"
89ctos[150] = "CAB"
91ctos[154] = "ACB"
92ctos[155] = "CBA"
93ctos[158] = "EACBD"
94ctos[162] = "ACBD"
95ctos[168] = "BA"
96ctos[170] = "DAECB"
98ctos[174] = "CEDBA"
99ctos[175] = "CAB"
100ctos[178] = "BA"
102ctos[184] = "DABC"
103ctos[185] = "BCA"
104ctos[186] = "ACB"
105ctos[187] = "BA"
107ctos[190] = "BAC"
108ctos[192] = "ACBD"
109ctos[195] = "CDBA"
110ctos[196] = "BA"
111ctos[198] = "CEDAB"
113ctos[201] = "BCA"
114ctos[203] = "BCA"
115ctos[205] = "CAB"
116ctos[208] = "DABC"
117ctos[210] = "DBCAE"
118ctos[212] = "BA"
119ctos[214] = "DBAC"
120ctos[219] = "BCA"
121ctos[221] = "BA"
122ctos[222] = "EDACB"
124ctos[235] = "CAB"
125ctos[236] = "BA"
126ctos[238] = "DAECB"
128ctos[242] = "BA"
129ctos[245] = "CAB"
130ctos[246] = "EFCAGDB"
131ctos[249] = "BA"
132ctos[252] = "BA"
133ctos[254] = "DCAB"
134ctos[256] = "BACD"
135ctos[262] = "BA"
136ctos[264] = "DABC"
137ctos[267] = "BA"
138ctos[270] = "BCDA"
140ctos[274] = "CBA"
141ctos[278] = "BA"
142ctos[285] = "CAB"
143ctos[286] = "BDAECF"
144ctos[288] = "AECBD"
145ctos[291] = "DBAC"
146ctos[294] = "EFGCABD"
147ctos[297] = "DACB"
148ctos[298] = "BA"
149ctos[300] = "ACDB"
150ctos[302] = "BAC"
151ctos[304] = "ECABDF"
153ctos[312] = "CAFBDE"
154ctos[315] = "BA"
155ctos[318] = "DCAEB"
156ctos[320] = "EABCDF"
158ctos[324] = "ABDC"
159ctos[325] = "EABDC"
160ctos[326] = "ACB"
161ctos[330] = "AEDCB"
162ctos[333] = "DABC"
163ctos[336] = "CABEDF"
165ctos[339] = "BAC"
166ctos[342] = "FGDEABC"
167ctos[345] = "AECDBF"
168ctos[348] = "BDAC"
170ctos[352] = "CEABDF"
171ctos[354] = "DCABEF"
172ctos[360] = "BCAED"
173ctos[364] = "BA"
174ctos[366] = "FGBDACE"
175ctos[368] = "AEDBGFC"
176ctos[369] = "BA"
177ctos[370] = "ACBD"
178ctos[372] = "ACDB"
179ctos[378] = "GBEAHDFC"
180ctos[380] = "BA"
181ctos[381] = "BA"
183ctos[387] = "CABDE"
184ctos[390] = "AFGDEBC"
185ctos[392] = "EBACDF"
186ctos[396] = "BAC"
187ctos[400] = "AFHCGDEB"
188ctos[402] = "ABDC"
189ctos[404] = "BA"
190ctos[405] = "DABFEC"
191ctos[406] = "ACDB"
193ctos[410] = "BDAC"
194ctos[414] = "CDAB"
195ctos[418] = "BAC"
196ctos[423] = "ECFBDGA"
198ctos[426] = "CAB"
199ctos[427] = "BCA"
200ctos[432] = "EGBAFHCD"
201ctos[434] = "AEBDC"
202ctos[435] = "BACD"
203ctos[437] = "BA"
204ctos[438] = "FGACBED"
207ctos[442] = "BEACD"
209ctos[448] = "AGBDEFHC"
210ctos[450] = "FGDBEAC"
211
212stoc = range(451)  # stein to cremona
213stoc[56] = "BA"
214stoc[77] = "ACB"
215stoc[84] = "BA"
216stoc[99] = "ACBD"
217stoc[102] = "ACB"
218stoc[106] = "BDCA"
219stoc[110] = "CBA"
220stoc[114] = "BCA"
221stoc[116] = "BCA"
223stoc[120] = "BA"
225stoc[123] = "BA"
226stoc[124] = "BA"
227stoc[126] = "BA"
229stoc[130] = "ACB"
230stoc[132] = "BA"
231stoc[136] = "BA"
232stoc[140] = "BA"
233stoc[141] = "DBCEA"
234stoc[142] = "BCEDA"
235stoc[144] = "BA"
236stoc[147] = "ACB"
237stoc[150] = "BCA"
239stoc[154] = "ACB"
240stoc[155] = "CBA"
241stoc[158] = "BDCEA"
242stoc[162] = "ACBD"
243stoc[168] = "BA"
244stoc[170] = "BEDAC"
245stoc[171] = "ACDB"
246stoc[174] = "EDACB"
247stoc[175] = "BCA"
248stoc[178] = "BA"
250stoc[184] = "BCDA"
251stoc[185] = "CAB"
252stoc[186] = "ACB"
253stoc[187] = "BA"
254stoc[189] = "ACDB"
255stoc[190] = "BAC"
256stoc[192] = "ACBD"
257stoc[195] = "DCAB"
258stoc[196] = "BA"
259stoc[198] = "DEACB"
260stoc[200] = "DACEB"
261stoc[201] = "CAB"
262stoc[203] = "CAB"
263stoc[205] = "BCA"
264stoc[208] = "BCDA"
265stoc[210] = "DBCAE"
266stoc[212] = "BA"
267stoc[214] = "CBDA"
268stoc[219] = "CAB"
269stoc[221] = "BA"
270stoc[222] = "CEDBA"
271stoc[234] = "CABDE"
272stoc[235] = "BCA"
273stoc[236] = "BA"
274stoc[238] = "BEDAC"
275stoc[240] = "CABD"
276stoc[242] = "BA"
277stoc[245] = "BCA"
278stoc[246] = "DGCFABE"
279stoc[249] = "BA"
280stoc[252] = "BA"
281stoc[254] = "CDBA"
282stoc[256] = "BACD"
283stoc[262] = "BA"
284stoc[264] = "BCDA"
285stoc[267] = "BA"
286stoc[270] = "DABC"
287stoc[272] = "ACDB"
288stoc[274] = "CBA"
289stoc[278] = "BA"
290stoc[285] = "BCA"
291stoc[286] = "CAEBDF"
293stoc[291] = "CBDA"
294stoc[294] = "EFDGABC"
295stoc[297] = "BDCA"
296stoc[298] = "BA"
298stoc[302] = "BAC"
299stoc[304] = "CDBEAF"
301stoc[312] = "BDAEFC"
302stoc[315] = "BA"
304stoc[320] = "BCDEAF"
306stoc[324] = "ABDC"
307stoc[325] = "BCEDA"
308stoc[326] = "ACB"
309stoc[330] = "AEDCB"
310stoc[333] = "BCDA"
311stoc[336] = "BCAEDF"
312stoc[338] = "AFDBCE"
313stoc[339] = "BAC"
314stoc[342] = "EFGCDAB"
315stoc[345] = "AECDBF"
317stoc[350] = "CAEDFB"
318stoc[352] = "CDAEBF"
319stoc[354] = "CDBAEF"
320stoc[360] = "CABED"
321stoc[364] = "BA"
322stoc[366] = "ECFDGAB"
324stoc[369] = "BA"
325stoc[370] = "ACBD"
327stoc[378] = "DBHFCGAE"
328stoc[380] = "BA"
329stoc[381] = "BA"
330stoc[384] = "DACEBFGH"
332stoc[390] = "AFGDEBC"
333stoc[392] = "CBDEAF"
334stoc[396] = "BAC"
335stoc[400] = "AHDFGBEC"
336stoc[402] = "ABDC"
337stoc[404] = "BA"
338stoc[405] = "BCFAED"
342stoc[414] = "CDAB"
343stoc[418] = "BAC"
344stoc[423] = "GDBEACF"
345stoc[425] = "ACDB"
346stoc[426] = "BCA"
347stoc[427] = "CAB"
348stoc[432] = "DCGHAEBF"
349stoc[434] = "ACEDB"
350stoc[435] = "BACD"
351stoc[437] = "BA"
352stoc[438] = "CEDGFAB"
353stoc[440] = "ACDB"
354stoc[441] = "BAECDF"
357stoc[448] = "ACHDEFBG"
358stoc[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
363def 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
372def 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
382def 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