Merge pull request #3418 from winklemad:fix/minimize-xtol-accepted-candidate

PiperOrigin-RevId: 952380184
Change-Id: Ied4ab1bfc91dbb55dbd9d2f2e64ab2288513c50e
This commit is contained in:
Copybara-Service
2026-07-22 15:57:46 -07:00
2 changed files with 13 additions and 0 deletions
+2
View File
@@ -416,6 +416,8 @@ def least_squares(
# Check termination condition on step norm.
if dx_norm < xtol * (xtol + np.linalg.norm(x)):
x = xnew
r = rnew
status = Status.DX_TOL
break
+11
View File
@@ -45,6 +45,17 @@ class MinimizeTest(absltest.TestCase):
self.assertIn('norm(gradient) < tol', out.getvalue())
self.assertIn('exact minimum found', out.getvalue())
def test_xtol_returns_accepted_candidate(self) -> None:
def residual(x):
return x - 1.0
out = io.StringIO()
x, _ = minimize.least_squares(
np.array([0.0]), residual, xtol=2.0, gtol=0.0, output=out
)
np.testing.assert_array_equal(x, np.array([1.0]))
self.assertIn('norm(dx) < tol', out.getvalue())
def test_jac_callback(self) -> None:
def residual(x):
return np.stack([1 - x[0, :], 10 * (x[1, :] - x[0, :] ** 2)])