Mercurial > hg > octave-lojdl
annotate scripts/general/private/__isequal__.m @ 14868:5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
* lin2mu.m, loadaudio.m, wavread.m, accumarray.m, bicubic.m, celldisp.m,
colon.m, cplxpair.m, dblquad.m, divergence.m, genvarname.m, gradient.m,
int2str.m, interp1.m, interp1q.m, interp2.m, interpn.m, loadobj.m, nthargout.m,
__isequal__.m, __splinen__.m, quadgk.m, quadl.m, quadv.m, rat.m, rot90.m,
rotdim.m, saveobj.m, subsindex.m, triplequad.m, delaunay3.m, griddata.m,
inpolygon.m, tsearchn.m, voronoi.m, get_first_help_sentence.m, which.m,
gray2ind.m, pink.m, dlmwrite.m, strread.m, textread.m, textscan.m, housh.m,
ishermitian.m, issymmetric.m, krylov.m, logm.m, null.m, rref.m,
compare_versions.m, copyfile.m, dump_prefs.m, edit.m, fileparts.m,
getappdata.m, isappdata.m, movefile.m, orderfields.m, parseparams.m,
__xzip__.m, rmappdata.m, setappdata.m, swapbytes.m, unpack.m, ver.m, fminbnd.m,
fminunc.m, fsolve.m, glpk.m, lsqnonneg.m, qp.m, sqp.m, configure_make.m,
copy_files.m, describe.m, get_description.m, get_forge_pkg.m, install.m,
installed_packages.m, is_architecture_dependent.m, load_package_dirs.m,
print_package_description.m, rebuild.m, repackage.m, save_order.m, shell.m,
allchild.m, ancestor.m, area.m, axes.m, axis.m, clabel.m, close.m, colorbar.m,
comet.m, comet3.m, contour.m, cylinder.m, ezmesh.m, ezsurf.m, findobj.m,
fplot.m, hist.m, isocolors.m, isonormals.m, isosurface.m, isprop.m, legend.m,
mesh.m, meshz.m, pareto.m, pcolor.m, peaks.m, plot3.m, plotmatrix.m, plotyy.m,
polar.m, print.m, __add_datasource__.m, __add_default_menu__.m,
__axes_limits__.m, __bar__.m, __clabel__.m, __contour__.m, __errcomm__.m,
__errplot__.m, __ezplot__.m, __file_filter__.m, __fltk_print__.m,
__ghostscript__.m, __gnuplot_print__.m, __go_draw_axes__.m,
__go_draw_figure__.m, __interp_cube__.m, __marching_cube__.m, __patch__.m,
__pie__.m, __plt__.m, __print_parse_opts__.m, __quiver__.m, __scatter__.m,
__stem__.m, __tight_eps_bbox__.m, __uigetdir_fltk__.m, __uigetfile_fltk__.m,
__uiputfile_fltk__.m, quiver.m, quiver3.m, rectangle.m, refreshdata.m,
ribbon.m, scatter.m, semilogy.m, shading.m, slice.m, subplot.m, surface.m,
surfl.m, surfnorm.m, text.m, uigetfile.m, uiputfile.m, whitebg.m, deconv.m,
mkpp.m, pchip.m, polyaffine.m, polyder.m, polygcd.m, polyout.m, polyval.m,
ppint.m, ppjumps.m, ppval.m, residue.m, roots.m, spline.m, splinefit.m,
addpref.m, getpref.m, setpref.m, ismember.m, setxor.m, arch_fit.m, arch_rnd.m,
arch_test.m, autoreg_matrix.m, diffpara.m, fftconv.m, filter2.m, hanning.m,
hurst.m, periodogram.m, triangle_sw.m, sinc.m, spectral_xdf.m, spencer.m,
stft.m, synthesis.m, unwrap.m, yulewalker.m, bicgstab.m, gmres.m, pcg.m, pcr.m,
__sprand_impl__.m, speye.m, spfun.m, sprandn.m, spstats.m, svds.m,
treelayout.m, treeplot.m, bessel.m, factor.m, legendre.m, perms.m, primes.m,
magic.m, toeplitz.m, corr.m, cov.m, mean.m, median.m, mode.m, qqplot.m,
quantile.m, ranks.m, zscore.m, logistic_regression_likelihood.m,
bartlett_test.m, chisquare_test_homogeneity.m, chisquare_test_independence.m,
kolmogorov_smirnov_test.m, run_test.m, u_test.m, wilcoxon_test.m, z_test.m,
z_test_2.m, bin2dec.m, dec2base.m, mat2str.m, strcat.m, strchr.m, strjust.m,
strtok.m, substr.m, untabify.m, assert.m, demo.m, example.m, fail.m, speed.m,
test.m, now.m: Use Octave coding conventions for cuddling parentheses in
scripts directory.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 17 Jul 2012 07:08:39 -0700 |
parents | 72c96de7a403 |
children | abf384f5d243 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12931
diff
changeset
|
1 ## Copyright (C) 2000-2012 Paul Kienzle |
5549 | 2 ## |
7016 | 3 ## This file is part of Octave. |
5549 | 4 ## |
7016 | 5 ## Octave is free software; you can redistribute it and/or modify it |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
5549 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5549 | 18 |
6945 | 19 ## Undocumented internal function. |
20 | |
5549 | 21 ## -*- texinfo -*- |
5550 | 22 ## @deftypefn {Function File} {} __isequal__ (@var{nans_compare_equal}, @var{x1}, @var{x2}, @dots{}) |
8812
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
23 ## Undocumented internal function. |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
24 ## @end deftypefn |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
25 |
5550 | 26 ## Return true if @var{x1}, @var{x2}, @dots{} are all equal and |
5549 | 27 ## @var{nans_compare_equal} evaluates to false. |
28 ## | |
29 ## If @var{nans_compare_equal} evaluates to true, then assume NaN == NaN. | |
30 | |
31 ## Modified by: William Poetra Yoga Hadisoeseno | |
32 | |
33 ## Algorithm: | |
34 ## | |
35 ## 1. Determine the class of x | |
36 ## 2. If x is of the struct, cell, list or char class, for each | |
37 ## argument after x, determine whether it has the same class | |
38 ## and size as x. | |
39 ## Otherwise, for each argument after x, verify that it is not | |
40 ## of the struct, cell, list or char class, and that it has | |
41 ## the same size as x. | |
42 ## 3. For each argument after x, compare it for equality with x: | |
43 ## a. struct compare each member by name, not by order (recursive) | |
44 ## b. cell/list compare each member by order (recursive) | |
45 ## c. char compare each member with strcmp | |
46 ## d. <other> compare each nonzero member, and assume NaN == NaN | |
47 ## if nans_compare_equal is nonzero. | |
48 | |
49 function t = __isequal__ (nans_compare_equal, x, varargin) | |
50 | |
51 if (nargin < 3) | |
6046 | 52 print_usage (); |
5549 | 53 endif |
54 | |
55 l_v = nargin - 2; | |
56 | |
6130 | 57 ## Generic tests. |
5549 | 58 |
6609 | 59 ## All arguments must either be of the same class or they must be |
60 ## numeric values. | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
61 t = (all (strcmp (class (x), |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
62 cellfun ("class", varargin, "uniformoutput", false))) |
9841
6f1ea8241c99
make isnumeric yield false on logicals
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
63 || ((isnumeric (x) || islogical (x)) |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
64 && all (cellfun ("isnumeric", varargin) |
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
65 | cellfun ("islogical", varargin)))); |
6609 | 66 |
6130 | 67 if (t) |
68 ## Test that everything has the same number of dimensions. | |
69 s_x = size (x); | |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
11126
diff
changeset
|
70 s_v = cellfun (@size, varargin, "uniformoutput", false); |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
71 t = all (length (s_x) == cellfun ("length", s_v)); |
6130 | 72 endif |
5549 | 73 |
6130 | 74 if (t) |
75 ## Test that everything is the same size since it has the same | |
76 ## dimensionality. | |
77 l_x = length (s_x); | |
78 s_v = reshape ([s_v{:}], length (s_x), []); | |
79 idx = 0; | |
80 while (t && idx < l_x) | |
81 idx++; | |
11126
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
82 t = all (s_x(idx) == s_v(idx,:)); |
6130 | 83 endwhile |
84 endif | |
5549 | 85 |
11459
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
86 ## From here on, compare objects as if they were structures. |
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
87 if (isobject (x)) |
11475
caf1fd72f587
__isequal__: avoid possible overloading of struct function for objects
John W. Eaton <jwe@octave.org>
parents:
11459
diff
changeset
|
88 x = builtin ("struct", x); |
caf1fd72f587
__isequal__: avoid possible overloading of struct function for objects
John W. Eaton <jwe@octave.org>
parents:
11459
diff
changeset
|
89 varargin = cellfun (@(x) builtin ("struct", x), varargin, |
caf1fd72f587
__isequal__: avoid possible overloading of struct function for objects
John W. Eaton <jwe@octave.org>
parents:
11459
diff
changeset
|
90 "uniformoutput", false); |
11459
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
91 endif |
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
92 |
6130 | 93 if (t) |
94 ## Check individual classes. | |
95 if (isstruct (x)) | |
96 ## Test the number of fields. | |
97 fn_x = fieldnames (x); | |
98 l_fn_x = length (fn_x); | |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
99 fn_v = cellfun ("fieldnames", varargin, "uniformoutput", false); |
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
100 t = all (l_fn_x == cellfun ("length", fn_v)); |
5549 | 101 |
6130 | 102 ## Test that all the names are equal. |
103 idx = 0; | |
104 s_fn_x = sort (fn_x); | |
105 while (t && idx < l_v) | |
10549 | 106 idx++; |
107 ## We'll allow the fieldnames to be in a different order. | |
108 t = all (strcmp (s_fn_x, sort (fn_v{idx}))); | |
6130 | 109 endwhile |
5549 | 110 |
6130 | 111 idx = 0; |
112 while (t && idx < l_fn_x) | |
10549 | 113 ## Test that all field values are equal. |
114 idx++; | |
115 args = {nans_compare_equal, {x.(fn_x{idx})}}; | |
116 for argn = 1:l_v | |
117 args{argn+2} = {varargin{argn}.(fn_x{idx})}; | |
118 endfor | |
119 ## Minimize function calls by calling for all the arguments at | |
120 ## once. | |
6130 | 121 t = __isequal__ (args{:}); |
122 endwhile | |
5549 | 123 |
6130 | 124 elseif (iscell (x)) |
125 ## Check that each element of a cell is equal. | |
126 l_x = numel (x); | |
127 idx = 0; | |
128 while (t && idx < l_x) | |
10549 | 129 idx++; |
130 args = {nans_compare_equal, x{idx}}; | |
131 for p = 1:l_v | |
132 args{p+2} = varargin{p}{idx}; | |
133 endfor | |
6130 | 134 t = __isequal__ (args{:}); |
135 endwhile | |
5549 | 136 |
6130 | 137 elseif (ischar (x)) |
5549 | 138 |
6130 | 139 ## Sizes are equal already, so we can just make everything into a |
140 ## row and test the rows. | |
141 for i = 1:l_v | |
10549 | 142 strings{i} = reshape (varargin{i}, 1, []); |
6130 | 143 endfor |
144 t = all (strcmp (reshape (x, 1, []), strings)); | |
5549 | 145 |
10260
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
146 elseif (isa (x, "function_handle")) |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
147 |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
148 ## The == operator is overloaded for handles. |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
149 t = all (cellfun ("eq", {x}, varargin)); |
10260
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
150 |
5549 | 151 else |
6130 | 152 ## Check the numeric types. |
153 | |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
154 f_x = find (x); |
6130 | 155 l_f_x = length (f_x); |
156 x = x(f_x); | |
157 for argn = 1:l_v | |
10549 | 158 y = varargin{argn}; |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
159 f_y = find (y); |
5549 | 160 |
10549 | 161 t = (l_f_x == length (f_y)) && all (f_x == f_y); |
162 if (!t) | |
6130 | 163 return; |
10549 | 164 endif |
5549 | 165 |
10549 | 166 y = y(f_y); |
167 m = (x == y); | |
168 t = all (m); | |
5549 | 169 |
11126
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
170 if (!t && nans_compare_equal) |
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
171 t = isnan (x(!m)) && isnan (y(!m)); |
11125
8a8eb099502e
Fix bug #31239 in isequalwithequalnans.m
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
172 endif |
8a8eb099502e
Fix bug #31239 in isequalwithequalnans.m
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
173 |
8a8eb099502e
Fix bug #31239 in isequalwithequalnans.m
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
174 if (!t) |
11126
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
175 return; |
10549 | 176 endif |
6130 | 177 endfor |
5549 | 178 |
6130 | 179 endif |
5549 | 180 endif |
181 | |
182 endfunction |