-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_multiples_plots.py
184 lines (165 loc) · 5.91 KB
/
make_multiples_plots.py
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
# -*- coding: utf-8 -*-
"""
creates all data for the examples related to the
fractional part of multiples of a given number number
"""
import math
import time
import matplotlib.lines
import matplotlib
import matplotlib.patches
import matplotlib.pyplot as plt
import common_functions as cf
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.preamble'] = \
r'\newcommand{\fracpart}[1]{\left\{ #1 \right\}}'
def fractional_part(in_value):
"""
return the value of the fractional part of the input
"""
return in_value-math.floor(in_value)
def create_single_plot(**kwargs):
"""
creates a pdf file illustrating the n-th term of a sequence
fractional_part(n*kwargs["in_value"]) for n being a natural number
"""
def plot_tick(in_pos, in_y_size, **kwargs):
plt.plot([in_pos, in_pos], [-in_y_size, in_y_size], **kwargs)
def place_label(x_pos, y_pos, in_str):
y_alignment = 'top'
if y_pos > 0:
y_alignment = 'bottom'
matplotlib.pyplot.text(
x_pos, y_pos, in_str,
horizontalalignment='center', verticalalignment=y_alignment)
cur_fig = plt.figure(figsize=(6.3, 0.6))
plt.axis('off')
main_color = [0, 0, 0]
short_tick_size = 1
long_tick_size = 2*short_tick_size
label_gap = 0.5*short_tick_size
plt.plot([0, 1], [0, 0], color=main_color)
for _ in range(1, kwargs['in_plot_num']):
plot_tick(
fractional_part(_*kwargs['in_value']), short_tick_size,
color=main_color,
linewidth=0.5)
for _ in [0, 1]:
plot_tick(_, short_tick_size, color=main_color)
place_label(_, -short_tick_size-label_gap, str(_))
title_color = [1, 1, 1]
cur_value = fractional_part(kwargs['in_value']*kwargs['in_plot_num'])
cur_value_str = kwargs['value_to_str_fun'](cur_value)
if kwargs['in_plot_num'] > 0:
title_color = main_color
plot_tick(cur_value, short_tick_size, color=[1, 0, 0])
place_label(
cur_value, long_tick_size+label_gap,
f'${cur_value_str}$')
fracpart_arg_str = kwargs['in_value_str']
if kwargs['in_plot_num'] != 1:
fracpart_arg_str = \
f'{kwargs["in_plot_num"]}\\cdot{kwargs["in_value_str"]}'
plt.title(
f'$$\\fracpart{{{fracpart_arg_str}}} = {cur_value_str}$$',
color=title_color)
d_x_lim = 0.08
plt.xlim([-d_x_lim, 1+d_x_lim])
plt.ylim([-long_tick_size, 3*long_tick_size])
output_folder = kwargs['output_folder']/kwargs['core_name']
output_folder.mkdir(parents=True, exist_ok=True)
plt.savefig(
output_folder/f'{kwargs["core_name"]}{kwargs["in_plot_num"]}.pdf',
bbox_inches='tight', pad_inches=0.01)
cur_fig.clf()
plt.close(cur_fig)
plt.close()
def generate_rational_data():
"""
generates all data for the "rational" example
"""
numer_val = 5
denom_val = 7
assert math.gcd(numer_val, denom_val) == 1
value_str = cf.to_latex_fraction(numer_val/denom_val, denom_val)
output_folder = cf.get_config_parameter('tmpDataFolder')
output_file_name = \
cf.get_config_parameter('fracPartsOfRationalMultiplesTex')
core_name = cf.to_core_name(output_file_name)
frame_num_limit = denom_val+3
for _ in range(frame_num_limit):
create_single_plot(
in_value=numer_val/denom_val,
in_value_str=value_str,
in_plot_num=_,
value_to_str_fun=lambda x: cf.to_latex_fraction(x, denom_val),
output_folder=output_folder,
core_name=core_name)
tex_str = \
'\\begin{frame}\n' \
' \\begin{example}\n' \
' Rozważmy ciąg ' \
f'${{\\paren{{\\fracpart{{n\\cdot {value_str}}}}}}}_{{n \\in \\N}}$.' \
'\n' \
' \\begin{center}\n' \
' \\begin{overprint}\n'
for _ in range(frame_num_limit):
cur_str = \
f' \\onslide<{_+1}>' \
r'\centerline{\includegraphics[width=\textwidth]{' \
f'{core_name}/{core_name}{_}.pdf' \
'}}\n'
tex_str += cur_str
tex_str += \
' \\end{overprint}\n' \
' \\end{center}\n' \
' \\end{example}\n' \
'\\end{frame}\n'
with open(
output_folder/output_file_name, 'w', encoding='utf-8') as tex_file:
tex_file.write(tex_str)
def to_approx_latex_str(in_val, decimal_places=5):
"""
returns the latex string representation of the input
"""
res = f'{{:.{decimal_places}f}}'.format(in_val).replace('.', '{,}')
return res+r'\ldots{}'
def generate_irrational_data():
"""
generates all data for the "irrational" animation
"""
core_value = math.pi
value_set = set()
value_str_set = set()
frame_num_limit = 600
output_file_name = \
cf.get_config_parameter('fracPartsOfIrrationalMultiplesTex')
core_name = cf.to_core_name(output_file_name)
frame_rate = 3
output_folder = cf.get_config_parameter('tmpDataFolder')
for _ in range(0, frame_num_limit):
cur_val = fractional_part(_*core_value)
assert cur_val not in value_set
value_set.add(cur_val)
cur_str = to_approx_latex_str(cur_val)
assert cur_str not in value_str_set
value_str_set.add(cur_str)
create_single_plot(
in_value=core_value,
in_value_str=r'\pi',
in_plot_num=_,
value_to_str_fun=to_approx_latex_str,
output_folder=output_folder,
core_name=core_name)
tex_str = \
r'\animategraphics[autoplay, loop, width=\textwidth]' \
f'{{{frame_rate}}}' \
f'{{./{core_name}/{core_name}}}' \
f'{{0}}{{{frame_num_limit-1}}}'
with open(
output_folder/output_file_name, 'w', encoding='utf-8') as tex_file:
tex_file.write(tex_str)
ST_TIME = time.time()
generate_rational_data()
generate_irrational_data()
print(f'runtime {time.time()-ST_TIME} [s]')