# HG changeset patch # User Jaroslav Hajek # Date 1259071324 -3600 # Node ID ecd750d1eabdc60a67c80ded35f1aa6ef955ff0e # Parent 7f4939e76684501e175d3b491389b1c235a2e396 move issymmetric & isdefinite to linear-algebra, create ishermitian diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,12 @@ +2009-11-24 Jaroslav Hajek + + * general/issymmetric.m: Move to linear-algebra. + * general/isdefinite.m: Ditto. + * linear-algebra/issymmetric.m: Use 0 as default tolerance. Optimize + this case. Check for symmetry, not hermitianness. + * linear-algebra/ishermitian.m: New function. + * linear-algebra/isdefinite.m: Use ishermitian instead of issymmetric. + 2009-11-24 Jaroslav Hajek * general/cellidx.m: Deprecate. diff --git a/scripts/general/module.mk b/scripts/general/module.mk --- a/scripts/general/module.mk +++ b/scripts/general/module.mk @@ -38,13 +38,11 @@ general/interpft.m \ general/is_duplicate_entry.m \ general/isa.m \ - general/isdefinite.m \ general/isdir.m \ general/isequal.m \ general/isequalwithequalnans.m \ general/isscalar.m \ general/issquare.m \ - general/issymmetric.m \ general/isvector.m \ general/loadobj.m \ general/logspace.m \ diff --git a/scripts/general/isdefinite.m b/scripts/linear-algebra/isdefinite.m rename from scripts/general/isdefinite.m rename to scripts/linear-algebra/isdefinite.m --- a/scripts/general/isdefinite.m +++ b/scripts/linear-algebra/isdefinite.m @@ -39,8 +39,8 @@ tol = 100*eps; endif endif - sym = issymmetric (x, tol); - if (sym > 0) + sym = ishermitian (x); + if (sym) ## Matrix is symmetric, check eigenvalues. mineig = min (eig (x)); if (mineig > tol) diff --git a/scripts/linear-algebra/ishermitian.m b/scripts/linear-algebra/ishermitian.m new file mode 100644 --- /dev/null +++ b/scripts/linear-algebra/ishermitian.m @@ -0,0 +1,67 @@ +## Copyright (C) 1996, 1997, 2002, 2003, 2004, 2005, 2006, 2007, 2008 +## John W. Eaton +## Copyright (C) 2009 VZLU Prague +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {} ishermitian (@var{x}, @var{tol}) +## Return true if @var{x} is symmetric within the tolerance specified by @var{tol}, +## otherwise return false. The default tolerance is zero (uses faster code). +## Matrix @var{x} is considered symmetric if +## @code{norm (@var{x} - @var{x}.', inf) / norm (@var{x}, inf) < @var{tol}}. +## @seealso{size, rows, columns, length, ismatrix, isscalar, +## issquare, isvector} +## @end deftypefn + +## Author: A. S. Hodel +## Created: August 1993 +## Adapted-By: jwe + +function retval = ishermitian (x, tol = 0) + + if (nargin < 1 || nargin > 2) + print_usage (); + endif + + retval = issquare (x); + if (retval) + if (tol == 0) + retval = all ((x == x')(:)); + else + norm_x = norm (x, inf); + retval = norm_x == 0 || norm (x - x', inf) / norm_x <= tol; + endif + endif + +endfunction + +%!assert(ishermitian (1)); +%!assert(!(ishermitian ([1, 2]))); +%!assert(ishermitian ([])); +%!assert(ishermitian ([1, 2; 2, 1])); +%!assert(!(ishermitian ("test"))); +%!assert(ishermitian ([1, 2.1; 2, 1.1], 0.2)); +%!assert(ishermitian ([1, -2i; 2i, 1])); +%!assert(!(ishermitian ("t"))); +%!assert(!(ishermitian (["te"; "et"]))); +%!error ishermitian ([1, 2; 2, 1], 0, 0); +%!error ishermitian (); + +%!test +%! s.a = 1; +%! assert(!(ishermitian (s))); diff --git a/scripts/general/issymmetric.m b/scripts/linear-algebra/issymmetric.m rename from scripts/general/issymmetric.m rename to scripts/linear-algebra/issymmetric.m --- a/scripts/general/issymmetric.m +++ b/scripts/linear-algebra/issymmetric.m @@ -1,5 +1,6 @@ ## Copyright (C) 1996, 1997, 2002, 2003, 2004, 2005, 2006, 2007, 2008 ## John W. Eaton +## Copyright (C) 2009 VZLU Prague ## ## This file is part of Octave. ## @@ -19,9 +20,9 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} issymmetric (@var{x}, @var{tol}) -## If @var{x} is symmetric within the tolerance specified by @var{tol}, -## then return the dimension of @var{x}. Otherwise, return 0. If -## @var{tol} is omitted, use a tolerance equal to the machine precision. +## Return true if @var{x} is a symmetric matrix within the tolerance specified +## by @var{tol}, otherwise return false. The default tolerance is zero (uses +## faster code). ## Matrix @var{x} is considered symmetric if ## @code{norm (@var{x} - @var{x}.', inf) / norm (@var{x}, inf) < @var{tol}}. ## @seealso{size, rows, columns, length, ismatrix, isscalar, @@ -32,36 +33,31 @@ ## Created: August 1993 ## Adapted-By: jwe -function retval = issymmetric (x, tol) +function retval = issymmetric (x, tol = 0) + + if (nargin < 1 || nargin > 2) + print_usage (); + endif - if (nargin == 1 || nargin == 2) - retval = issquare (x); - if (retval != 0) - if (nargin == 1) - if (isa (x, "single")) - tol = eps("single"); - else - tol = eps; - endif - endif + retval = issquare (x); + if (retval) + if (tol == 0) + retval = all ((x == x.')(:)); + else norm_x = norm (x, inf); - if (norm_x != 0 && norm (x - x', inf) / norm_x > tol) - retval = 0; - endif + retval = norm_x == 0 || norm (x - x.', inf) / norm_x <= tol; endif - else - print_usage (); endif endfunction %!assert(issymmetric (1)); %!assert(!(issymmetric ([1, 2]))); -%!assert(!(issymmetric ([]))); -%!assert(issymmetric ([1, 2; 2, 1]) == 2); +%!assert(issymmetric ([])); +%!assert(issymmetric ([1, 2; 2, 1])); %!assert(!(issymmetric ("test"))); -%!assert(issymmetric ([1, 2.1; 2, 1.1], 0.2) == 2); -%!assert(issymmetric ([1, 2i; -2i, 1])); +%!assert(issymmetric ([1, 2.1; 2, 1.1], 0.2)); +%!assert(issymmetric ([1, 2i; 2i, 1])); %!assert(!(issymmetric ("t"))); %!assert(!(issymmetric (["te"; "et"]))); %!error issymmetric ([1, 2; 2, 1], 0, 0); diff --git a/scripts/linear-algebra/module.mk b/scripts/linear-algebra/module.mk --- a/scripts/linear-algebra/module.mk +++ b/scripts/linear-algebra/module.mk @@ -9,6 +9,9 @@ linear-algebra/duplication_matrix.m \ linear-algebra/expm.m \ linear-algebra/housh.m \ + linear-algebra/isdefinite.m \ + linear-algebra/ishermitian.m \ + linear-algebra/issymmetric.m \ linear-algebra/krylov.m \ linear-algebra/krylovb.m \ linear-algebra/logm.m \