From 79e2561bd38f96b80accdd6f9ab4b405933338c0 Mon Sep 17 00:00:00 2001 From: n0228a Date: Mon, 3 Aug 2026 12:53:48 +0200 Subject: [PATCH] switch to brentq solver --- src/gstools/covmodel/tools.py | 10 +++++++--- tests/test_covmodel.py | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/gstools/covmodel/tools.py b/src/gstools/covmodel/tools.py index 1d26d062..6737786a 100644 --- a/src/gstools/covmodel/tools.py +++ b/src/gstools/covmodel/tools.py @@ -27,7 +27,7 @@ import numpy as np from hankel import SymmetricFourierTransform as SFT from scipy import special as sps -from scipy.optimize import root +from scipy.optimize import root_scalar from gstools.tools.geometric import no_of_angles, set_angles, set_anis from gstools.tools.misc import list_format @@ -439,8 +439,12 @@ def percentile_scale(model, per=0.9): def curve(x): return 1.0 - model.correlation(x) - per - # take 'per * len_rescaled' as initial guess - return root(curve, per * model.len_rescaled)["x"][0] + # upper bound for bracket where curve(b) > 0 + b = float(max(model.len_rescaled, 1e-5)) + while curve(b) <= 0: + b *= 2.0 + + return root_scalar(curve, bracket=[0.0, b], method="brentq").root def set_arg_bounds(model, check_args=True, **kwargs): diff --git a/tests/test_covmodel.py b/tests/test_covmodel.py index bf079472..12d34ddd 100644 --- a/tests/test_covmodel.py +++ b/tests/test_covmodel.py @@ -351,6 +351,12 @@ def test_covmodel_class(self): self.assertRaises(ValueError, Gau_fix, latlon=True) # check inputs self.assertRaises(ValueError, model_std.percentile_scale, per=-1.0) + # check positive percentile scale for SuperSpherical across nu values + for nu in (0.5, 1.0, 2.0, 5.0, 10.0): + m = SuperSpherical(dim=2, nu=nu, len_scale=10.0) + ps = m.percentile_scale(0.9) + self.assertGreater(ps, 0.0) + self.assertAlmostEqual(m.correlation(ps), 0.1, places=5) self.assertRaises(ValueError, Gaussian, anis=-1.0) self.assertRaises(ValueError, Gaussian, len_scale=[1, -1]) self.assertRaises(ValueError, check_arg_in_bounds, model_std, "wrong")