Note
Go to the end to download the full example code.
2-Dimensional InterpolationΒΆ
7 import ndsplines
8 import numpy as np
9 import matplotlib.pyplot as plt
10 from scipy import interpolate
11 from scipy.stats import norm
12 import itertools
13
14
15 def gaussian(x_in):
16 z = norm.ppf(0.995)
17 x = z * (2 * x_in - 1)
18 return norm.pdf(x)
19
20
21 def sin(x_in):
22 x = np.pi * (x_in - 0.5)
23 return np.sin(x)
24
25
26 def tanh(x_in):
27 x = 2 * np.pi * (x_in - 0.5)
28 return np.tanh(x)
29
30
31 def dist(x_in, y_in):
32 return np.sqrt((x_in - 0.25) ** 2 + (y_in - 0.25) ** 2)
33
34
35 funcs = [gaussian, sin, tanh]
36
37
38 def wrap2d(funcx, funcy):
39 def func2d(x_in, y_in):
40 return funcx(x_in) * funcy(y_in)
41
42 func2d.__name__ = "_".join([funcx.__name__, funcy.__name__])
43 return func2d
44
45
46 funcs = [
47 wrap2d(*funcs_to_wrap)
48 for funcs_to_wrap in itertools.combinations_with_replacement(funcs, r=2)
49 ]
50 funcs.append(dist)
51
52 x = np.linspace(0, 1, 7)
53 y = np.linspace(0, 1, 7)
54
55 xx = np.linspace(0, 1, 64)
56 yy = np.linspace(0, 1, 64)
57
58 xx = np.linspace(-0.25, 1.25, 64)
59 yy = np.linspace(-0.25, 1.25, 64)
60 k = 3
61
62
63 meshx, meshy = np.meshgrid(x, y, indexing="ij")
64 gridxy = np.stack((meshx, meshy), axis=-1)
65
66
67 meshxx, meshyy = np.meshgrid(xx, yy, indexing="ij")
68 gridxxyy = np.stack((meshxx, meshyy), axis=-1)
69
70 for func in funcs:
71 fvals = func(meshx, meshy)
72 truef = func(meshxx, meshyy)
73 test_NDBspline = ndsplines.make_interp_spline(
74 gridxy,
75 fvals,
76 )
77 test_RectSpline = interpolate.RectBivariateSpline(x, y, fvals)
78
79 fig = plt.figure()
80 ax = fig.add_subplot(111, projection="3d")
81
82 ax.plot_wireframe(meshxx, meshyy, truef, alpha=0.25, color="C0")
83 ax.plot_wireframe(meshxx, meshyy, test_NDBspline(gridxxyy), color="C1")
84 ax.plot_wireframe(
85 meshxx, meshyy, test_RectSpline(meshxx, meshyy, grid=False), color="C2"
86 )
87 plt.show()
Total running time of the script: (0 minutes 0.542 seconds)






