1######################################################################## 2## 3## Copyright (C) 2014-2021 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {} isdiag (@var{A}) 28## Return true if @var{A} is a diagonal matrix. 29## @seealso{isbanded, istril, istriu, diag, bandwidth} 30## @end deftypefn 31 32function retval = isdiag (A) 33 34 if (nargin != 1) 35 print_usage (); 36 endif 37 38 if (strfind (typeinfo (A), "diagonal matrix")) 39 retval = true; 40 elseif ((isnumeric (A) || islogical (A)) && ndims (A) == 2) 41 [i, j] = find (A); 42 retval = all (i == j); 43 else 44 retval = false; 45 endif 46 47endfunction 48 49 50%!assert (isdiag ("string"), false) 51%!assert (isdiag (zeros (2,2,2)), false) 52%!assert (isdiag (zeros (2))) 53%!assert (isdiag ([])) 54%!assert (isdiag (1)) 55%!assert (isdiag ([1, 1]), false) 56%!assert (isdiag ([1; 1]), false) 57%!assert (isdiag (eye (10))) 58%!assert (isdiag (single (eye (10)))) 59%!assert (isdiag (logical (eye (10)))) 60%!assert (isdiag (speye (1e2))) 61%!assert (isdiag (diag (1:10))) 62 63## Test input validation 64%!error isdiag () 65%!error isdiag (1,2) 66