]> Creatis software - CreaPhase.git/blob - octave_packages/m/linear-algebra/ishermitian.m
update packages
[CreaPhase.git] / octave_packages / m / linear-algebra / ishermitian.m
1 ## Copyright (C) 1996-2012 John W. Eaton
2 ## Copyright (C) 2009 VZLU Prague
3 ##
4 ## This file is part of Octave.
5 ##
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.
10 ##
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.
15 ##
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/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {} ishermitian (@var{x})
22 ## @deftypefnx {Function File} {} ishermitian (@var{x}, @var{tol})
23 ## Return true if @var{x} is Hermitian within the tolerance specified by
24 ## @var{tol}.
25 ## The default tolerance is zero (uses faster code).
26 ## Matrix @var{x} is considered symmetric if
27 ## @code{norm (@var{x} - @var{x}', Inf) / norm (@var{x}, Inf) < @var{tol}}.
28 ## @seealso{issymmetric, isdefinite}
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 = isnumeric (x) && 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)));