Mercurial > hg > octave-terminal
changeset 7558:690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
author | Sebastien Loisel |
---|---|
date | Wed, 05 Mar 2008 04:44:49 -0500 |
parents | 2ba84879f961 |
children | 07522d7dcdf8 |
files | scripts/ChangeLog scripts/polynomial/roots.m |
diffstat | 2 files changed, 35 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,12 @@ +2008-03-05 Ben Abbott <bpabbott@mac.com> + + * polynomial/roots.m: Catch Infs and/or NaNs. + +2008-03-05 Sebastien Loisel <loisel@temple.edu> + + * polynomial/roots.m: Apply a scaling factor to the removal of the + leading zeros. + 2008-03-04 John W. Eaton <jwe@octave.org> * 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 <bpabbott@mac.com> +2008-03-04 Ben Abbott <bpabbott@mac.com> * polynomial/polyfit.m: Modified tests to respect a relative tolerance.
--- 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)