diff --git a/python/ffsim/optimize/linear_method.py b/python/ffsim/optimize/linear_method.py index b9d2a5997..e8f47a6f9 100644 --- a/python/ffsim/optimize/linear_method.py +++ b/python/ffsim/optimize/linear_method.py @@ -149,10 +149,8 @@ def minimize_linear_method( """ # noqa: E501 if regularization < 0: raise ValueError(f"regularization must be nonnegative. Got {regularization}.") - if not 0 < variation < 1: - raise ValueError( - f"variation must be strictly between 0 and 1. Got {variation}." - ) + if not 0 <= variation <= 1: + raise ValueError(f"variation must be between 0 and 1. Got {variation}.") if maxiter < 1: raise ValueError(f"maxiter must be at least 1. Got {maxiter}.") @@ -160,7 +158,7 @@ def minimize_linear_method( optimize_hyperparameters_args = dict(method="L-BFGS-B") regularization_param = math.sqrt(regularization) - variation_param = math.atanh(2 * variation - 1) + variation_param = math.atanh(2 * min(1 - 1e-8, max(1e-8, variation)) - 1) params = x0.copy() converged = False intermediate_result = OptimizeResult( diff --git a/tests/optimize/linear_method_test.py b/tests/optimize/linear_method_test.py index 763309379..cb475f47a 100644 --- a/tests/optimize/linear_method_test.py +++ b/tests/optimize/linear_method_test.py @@ -119,11 +119,11 @@ def callback(intermediate_result): ) with pytest.raises(ValueError, match="variation"): result = ffsim.optimize.minimize_linear_method( - params_to_vec, x0=x0, hamiltonian=hamiltonian, variation=0 + params_to_vec, x0=x0, hamiltonian=hamiltonian, variation=-0.1 ) with pytest.raises(ValueError, match="variation"): result = ffsim.optimize.minimize_linear_method( - params_to_vec, x0=x0, hamiltonian=hamiltonian, variation=1 + params_to_vec, x0=x0, hamiltonian=hamiltonian, variation=1.1 ) with pytest.raises(ValueError, match="maxiter"): result = ffsim.optimize.minimize_linear_method(