Mercurial > octave-nkf
annotate scripts/general/private/__isequal__.m @ 18819:1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Increase performance of isequal.
__isequal__.m: When there are mixed operands (char, numeric) use '==' rather
than strcmp for equality. Use cellindexmat instead of for loop when testing
cells. Use cellfun call with ndims to check rough size equality of objects.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 27 May 2014 14:15:08 -0700 |
parents | fcd87f68af4f |
children | db92e7e28e1f |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17338
diff
changeset
|
1 ## Copyright (C) 2000-2013 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 | |
18819
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) | |
18819
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
44 ## b. object converted to struct, and then compared as stated above |
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 |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
47 ## e fcn_handle compare using overloade 'eq' operator |
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 |
18819
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))) |
18819
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. | |
18819
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
67 t = all (ndims (x) == cellfun ("ndims", varargin)); |
6130 | 68 endif |
5549 | 69 |
6130 | 70 if (t) |
18819
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 |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
75 t = all (size (x,k) == cellfun ("size", varargin, k)); |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
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. |
18819
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)) |
11475
caf1fd72f587
__isequal__: avoid possible overloading of struct function for objects
John W. Eaton <jwe@octave.org>
parents:
11459
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 endfor |
11459
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
85 endif |
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
86 |
6130 | 87 if (t) |
88 ## Check individual classes. | |
89 if (isstruct (x)) | |
90 ## Test the number of fields. | |
91 fn_x = fieldnames (x); | |
18538
fcd87f68af4f
Deprecate nfields and replace with numfields.
Rik <rik@octave.org>
parents:
18447
diff
changeset
|
92 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
|
93 fn_v = cellfun ("fieldnames", varargin, "uniformoutput", false); |
18819
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
94 t = all (l_fn_x == cellfun ("numel", fn_v)); |
5549 | 95 |
6130 | 96 ## Test that all the names are equal. |
97 idx = 0; | |
98 s_fn_x = sort (fn_x); | |
99 while (t && idx < l_v) | |
10549 | 100 idx++; |
101 ## We'll allow the fieldnames to be in a different order. | |
102 t = all (strcmp (s_fn_x, sort (fn_v{idx}))); | |
6130 | 103 endwhile |
5549 | 104 |
6130 | 105 idx = 0; |
106 while (t && idx < l_fn_x) | |
10549 | 107 ## Test that all field values are equal. |
108 idx++; | |
18819
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
109 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
|
110 args(1:2) = {nans_compare_equal, {x.(fn_x{idx})}}; |
10549 | 111 for argn = 1:l_v |
112 args{argn+2} = {varargin{argn}.(fn_x{idx})}; | |
113 endfor | |
18819
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
114 ## Minimize function calls by calling for all the arguments at once. |
6130 | 115 t = __isequal__ (args{:}); |
116 endwhile | |
5549 | 117 |
6130 | 118 elseif (iscell (x)) |
119 ## Check that each element of a cell is equal. | |
120 l_x = numel (x); | |
121 idx = 0; | |
122 while (t && idx < l_x) | |
10549 | 123 idx++; |
18819
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
124 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
|
125 args(1:2) = {nans_compare_equal, x{idx}}; |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
126 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
|
127 |
6130 | 128 t = __isequal__ (args{:}); |
129 endwhile | |
5549 | 130 |
18819
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
131 elseif (ischar (x) && all (cellfun ("isclass", varargin, "char"))) |
6130 | 132 ## Sizes are equal already, so we can just make everything into a |
133 ## row and test the rows. | |
18819
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
134 n_x = numel (x); |
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
135 strings = cell (1, l_v); |
6130 | 136 for i = 1:l_v |
18819
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
137 strings{i} = reshape (varargin{i}, 1, n_x); |
6130 | 138 endfor |
18819
1f170b211be3
Make isequal matlab compatible when comparing char to numeric (bug #42408).
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
139 t = all (strcmp (reshape (x, 1, n_x), strings)); |
5549 | 140 |
10260
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
141 elseif (isa (x, "function_handle")) |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
142 ## 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
|
143 t = all (cellfun ("eq", {x}, varargin)); |
10260
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
144 |
5549 | 145 else |
6130 | 146 ## Check the numeric types. |
147 | |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
148 f_x = find (x); |
6130 | 149 l_f_x = length (f_x); |
150 x = x(f_x); | |
151 for argn = 1:l_v | |
10549 | 152 y = varargin{argn}; |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
153 f_y = find (y); |
5549 | 154 |
10549 | 155 t = (l_f_x == length (f_y)) && all (f_x == f_y); |
156 if (!t) | |
17278
79d4b6089968
Fix isequal for sparse matrix (bug #37321)
Stefan Mahr <dac922@gmx.de>
parents:
17176
diff
changeset
|
157 break; |
10549 | 158 endif |
5549 | 159 |
10549 | 160 y = y(f_y); |
161 m = (x == y); | |
162 t = all (m); | |
5549 | 163 |
11126
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
164 if (!t && nans_compare_equal) |
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
165 t = isnan (x(!m)) && isnan (y(!m)); |
11125
8a8eb099502e
Fix bug #31239 in isequalwithequalnans.m
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
166 endif |
8a8eb099502e
Fix bug #31239 in isequalwithequalnans.m
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
167 |
8a8eb099502e
Fix bug #31239 in isequalwithequalnans.m
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
168 if (!t) |
17278
79d4b6089968
Fix isequal for sparse matrix (bug #37321)
Stefan Mahr <dac922@gmx.de>
parents:
17176
diff
changeset
|
169 break; |
10549 | 170 endif |
6130 | 171 endfor |
5549 | 172 |
6130 | 173 endif |
5549 | 174 endif |
175 | |
176 endfunction | |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17285
diff
changeset
|
177 |