octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

a few minor changes proposal


From: Jaroslav Hajek
Subject: a few minor changes proposal
Date: Wed, 25 Nov 2009 11:18:13 +0100

hi all,

I propose the following changes:
1. Deprecate cellidx. This seems to be a leftover from the old control functions, that was moved to general probably because it was used elsewhere. Alas, it is not used anywhere now. It even talks about signals, not strings, and the name is not descriptive enough to suggest that it is a string operation (generally those are prefixed by str...). The same goal is  achievable using ismember, and more efficiently because the cellidx implementation just uses a double loop.

2. change the behaviour of issquare (x) to a simple ndims (x) == 2 && rows (x) == columns (x). Currently, issquare ([]) is false, which I think is damn confusing and unexpected.

3. Move issymmetric and isdefinite to linear-algebra. Contrary to its name and documentation, issymmetric actually checks for hermitianness, so create also ishermitian to clean up. Also, I think the default tolerance should be zero (check for exact symmetry), boiling down to the simple test: all ((x == x.')(:)). And finally, like for the issquare method, these should be made consistent for 0x0 matrix.

patches attached.

any objections/comments?
all of these funcs are specific to Octave.

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


# HG changeset patch
# User Jaroslav Hajek <address@hidden>
# Date 1259068322 -3600
# Node ID 162160c8347df7848400a59ce92851217f736eac
# Parent  c0aeedd8fb86f4bb43148221984eb2d5a53703b9
make issquare consistent for empty matrices

diff --git a/scripts/ChangeLog b/scripts/ChangeLog
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -0,0 +1,5 @@
+2009-11-24  Jaroslav Hajek  <address@hidden>
+
+    * general/issquare.m: Change to return consistent result for empty
+    matrices.
+
diff --git a/scripts/general/issquare.m b/scripts/general/issquare.m
--- a/scripts/general/issquare.m
+++ b/scripts/general/issquare.m
@@ -18,8 +18,8 @@
 
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {} issquare (@var{x})
-## If @var{x} is a square matrix, then return the dimension of @var{x}.
-## Otherwise, return 0.
+## If @var{x} is a square matrix, return true.
+## Otherwise, return false.
 ## @seealso{size, rows, columns, length, ismatrix, isscalar, isvector}
 ## @end deftypefn
 
@@ -29,14 +29,12 @@
 
 function retval = issquare (x)
 
-  retval = 0;
-
   if (nargin == 1)
-    if (ismatrix (x) && ndims (x) < 3)
-      [nr, nc] = size (x);
-      if (nr == nc && nr > 0)
-        retval = nr;
-      endif
+    if (ndims (x) == 2)
+      [r, c] = size (x);
+      retval = r == c;
+    else
+      retval = false;
     endif
   else
     print_usage ();
@@ -50,7 +48,7 @@
 
 %!assert(!(issquare ([])));
 
-%!assert(issquare ([1, 2; 3, 4]) == 2);
+%!assert(issquare ([1, 2; 3, 4]));
 
 %!test
 %! warn_str_to_num = 0;
# HG changeset patch
# User Jaroslav Hajek <address@hidden>
# Date 1259068543 -3600
# Node ID e9b4d3d1e1ce71d96b58e410c69fc24674fc7555
# Parent  162160c8347df7848400a59ce92851217f736eac
deprecate cellidx

diff --git a/scripts/ChangeLog b/scripts/ChangeLog
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -0,0 +1,4 @@
+2009-11-24  Jaroslav Hajek  <address@hidden>
+
+    * general/cellidx.m: Deprecate.
+
diff --git a/scripts/general/cellidx.m b/scripts/deprecated/cellidx.m
rename from scripts/general/cellidx.m
rename to scripts/deprecated/cellidx.m
--- a/scripts/general/cellidx.m
+++ b/scripts/deprecated/cellidx.m
@@ -34,8 +34,17 @@
 ## screen and exits with an error.
 ## @end deftypefn
 
+## deprecated in version 3.4
+
 function [idxvec,errmsg]  = cellidx (listvar, strlist)
 
+  persistent warned = false;
+  if (! warned)
+    warned = true;
+    warning ("Octave:deprecated-function",
+             "cellidx is obsolete and will be removed from a future version of Octave; use ismember instead.");
+  endif
+
   if (nargin != 2)
     print_usage ();
   endif
diff --git a/scripts/deprecated/module.mk b/scripts/deprecated/module.mk
--- a/scripts/deprecated/module.mk
+++ b/scripts/deprecated/module.mk
@@ -9,6 +9,7 @@
   deprecated/binomial_inv.m \
   deprecated/binomial_pdf.m \
   deprecated/binomial_rnd.m \
+  deprecated/cellidx.m \
   deprecated/chisquare_cdf.m \
   deprecated/chisquare_inv.m \
   deprecated/chisquare_pdf.m \
diff --git a/scripts/general/module.mk b/scripts/general/module.mk
--- a/scripts/general/module.mk
+++ b/scripts/general/module.mk
@@ -12,7 +12,6 @@
   general/blkdiag.m \
   general/cart2pol.m \
   general/cart2sph.m \
-  general/cellidx.m \
   general/cell2mat.m \
   general/celldisp.m \
   general/circshift.m \
# HG changeset patch
# User Jaroslav Hajek <address@hidden>
# Date 1259071324 -3600
# Node ID 514a13dbfa59b9bc67c509f68457d0d1d3e5ab76
# Parent  e9b4d3d1e1ce71d96b58e410c69fc24674fc7555
move issymmetric & isdefinite to linear-algebra, create ishermitian

diff --git a/scripts/ChangeLog b/scripts/ChangeLog
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -0,0 +1,9 @@
+2009-11-24  Jaroslav Hajek  <address@hidden>
+
+    * 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.
+
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
+## <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 <address@hidden>
+## 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,8 @@
 
 ## -*- 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 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,
@@ -32,25 +32,20 @@
 ## Created: August 1993
 ## Adapted-By: jwe
 
-function retval = issymmetric (x, tol)
+function retval = issymmetric (x, tol = 0)
 
-  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
+  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);
-      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
@@ -58,10 +53,10 @@
 %!assert(issymmetric (1));
 %!assert(!(issymmetric ([1, 2])));
 %!assert(!(issymmetric ([])));
-%!assert(issymmetric ([1, 2; 2, 1]) == 2);
+%!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 \


reply via email to

[Prev in Thread] Current Thread [Next in Thread]