Skip to content

Commit

Permalink
MNT: return interp and extrap results directly
Browse files Browse the repository at this point in the history
  • Loading branch information
MateusStano committed Mar 31, 2024
1 parent 07f9ed5 commit fa8a892
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions rocketpy/mathutils/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,7 @@ def linear_interpolation(x, x_min, x_max, x_data, y_data, coeffs):
y_left = y_data[x_interval - 1]
dx = float(x_data[x_interval] - x_left)
dy = float(y_data[x_interval] - y_left)
y = (x - x_left) * (dy / dx) + y_left
return y
return (x - x_left) * (dy / dx) + y_left

self._interpolation_func = linear_interpolation

Expand All @@ -376,8 +375,7 @@ def akima_interpolation(x, x_min, x_max, x_data, y_data, coeffs):
x_interval = bisect_left(x_data, x)
x_interval = x_interval if x_interval != 0 else 1
a = coeffs[4 * x_interval - 4 : 4 * x_interval]
y = a[3] * x**3 + a[2] * x**2 + a[1] * x + a[0]
return y
return a[3] * x**3 + a[2] * x**2 + a[1] * x + a[0]

self._interpolation_func = akima_interpolation

Expand All @@ -388,8 +386,7 @@ def spline_interpolation(x, x_min, x_max, x_data, y_data, coeffs):
x_interval = max(x_interval, 1)
a = coeffs[:, x_interval - 1]
x = x - x_data[x_interval - 1]
y = a[3] * x**3 + a[2] * x**2 + a[1] * x + a[0]
return y
return a[3] * x**3 + a[2] * x**2 + a[1] * x + a[0]

self._interpolation_func = spline_interpolation

Expand Down Expand Up @@ -421,21 +418,18 @@ def natural_extrapolation(x, x_min, x_max, x_data, y_data, coeffs):
y_left = y_data[x_interval - 1]
dx = float(x_data[x_interval] - x_left)
dy = float(y_data[x_interval] - y_left)
y = (x - x_left) * (dy / dx) + y_left
return y
return (x - x_left) * (dy / dx) + y_left

elif interpolation == 1: # polynomial

def natural_extrapolation(x, x_min, x_max, x_data, y_data, coeffs):
y = np.sum(coeffs * x ** np.arange(len(coeffs)))
return y
return np.sum(coeffs * x ** np.arange(len(coeffs)))

elif interpolation == 2: # akima

def natural_extrapolation(x, x_min, x_max, x_data, y_data, coeffs):
a = coeffs[:4] if x < x_min else coeffs[-4:]
y = a[3] * x**3 + a[2] * x**2 + a[1] * x + a[0]
return y
return a[3] * x**3 + a[2] * x**2 + a[1] * x + a[0]

elif interpolation == 3: # spline

Expand All @@ -446,8 +440,7 @@ def natural_extrapolation(x, x_min, x_max, x_data, y_data, coeffs):
else:
a = coeffs[:, -1]
x = x - x_data[-2]
y = a[3] * x**3 + a[2] * x**2 + a[1] * x + a[0]
return y
return a[3] * x**3 + a[2] * x**2 + a[1] * x + a[0]

self._extrapolation_func = natural_extrapolation
elif extrapolation == 2: # constant
Expand Down

0 comments on commit fa8a892

Please sign in to comment.