-
Notifications
You must be signed in to change notification settings - Fork 4
/
visualization.py
129 lines (108 loc) · 5.27 KB
/
visualization.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
import matplotlib.pyplot as plt
import numpy as np
from comparison import compare_approximation_interpolation
plt.style.use("ggplot")
def visualization(fx, px, xn, history, interval, n, compare_interpolation=True):
x, xn, Interp_x, Approx_err_xn, Approx_err_interval, Interp_err_xn, Interp_err_interval = (
compare_approximation_interpolation(fx, px, xn, interval, n)
)
plt.figure(figsize=(14, 5))
plt.scatter(xn, Approx_err_xn, linewidth=1.5, label="Remez Approximation Points")
plt.plot(x, Approx_err_interval, linewidth=3, label="Remez Approximation Error")
if compare_interpolation:
plt.scatter(Interp_x, Interp_err_xn, linewidth=1.5, label="Lagrange Interpolation Points")
plt.plot(x, Interp_err_interval, linewidth=1.5, label="Lagrange Interpolation Error")
plt.xlabel("X Interval")
plt.ylabel("Error")
plt.title(f"Error on Interval {interval}, degree = {n}\nf(x)= {fx}")
plt.legend(loc="center", bbox_to_anchor=(0.5, -0.25), ncol=4)
plt.tight_layout()
plt.savefig(f"./images/single_plot/{fx}_{interval}_{n}.png", dpi=300)
plt.show()
print(f"f(x) = {fx}, interval = {interval}\n")
print(f"Polynomial degree = {n}")
print(f"Pn(x):\n{px}\n")
print(f"xn points:\n{xn}\n")
print(f"Converge iteration: {len(history['e'])}")
print(f"MAE of approximation: {max(np.abs(Approx_err_interval))}")
print(f"MAE of interpolation: {max(np.abs(Interp_err_interval))}")
def visualization_pipeline(fx, px, xn, history, interval, n, history_error=None):
"""
Visualization for plenty of functions, auto-pipeline, including a 'whether continue' to decide whether to plot.
"""
x, xn, Interp_x, Approx_err_xn, Approx_err_interval, Interp_err_xn, Interp_err_interval = (
compare_approximation_interpolation(fx, px, xn, interval, n)
)
# whether continue to plot
max_interval_error = np.max(Approx_err_interval)
thershold = np.array([history_error, max(Approx_err_xn), min(Approx_err_interval)])
new = np.array([max_interval_error, max_interval_error, min(Approx_err_xn)])
whether_continue = (new <= thershold).all()
if whether_continue:
plt.figure(figsize=(14, 5))
plt.scatter(xn, Approx_err_xn, linewidth=1.5, label="Remez Approximation Points")
plt.plot(x, Approx_err_interval, linewidth=3, label="Remez Approximation Error")
plt.scatter(Interp_x, Interp_err_xn, linewidth=1.5, label="Lagrange Interpolation Points")
plt.plot(x, Interp_err_interval, linewidth=1.5, label="Lagrange Interpolation Error")
plt.xlabel("X Interval")
plt.ylabel("Error")
plt.title(f"Error on Interval {interval}, degree = {n}\nf(x)= {fx}")
plt.legend(loc="center", bbox_to_anchor=(0.5, -0.25), ncol=4)
plt.tight_layout()
plt.savefig(f"./images/pipeline_plot/{fx}_{interval}_{n}.png", dpi=300)
plt.show()
print(f"f(x) = {fx}, interval = {interval}\n")
print(f"Polynomial degree = {n}")
print(f"Pn(x):\n{px}\n")
print(f"xn points:\n{xn}\n")
print(f"Converge iteration: {len(history['e'])}")
print(f"MAE of approximation: {max(np.abs(Approx_err_interval))}")
print(f"MAE of interpolation: {max(np.abs(Interp_err_interval))}")
return max_interval_error if whether_continue else None
def visualization_px_with_fx(fx, px, xn, history, interval, n):
def f(x):
return eval(fx)
x, xn, Interp_x, Approx_err_xn, Approx_err_interval, Interp_err_xn, Interp_err_interval = (
compare_approximation_interpolation(fx, px, xn, interval, n)
)
_, ax = plt.subplots(1, 2, figsize=(14, 5))
ax = ax.flatten()
ax[0].scatter(xn, Approx_err_xn, linewidth=1.5, label="Remez Approximation Points")
ax[0].plot(x, Approx_err_interval, linewidth=1.5, label="Remez Approximation Error")
ax[0].scatter(Interp_x, Interp_err_xn, linewidth=1.5, label="Lagrange Interpolation Points")
ax[0].plot(x, Interp_err_interval, "-", label="Lagrange Interpolation Error")
ax[0].set(
xlabel="X Interval",
ylabel="Error",
title=f"Error on Interval {interval}, degree = {n}\nf(x)= {fx}",
)
box = ax[0].get_position()
ax[0].set_position([box.x0, box.y0, box.width, box.height * 0.8])
ax[0].legend(loc="center", bbox_to_anchor=(0.5, 1.2), ncol=4)
# err_list = history["e"]
# ax[1].plot(range(len(err_list)), err_list, "d--", linewidth=1.5)
# ax[1].set(
# xlabel="Epoch",
# ylabel="Max Error",
# title=f"Error On Points In Every Epoches",
# xticks=np.linspace(1, 10, 10),
# xlim=(0.5, len(err_list) + 0.5),
# )
ax[1].plot(x, [f(j) for j in x], linewidth=1.5)
ax[1].set(
xlabel="X Interval",
ylabel="Y",
title=f"f(x)",
# xticks=np.linspace(1, 10, 10),
# xlim=(0.5, len(err_list) + 0.5),
)
plt.tight_layout()
plt.savefig(f"./images/compare_plot/{fx}_{interval}_{n}.png", dpi=300)
plt.show()
print(f"f(x) = {fx}, interval = {interval}\n")
print(f"Polynomial degree = {n}")
print(f"Pn(x) = \n{px}\n")
print(f"xn points:\n{xn}\n")
print(f"Converge iteration: {len(history['e'])}")
print(f"MAE of approximation: {max(np.abs(Approx_err_interval))}")
print(f"MAE of interpolation: {max(np.abs(Interp_err_interval))}")