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

TST: optimize.minimize: test improved error message #65

Merged
merged 1 commit into from
May 8, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def equality_constrained_sqp(fun_and_constr, grad_and_jac, lagr_hess,
raise ValueError(
"The 'expected square matrix' error can occur if there are"
" more equality constraints than independent variables."
" Consider how your constraints are setup, or use"
" Consider how your constraints are set up, or use"
" factorization_method='SVDFactorization'."
) from e
else:
Expand Down
19 changes: 18 additions & 1 deletion scipy/optimize/tests/test_minimize_constrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
Bounds,
minimize,
BFGS,
SR1)
SR1,
rosen)


class Maratos:
Expand Down Expand Up @@ -747,6 +748,22 @@ def nci(x):
assert_allclose(res.fun, ref.fun)


def test_gh20665_too_many_constraints():
# gh-20665 reports a confusing error message when there are more equality
# constraints than variables. Check that the error message is improved.
message = "...more equality constraints than independent variables..."
with pytest.raises(ValueError, match=message):
x0 = np.ones((2,))
A_eq, b_eq = np.arange(6).reshape((3, 2)), np.ones((3,))
g = NonlinearConstraint(lambda x: A_eq @ x, lb=b_eq, ub=b_eq)
minimize(rosen, x0, method='trust-constr', constraints=[g])
# no error with `SVDFactorization`
with np.testing.suppress_warnings() as sup:
sup.filter(UserWarning)
minimize(rosen, x0, method='trust-constr', constraints=[g],
options={'factorization_method': 'SVDFactorization'})


class TestBoundedNelderMead:

@pytest.mark.parametrize('bounds, x_opt',
Expand Down