1######################################################################## 2## 3## Copyright (C) 2019-2021 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {} mustBeGreaterThanOrEqual (@var{x}, @var{c}) 28## 29## Require that input @var{x} is greater than or equal to @var{c}. 30## 31## Raise an error if any element of the input @var{x} is not greater than 32## or equal to @var{c}, as determined by @code{@var{x} >= @var{c}}. 33## 34## @seealso{mustBeGreaterThan, mustBeLessThanOrEqual, ge} 35## @end deftypefn 36 37function mustBeGreaterThanOrEqual (x, c) 38 39 if (nargin != 2) 40 print_usage (); 41 endif 42 43 tf = (x >= c)(:); 44 if (! all (tf)) 45 label = inputname (1); 46 if (isempty (label)) 47 label = "input"; 48 endif 49 bad_idx = find (! tf); 50 try 51 bad_val = x(bad_idx); 52 errmsg = sprintf ("%s must be greater than or equal to %f; found %d elements that were not: values %s", ... 53 label, c, numel (bad_idx), mat2str (bad_val)); 54 catch 55 errmsg = sprintf ("%s must be greater than or equal to %f; found %d elements that were not: indexes %s", ... 56 label, c, numel (bad_idx), mat2str (bad_idx)); 57 end_try_catch 58 error (errmsg); 59 endif 60 61endfunction 62 63 64%!test 65%! mustBeGreaterThanOrEqual (42, 0); 66%! mustBeGreaterThanOrEqual (Inf, 9999); 67%! mustBeGreaterThanOrEqual (42, 42); 68%! mustBeGreaterThanOrEqual (Inf, Inf); 69 70%!error <Invalid call> mustBeGreaterThanOrEqual () 71%!error <Invalid call> mustBeGreaterThanOrEqual (1) 72%!error <Invalid call> mustBeGreaterThanOrEqual (1,2,3) 73%!error <must be greater than or equal to 2> mustBeGreaterThanOrEqual (1, 2) 74%!error <must be greater than or equal to 0> mustBeGreaterThanOrEqual (NaN, 0) 75