CoCalc Shared Fileswww / cgi-bin / mfd / mfe_known.py
Author: William A. Stein
1import constants, misc, os, pg, string, math
2
3# maximum ranges for drawing pictures.
4wt_max_range = 20
5lv_max_range = 100
6
7weight_range = 7;
8level_range = 10;
9
10db = pg.DB("modular")
11
12class TempFile:
13    def __init__(self, _fname=""):
14        if _fname=="":
15            _fname = misc.newtempfilename()
16        self.fname = _fname
17
18    def __repr__(self):
19        return "A temporary file named %s."%self.fname
20
21    #def __del__(self):
22    #os.system('rm "%s"'%self.fname)
23
24    def basename(self):
25        return self.fname[string.rfind(self.fname,"/")+1:]
26
27
28class KnownData:
29    def __init__(self, _level=1, _weight=2, _table=""):
30        if _level <= level_range:
31            _level = level_range+1;
32        if _weight <= weight_range:
33            _weight = weight_range+1
34        _level=int(_level)
35        _weight=int(_weight)
36        self.level = _level
37        self.weight = _weight
38        self.level_max  = _level + level_range
39        self.level_min  = _level - level_range
40        self.weight_max = _weight + weight_range
41        self.weight_min = _weight - weight_range
42        self.table      = _table
43
44    def __repr__(self):
45        return "A known query of %s with %s<=N<=%s and %s<=k<=%s."%\
46               (self.table, self.level_min, self.level_max, \
47                self.weight_min, self.weight_max)
48
49
50    def list(self):   # returns empty list if table doesn't exist
51        if not (self.table in db.get_tables()):
52            return []
53        if self.table == "":
54            query = "SELECT DISTINCT level, weight FROM mod WHERE " + \
55                    "%s <= level  AND %s >= level AND "%(self.level_min, self.level_max) +\
56                    "%s<= weight AND %s >= weight "%(self.weight_min, self.weight_max) +\
57                    "ORDER BY level, weight";
58        else:
59            query = "SELECT DISTINCT level, weight FROM mod, %s WHERE "%self.table + \
60                    "%s <= level  AND %s >= level AND "%(self.level_min, self.level_max) +\
61                    "%s<= weight AND %s >= weight "%(self.weight_min, self.weight_max) +\
62                    "AND mod_id=id " +\
63                    "ORDER BY level, weight";
64        x = db.query(query).dictresult()
65        return x
66
67    def latex(self):
68        if self.weight_max > self.weight_min + wt_max_range:
69            self.weight_max = self.weight_min + wt_max_range
70        if self.level_max > self.level_min + lv_max_range:
71            self.level_max = self.level_min + lv_max_range
72        ans = TempFile()
73        file = open(ans.fname,"w")
74        file.write("\\documentclass{article}\n\\pagestyle{empty}\n\\usepackage{fullpage}"
75                   "\n\\usepackage{pstricks}\n\\begin{document}\n\\noindent\n")
76        unit=int(500/max(self.level_max-self.level_min,self.weight_max-self.weight_min))
77        file.write("\\psset{unit=%spt}\n"%unit)
78        x_max = self.level_max - self.level_min
79        y_max = self.weight_max - self.weight_min
80        file.write("\\pspicture(0,0)(%s,%s)\n"%(x_max, y_max))
81        file.write("\\psset{gridcolor=lightgray}\n\\psset{gridlabels=0pt}\n"
82                   "\\psset{subgriddiv=1}\\psgrid\n")
83
85        if max(x_max, y_max) > 100:
87        x_ticks = max(1,min(15, x_max/2))
88        for i in range(0,x_ticks+1):
89            x = int(i*(x_max / x_ticks))
90            file.write("\\rput(%s,%s){\\tiny $%s$}\n"% (x,-pad,self.level_min+x))
91        y_ticks = max(1,min(15, y_max/2))
92        for i in range(0,y_ticks+1):
93            y = int(i*(y_max / y_ticks))
94            file.write("\\rput(%s,%s){\\tiny $%s$}\n"% (-pad,y,self.weight_min+y))
95
96        list = self.list()
97        dot_size = 0.3
98        file.write("\\newrgbcolor{crimson}{0.7 0.1 0.3}\n")
99        for x in list:
100            file.write("\\pscircle*[linecolor=crimson](%s,%s){%s}\n"%(x['level']-self.level_min,x['weight']-self.weight_min,dot_size))
101
102        file.write("\\endpspicture\\end{document}")
103        file.close()
104        return ans
105
106    def known_array(self):
107        weights = self.weight_max - self.weight_min + 1
108        levels = self.level_max - self.level_min + 1
109        m = [[0 for i in range(levels)] for i in range(weights)]
110        for x in self.list():
111            i = x['weight']-self.weight_min
112            j = x['level']-self.level_min
113            #query = " SELECT * FROM mod, %s WHERE "%self.table + \
114            #        " level=%s AND weight=%s"%(x['level'], x['weight']) +\
115            #        " AND mod_id=id";
116            #Number_Known = len(db.query(query).dictresult())
117            Number_Known = 1
118            m[i][j] = Number_Known
119        return m
120
121
122    def level_weight_table(self):
123        m = self.known_array()
124        s=""
125
126        #s=s+"<script language=javascript>\n function display_modular_forms_space(level, weight){"+\
127         #  'url = "%s/mfe_full_html.py?level=" + level + "&weight=" + weight;\n'%(constants.CGIROOT)+\
128          # 'winpops=window.open(url, "","width=600,height=500,toolbar, scrollbars,resizable,")}\n'
129        #s=s+"</script>"
130
131
132        s=s+"<table border=0 cellspacing=1 cellpadding=0 bgcolor=lightgrey>"
133
134        weights=range(self.weight_max - self.weight_min + 1)
135        weights.reverse()
136        max_known = max(1,max([max(m[r]) for r in weights]))
137        COLOR = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
138        for r in weights:
139            s=s+'<tr><td align=center bgcolor="white">%s&nbsp</td>'%\
140               (r+self.weight_min)
141            for c in range(self.level_max - self.level_min + 1):
142                m[r][c] = 15 - int(math.ceil(15*m[r][c]/max_known))  # integer between 0 and 15.
143                weight  = self.weight_min + r
144                level = self.level_min  + c
145                col = COLOR[m[r][c]]
146                #color = "#%s%s%s%s%s%s"%(col,col,col,col,col,col)
147                color = "#%s%s%s%s%s%s"%(col,col,col,col,'F','F')
148                url = constants.CGIROOT + "/mfe_full_html.py?level=" + str(level) + \
149                      "&weight=" + str(weight)
150                #target='target="_blank"'
151                target=""
152                s=s+'<td bgcolor=%s><a href="%s" %s>'%(color,url,target)+ \
153                   '<tt>&nbsp&nbsp&nbsp</tt></a></td>'# <a href="%s"></a></td>'%url2
154            # end for
155            s=s+"</tr>\n<tr>"
156
157        s=s+"<td>&nbsp</td>"
159            for i in range(len(x)-4):
160                x=x+"&nbsp"
161            return x
162        for c in range(self.level_max - self.level_min + 1):
163            s=s+'<td align=center bgcolor="white">%s</td>'%\
164               (self.level_min+c)
165        s=s+"</tr></table>"
166        return "<table border=5 bgcolor=white cellpadding=10><tr><td>" + s + "</td></tr></table>"
167
168
169    def html_table(self):
170        urlleft = constants.CGIROOT + "/mfe_known_html.py?level_weight=%s,%s&table=%s"%\
171                  (self.level-2*level_range+1,self.weight,self.table)
172        urlright= constants.CGIROOT + "/mfe_known_html.py?level_weight=%s,%s&table=%s"%\
173                  (self.level+2*level_range-1,self.weight,self.table)
174        urlup= constants.CGIROOT + "/mfe_known_html.py?level_weight=%s,%s&table=%s"%\
175                  (self.level, self.weight+2*weight_range-1,self.table)
176        urldown= constants.CGIROOT + "/mfe_known_html.py?level_weight=%s,%s&table=%s"%\
177                  (self.level, self.weight-2*weight_range+1,self.table)
178        s = "<table>"+\
179            "<tr>"+\
180            "<td><a href='%s'>/\</a><br><img align=center src='%s/weight.gif'><br><a href='%s'>\/</a></td><td>"%(urlup,constants.IMAGES,urldown) + \
181            self.level_weight_table() + \
182            "</td></tr><tr><td>&nbsp</td><td align=center>"+\
183            "<a href='%s'><</a>&nbsp &nbsp&nbsp<img align=top src='%s/level.gif'>&nbsp&nbsp<a href='%s'>></a></td></table>"%(urlleft,constants.IMAGES,urlright)
184        return s
185