Mercurial > hg > octave-lyh
changeset 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 | 7f4939e76684 |
children | 5b733adba096 |
files | scripts/ChangeLog scripts/general/isdefinite.m scripts/general/issymmetric.m scripts/general/module.mk scripts/linear-algebra/isdefinite.m scripts/linear-algebra/ishermitian.m scripts/linear-algebra/issymmetric.m scripts/linear-algebra/module.mk |
diffstat | 6 files changed, 100 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,12 @@ +2009-11-24 Jaroslav Hajek <highegg@gmail.com> + + * 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 <highegg@gmail.com> * general/cellidx.m: Deprecate.
--- 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 \
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)
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 +## <http://www.gnu.org/licenses/>. + +## -*- 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 <scotte@eng.auburn.edu> +## 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)));
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);
--- 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 \