From: William P. Y. H <wil...@ya...> - 2005-11-12 06:42:13
|
I've modified isequal.m and renamed it _isequal.m, and then wrote isequal.m and isequalwithequalnans.m to call _isequal.m. It passed simple tests, but I think it needs to be tested with more complex cases. Any volunteers? Oh, and there's a small problem: - isequalwithequalnans(1+nan*i,1-nan*i) returns 1, and - isequalwithequalnans(1+nan*i,1+nan*i) returns 1, too. Shouldn't the former return 0? (considering 1+nan*i => NaN - NaNi and 1-nan*i => NaN + NaNi) ---------- _isequal.m ---------- ## 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 -*- ## @deftypefn {Function File} {} _isequal (@var{_nan}, @var{x1}, @var{x2}, ...) ## Return 1 if @var{x1}, @var{x2}, ... are all equal and @var{_nan} is 0. ## ## If @var{_nan} is nonzero, then NaN == NaN. ## @end deftypefn ## ## @seealso{isequal, isequalwithequalnans} ## Modified by William Poetra Yoga Hadisoeseno function t = _isequal(_nan,x,varargin) if nargin < 3 usage("_isequal(_nan, x1, x2, ...)"); endif l_v = nargin - 2; if isstruct(x) n_x = length(fieldnames(x)); t = 1; 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(_nan,v,getfield(y,k)); endfor if !t, return; endif endfor elseif iscell(x) || islist(x) x = x(:); l_x = length(x); t = 1; 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(_nan,x{p},y{p}); endfor if !t, return; endif endfor elseif ischar(x) l_x = length(x); t = 1; for argn = 1:l_v y = varargin{argn}; t = t && ischar(y) && (l_x == length(y)); endfor if !t, return; endif for argn = 1:l_v t = t && strcmp(x,varargin{argn}); endfor else s_x = size(x); t = 1; for argn = 1:l_v t = t && (s_x == size(varargin{argn})); 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 _nan f_nm = find(!m); t = isnan(x(f_nm)) && isnan(y(f_nm)); else return endif endif endfor endif endfunction --------- isequal.m --------- ## 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 -*- ## @deftypefn {Function File} {} isequal (@var{x1}, @var{x2}, ...) ## Return 1 if all of @var{x1}, @var{x2}, ... are equal. ## @end deftypefn ## ## @seealso{isequalwithequalnans} function retval = isequal(x,varargin) if (nargin < 2) usage("isequal (x1, x2, ...)"); end retval = _isequal(0,x,varargin{:}); endfunction ---------------------- isequalwithequalnans.m ---------------------- ## 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 -*- ## @deftypefn {Function File} {} isequalwithequalnans (@var{x1}, @var{x2}, ...) ## Assuming NaN == NaN, return 1 if all of @var{x1}, @var{x2}, ... are equal. ## @end deftypefn ## ## @seealso{isequal} function retval = isequalwithequalnans(x,varargin) if (nargin < 2) usage("isequalwithequalnans (x1, x2, ...)"); end retval = _isequal(1,x,varargin{:}); endfunction William Poetra Yoga Hadisoeseno __________________________________ Yahoo! Mail - PC Magazine Editors' Choice 2005 http://mail.yahoo.com |