# HG changeset patch # User Sebastien Loisel # Date 1204710289 18000 # Node ID 690c91f741b8668661ffd66c1dcf9d24db80d7d0 # Parent 2ba84879f961fbc023aab0c72c593c70968fe2b2 Apply a scaling factor to leading zero removal in roots.m diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,12 @@ +2008-03-05 Ben Abbott + + * polynomial/roots.m: Catch Infs and/or NaNs. + +2008-03-05 Sebastien Loisel + + * polynomial/roots.m: Apply a scaling factor to the removal of the + leading zeros. + 2008-03-04 John W. Eaton * plot/print.m: Fix oops in applying last change. @@ -15,7 +24,7 @@ * pkg/pkg.m (pkg:configure_make): Make it work with recent changes in isspace handling with cell arrays of strings. -2008-03-04 Ben Abbott +2008-03-04 Ben Abbott * polynomial/polyfit.m: Modified tests to respect a relative tolerance. diff --git a/scripts/polynomial/roots.m b/scripts/polynomial/roots.m --- a/scripts/polynomial/roots.m +++ b/scripts/polynomial/roots.m @@ -83,16 +83,22 @@ if (nargin != 1 || min (size (v)) > 1) print_usage (); + elseif (any (isnan(v) | isinf(v))) + error ("roots: inputs must not contain Inf or NaN") endif - n = length (v); - v = reshape (v, 1, n); + n = numel (v); + v = v(:); ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the ## leading k zeros and n - k - l roots of the polynomial are zero. - f = find (v); - m = max (size (f)); + if (isempty (v)) + f = v; + else + f = find (v ./ max (abs (v))); + endif + m = numel (f); if (m > 0 && n > 1) v = v(f(1):f(m)); @@ -114,9 +120,24 @@ endfunction +%!test +%! p = [poly([3 3 3 3]), 0 0 0 0]; +%! r = sort (roots (p)); +%! assert (r, [0; 0; 0; 0; 3; 3; 3; 3], 0.001) + %!assert(all (all (abs (roots ([1, -6, 11, -6]) - [3; 2; 1]) < sqrt (eps)))); %!assert(isempty (roots ([]))); %!error roots ([1, 2; 3, 4]); + +%!assert(isempty (roots (1))); + %!error roots ([1, 2; 3, 4]); + +%!error roots ([1 Inf 1]); + +%!error roots ([1 NaN 1]); + +%!assert(roots ([1e-200, -1e200, 1]), 1e-200) +%!assert(roots ([1e-200, -1e200 * 1i, 1]), -1e-200 * 1i)