Mercurial > octave
annotate scripts/general/private/__isequal__.m @ 21562:6c2fd62db1f7
maint: Eliminate accidental double spaces in code.
* make_int.cc, file-editor-tab.h, file-editor.cc, shortcut-manager.h,
__contourc__.cc, cellfun.cc, debug.cc, errwarn.h, graphics.in.h, jit-ir.h,
oct-stream.cc, quadcc.cc, qz.cc, sparse-xdiv.cc, symtab.cc, __init_fltk__.cc,
ov-class.cc, ov-float.h, ov-scalar.h, op-int.h, CNDArray.cc, CSparse.cc,
MSparse.cc, Sparse.cc, boolNDArray.cc, chNDArray.cc, dNDArray.cc, dSparse.cc,
fCNDArray.cc, fNDArray.cc, int16NDArray.cc, int32NDArray.cc, int64NDArray.cc,
int8NDArray.cc, intNDArray.cc, uint16NDArray.cc, uint32NDArray.cc,
uint64NDArray.cc, uint8NDArray.cc, randgamma.h, randmtzig.h, randpoisson.c,
randpoisson.h, Sparse-op-defs.h, oct-time.cc, kpse.cc, lo-array-errwarn.h,
lo-ieee.h, del2.m, num2str.m, __isequal__.m, lookfor.m, hsv2rgb.m,
colorspace_conversion_revert.m, fminbnd.m, create_pkgadddel.m, shading.m,
struct2hdl.m, gallery.m, discrete_cdf.m, kolmogorov_smirnov_test.m, assert.m,
datestr.m, classes.tst, jit.tst:
Eliminate accidental double spaces in code.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 30 Mar 2016 15:19:12 -0700 |
parents | 77f5591878bf |
children | bac0d6f07a3e |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19597
diff
changeset
|
1 ## Copyright (C) 2000-2015 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 -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
22 ## @deftypefn {} {} __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 | |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
36 ## 2. If x and all other arguments have the same class, then check that the |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
37 ## number of dimensions and then the size of each dimension match. |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
38 ## If not all arguments share the same class, then verify that all of the |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
39 ## arguments belong to a comparable "numeric" class which includes |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
40 ## numeric, logical, and character arrays. Check that number of dimensions |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
41 ## and size of each dimension match. |
5549 | 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) | |
20072
8e42898f4d73
Don't throw classdef-to-struct warning during isequal (bug #43759).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
44 ## b. object convert to struct, and then compare as stated above |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
45 ## c. cell compare each member by order (recursive) |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
46 ## d. char compare each member with strcmp |
21562
6c2fd62db1f7
maint: Eliminate accidental double spaces in code.
Rik <rik@octave.org>
parents:
20955
diff
changeset
|
47 ## e fcn_handle compare using overloaded 'eq' operator |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
48 ## f. <other> compare each nonzero member, and assume NaN == NaN |
5549 | 49 ## if nans_compare_equal is nonzero. |
50 | |
51 function t = __isequal__ (nans_compare_equal, x, varargin) | |
52 | |
53 l_v = nargin - 2; | |
54 | |
6130 | 55 ## Generic tests. |
5549 | 56 |
6609 | 57 ## All arguments must either be of the same class or they must be |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
58 ## "numeric" values. |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
59 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
|
60 cellfun ("class", varargin, "uniformoutput", false))) |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
61 || ((isreal (x) || isnumeric (x)) |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
62 && all (cellfun ("isreal", varargin) |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
63 | cellfun ("isnumeric", varargin)))); |
6609 | 64 |
6130 | 65 if (t) |
66 ## Test that everything has the same number of dimensions. | |
20890
ef9b2a88fbb5
Fixed isequal for comparison of objects with overridden size (bug #44334)
Oliver Heimlich <oheim@posteo.de>
parents:
20072
diff
changeset
|
67 t = all (builtin ("ndims", x) == cellfun ("ndims", varargin)); |
6130 | 68 endif |
5549 | 69 |
6130 | 70 if (t) |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
71 ## Test that everything is the same size since the dimensionality matches. |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
72 nd = ndims (x); |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
73 k = 1; |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
74 do |
20890
ef9b2a88fbb5
Fixed isequal for comparison of objects with overridden size (bug #44334)
Oliver Heimlich <oheim@posteo.de>
parents:
20072
diff
changeset
|
75 t = all (builtin ("size", x, k) == cellfun ("size", varargin, k)); |
20955
77f5591878bf
maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents:
20907
diff
changeset
|
76 until (! t || k++ == nd); |
6130 | 77 endif |
5549 | 78 |
11459
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
79 ## From here on, compare objects as if they were structures. |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
80 if (t && isobject (x)) |
20072
8e42898f4d73
Don't throw classdef-to-struct warning during isequal (bug #43759).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
81 ## Locally suppress class-to-struct warning. We know what we are doing. |
8e42898f4d73
Don't throw classdef-to-struct warning during isequal (bug #43759).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
82 warning ("off", "Octave:classdef-to-struct", "local"); |
11475
caf1fd72f587
__isequal__: avoid possible overloading of struct function for objects
John W. Eaton <jwe@octave.org>
parents:
11459
diff
changeset
|
83 x = builtin ("struct", x); |
18447
ff7e7928f160
Fix isequal* parse failure when comparing object to non-object (bug #41531).
Felipe G. Nievinski <fgnievinski@gmail.com>
parents:
17744
diff
changeset
|
84 for i = 1:numel (varargin) |
ff7e7928f160
Fix isequal* parse failure when comparing object to non-object (bug #41531).
Felipe G. Nievinski <fgnievinski@gmail.com>
parents:
17744
diff
changeset
|
85 varargin{i} = builtin ("struct", varargin{i}); |
ff7e7928f160
Fix isequal* parse failure when comparing object to non-object (bug #41531).
Felipe G. Nievinski <fgnievinski@gmail.com>
parents:
17744
diff
changeset
|
86 endfor |
11459
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
87 endif |
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
88 |
6130 | 89 if (t) |
90 ## Check individual classes. | |
91 if (isstruct (x)) | |
92 ## Test the number of fields. | |
93 fn_x = fieldnames (x); | |
18538
fcd87f68af4f
Deprecate nfields and replace with numfields.
Rik <rik@octave.org>
parents:
18447
diff
changeset
|
94 l_fn_x = numfields (x); |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
95 fn_v = cellfun ("fieldnames", varargin, "uniformoutput", false); |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
96 t = all (l_fn_x == cellfun ("numel", fn_v)); |
5549 | 97 |
6130 | 98 ## Test that all the names are equal. |
99 idx = 0; | |
100 s_fn_x = sort (fn_x); | |
101 while (t && idx < l_v) | |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20072
diff
changeset
|
102 idx += 1; |
10549 | 103 ## We'll allow the fieldnames to be in a different order. |
104 t = all (strcmp (s_fn_x, sort (fn_v{idx}))); | |
6130 | 105 endwhile |
5549 | 106 |
6130 | 107 idx = 0; |
108 while (t && idx < l_fn_x) | |
10549 | 109 ## Test that all field values are equal. |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20072
diff
changeset
|
110 idx += 1; |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
111 args = cell (1, 2+l_v); |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
112 args(1:2) = {nans_compare_equal, {x.(fn_x{idx})}}; |
10549 | 113 for argn = 1:l_v |
114 args{argn+2} = {varargin{argn}.(fn_x{idx})}; | |
115 endfor | |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
116 ## Minimize function calls by calling for all the arguments at once. |
6130 | 117 t = __isequal__ (args{:}); |
118 endwhile | |
5549 | 119 |
6130 | 120 elseif (iscell (x)) |
121 ## Check that each element of a cell is equal. | |
122 l_x = numel (x); | |
123 idx = 0; | |
124 while (t && idx < l_x) | |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20072
diff
changeset
|
125 idx += 1; |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
126 args = cell (1, 2+l_v); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18785
diff
changeset
|
127 args(1:2) = {nans_compare_equal, x{idx}}; |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
128 args(3:end) = [cellindexmat(varargin, idx){:}]; |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
129 |
6130 | 130 t = __isequal__ (args{:}); |
131 endwhile | |
5549 | 132 |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
133 elseif (ischar (x) && all (cellfun ("isclass", varargin, "char"))) |
6130 | 134 ## Sizes are equal already, so we can just make everything into a |
135 ## row and test the rows. | |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
136 n_x = numel (x); |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
137 strings = cell (1, l_v); |
6130 | 138 for i = 1:l_v |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
139 strings{i} = reshape (varargin{i}, 1, n_x); |
6130 | 140 endfor |
18785
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
141 t = all (strcmp (reshape (x, 1, n_x), strings)); |
5549 | 142 |
10260
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
143 elseif (isa (x, "function_handle")) |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
144 ## 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
|
145 t = all (cellfun ("eq", {x}, varargin)); |
10260
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
146 |
5549 | 147 else |
6130 | 148 ## Check the numeric types. |
149 | |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
150 f_x = find (x); |
6130 | 151 l_f_x = length (f_x); |
152 x = x(f_x); | |
153 for argn = 1:l_v | |
10549 | 154 y = varargin{argn}; |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
155 f_y = find (y); |
5549 | 156 |
10549 | 157 t = (l_f_x == length (f_y)) && all (f_x == f_y); |
20955
77f5591878bf
maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents:
20907
diff
changeset
|
158 if (! t) |
17278
79d4b6089968
Fix isequal for sparse matrix (bug #37321)
Stefan Mahr <dac922@gmx.de>
parents:
17176
diff
changeset
|
159 break; |
10549 | 160 endif |
5549 | 161 |
10549 | 162 y = y(f_y); |
163 m = (x == y); | |
164 t = all (m); | |
5549 | 165 |
20955
77f5591878bf
maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents:
20907
diff
changeset
|
166 if (! t && nans_compare_equal) |
77f5591878bf
maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents:
20907
diff
changeset
|
167 t = isnan (x(! m)) && isnan (y(! m)); |
11125
8a8eb099502e
Fix bug #31239 in isequalwithequalnans.m
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
168 endif |
8a8eb099502e
Fix bug #31239 in isequalwithequalnans.m
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
169 |
20955
77f5591878bf
maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents:
20907
diff
changeset
|
170 if (! t) |
17278
79d4b6089968
Fix isequal for sparse matrix (bug #37321)
Stefan Mahr <dac922@gmx.de>
parents:
17176
diff
changeset
|
171 break; |
10549 | 172 endif |
6130 | 173 endfor |
5549 | 174 |
6130 | 175 endif |
5549 | 176 endif |
177 | |
178 endfunction | |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17285
diff
changeset
|
179 |