From c65777b40013b0cf1c015373b500dd07d2f65a61 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 29 Sep 2026 02:33:51 +0200 Subject: [PATCH] cultmath: stop the projected-gradient reference once it stops moving Co-Authored-By: Claude Sonnet 5 --- .../tests/CultMath.Tests/BoundedLeastSquaresTests.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/cultmath/tests/CultMath.Tests/BoundedLeastSquaresTests.cs b/packages/cultmath/tests/CultMath.Tests/BoundedLeastSquaresTests.cs index 0d99b341..e14cb50c 100644 --- a/packages/cultmath/tests/CultMath.Tests/BoundedLeastSquaresTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/BoundedLeastSquaresTests.cs @@ -75,7 +75,14 @@ public sealed class BoundedLeastSquaresTests { for (var j = 0; j < n; j++) x[j] = (float)xd[j]; var g = Gradient(m, n, a, b, x); - for (var j = 0; j < n; j++) xd[j] = Math.Clamp(xd[j] - step * g[j], lo[j], hi[j]); + var moved = 0.0; + for (var j = 0; j < n; j++) + { + var next = Math.Clamp(xd[j] - step * g[j], lo[j], hi[j]); + moved = Math.Max(moved, Math.Abs(next - xd[j])); + xd[j] = next; + } + if (moved < 1e-10) break; } for (var j = 0; j < n; j++) x[j] = (float)xd[j]; return Cost(m, n, a, b, x);