-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawspec.sage
190 lines (149 loc) · 5.53 KB
/
drawspec.sage
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
from tikz import *
class NumberFieldSpec():
def __init__(self, F, nprimes=10):
self._number_field = F
self._fat_ramify = 1
self._draw_random_line = False
self._nprimes = nprimes
self._color_classes = False
self._curve = False
self._pic = self._draw_spec()
def _draw_spec(self):
return draw_spec(F=self._number_field,
npoints=self._nprimes,
fat_factor=self._fat_ramify,
draw_random_line=self._draw_random_line,
color_classes=self._color_classes,
curve=self._curve)
def code(self):
return self._pic.code()
def pic(self):
return self._pic
def set_nprimes(self, nprimes):
self._nprimes = nprimes
self._pic = self._draw_spec()
def fat_ramify(self, fat_factor):
self._fat_ramify = fat_factor
self._pic = self._draw_spec()
def write_image(self, filename):
return self._pic.write_image(filename)
def color_classes(self, hlp):
self._color_classes = hlp
self._pic = self._draw_spec()
def demo(self):
return self.pic().demo()
def curve(self, flag):
self._curve = flag
self._pic = self._draw_spec()
def draw_spec(F,
npoints,
fat_factor=1,
draw_random_line=False,
color_classes=False,
curve=False):
prime_list = primes_first_n(npoints)
# c = F.degree() / 2 + 1
c = 2.5
radius_factor = 1 / F.degree()
d = F.degree()
r = radius_factor * d
pic = Picture()
prime_id_list = [F.ideal(p).factor() for p in prime_list]
coord_list = [
fp_coords(fplist, n, c, r) for n, fplist in enumerate(prime_id_list)
]
if color_classes:
G = F.class_group()
colors = []
for i in range(G.order()):
colors.append(generate_new_color(colors, pastel_factor=0.6))
color_dict = dict(zip(list(G), colors))
color_dict[G.identity()] = [.3, .3, .3]
for n, fplist in enumerate(prime_id_list):
pts = coord_list[n]
for i, (fp, mult) in enumerate(fplist):
if color_classes:
rgb = color_dict[G(fp)]
# print("rgb = ", rgb)
color = f"rgb,1:red,{rgb[0]};green,{rgb[1]};blue,{rgb[2]}"
color = "{" + color + "}"
else:
color = "black"
pic.filldraw(pts[i],
circle(0.05 + fat_factor * (mult - 1) / 100),
fill=color)
if draw_random_line:
# this is probably unnecessary since curve exists
rand_current = randrange(len(coord_list[0]))
rand_pt = coord_list[0][rand_current]
for n in range(1, len(coord_list)):
rand_next = randrange(len(coord_list[n]))
if prime_id_list[n - 1][rand_current][1] > 1:
outangle = -90 * sign(len(coord_list[n]) - 2.1 * rand_next)
else:
outangle = 0
if prime_id_list[n][rand_next][1] > 1:
inangle = 90 * sign(len(coord_list[n]) - 2.1 * rand_next)
else:
inangle = 180
pic.draw(
line([
coord_list[n - 1][rand_current], coord_list[n][rand_next]
],
op=f'to[out={outangle},in={inangle}]'))
rand_current = rand_next
if curve:
for n in range(npoints):
for coord_this in coord_list[n]:
for coord_next in (coord_list + [[(npoints, c)]])[n + 1]:
pic.draw(
line([coord_this, coord_next],
op=f'to[out={0},in={180}]'))
pic.draw(line([(npoints, c), (npoints + 1, c)]), dashed=True)
# draw generic points:
pic.draw((npoints + 2, c), node("$(0)$"))
# draw spec Z
pic.filldraw(line([(0, 0), (npoints, 0)]))
pic.draw(line([(npoints, 0), (npoints + 1, 0)]), dashed=True)
pic.draw((npoints + 2, 0), node("$(0)$"))
for n, p in enumerate(prime_list):
pt = (n, 0)
pic.filldraw(pt, circle(0.05))
pic.draw(pt, node(f"$({p})$"), below=True)
# for n in range(npoints):
# if len(points[n]) ==
# pic.filldraw(line())
return pic
def fp_coords(fplist, x_coord, c, r):
N = len(fplist)
if N == 1:
return [(x_coord, c)]
else:
return [(x_coord, c - r + 2 * r * i / (N - 1)) for i in range(N)]
def test_draw():
npoints = 20
K = NumberField(x ^ 3 + 15, "a")
print("class number:", K.class_number())
# K = QuadraticField(2)
draw_spec(K, npoints, color_classes=True).write_image("test.pdf")
return 0
# from https://gist.github.com/adewes/5884820:
import random
def get_random_color(pastel_factor=0.5):
return [(x + pastel_factor) / (1.0 + pastel_factor)
for x in [random.uniform(0, 1.0) for i in [1, 2, 3]]]
def color_distance(c1, c2):
return sum([abs(x[0] - x[1]) for x in zip(c1, c2)])
def generate_new_color(existing_colors, pastel_factor=0.5):
max_distance = None
best_color = None
for i in range(0, 100):
color = get_random_color(pastel_factor=pastel_factor)
if not existing_colors:
return color
best_distance = min(
[color_distance(color, c) for c in existing_colors])
if not max_distance or best_distance > max_distance:
max_distance = best_distance
best_color = color
return best_color