From fa8a89208fcfb0234df25ed88d7c4d457c307643 Mon Sep 17 00:00:00 2001 From: MateusStano Date: Sun, 31 Mar 2024 12:50:44 +0200 Subject: [PATCH] MNT: return interp and extrap results directly --- rocketpy/mathutils/function.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/rocketpy/mathutils/function.py b/rocketpy/mathutils/function.py index 2e3ea0e63..02277ce1c 100644 --- a/rocketpy/mathutils/function.py +++ b/rocketpy/mathutils/function.py @@ -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 @@ -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 @@ -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 @@ -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 @@ -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