Skip to content
Merged
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
15 changes: 7 additions & 8 deletions mmapy/mma_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,13 +777,13 @@ def objfn(xy):

# Check constraint is active
self.assertLess(
abs(c_val[0, 0]), 0.1, msg="Constraint should be approximately active"
abs(c_val[0, 0]), 0.05, msg="Constraint should be approximately active"
)

# Check multiplier is positive
self.assertGreater(
lm.general_constraints[0, 0],
0.1,
0.01,
msg="Active constraint should have positive multiplier",
)

Expand Down Expand Up @@ -883,7 +883,7 @@ def objfn(xy):
# Stationarity: ∇f - ξ should be small
self.assertLess(
residual_norm,
0.5,
0.01,
msg=f"Stationarity at bounds violated: ||∇f - ξ|| = {residual_norm}",
)

Expand Down Expand Up @@ -968,7 +968,7 @@ def objfn(xy):
# Stationarity: ∇f + η should be small
self.assertLess(
residual_norm,
0.5,
0.01,
msg=f"Stationarity at upper bounds violated: ||∇f + η|| = {residual_norm}",
)

Expand Down Expand Up @@ -1134,14 +1134,13 @@ def objfn(x):

# Check scaling relationship
base_lambda = results[0]["lambda_lower"]
for i, r in enumerate(results[1:], start=1):
for _, r in enumerate(results[1:], start=1):
expected_lambda = base_lambda * (r["scale"] / results[0]["scale"])
actual_lambda = r["lambda_lower"]
relative_error = abs(actual_lambda - expected_lambda) / max(expected_lambda, 1e-6)

self.assertLess(
relative_error,
0.2, # 20% tolerance
0.01, # 1% tolerance
msg=f"Multiplier should scale with objective. "
f"Expected {expected_lambda:.4f}, got {actual_lambda:.4f}",
)
Comment on lines 1140 to 1146

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the manual calculation of relative error works, using np.testing.assert_allclose is more idiomatic and robust for comparing floating-point numbers in NumPy-based tests. It clearly expresses the intent of checking for approximate equality with a relative tolerance and handles edge cases around zero more gracefully.

Suggested change
relative_error = abs(actual_lambda - expected_lambda) / max(expected_lambda, 1e-6)
self.assertLess(
relative_error,
0.2, # 20% tolerance
0.01, # 1% tolerance
msg=f"Multiplier should scale with objective. "
f"Expected {expected_lambda:.4f}, got {actual_lambda:.4f}",
)
np.testing.assert_allclose(
actual_lambda,
expected_lambda,
rtol=0.01, # 1% tolerance
err_msg=(
"Multiplier should scale with objective. "
f"Expected {expected_lambda:.4f}, got {actual_lambda:.4f}"
),
)

Expand Down Expand Up @@ -1253,7 +1252,7 @@ def objfn(xy):
relative_deviation = abs(prod - mean_product) / mean_product
self.assertLess(
relative_deviation,
0.3, # 30% tolerance
0.01, # 1% tolerance
msg="λ * scale should be approximately constant",
)
Comment on lines 1252 to 1257

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved readability and to follow common testing patterns with NumPy, consider using np.testing.assert_allclose. This function is designed for comparing floating-point values and makes the intent of a relative tolerance check clearer than manually calculating the deviation.

Suggested change
relative_deviation = abs(prod - mean_product) / mean_product
self.assertLess(
relative_deviation,
0.3, # 30% tolerance
0.01, # 1% tolerance
msg="λ * scale should be approximately constant",
)
np.testing.assert_allclose(
prod,
mean_product,
rtol=0.01, # 1% tolerance
err_msg="λ * scale should be approximately constant",
)


Expand Down
Loading