From f60fc5cdef7535b4ea8a543eb2c88cb431ca9c88 Mon Sep 17 00:00:00 2001 From: Richard Waite Date: Thu, 24 Oct 2024 11:05:12 +0100 Subject: [PATCH] Allow any non-finite value to indicate no bound --- .../unittest_ndbase_cost_function_wrapper.m | 4 ++-- swfiles/+ndbase/cost_function_wrapper.m | 15 +++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/+sw_tests/+unit_tests/unittest_ndbase_cost_function_wrapper.m b/+sw_tests/+unit_tests/unittest_ndbase_cost_function_wrapper.m index 833348b6..bca8e859 100644 --- a/+sw_tests/+unit_tests/unittest_ndbase_cost_function_wrapper.m +++ b/+sw_tests/+unit_tests/unittest_ndbase_cost_function_wrapper.m @@ -9,8 +9,8 @@ properties (TestParameter) bound_param_name = {'lb', 'ub'} - no_lower_bound = {[], [-inf, -inf]}; - no_upper_bound = {[], [inf, inf]}; + no_lower_bound = {[], [-inf, -inf], [NaN, -inf]}; + no_upper_bound = {[], [inf, inf], [inf, NaN]}; errors = {ones(1,3), [], zeros(1,3), 'NoField'} end diff --git a/swfiles/+ndbase/cost_function_wrapper.m b/swfiles/+ndbase/cost_function_wrapper.m index 77aab237..326eb8c5 100644 --- a/swfiles/+ndbase/cost_function_wrapper.m +++ b/swfiles/+ndbase/cost_function_wrapper.m @@ -40,19 +40,18 @@ % % `lb` % : Optional vector of doubles corresponding to the lower bound of the -% parameters. Empty vector [] or vector of -inf interpreted as no lower -% bound. +% parameters. Empty vector [] or vector of non-finite elements +% (e.g. -inf and NaN) are interpreted as no lower bound. % % `ub` % : Optional vector of doubles corresponding to the upper bound of the -% parameters. Empty vector [] or vector of inf interpreted as no upper -% bound. +% parameters. Empty vector [] or vector of non-finite elements +% (e.g. inf and NaN) are interpreted as no upper bound. % % `ifix` % : Optional vector of ints corresponding of indices of parameters to fix % (overides bounds if provided) -% -% ### Examples + properties (SetObservable) % data cost_func @@ -125,8 +124,8 @@ function init_bound_parameter_transforms(obj, pars, lb, ub, ifix) obj.ifixed = []; ipars = 1:numel(pars); % used later for ipar = ipars - has_lb = ~isempty(lb) && lb(ipar) > -inf; - has_ub = ~isempty(ub) && ub(ipar) < inf; + has_lb = ~isempty(lb) && isfinite(lb(ipar)); + has_ub = ~isempty(ub) && isfinite(ub(ipar)); is_fixed = any(uint8(ifix) == ipar); if has_lb && has_ub % both bounds specified and parameter not fixed