From bug-request at octave dot org Tue Nov 29 02:05:53 2005 Subject: Re: isequal From: William Poetra Yoga Hadisoeseno To: bug at octave dot org Date: Tue, 29 Nov 2005 16:04:49 +0800 ------=_Part_4684_20649011.1133251489667 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I'm sending the patch for isequal (along with __isequal__, isequalwithequalnans, and the ChangeLog entry) in the attachment. Is the ChangeLog entry correct (26/11)? If not, please change it. Btw, what's wrong with the patch for blkdiag.m? I didn't see it being applied... I planned to get blkdiag.m applied before isequal, but maybe John wanted isequal first? -- William Poetra Yoga Hadisoeseno ------=_Part_4684_20649011.1133251489667 Content-Type: text/plain; name=isequal.diff.txt; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="isequal.diff.txt" Index: scripts/ChangeLog =================================================================== RCS file: /cvs/octave/scripts/ChangeLog,v retrieving revision 1.467 diff -u -r1.467 ChangeLog --- a/scripts/ChangeLog 23 Nov 2005 03:55:08 -0000 1.467 +++ b/scripts/ChangeLog 29 Nov 2005 07:45:06 -0000 at @ -1,3 +1,10 @@ +2005-11-26 William Poetra Yoga Hadisoeseno + + * general/isequal.m: Add a new option nans_compare_equal + and rename to general/__isequal__.m. + * general/isequal.m, general/isequalwithequalnans.m: Wrapper + scripts for general/__isequal__.m. + 2005-11-22 John W. Eaton * plot/axis.m: Use %.16g instead of just %g to format ranges for Index: scripts/general/__isequal__.m =================================================================== RCS file: scripts/general/__isequal__.m diff -N scripts/general/__isequal__.m --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ b/scripts/general/__isequal__.m 29 Nov 2005 07:45:06 -0000 at @ -0,0 +1,206 @@ +## Copyright (C) 2000 Paul Kienzle +## +## This program 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 2 of the License, or +## (at your option) any later version. +## +## This program 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 this program; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +## -*- texinfo -*- +## at deftypefn {Function File} {} __isequal__ (@var{nans_compare_equal}, +## at var{x1}, @var{x2}, ...) +## Return true if at var{x1}, @var{x2}, ... are all equal and +## at var{nans_compare_equal} evaluates to false. +## +## If at var{nans_compare_equal} evaluates to true, then assume NaN == NaN. +## at end deftypefn +## +## at seealso{isequal, isequalwithequalnans} + +## Modified by: William Poetra Yoga Hadisoeseno + +## Algorithm: +## +## 1. Determine the class of x +## 2. If x is of the struct, cell, list or char class, for each +## argument after x, determine whether it has the same class +## and size as x. +## Otherwise, for each argument after x, verify that it is not +## of the struct, cell, list or char class, and that it has +## the same size as x. +## 3. For each argument after x, compare it for equality with x: +## a. struct compare each member by name, not by order (recursive) +## b. cell/list compare each member by order (recursive) +## c. char compare each member with strcmp +## d. compare each nonzero member, and assume NaN == NaN +## if nans_compare_equal is nonzero. + +function t = __isequal__ (nans_compare_equal, x, varargin) + + if (nargin < 3) + usage ("__isequal__ (nans_compare_equal, x1, x2, ...)"); + endif + + l_v = nargin - 2; + + if (isstruct (x)) + + n_x = length (fieldnames (x)); + + t = true; + for argn = 1:l_v + y = varargin{argn}; + t = t && isstruct (y) && (n_x == length (fieldnames (y))); + endfor + if (!t) + return; + endif + + for argn = 1:l_v + y = varargin{argn}; + for [v, k] = x + t = t && struct_contains (y, k) \ + && __isequal__ (nans_compare_equal, v, getfield (y, k)); + endfor + if (!t) + return; + endif + endfor + + elseif ((iscell (x)) || (islist (x))) + + x = x(:); + l_x = length (x); + + t = true; + for argn = 1:l_v + y = varargin{argn}(:); + t = t && (iscell (y) || islist (y)) && (l_x == length (y)); + endfor + if (!t) + return; + endif + + for argn = 1:l_v + y = varargin{argn}(:); + for p = 1:l_x + t = t && __isequal__ (nans_compare_equal, x{p}, y{p}); + endfor + if (!t) + return; + endif + endfor + + elseif (ischar (x)) + + l_x = size (x); + + t = true; + for argn = 1:l_v + y = varargin{argn}; + t = t && ischar (y) && (l_x == size (y)); + endfor + if (!t) + return; + endif + + for argn = 1:l_v + t = t && strcmp (x, varargin{argn}); + endfor + + else + + s_x = size (x); + + t = true; + for argn = 1:l_v + y = varargin{argn}; + t = t && (! (isstruct (y) || iscell (y) || islist (y) || ischar (y))) \ + && (s_x == size (y)); + endfor + if (!t) + return; + endif + + if (issparse (x)) + f_x = spfind (x); + else + f_x = find (x); + endif + l_f_x = length (f_x); + x = x(f_x); + for argn = 1:l_v + y = varargin{argn}; + if (issparse (y)) + f_y = spfind (y); + else + f_y = find (y); + endif + + t = (l_f_x == length (f_y)) && all (f_x == f_y); + if (!t) + return; + endif + + y = y(f_y); + m = (x == y); + t = all (m); + + if (!t) + if (nans_compare_equal) + t = isnan (x(!m)) && isnan (y(!m)); + else + return; + endif + endif + endfor + + endif + +endfunction + +# test for equality +%!assert(__isequal__(0,[1,2,3,4],[1,2,3,4])) +%!assert(__isequal__(1,{1,2,NaN,4},{1,2,NaN,4})) +%!assert(__isequal__(1,[1,2,NaN,4],[1,2,NaN,4])) +%!assert(__isequal__(0,['a','b','c','d'],['a','b','c','d'])) +# test for inequality +%!assert(__isequal__(0,[1,2,3,4],[1;2;3;4]),false) +%!assert(__isequal__(0,{1,2,3,4},[1,2,3,4]),false) +%!assert(__isequal__(0,[1,2,3,4],{1,2,3,4}),false) +%!assert(__isequal__(0,[1,2,NaN,4],[1,2,NaN,4]),false) +%!assert(__isequal__(1,[1,2,NaN,4],[1,NaN,3,4]),false) +%!assert(__isequal__(1,[1,2,NaN,4],[1,2,3,4]),false) +%!assert(__isequal__(0,['a','b','c','d'],['a';'b';'c';'d']),false) +# test for equality (struct) +%!test +%! A = struct (); +%! A.char1 = "abcd"; +%! A.int1 = 123; +%! B = struct (); +%! B.char1 = "abcd"; +%! B.int1 = 123; +%! C = struct (); +%! C.char1 = "abcd"; +%! C.int1 = 123; +%! assert (__isequal__ (0, A, B, C)); +# test for inequality (struct) +%!test +%! A = struct (); +%! A.char1 = "abcd"; +%! A.int1 = NaN; +%! B = struct (); +%! B.char1 = "abcd"; +%! B.int1 = NaN; +%! C = struct (); +%! C.char1 = "abcd"; +%! C.int1 = NaN; +%! assert (__isequal__ (0, A, B, C), false); Index: scripts/general/isequal.m =================================================================== RCS file: /cvs/octave/scripts/general/isequal.m,v retrieving revision 1.5 diff -u -r1.5 isequal.m --- a/scripts/general/isequal.m 15 Jun 2005 17:03:54 -0000 1.5 +++ b/scripts/general/isequal.m 29 Nov 2005 07:45:08 -0000 at @ -1,81 +1,35 @@ -## Copyright (C) 2000 Paul Kienzle +## Copyright (C) 2005 William Poetra Yoga Hadisoeseno ## ## 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 2, or (at your option) -## any later version. +## 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 2 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. +## 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, write to the Free -## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -## 02110-1301, USA. +## along with Octave; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## -*- texinfo -*- -## at deftypefn {Function File} {} isequal (@var{x1}, @var{x2}, @dots{}, @var{xN}) -## Return true if all parts of at var{x1}, @var{x2}, @dots{}, @var{xN} are -## equal. +## at deftypefn {Function File} {} isequal (@var{x1}, @var{x2}, ...) +## Return true if all of at var{x1}, @var{x2}, ... are equal. ## at end deftypefn +## +## at seealso{isequalwithequalnans} -## Author: Paul Kienzle -## Adapted-by: jwe - -function t = isequal (x, varargin) +function tf = isequal (x, varargin) if (nargin < 2) - usage ("isequal (x, y, ...)"); - endif + usage ("isequal (x1, x2, ...)"); + end - for arg = 1:length (varargin) - y = varargin{arg}; - if (isstruct (x)) - t = isstruct (y); - for [v, k] = x - t = (t - && struct_contains (y, k) - && isequal (getfield (x, k), getfield (y, k))); - endfor - for [v, k] = y - t = t && struct_contains (x, k); - endfor - elseif (islist (x)) - t = islist(y) && length(x) == length(y); - if (! t) - return; - endif - for i = 1:length (x) - t = isequal (x{i}, y{i}); - if (! t) - return; - endif - endfor - elseif (any (size (x) != size (y))) - t = false; - elseif (iscell (x) || islist (x)) - t = iscell (y) || islist (y); - if (! t) - return; - endif - x = x(:); - y = y(:); - for i = 1:length (x) - t = isequal (x{i}, y{i}); - if (! t) - return; - endif - endfor - else - t = all (x(:) == y(:)); - endif - if (! t) - return; - endif - endfor + tf = __isequal__ (0, x, varargin{:}); endfunction + Index: scripts/general/isequalwithequalnans.m =================================================================== RCS file: scripts/general/isequalwithequalnans.m diff -N scripts/general/isequalwithequalnans.m --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ b/scripts/general/isequalwithequalnans.m 29 Nov 2005 07:45:08 -0000 at @ -0,0 +1,35 @@ +## Copyright (C) 2005 William Poetra Yoga Hadisoeseno +## +## 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 2 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; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +## -*- texinfo -*- +## at deftypefn {Function File} {} isequalwithequalnans (@var{x1}, @var{x2}, ...) +## Assuming NaN == NaN, return true if all of at var{x1}, @var{x2}, ... are equal. +## at end deftypefn +## +## at seealso{isequal} + +function tf = isequalwithequalnans (x, varargin) + + if (nargin < 2) + usage ("isequalwithequalnans (x1, x2, ...)"); + end + + tf = __isequal__ (1, x, varargin{:}); + +endfunction + ------=_Part_4684_20649011.1133251489667-- ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------