-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_readme.py
57 lines (39 loc) · 2.02 KB
/
generate_readme.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
#! /bin/python
import re
from sympy import exp, ln, sin, cos
from sympy.abc import x
from chebyshev.approximation import get_best_approximation
table_of_contents = []
function_entries = []
def generate_for_readme(approximation):
function_filename = re.sub(r'( |/)', '_', str(approximation.function))
approximation_plot_filename = f'images/{function_filename}_approximation.png'
absolute_error_plot_filename = f'images/{function_filename}_absolute_error.png'
table_of_contents.append((str(approximation.function),
re.sub(r'(\+|/|\(|\))', '', str(approximation.function))
.replace(' ', '-')))
function_entries.append(f"""## `{approximation.function}`
Coefficients for `{approximation.function}` on the `[{approximation.interval[0]}, {approximation.interval[
1]}]` interval:
{approximation.get_coeffs_as_table()}
Maximum error on that interval is `{approximation.get_error()}`
<img src="{approximation_plot_filename}" alt="{approximation_plot_filename}" width="50%"><img src="{absolute_error_plot_filename}" alt="{absolute_error_plot_filename}" width="50%">
""")
plotted = approximation.plot_approximation(show=False)
backend = plotted.backend(plotted)
backend.process_series()
backend.fig.savefig(approximation_plot_filename, dpi=300)
plotted = approximation.plot_absolute_error(show=False)
backend = plotted.backend(plotted)
backend.process_series()
backend.fig.savefig(absolute_error_plot_filename, dpi=300)
generate_for_readme(get_best_approximation(exp(x), (0, 1), 6, 7, point=0.5))
generate_for_readme(get_best_approximation(ln(1 + x), (0, 1), 6, 20))
generate_for_readme(get_best_approximation(sin(x) / x, (-1, 1), 8, 18))
generate_for_readme(get_best_approximation(cos(x), (-1, 1), 8, 18))
print('## Approximated Functions\n')
for (function_name, function_link) in table_of_contents:
print(f'* [`{function_name}`](#{function_link})')
print('\n')
for function_entry in function_entries:
print(function_entry)