changeset 30589:913f765057d9

poly.m: Improve input validation (bug #61759) * poly.m: Add checks for non-numeric input, N-dimensional arrays, and non-square matrices. Add BIST tests.
author Arun Giridhar <arungiridhar@gmail.com>
date Mon, 03 Jan 2022 11:49:05 -0800
parents ed17822e7662
children 50bf281760e0
files scripts/polynomial/poly.m
diffstat 1 files changed, 15 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/polynomial/poly.m	Sat Jan 01 19:43:52 2022 -0800
+++ b/scripts/polynomial/poly.m	Mon Jan 03 11:49:05 2022 -0800
@@ -61,17 +61,21 @@
     print_usage ();
   endif
 
-  m = min (size (x));
-  n = max (size (x));
-  if (m == 0)
+  if (! isnumeric (x))
+    error ("poly: input must be numeric, not type %s", class (x));
+  elseif (ndims (x) > 2)
+    error ("poly: input must be a vector or a square matrix");
+  elseif (isempty (x))
     y = 1;
     return;
-  elseif (m == 1)
+  elseif (isvector (x))
+    n = numel (x);
     v = x;
-  elseif (m == n)
+  elseif (! issquare (x))
+    error ("poly: input matrix must be square");
+  else
+    n = size (x,1);
     v = eig (x);
-  else
-    print_usage ();
   endif
 
   y = zeros (1, n+1);
@@ -113,4 +117,7 @@
 %! assert (y, [1 + 0i, -9 - 3i, 25 + 24i, -17 - 57i, -12 + 36i]);
 
 %!error <Invalid call> poly ()
-%!error poly ([1, 2, 3; 4, 5, 6])
+%!error <input must be numeric> poly ("foo")
+%!error <input must be numeric> poly ({1, "foo"; "bar", 1})
+%!error <input must be a vector or a square matrix> poly (ones (2, 2, 2))
+%!error <matrix must be square> poly ([1, 2, 3; 4, 5, 6])