Mercurial > octave
changeset 20661:014003273737
isdiag.m: Add shortcut code for inputs which are diagonal matrices already.
* isdiag.m: Check if the typeinfo indicates the input is a diagonal matrix and
return true. Rework BIST tests.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 27 Oct 2015 08:41:36 -0700 |
parents | 63f975ff1f7c |
children | 5c3dc2650e4f |
files | scripts/linear-algebra/isdiag.m |
diffstat | 1 files changed, 11 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/linear-algebra/isdiag.m Mon Oct 26 15:28:36 2015 -0700 +++ b/scripts/linear-algebra/isdiag.m Tue Oct 27 08:41:36 2015 -0700 @@ -30,25 +30,28 @@ print_usage (); endif - retval = (isnumeric (A) || islogical (A)) && ndims (A) == 2; - if (retval) + if (strcmp (typeinfo (A), "diagonal matrix")) + retval = true; + elseif ((isnumeric (A) || islogical (A)) && ndims (A) == 2) [i, j] = find (A); retval = all (i == j); + else + retval = false; endif endfunction -%!assert (! isdiag ("string")) +%!assert (isdiag ("string"), false) +%!assert (isdiag (zeros (2,2,2)), false) %!assert (isdiag ([])) - %!assert (isdiag (1)) -%!assert (! isdiag ([1, 1])) -%!assert (! isdiag ([1; 1])) +%!assert (isdiag ([1, 1]), false) +%!assert (isdiag ([1; 1]), false) %!assert (isdiag (eye (10))) -%!assert (issymmetric ([i, 0; 0, 1 + i])) -%!assert (isdiag (speye (1000000))) %!assert (isdiag (logical (eye (10)))) +%!assert (isdiag (speye (1e6))) +%!assert (isdiag (diag (1:10))) ## Test input validation %!error isdiag ()