-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_approxfun.py
106 lines (75 loc) · 2.58 KB
/
test_approxfun.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
# from matplotlib import pyplot as plt
from jax import grad, numpy as np, vmap
from pysages.grids import Chebyshev, Grid
from pysages.approxfun import (
SpectralGradientFit,
SpectralSobolev1Fit,
build_fitter,
build_evaluator,
build_grad_evaluator,
compute_mesh,
)
# Test functions
def gaussian(a, mu, sigma, x):
return a * np.exp(-((x - mu) ** 2) / sigma)
def g(x):
return gaussian(1.5, 0.2, 0.03, x) + gaussian(0.5, -0.5, 0.05, x) + gaussian(1.25, 0.9, 1.5, x)
def f(x):
return 0.35 * np.cos(5 * x) + 0.7 * np.sin(-2 * x)
def test_fourier_approx():
grid = Grid(lower=(-np.pi,), upper=(np.pi,), shape=(512,), periodic=True)
x = np.pi * compute_mesh(grid)
# Periodic function and its gradient
y = vmap(f)(x.flatten()).reshape(x.shape)
dy = vmap(grad(f))(x.flatten()).reshape(x.shape)
model = SpectralGradientFit(grid)
fit = build_fitter(model)
evaluate = build_evaluator(model)
get_grad = build_grad_evaluator(model)
fun = fit(dy)
assert np.all(np.isclose(y, evaluate(fun, x))).item()
assert np.all(np.isclose(dy, get_grad(fun, x))).item()
# fig, ax = plt.subplots()
# ax.plot(x, dy)
# ax.plot(x, get_grad(fun, x))
# plt.show()
# fig, ax = plt.subplots()
# ax.plot(x, y)
# ax.plot(x, evaluate(fun, x))
# plt.show()
model = SpectralSobolev1Fit(grid)
fit = build_fitter(model)
evaluate = build_evaluator(model)
get_grad = build_grad_evaluator(model)
sfun = fit(y, dy)
assert np.all(np.isclose(y, evaluate(sfun, x))).item()
assert np.all(np.isclose(dy, get_grad(sfun, x))).item()
assert np.linalg.norm(fun.coefficients - sfun.coefficients) < 1e-8
# fig, ax = plt.subplots()
# ax.plot(x, dy)
# ax.plot(x, get_grad(sfun, x))
# plt.show()
# fig, ax = plt.subplots()
# ax.plot(x, y)
# ax.plot(x, evaluate(sfun, x))
# plt.show()
def test_cheb_approx():
grid = Grid[Chebyshev](lower=(-1.0,), upper=(1.0,), shape=(256,))
x = compute_mesh(grid)
y = vmap(g)(x.flatten()).reshape(x.shape)
dy = vmap(grad(g))(x.flatten()).reshape(x.shape)
model = SpectralSobolev1Fit(grid)
fit = build_fitter(model)
evaluate = build_evaluator(model)
get_grad = build_grad_evaluator(model)
fun = fit(y, dy)
assert np.linalg.norm(y - evaluate(fun, x)) < 1e-2
assert np.linalg.norm(dy - get_grad(fun, x)) < 5e-2
# fig, ax = plt.subplots()
# ax.plot(x, y)
# ax.plot(x, evaluate(fun, x))
# plt.show()
# fig, ax = plt.subplots()
# ax.plot(x, dy)
# ax.plot(x, get_grad(fun, x))
# plt.show()