-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.sage
317 lines (298 loc) · 11 KB
/
code.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# coding=utf-8
r"""
Code for MCF Algorithms Cheat Sheets
"""
import slabbe.mult_cont_frac as mcf
from slabbe import TikzPicture
import numpy as np
from sage.functions.other import floor
###########
# detect draft or final
###########
with open('_version.txt', 'r') as f:
VERSION = f.readline().strip()
if VERSION not in ('draft', 'arxiv', 'arxiv_hd'):
raise ValueError("should be draft or arxiv or "
"arxiv_hd not (={})".format(VERSION))
print "Using parameters for VERSION={}".format(VERSION)
###########
# Global function
###########
@parallel
def algo_to_tex(algo, cylinders_depth=[1,2,3]):
lines = []
lines.append(r"\section{%s algorithm}" % algo.name())
lines.append(r"\subsection{Definition}")
lines.append(r"\input{def_%s.tex}" % algo.class_name())
lines.append(r"\subsection{Matrices}")
lines.append(matrices(algo, ncols=3))
lines.append(r"\subsection{Cylinders}")
if VERSION == 'draft':
cylinders_depth.pop()
for d in cylinders_depth:
lines.append(include_graphics_cylinders(algo,d,width=.3))
lines.append(r"\subsection{Density function}")
lines.append(input_density(algo))
lines.append(r"\subsection{Invariant measure}")
if VERSION == 'arxiv':
ext = 'png'
dpi = 30
else:
ext = 'pdf'
dpi = 100 # I think dpi is ignored when ext is 'pdf'
lines.append(include_graphics_inv_measure(algo, n_iterations=10^6,
ndivs=30, width=.8, ext=ext, dpi=dpi))
lines.append(r"\subsection{Natural extension}")
if VERSION == 'arxiv':
ext = 'png'
density = 60
else:
ext = 'pdf'
density = 100 # I think density is ignored when ext is 'pdf'
lines.append(include_graphics_nat_ext(algo, n_iterations=1000,
marksize=1, width=1, ext=ext, density=density))
#lines.append(include_graphics_nat_ext_PIL(algo))
lines.append(r"\subsection{Lyapunov exponents}")
if VERSION == 'draft':
n_iterations=10^6
else:
n_iterations=10^8
lines.append(lyapunov_array(algo, ntimes=30, n_iterations=n_iterations))
lines.append(r"\subsection{Substitutions}")
lines.append(substitutions(algo, ncols=3))
lines.append(r"\subsection{$S$-adic word example}")
lines.append(s_adic_word(algo, nsubs=10))
lines.append(r"\subsection{Discrepancy}")
if VERSION == 'draft':
length=20
fontsize=30
bins=10
else:
length=200
fontsize=20
bins=20
lines.append(discrepancy_histogram(algo, length=length, width=.6,
fontsize=fontsize,figsize=[8,3],bins=bins))
lines.append(r"\subsection{Dual substitutions}")
lines.append(dual_substitutions(algo, ncols=3))
lines.append(r"\subsection{E one star}")
lines.append(dual_patch(algo, (1,e,pi), minsize=70, nsubs=5,
height="3cm"))
lines.append(r"\newpage")
file_tex = 'section_{}.tex'.format(algo.class_name())
write_to_file(file_tex, "\n".join(lines))
with open('sections.tex', 'a') as f:
f.write(r"\input{{{}}}".format(file_tex)+'\n')
print "Done with algo {}".format(algo.class_name())
###########
# Functions
###########
def input_density(algo):
import os
filename = "densityf_{}.tex".format(algo.class_name())
if os.path.exists(filename):
return r"\input{{{}}}".format(filename)
else:
return "Unknown"
def include_graphics_inv_measure(algo, n_iterations=10^6, ndivs=40,
width=1, ext='pdf', dpi=50):
try:
fig = algo.invariant_measure_wireframe_plot(n_iterations,
ndivs, norm='1')
except Exception as err:
return "{}: {}".format(err.__class__.__name__, err)
file = 'density_{}.{}'.format(algo.class_name(), ext)
if ext == 'png':
fig.savefig(file, dpi=dpi)
elif ext == 'pdf':
fig.savefig(file)
print "Creation of the file {}".format(file)
return r"\includegraphics[width={}\linewidth]{{{}}}".format(width, file)
def include_graphics_nat_ext(algo, n_iterations, marksize, width=1,
ext='png', density=100):
try:
t = algo.natural_extension_tikz(n_iterations, marksize=marksize,
group_size="2 by 2")
except Exception as err:
return "{}: {}".format(err.__class__.__name__, err)
file = 'nat_ext_{}.{}'.format(algo.class_name(), ext)
if ext == 'png':
print t.png(file, density=density)
elif ext == 'pdf':
print t.pdf(file)
else:
raise ValueError('Unknown extention ext(={})'.format(ext))
lines = []
lines.append(r"\input{nat_ext_def.tex}")
lines.append(r"\includegraphics[width={}"
"\linewidth]{{{}}}".format(width, file))
return '\n'.join(lines)
def include_graphics_cylinders(algo, n, width=.3):
try:
cocycle = algo.matrix_cocycle()
except Exception as err:
return "{}: {}".format(err.__class__.__name__, err)
s = cocycle.tikz_n_cylinders(n, scale=3)
file = 'cylinders_{}_n{}.pdf'.format(algo.class_name(), n)
print s.pdf(file)
return r"\includegraphics[width={}\linewidth]{{{}}}".format(width, file)
def include_graphics_nat_ext_PIL(algo, width=1):
c = {}
c[1] = c[2] = c[3] = [0,0,0]
c[12] = c[13] = c[23] = c[21] = c[31] = c[32] = [255,0,0]
b = [1,2,3,12,13,21,23,31,32]
draw = 'image_right'
n_iterations = 10^3
P = algo.natural_extension_part_png(n_iterations, draw=draw,
branch_order=b, color_dict=c, urange=(-.6,.6), vrange=(-.6,.6))
file = 'nat_ext_{}_{}.png'.format(algo.class_name(), draw)
P.save(file)
print "Creation of the file {}".format(file)
return r"\includegraphics[width={}\linewidth]{{{}}}".format(width, file)
def lyapunov_array(algo, ntimes, n_iterations):
from slabbe.lyapunov import lyapunov_table
try:
T = lyapunov_table(algo, ntimes, n_iterations)
except Exception as err:
return "{}: {}".format(err.__class__.__name__, err)
lines = []
lines.append(r"(using {} orbits of ".format(ntimes))
lines.append(r"{} iterations each)\\".format(n_iterations))
lines.append(latex(T))
return "\n".join(lines)
def substitutions(algo, ncols=3):
try:
D = algo.substitutions()
except Exception as err:
return "{}: {}".format(err.__class__.__name__, err)
return dict_to_array(D, ncols, entry_code=r"\sigma_{{{}}}=\left\{{{}\right.")
def dual_substitutions(algo, ncols=3):
try:
D = algo.dual_substitutions()
except Exception as err:
return "{}: {}".format(err.__class__.__name__, err)
return dict_to_array(D, ncols, entry_code=r"\sigma^*_{{{}}}=\left\{{{}\right.")
def matrices(algo, ncols=3):
try:
cocycle = algo.matrix_cocycle()
except Exception as err:
return "{}: {}".format(err.__class__.__name__, err)
D = cocycle.gens()
entry_code = r"M_{{{}}}={{\arraycolsep=2pt{}}}"
return dict_to_array(D, ncols, entry_code=entry_code)
def dict_to_array(D, ncols=3, entry_code=r"M({})={}"):
lines = []
lines.append(r"\[")
lines.append(r"\begin{array}{%s}" % ('l'*ncols))
for i,key in enumerate(sorted(D.keys())):
v = D[key]
lines.append(entry_code.format(key,latex(v)))
if i % ncols == ncols-1:
lines.append(r"\\")
else:
lines.append(r"&")
lines.append(r"\end{array}")
lines.append(r"\]")
return '\n'.join(lines)
def s_adic_word(algo, nsubs=5, k=21):
start = (1,e,pi)
it = algo.coding_iterator(start)
try:
w = algo.s_adic_word(start)
except Exception as err:
return "{}: {}".format(err.__class__.__name__, err)
lines = []
lines.append(r"Using vector $v={}$:".format(latex(start)))
lines.append(r"\begin{align*}")
lines.append(r"w &=")
for _ in range(nsubs):
key = next(it)
lines.append(r"\sigma_{{{}}}".format(key))
lines.append(r"\cdots(1)\\")
lines.append(r"& = {}".format(w))
lines.append(r"\end{align*}")
C = map(w[:10000].number_of_factors, range(k))
lines.append(r"Factor Complexity of $w$ is ")
lines.append(r"$(p_w(n))_{{0\leq n \leq {}}} =$".format(k-1))
lines.append(r"\[")
lines.append(r"({})".format(', '.join(map(str,C))))
lines.append(r"\]")
return '\n'.join(lines)
def dual_patch(algo, v, minsize=100, nsubs=5, height="3cm"):
try:
n = 2
P = algo.e_one_star_patch(v, n)
while len(P) < minsize:
n += 1
P = algo.e_one_star_patch(v, n)
except Exception as err:
return "{}: {}".format(err.__class__.__name__, err)
it = algo.coding_iterator(v)
s = P.plot_tikz()
file = 'dual_patch_{}.pdf'.format(algo.class_name())
print TikzPicture(s).pdf(file)
lines = []
lines.append(r"Using vector $v={}$, the {}-th ".format(latex(v),n))
lines.append(r"iteration on the unit cube is:")
lines.append(r"\[")
for _ in range(nsubs):
key = next(it)
lines.append(r"E_1^*(\sigma^*_{{{}}})".format(key))
if nsubs < n:
lines.append(r"\cdots")
lines.append(r"(\includegraphics[width=1em]{cube.pdf})=")
lines.append(r"\]")
lines.append(r"\begin{center}")
lines.append(r"\includegraphics[height={}]{{{}}}".format(height, file))
lines.append(r"\end{center}")
return '\n'.join(lines)
def unit_cube():
from sage.combinat.e_one_star import E1Star, Patch, Face
cube = Patch([Face((1,0,0),1), Face((0,1,0),2), Face((0,0,1),3)])
s = cube.plot_tikz()
print TikzPicture(s).pdf('cube.pdf')
def discrepancy_histogram(algo, length, width=.6, fontsize=30,
figsize=[8,3], bins=10):
try:
D = algo.discrepancy_statistics(length)
except Exception as err:
return "{}: {}".format(err.__class__.__name__, err)
H = histogram(D.values(), bins=bins)
file = 'discrepancy_histo_{}.png'.format(algo.class_name())
H.save(file, fontsize=fontsize, figsize=figsize)
print "Creation of the file {}".format(file)
lines = []
lines.append(r"Discrepancy \cite{{MR593979}} for all {}".format(len(D)))
lines.append(" $S$-adic words with directions")
lines.append("$v\in\mathbb{N}^3_{>0}$")
lines.append(r"such that $v_1+v_2+v_3={}$:".format(length))
lines.append(r"\begin{center}")
lines.append(r"\includegraphics[width={}\linewidth]{{{}}}".format(width, file))
lines.append(r"\end{center}")
return '\n'.join(lines)
###################
# Utility functions
###################
def write_to_file(filename, s):
with open(filename, 'w') as f:
f.write(s)
print "Creation of the file {}".format(filename)
###################
# Script
###################
is_script = True
if is_script:
with open('sections.tex','w') as f:
# erase this file
pass
unit_cube()
L = [(mcf.Brun(), [1,2,3,4,5,6]),
(mcf.Poincare(), [1,2,3,4]),
(mcf.Selmer(), [1,2,3,4,5,6]),
(mcf.FullySubtractive(), [1,2,3,4,5,6]),
(mcf.ARP(), [1,2,3]),
(mcf.Reverse(), [1,2,3,4,5]),
(mcf.Cassaigne(), [1,2,3,4,5,6,7,8,9]),
#(mcf.ArnouxRauzy(), [1,2,3,4,5,6])
]
list(algo_to_tex(L))