Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: fix extrapolation of multivariable functions. #462

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion rocketpy/mathutils/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,29 @@ def source(x):

# Finally set data source as source
self.source = source
if self.__interpolation__ is None:

# Update extrapolation method
if (
self.__extrapolation__ is None
or self.__extrapolation__ == "natural"
):
self.set_extrapolation("natural")
else:
raise ValueError(
"Multidimensional datasets only support natural extrapolation."
)

# Set default multidimensional interpolation if it hasn't
if (
self.__interpolation__ is None
or self.__interpolation__ == "shepard"
):
self.set_interpolation("shepard")
else:
raise ValueError(
"Multidimensional datasets only support shepard interpolation."
)

# Return self
return self

Expand Down
70 changes: 70 additions & 0 deletions tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,73 @@ def test_integral_spline_interpolation(request, func, a, b):
func.integral(a, b, numerical=True),
atol=1e-3,
)


@pytest.mark.parametrize("a", [-1, 0, 1])
@pytest.mark.parametrize("b", [-1, 0, 1])
def test_multivariable_dataset(a, b):
"""Test the Function class with a multivariable dataset."""
# Test plane f(x,y) = x + y
source = [
(-1, -1, -2),
(-1, 0, -1),
(-1, 1, 0),
(0, -1, -1),
(0, 0, 0),
(0, 1, 1),
(1, -1, 0),
(1, 0, 1),
(1, 1, 2),
]
func = Function(source=source, inputs=["x", "y"], outputs=["z"])

# Assert interpolation and extrapolation methods
assert func.get_interpolation_method() == "shepard"
assert func.get_extrapolation_method() == "natural"

# Assert values
assert np.isclose(func(a, b), a + b, atol=1e-6)


@pytest.mark.parametrize("a", [-1, -0.5, 0, 0.5, 1])
@pytest.mark.parametrize("b", [-1, -0.5, 0, 0.5, 1])
def test_multivariable_function(a, b):
"""Test the Function class with a multivariable function."""
# Test plane f(x,y) = sin(x + y)
source = lambda x, y: np.sin(x + y)
func = Function(source=source, inputs=["x", "y"], outputs=["z"])

# Assert values
assert np.isclose(func(a, b), np.sin(a + b), atol=1e-6)


@patch("matplotlib.pyplot.show")
def test_multivariable_dataset_plot(mock_show):
"""Test the plot method of the Function class with a multivariable dataset."""
# Test plane f(x,y) = x - y
source = [
(-1, -1, -1),
(-1, 0, -1),
(-1, 1, -2),
(0, 1, 1),
(0, 0, 0),
(0, 1, -1),
(1, -1, 2),
(1, 0, 1),
(1, 1, 0),
]
func = Function(source=source, inputs=["x", "y"], outputs=["z"])

# Assert plot
assert func.plot() == None


@patch("matplotlib.pyplot.show")
def test_multivariable_function_plot(mock_show):
"""Test the plot method of the Function class with a multivariable function."""
# Test plane f(x,y) = sin(x + y)
source = lambda x, y: np.sin(x * y)
func = Function(source=source, inputs=["x", "y"], outputs=["z"])

# Assert plot
assert func.plot() == None
Loading