Mercurial > octave
diff scripts/ode/ode15i.m @ 28045:13dba3c069f8
Update input validation for odeXXX.m functions.
* ode15i.m, ode15s.m, ode23.m, ode45.m:
Use name of function in error messages directly rather than interpolating
variable "solver" in to a character matrix. Use double quotes around
field values such as "Jacobian" rather than single quotes. Use intermediate
variables to make the code more readable. Fix incorrect validation and tighten
the acceptance tests for Jacobian and Mass fields. Improve error messages from
generic "invalid argument" to more specifically state what is wrong with the
input.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 05 Feb 2020 09:14:08 -0800 |
parents | ace8dc642e4d |
children | 5664362da646 |
line wrap: on
line diff
--- a/scripts/ode/ode15i.m Tue Feb 04 16:29:37 2020 -0800 +++ b/scripts/ode/ode15i.m Wed Feb 05 09:14:08 2020 -0800 @@ -119,7 +119,7 @@ if (ischar (options.Jacobian)) if (! exist (options.Jacobian)) error ("Octave:invalid-input-arg", - [solver ": function '" options.Jacobian "' not found"]); + ['ode15i: "Jacobian" function "' options.Jacobian '" not found']); endif options.Jacobian = str2func (options.Jacobian); endif @@ -129,13 +129,13 @@ if (ischar (options.OutputFcn)) if (! exist (options.OutputFcn)) error ("Octave:invalid-input-arg", - [solver ": function '" options.OutputFcn "' not found"]); + ['ode15i: "OutputFcn" function "' options.OutputFcn '" not found']); endif options.OutputFcn = str2func (options.OutputFcn); endif if (! is_function_handle (options.OutputFcn)) error ("Octave:invalid-input-arg", - [solver ": invalid value assigned to field 'OutputFcn'"]); + 'ode15i: "OutputFcn" must be a valid function handle'); endif endif @@ -143,13 +143,13 @@ if (ischar (options.Events)) if (! exist (options.Events)) error ("Octave:invalid-input-arg", - [solver ": function '" options.Events "' not found"]); + ['ode15i: "Events" function "' options.Events '" not found']); endif options.Events = str2func (options.Events); endif if (! is_function_handle (options.Events)) error ("Octave:invalid-input-arg", - [solver ": invalid value assigned to field 'Events'"]); + 'ode15i: "Events" must be a valid function handle'); endif endif @@ -178,45 +178,45 @@ options.havejac = true; if (iscell (options.Jacobian)) if (numel (options.Jacobian) == 2) - if (issparse (options.Jacobian{1}) && issparse (options.Jacobian{2})) - options.havejacsparse = true; # Jac is sparse cell + J1 = options.Jacobian{1}; + J2 = options.Jacobian{2}; + if ( ! issquare (J1) || ! issquare (J2) + || rows (J1) != n || rows (J2) != n + || ! isnumeric (J1) || ! isnumeric (J2) + || ! isreal (J1) || ! isreal (J2)) + error ("Octave:invalid-input-arg", + 'ode15i: "Jacobian" matrices must be real square matrices'); endif - - if (any (size (options.Jacobian{1}) != [n n]) - || any (size (options.Jacobian{2}) != [n n]) - || ! isnumeric (options.Jacobian{1}) - || ! isnumeric (options.Jacobian{2}) - || ! isreal (options.Jacobian{1}) - || ! isreal (options.Jacobian{2})) - error ("Octave:invalid-input-arg", - [solver ": invalid value assigned to field 'Jacobian'"]); + if (issparse (J1) && issparse (J2)) + options.havejacsparse = true; # Jac is sparse cell endif else error ("Octave:invalid-input-arg", - [solver ": invalid value assigned to field 'Jacobian'"]); + 'ode15i: invalid value assigned to field "Jacobian"'); endif elseif (is_function_handle (options.Jacobian)) options.havejacfun = true; if (nargin (options.Jacobian) == 3) - [A, B] = options.Jacobian (trange(1), y0, yp0); - if (issparse (A) && issparse (B)) - options.havejacsparse = true; # Jac is sparse fun - endif + [J1, J2] = options.Jacobian (trange(1), y0, yp0); - if (any (size (A) != [n n]) || any (size (B) != [n n]) - || ! isnumeric (A) || ! isnumeric (B) || ! isreal (A) - || ! isreal (B)) + if ( ! issquare (J1) || rows (J1) != n + || ! isnumeric (J1) || ! isreal (J1) + || ! issquare (J2) || rows (J2) != n + || ! isnumeric (J2) || ! isreal (J2)) error ("Octave:invalid-input-arg", - [solver ": invalid value assigned to field 'Jacobian'"]); + 'ode15i: "Jacobian" function must evaluate to a real square matrix'); + endif + if (issparse (J1) && issparse (J2)) + options.havejacsparse = true; # Jac is sparse fun endif else error ("Octave:invalid-input-arg", - [solver ": invalid value assigned to field 'Jacobian'"]); + 'ode15i: invalid value assigned to field "Jacobian"'); endif else - error ("Octave:invalid-input-arg", - [solver ": invalid value assigned to field 'Jacobian'"]); + error ("Octave:invalid-input-arg", + 'ode15i: "Jacobian" field must be a function handle or 2-element cell array of square matrices'); endif endif @@ -225,7 +225,7 @@ if (numel (options.AbsTol) != 1 && numel (options.AbsTol) != n) error ("Octave:invalid-input-arg", - [solver ": invalid value assigned to field 'AbsTol'"]); + 'ode15i: invalid value assigned to field "AbsTol"'); elseif (numel (options.AbsTol) == n) options.haveabstolvec = true;