3
3
4
4
class RBFInterpolator :
5
5
6
- def __init__ (self , z , x , y , hx , hy ):
6
+ def __init__ (self , z , hx , hy ):
7
7
"""
8
8
Initializes the RBF Interpolator.
9
9
@@ -14,43 +14,41 @@ def __init__(self, z, x, y, hx, hy):
14
14
:param hy: Grid spacing in the y-direction.
15
15
"""
16
16
17
- self ._z = z
18
- self ._x = x
19
- self ._y = y
20
- self ._hx = hx
21
- self ._hy = hy
22
- self ._nx , self ._ny = z .shape
17
+ self .__z = z
18
+ self .__hx = hx
19
+ self .__hy = hy
20
+ self .__nx , self ._ny = z .shape
23
21
24
- def _get_coordinates (self ):
22
+ def __get_coordinates (self , x , y ):
25
23
"""
26
24
Determines the x and y coordinates of the bottom-left corner of the grid cell
27
25
28
26
:return: A tuple containing the coordinates and its corresponding indices
29
27
"""
30
28
31
29
# gets the grid steps to x
32
- i_minus_star = int (np .floor (self . _x / self ._hx ))
33
- if i_minus_star > self ._nx - 1 :
30
+ i_minus_star = int (np .floor (x / self .__hx ))
31
+ if i_minus_star > self .__nx - 1 :
34
32
raise Exception ("x is out of bounds" )
35
33
36
34
# final i index for interpolation
37
- i_minus = i_minus_star if i_minus_star < self ._nx - 1 else self ._nx - 1
35
+ i_minus = i_minus_star if i_minus_star < self .__nx - 1 else self .__nx - 1
38
36
39
37
# gets the grid steps to y
40
- j_minus_star = int (np .floor (self . _y / self ._hy ))
38
+ j_minus_star = int (np .floor (y / self .__hy ))
41
39
if j_minus_star > self ._ny - 1 :
42
40
raise Exception ("y is out of bounds" )
43
41
44
42
# final j index for interpolation
45
43
j_minus = j_minus_star if j_minus_star < self ._ny - 1 else self ._ny - 1
46
44
47
45
# computes the coordinates at the computed indices
48
- x_minus = i_minus * self ._hx
49
- y_minus = j_minus * self ._hy
46
+ x_minus = i_minus * self .__hx
47
+ y_minus = j_minus * self .__hy
50
48
51
49
return x_minus , y_minus , i_minus , j_minus
52
50
53
- def _euclidean_distances (self , x_minus , y_minus ):
51
+ def __euclidean_distances (self , x_minus , y_minus , x , y ):
54
52
"""
55
53
Calculates Euclidean distances between (x,y) and the surrounding grid points in the unit cell
56
54
@@ -60,15 +58,15 @@ def _euclidean_distances(self, x_minus, y_minus):
60
58
[bottom left, top left, bottom right, top right]
61
59
"""
62
60
63
- bottom_left = np .sqrt ((x_minus - self . _x ) ** 2 + (y_minus - self . _y ) ** 2 )
64
- top_left = np .sqrt ((x_minus - self . _x ) ** 2 + (y_minus + self ._hy - self . _y ) ** 2 )
65
- bottom_right = np .sqrt ((x_minus + self ._hx - self . _x ) ** 2 + (y_minus - self . _y ) ** 2 )
66
- top_right = np .sqrt ((x_minus + self ._hx - self . _x ) ** 2 + (y_minus + self ._hy - self . _y ) ** 2 )
61
+ bottom_left = np .sqrt ((x_minus - x ) ** 2 + (y_minus - y ) ** 2 )
62
+ top_left = np .sqrt ((x_minus - x ) ** 2 + (y_minus + self .__hy - y ) ** 2 )
63
+ bottom_right = np .sqrt ((x_minus + self .__hx - x ) ** 2 + (y_minus - y ) ** 2 )
64
+ top_right = np .sqrt ((x_minus + self .__hx - x ) ** 2 + (y_minus + self .__hy - y ) ** 2 )
67
65
68
66
return bottom_left , top_left , bottom_right , top_right
69
67
70
68
@staticmethod
71
- def _rbf (d , gamma ):
69
+ def __rbf (d , gamma ):
72
70
"""
73
71
Computes the Radial Basis Function (RBF) for a given distance and gamma
74
72
@@ -78,27 +76,30 @@ def _rbf(d, gamma):
78
76
"""
79
77
return np .exp (- gamma * d ** 2 )
80
78
81
- def rbf_interpolate (self ):
79
+ def interpolate (self , x , y ):
82
80
"""
83
81
Performs the Radial Basis function (RBF) interpolation for the point (x,y)
84
82
85
83
:return: the interpolated value at (x,y)
86
84
"""
87
85
88
- x_minus , y_minus , i_minus , j_minus = self ._get_coordinates ( )
86
+ x_minus , y_minus , i_minus , j_minus = self .__get_coordinates ( x , y )
89
87
90
- distances = self ._euclidean_distances (x_minus , y_minus )
88
+ distances = self .__euclidean_distances (x_minus , y_minus , x , y )
91
89
92
- h_diag_squared = self ._hx ** 2 + self ._hy ** 2
90
+ h_diag_squared = self .__hx ** 2 + self .__hy ** 2
93
91
gamma = - np .log (0.005 ) / h_diag_squared
94
92
95
- rbf_weights = [self ._rbf (d , gamma ) for d in distances ]
93
+ rbf_weights = [self .__rbf (d , gamma ) for d in distances ]
96
94
97
95
sum_rbf = np .sum (rbf_weights )
98
- interpolated = rbf_weights [0 ] * self ._z [i_minus , j_minus ]
99
- interpolated += rbf_weights [1 ] * self ._z [i_minus , j_minus + 1 ]
100
- interpolated += rbf_weights [2 ] * self ._z [i_minus + 1 , j_minus ]
101
- interpolated += rbf_weights [3 ] * self ._z [i_minus + 1 , j_minus + 1 ]
96
+ interpolated = rbf_weights [0 ] * self .__z [i_minus , j_minus ]
97
+ interpolated += rbf_weights [1 ] * self .__z [i_minus , j_minus + 1 ]
98
+ interpolated += rbf_weights [2 ] * self .__z [i_minus + 1 , j_minus ]
99
+ interpolated += rbf_weights [3 ] * self .__z [i_minus + 1 , j_minus + 1 ]
102
100
interpolated /= sum_rbf
103
101
104
102
return interpolated
103
+
104
+ def interpolate_all (self ):
105
+ pass
0 commit comments