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 8 files changed, 207 insertions(+), 134 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Nov 24 14:15:43 2009 +0100
+++ b/scripts/ChangeLog	Tue Nov 24 15:02:04 2009 +0100
@@ -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/isdefinite.m	Tue Nov 24 14:15:43 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,60 +0,0 @@
-## Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Gabriele Pannocchia
-##
-## 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} {} isdefinite (@var{x}, @var{tol})
-## Return 1 if @var{x} is symmetric positive definite within the
-## tolerance specified by @var{tol} or 0 if @var{x} is symmetric
-## positive semidefinite.  Otherwise, return -1.  If @var{tol}
-## is omitted, use a tolerance equal to 100 times the machine precision.
-## @seealso{issymmetric}
-## @end deftypefn
-
-## Author: Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
-## Created: November 2003
-## Adapted-By: jwe
-
-function retval = isdefinite (x, tol)
-
-  if (nargin == 1 || nargin == 2)
-    if (nargin == 1)
-      if (isa (x, "single"))
-	tol = 100 * eps("single");
-      else
-	tol = 100*eps; 
-      endif
-    endif
-    sym = issymmetric (x, tol);
-    if (sym > 0)
-      ## Matrix is symmetric, check eigenvalues.
-      mineig = min (eig (x));
-      if (mineig > tol)
-	retval = 1;
-      elseif (mineig > -tol)
-	retval = 0;
-      else
-	retval = -1;
-      endif
-    else
-      error ("isdefinite: matrix x must be symmetric");
-    endif
-  else
-    print_usage ();
-  endif
-
-endfunction
--- a/scripts/general/issymmetric.m	Tue Nov 24 14:15:43 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,72 +0,0 @@
-## Copyright (C) 1996, 1997, 2002, 2003, 2004, 2005, 2006, 2007, 2008
-##               John W. Eaton
-##
-## 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} {} 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.
-## 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 = issymmetric (x, tol)
-
-  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
-      norm_x = norm (x, inf);
-      if (norm_x != 0 && norm (x - x', inf) / norm_x > tol)
-        retval = 0;
-      endif
-    endif
-  else
-    print_usage ();
-  endif
-
-endfunction
-
-%!assert(issymmetric (1));
-%!assert(!(issymmetric ([1, 2])));
-%!assert(!(issymmetric ([])));
-%!assert(issymmetric ([1, 2; 2, 1]) == 2);
-%!assert(!(issymmetric ("test")));
-%!assert(issymmetric ([1, 2.1; 2, 1.1], 0.2) == 2);
-%!assert(issymmetric ([1, 2i; -2i, 1]));
-%!assert(!(issymmetric ("t")));
-%!assert(!(issymmetric (["te"; "et"])));
-%!error issymmetric ([1, 2; 2, 1], 0, 0);
-%!error issymmetric ();
-
-%!test
-%! s.a = 1;
-%! assert(!(issymmetric (s)));
--- a/scripts/general/module.mk	Tue Nov 24 14:15:43 2009 +0100
+++ b/scripts/general/module.mk	Tue Nov 24 15:02:04 2009 +0100
@@ -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 \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/linear-algebra/isdefinite.m	Tue Nov 24 15:02:04 2009 +0100
@@ -0,0 +1,60 @@
+## Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Gabriele Pannocchia
+##
+## 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} {} isdefinite (@var{x}, @var{tol})
+## Return 1 if @var{x} is symmetric positive definite within the
+## tolerance specified by @var{tol} or 0 if @var{x} is symmetric
+## positive semidefinite.  Otherwise, return -1.  If @var{tol}
+## is omitted, use a tolerance equal to 100 times the machine precision.
+## @seealso{issymmetric}
+## @end deftypefn
+
+## Author: Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
+## Created: November 2003
+## Adapted-By: jwe
+
+function retval = isdefinite (x, tol)
+
+  if (nargin == 1 || nargin == 2)
+    if (nargin == 1)
+      if (isa (x, "single"))
+	tol = 100 * eps("single");
+      else
+	tol = 100*eps; 
+      endif
+    endif
+    sym = ishermitian (x);
+    if (sym)
+      ## Matrix is symmetric, check eigenvalues.
+      mineig = min (eig (x));
+      if (mineig > tol)
+	retval = 1;
+      elseif (mineig > -tol)
+	retval = 0;
+      else
+	retval = -1;
+      endif
+    else
+      error ("isdefinite: matrix x must be symmetric");
+    endif
+  else
+    print_usage ();
+  endif
+
+endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/linear-algebra/ishermitian.m	Tue Nov 24 15:02:04 2009 +0100
@@ -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)));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/linear-algebra/issymmetric.m	Tue Nov 24 15:02:04 2009 +0100
@@ -0,0 +1,68 @@
+## 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} {} issymmetric (@var{x}, @var{tol})
+## 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,
+## issquare, isvector}
+## @end deftypefn
+
+## Author: A. S. Hodel <scotte@eng.auburn.edu>
+## Created: August 1993
+## Adapted-By: jwe
+
+function retval = issymmetric (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(issymmetric (1));
+%!assert(!(issymmetric ([1, 2])));
+%!assert(issymmetric ([]));
+%!assert(issymmetric ([1, 2; 2, 1]));
+%!assert(!(issymmetric ("test")));
+%!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);
+%!error issymmetric ();
+
+%!test
+%! s.a = 1;
+%! assert(!(issymmetric (s)));
--- a/scripts/linear-algebra/module.mk	Tue Nov 24 14:15:43 2009 +0100
+++ b/scripts/linear-algebra/module.mk	Tue Nov 24 15:02:04 2009 +0100
@@ -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 \