Skip to content

Commit

Permalink
optimization of get_value_opt in set_get_value_opt
Browse files Browse the repository at this point in the history
  • Loading branch information
styris00 committed Dec 3, 2023
1 parent a964662 commit b83059c
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions rocketpy/mathutils/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,19 +488,18 @@ def get_value_opt(x):

# change the function's name to avoid mypy's error
def get_value_opt_multiple(*args):
x = np.array([[float(x) for x in list(args)]])
numerator_sum = 0
denominator_sum = 0
for i in range(len_y_data):
sub = x_data[i] - x
distance = np.linalg.norm(sub)
if distance == 0:
numerator_sum = y_data[i]
denominator_sum = 1
break
weight = distance ** (-3)
numerator_sum = numerator_sum + y_data[i] * weight
denominator_sum = denominator_sum + weight
x = np.array([[float(val) for val in args]])
sub_matrix = x_data - x
distances_squared = np.sum(sub_matrix**2, axis=1)

zero_distance_index = np.where(distances_squared == 0)[0]
if len(zero_distance_index) > 0:
return y_data[zero_distance_index[0]]

weights = distances_squared**(-1.5)
numerator_sum = np.sum(y_data * weights)
denominator_sum = np.sum(weights)

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

View check run for this annotation

Codecov / codecov/patch

rocketpy/mathutils/function.py#L499-L501

Added lines #L499 - L501 were not covered by tests

return numerator_sum / denominator_sum

get_value_opt = get_value_opt_multiple
Expand Down

0 comments on commit b83059c

Please sign in to comment.