forked from floli/PyRBF
-
Notifications
You must be signed in to change notification settings - Fork 2
/
slides.py
313 lines (293 loc) · 11 KB
/
slides.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
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
from rbf_qr import *
from rbf import *
import basisfunctions
import mesh
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from timeit import default_timer as timer
import math
from tqdm import tqdm
def intro_slide():
X = np.linspace(-5, 5, 100)
Y = np.linspace(-5, 5, 100)
mesh = np.array(np.meshgrid(X, Y))
shape = 0.4
vals = np.exp(-shape**2 * (mesh[0, :]**2 + mesh[1, :]**2))
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(mesh[0, :], mesh[1,:], vals, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.set_zlim(0, 1.01)
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([-1])
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
#fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
def equidistant_convergence_1d():
error = []
error_sep = []
cond_sep = []
def func(x):
return np.exp(-np.abs(x - 3)**2) + 2
cond = []
N_vals = []
for N in np.linspace(5, 1000, 100):
halflength = 1
N_vals.append(N)
print("N =", N)
in_mesh = np.linspace(-halflength, halflength, N)
halflength -= halflength*0.2
test_mesh = np.linspace(-halflength, halflength, 2000)
rbfqr = RBF_QR_1D(1e-5, in_mesh, func(in_mesh))
error.append(rbfqr.RMSE(func, test_mesh))
cond.append(np.linalg.cond(rbfqr.A))
print("Cond QR:", cond[-1])
print("RMSE QR:", error[-1])
separated = SeparatedConsistent(Gaussian(Gaussian.shape_param_from_m(5, in_mesh)), in_mesh, func(in_mesh))
cond_sep.append(separated.condC)
error_sep.append(separated.RMSE(func, test_mesh))
print("RMSE Sep:", error_sep[-1])
color_qr = "xkcd:red"
color_poly = "xkcd:blue"
fig, ax1 = plt.subplots()
ax1.set_yscale("log")
ax1.set_xscale("log")
ax1.set_ylabel("RMSE")
ax1.plot(N_vals, error, label="Error RBF-QR",
color=color_qr, linestyle="dashed")
ax1.plot(N_vals, error_sep, label="Error Separated Poly",
color=color_poly, linestyle="dashed")
ax2 = ax1.twinx()
ax2.set_yscale("log")
ax2.set_xscale("log")
ax2.set_ylabel("Condition")
ax1.set_xlabel("Mesh size")
ax2.plot(N_vals, cond,
label="Condition RBF-QR",
color=color_qr)
ax2.plot(N_vals, cond_sep,
label="Condition Separated Poly", color=color_poly)
ax1.legend(loc="upper left")
ax2.legend(loc="center left")
ax1.set_title("RMSE Uniform mesh")
plt.show()
def gc_convergence_1d():
error = []
error_sep = []
cond_sep = []
def func(x):
""" Testfunction """
return np.exp(-np.abs(x - 3)**2) + 2
cond = []
N_vals = []
h_vals = []
for N in range(5, 250):
halflength = 1
N_vals.append(N)
print("N =", N)
in_mesh = np.polynomial.chebyshev.chebgauss(N)[0] * halflength
halflength -= halflength*0.2
test_mesh = np.linspace(-halflength, halflength, 2000)
rbfqr = RBF_QR_1D(1e-5, in_mesh, func(in_mesh))
error.append(rbfqr.RMSE(func, test_mesh))
cond.append(np.linalg.cond(rbfqr.A))
print("Cond QR:", cond[-1])
print("RMSE QR:", error[-1])
separated = SeparatedConsistent(Gaussian(Gaussian.shape_param_from_m(5, in_mesh)), in_mesh, func(in_mesh))
cond_sep.append(separated.condC)
error_sep.append(separated.RMSE(func, test_mesh))
print("RMSE Sep:", error_sep[-1])
color_qr = "xkcd:red"
color_poly = "xkcd:blue"
fig, ax1 = plt.subplots()
ax1.set_yscale("log")
ax1.set_xscale("log")
ax1.set_ylabel("RMSE")
ax1.set_xlabel("Mesh size")
ax1.plot(N_vals, error, label="Error RBF-QR",
color=color_qr, linestyle="dashed")
ax1.plot(N_vals, error_sep, label="Error Separated Poly",
color=color_poly, linestyle="dashed")
ax2 = ax1.twinx()
color2 = "xkcd:green"
color2sep = "xkcd:orange"
ax2.set_yscale("log")
ax2.set_xscale("log")
ax2.set_ylabel("Condition")
ax2.plot(N_vals, cond,
label="Condition RBF-QR",
color=color_qr)
ax2.plot(N_vals, cond_sep,
label="Condition Separated Poly", color=color_poly)
ax1.legend(loc="upper left")
ax2.legend(loc="center left")
ax1.set_title("RMSE Gauss-Chebyshev")
plt.show()
def equidistant_convergence_2d():
def func(mesh):
return np.sin(mesh[0, :]) - np.cos(mesh[1, :])
N_vals = []
error_qr = []
error_poly = []
cond_poly = []
cond_qr = []
for N in range(5, 55):
N_vals.append(N)
print("N=", N)
halflength = 0.5
X = np.linspace(-halflength, halflength, N)
Y = np.copy(X)
in_mesh = np.array(np.meshgrid(X, Y))
in_mesh_flat = in_mesh.reshape(2, -1)
halflength -= 0.1
X_test = np.linspace(-halflength, halflength, 50)
Y_test = np.copy(X_test)
test_mesh = np.array(np.meshgrid(X_test, Y_test))
test_mesh_flat = test_mesh.reshape(2, -1)
in_vals = func(in_mesh_flat)
rbfqr = RBF_QR_2D(1e-3, in_mesh_flat, in_vals)
poly = SeparatedConsistent(functools.partial(Gaussian(), shape=5), in_mesh_flat.transpose(),
in_vals.transpose())
error_qr.append(rbfqr.RMSE(func, test_mesh_flat))
error_poly.append(poly.RMSE(lambda mesh: func(mesh.transpose()), test_mesh_flat.transpose()))
cond_qr.append(np.linalg.cond(rbfqr.A))
cond_poly.append(poly.condC)
print("RMSE QR: ", error_qr[-1])
print("RMSE poly: ", error_poly[-1])
print("Cond QR: ", cond_qr[-1])
print("Cond poly: ", cond_poly[-1])
color_qr = "xkcd:red"
color_poly = "xkcd:blue"
fig, ax1 = plt.subplots()
ax1.set_ylabel("RMSE")
ax1.set_xlabel("Mesh size in one dimension")
ax1.set_yscale("log")
ax1.plot(N_vals, error_qr, label="RMSE RBF-QR", color=color_qr, linestyle="dashed")
ax1.plot(N_vals, error_poly, label="RMSE Separated Poly", color=color_poly, linestyle="dashed")
ax2 = ax1.twinx()
ax2.set_yscale("log")
ax2.set_ylabel("Condition")
ax2.plot(N_vals, cond_qr, label="Condition RBF-QR", color=color_qr)
ax2.plot(N_vals, cond_poly, label="Condition Separated Poly", color=color_poly)
ax1.legend(loc="upper left")
ax2.legend(loc="center left")
ax1.set_title("RMSE Uniform mesh 2D")
plt.show()
def gc_convergence_2d():
def func(mesh):
return np.sin(mesh[0, :]) - np.cos(mesh[1, :])
N_vals = []
error_qr = []
error_poly = []
cond_poly = []
cond_qr = []
for N in range(5, 55):
N_vals.append(N)
print("N=", N)
halflength = 0.5
X = np.polynomial.chebyshev.chebgauss(N)[0] * halflength
Y = np.copy(X)
in_mesh = np.array(np.meshgrid(X, Y))
in_mesh_flat = in_mesh.reshape(2, -1)
halflength -= 0.1
X_test = np.linspace(-halflength, halflength, 50)
Y_test = np.copy(X_test)
test_mesh = np.array(np.meshgrid(X_test, Y_test))
test_mesh_flat = test_mesh.reshape(2, -1)
in_vals = func(in_mesh_flat)
rbfqr = RBF_QR_2D(1e-3, in_mesh_flat, in_vals)
poly = SeparatedConsistent(functools.partial(Gaussian(), shape=5), in_mesh_flat.transpose(),
in_vals.transpose())
error_qr.append(rbfqr.RMSE(func, test_mesh_flat))
error_poly.append(poly.RMSE(lambda mesh: func(mesh.transpose()), test_mesh_flat.transpose()))
cond_qr.append(np.linalg.cond(rbfqr.A))
cond_poly.append(poly.condC)
print("RMSE QR: ", error_qr[-1])
print("RMSE poly: ", error_poly[-1])
print("Cond QR: ", cond_qr[-1])
print("Cond poly: ", cond_poly[-1])
color_qr = "xkcd:red"
color_poly = "xkcd:blue"
fig, ax1 = plt.subplots()
ax1.set_ylabel("RMSE")
ax1.set_yscale("log")
ax1.set_xlabel("Mesh size in one dimension")
ax1.plot(N_vals, error_qr, label="RMSE RBF-QR", color=color_qr, linestyle="dashed")
ax1.plot(N_vals, error_poly, label="RMSE Separated Poly", color=color_poly, linestyle="dashed")
ax2 = ax1.twinx()
ax2.set_yscale("log")
ax2.set_ylabel("Condition")
ax2.plot(N_vals, cond_qr, label="Condition RBF-QR", color=color_qr)
ax2.plot(N_vals, cond_poly, label="Condition Separated Poly", color=color_poly)
ax1.legend(loc="upper left")
ax2.legend(loc="center left")
ax1.set_title("Gauss-Chebyshev mesh 2D")
plt.show()
def equidistant_time_2d():
def func(mesh):
return np.sin(mesh[0, :]) - np.cos(mesh[1, :])
N_vals = []
offline_qr = []
offline_poly = []
online_poly = []
online_qr = []
for N in range(5, 35):
N_vals.append(N)
print("N=", N)
halflength = 0.5
X = np.linspace(-halflength, halflength, N)
Y = np.copy(X)
in_mesh = np.array(np.meshgrid(X, Y))
in_mesh_flat = in_mesh.reshape(2, -1)
halflength -= 0.1
X_test = np.linspace(-halflength, halflength, 50)
Y_test = np.copy(X_test)
test_mesh = np.array(np.meshgrid(X_test, Y_test))
test_mesh_flat = test_mesh.reshape(2, -1)
in_vals = func(in_mesh_flat)
start = timer()
rbfqr = RBF_QR_2D(1e-3, in_mesh_flat, in_vals)
end = timer(); offline_qr.append(end-start)
start = timer()
poly = SeparatedConsistent(functools.partial(Gaussian(), shape=5), in_mesh_flat.transpose(),
in_vals.transpose())
end = timer(); offline_poly.append(end - start)
start = timer()
rbfqr(test_mesh_flat)
end = timer(); online_qr.append(end - start)
start = timer()
poly(test_mesh_flat.transpose())
end = timer(); online_poly.append(end - start)
print("Offline QR: ", offline_qr[-1])
print("Offline poly: ", offline_poly[-1])
print("Online QR: ", online_qr[-1])
print("Online poly: ", online_poly[-1])
color_qr = "xkcd:red"
color_poly = "xkcd:blue"
fig, ax1 = plt.subplots()
ax1.set_ylabel("Time (s)")
ax1.set_xlabel("Mesh size in one dimension")
ax1.set_yscale("log")
ax1.plot(N_vals, offline_qr, label="Offline RBF-QR", color=color_qr, linestyle="dashed")
ax1.plot(N_vals, offline_poly, label="Offline Separated Poly", color=color_poly, linestyle="dashed")
ax1.plot(N_vals, online_qr, label="Online RBF-QR", color=color_qr)
ax1.plot(N_vals, online_poly, label="Online Separated Poly", color=color_poly)
ax1.legend(loc=2)
ax1.set_title("Computation time 2D, split into offline and online phase")
plt.show()
def combined():
equidistant_convergence_1d()
gc_convergence_1d()
def main():
# intro_slide()
# combined()
# equidistant_convergence_1d()
gc_convergence_1d()
# equidistant_convergence_2d()
# equidistant_time_2d()
# gc_convergence_2d()
if __name__ == "__main__":
main()