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

ENH: Function Reverse Arithmetic Priority #488

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 9 additions & 6 deletions rocketpy/mathutils/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
extrapolation, plotting and algebra.
"""

# Arithmetic priority
__array_ufunc__ = None

def __init__(
self,
source,
Expand Down Expand Up @@ -1837,7 +1840,7 @@
return Function(lambda x: (self.get_value(x) + other(x)))
# If other is Float except...
except AttributeError:
if isinstance(other, (float, int, complex)):
if isinstance(other, (float, int, complex, np.ndarray)):
# Check if Function object source is array or callable
if isinstance(self.source, np.ndarray):
# Operate on grid values
Expand Down Expand Up @@ -1967,7 +1970,7 @@
return Function(lambda x: (self.get_value(x) * other(x)))
# If other is Float except...
except AttributeError:
if isinstance(other, (float, int, complex)):
if isinstance(other, (float, int, complex, np.ndarray)):
# Check if Function object source is array or callable
if isinstance(self.source, np.ndarray):
# Operate on grid values
Expand Down Expand Up @@ -2056,7 +2059,7 @@
return Function(lambda x: (self.get_value_opt(x) / other(x)))
# If other is Float except...
except AttributeError:
if isinstance(other, (float, int, complex)):
if isinstance(other, (float, int, complex, np.ndarray)):
# Check if Function object source is array or callable
if isinstance(self.source, np.ndarray):
# Operate on grid values
Expand Down Expand Up @@ -2095,7 +2098,7 @@
A Function object which gives the result of other(x)/self(x).
"""
# Check if Function object source is array and other is float
if isinstance(other, (float, int, complex)):
if isinstance(other, (float, int, complex, np.ndarray)):
if isinstance(self.source, np.ndarray):
# Operate on grid values
ys = other / self.y_array
Expand Down Expand Up @@ -2163,7 +2166,7 @@
return Function(lambda x: (self.get_value_opt(x) ** other(x)))
# If other is Float except...
except AttributeError:
if isinstance(other, (float, int, complex)):
if isinstance(other, (float, int, complex, np.ndarray)):
# Check if Function object source is array or callable
if isinstance(self.source, np.ndarray):
# Operate on grid values
Expand Down Expand Up @@ -2202,7 +2205,7 @@
A Function object which gives the result of other(x)**self(x).
"""
# Check if Function object source is array and other is float
if isinstance(other, (float, int, complex)):
if isinstance(other, (float, int, complex, np.ndarray)):

Check warning on line 2208 in rocketpy/mathutils/function.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/mathutils/function.py#L2208

Added line #L2208 was not covered by tests
if isinstance(self.source, np.ndarray):
# Operate on grid values
ys = other**self.y_array
Expand Down
16 changes: 16 additions & 0 deletions tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,19 @@ def test_shepard_interpolation(x, y, z_expected):
func = Function(source=source, inputs=["x", "y"], outputs=["z"])
z = func(x, y)
assert np.isclose(z, z_expected, atol=1e-8).all()


def test_arithmetic_priority():
"""Test the arithmetic priority of the Function class, specially
comparing to the numpy array operations.
"""
func_lambda = Function(lambda x: x**2)
func_array = Function([(0, 0), (1, 1), (2, 4)])
array = np.array([1])

assert isinstance(func_lambda + func_array, Function)
assert isinstance(func_array + func_lambda, Function)
assert isinstance(func_lambda + array, Function)
assert isinstance(array + func_lambda, Function)
assert isinstance(func_array + array, Function)
assert isinstance(array + func_array, Function)
Loading