1 ## Copyright (C) 1996-2012 John W. Eaton
2 ## Copyright (C) 2009 VZLU Prague
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {} issymmetric (@var{x})
22 ## @deftypefnx {Function File} {} issymmetric (@var{x}, @var{tol})
23 ## Return true if @var{x} is a symmetric matrix within the tolerance specified
24 ## by @var{tol}. The default tolerance is zero (uses faster code).
25 ## Matrix @var{x} is considered symmetric if
26 ## @code{norm (@var{x} - @var{x}.', Inf) / norm (@var{x}, Inf) < @var{tol}}.
27 ## @seealso{ishermitian, isdefinite}
30 ## Author: A. S. Hodel <scotte@eng.auburn.edu>
31 ## Created: August 1993
34 function retval = issymmetric (x, tol = 0)
36 if (nargin < 1 || nargin > 2)
40 retval = isnumeric (x) && issquare (x);
43 retval = all ((x == x.')(:));
45 norm_x = norm (x, inf);
46 retval = norm_x == 0 || norm (x - x.', inf) / norm_x <= tol;
52 %!assert(issymmetric (1));
53 %!assert(!(issymmetric ([1, 2])));
54 %!assert(issymmetric ([]));
55 %!assert(issymmetric ([1, 2; 2, 1]));
56 %!assert(!(issymmetric ("test")));
57 %!assert(issymmetric ([1, 2.1; 2, 1.1], 0.2));
58 %!assert(issymmetric ([1, 2i; 2i, 1]));
59 %!assert(!(issymmetric ("t")));
60 %!assert(!(issymmetric (["te"; "et"])));
61 %!error issymmetric ([1, 2; 2, 1], 0, 0);
62 %!error issymmetric ();
66 %! assert(!(issymmetric (s)));