comparison scripts/linear-algebra/ishermitian.m @ 9869:ecd750d1eabd

move issymmetric & isdefinite to linear-algebra, create ishermitian
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 24 Nov 2009 15:02:04 +0100
parents
children 73fc43e01f4c
comparison
equal deleted inserted replaced
9868:7f4939e76684 9869:ecd750d1eabd
1 ## Copyright (C) 1996, 1997, 2002, 2003, 2004, 2005, 2006, 2007, 2008
2 ## John W. Eaton
3 ## Copyright (C) 2009 VZLU Prague
4 ##
5 ## This file is part of Octave.
6 ##
7 ## Octave is free software; you can redistribute it and/or modify it
8 ## under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 3 of the License, or (at
10 ## your option) any later version.
11 ##
12 ## Octave is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ## General Public License for more details.
16 ##
17 ## You should have received a copy of the GNU General Public License
18 ## along with Octave; see the file COPYING. If not, see
19 ## <http://www.gnu.org/licenses/>.
20
21 ## -*- texinfo -*-
22 ## @deftypefn {Function File} {} ishermitian (@var{x}, @var{tol})
23 ## Return true if @var{x} is symmetric within the tolerance specified by @var{tol},
24 ## otherwise return false. 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{size, rows, columns, length, ismatrix, isscalar,
28 ## issquare, isvector}
29 ## @end deftypefn
30
31 ## Author: A. S. Hodel <scotte@eng.auburn.edu>
32 ## Created: August 1993
33 ## Adapted-By: jwe
34
35 function retval = ishermitian (x, tol = 0)
36
37 if (nargin < 1 || nargin > 2)
38 print_usage ();
39 endif
40
41 retval = issquare (x);
42 if (retval)
43 if (tol == 0)
44 retval = all ((x == x')(:));
45 else
46 norm_x = norm (x, inf);
47 retval = norm_x == 0 || norm (x - x', inf) / norm_x <= tol;
48 endif
49 endif
50
51 endfunction
52
53 %!assert(ishermitian (1));
54 %!assert(!(ishermitian ([1, 2])));
55 %!assert(ishermitian ([]));
56 %!assert(ishermitian ([1, 2; 2, 1]));
57 %!assert(!(ishermitian ("test")));
58 %!assert(ishermitian ([1, 2.1; 2, 1.1], 0.2));
59 %!assert(ishermitian ([1, -2i; 2i, 1]));
60 %!assert(!(ishermitian ("t")));
61 %!assert(!(ishermitian (["te"; "et"])));
62 %!error ishermitian ([1, 2; 2, 1], 0, 0);
63 %!error ishermitian ();
64
65 %!test
66 %! s.a = 1;
67 %! assert(!(ishermitian (s)));