summaryrefslogtreecommitdiff
path: root/inst/misc/test/stk_isequal_tolrel.m
blob: b199066e18bbfed5466168ee511783fd49c65ebe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
% STK_ISEQUAL_TOLREL tests approximate equality of two matrices or structures.
%
% CALL: BOOL = stk_isequal_tolrel(A, B, TOLREL)
%
%   returns true if A and B are numeric arrays of the same size, such that for
%   any pair (a, b) of corresponding entries,
%
%      abs(b - a) <= TOLABS,                                                (1)
%
%   where
%
%      TOLABS = max(abs(A(:)), abs(B(:))) * TOLREL.
%
%   For numeric array, the function returns false is either
%
%    * the array don't have identical sizes, or
%    * the array have identical sizes but (1) doesn't hold.
%
%   If A and B are structures with the same list of fieldnames, the function
%   works recursively on the fields, and returns true iff all the fields are
%   approximately equal.
%
% CALL: b = stk_isequal_tolrel(a, b)
%
%   uses the default value 1e-8 for TOLREL.
%
% See also isequal.

% Copyright Notice
%
%    Copyright (C) 2018 CentraleSupelec
%    Copyright (C) 2012, 2013 SUPELEC
%
%    Authors:   Julien Bect        <julien.bect@centralesupelec.fr>
%               Emmanuel Vazquez   <emmanuel.vazquez@centralesupelec.fr>

% Copying Permission Statement
%
%    This file is part of
%
%            STK: a Small (Matlab/Octave) Toolbox for Kriging
%               (https://github.com/stk-kriging/stk/)
%
%    STK 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 3  of the License, or  (at your
%    option) any later version.
%
%    STK 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 STK.  If not, see <http://www.gnu.org/licenses/>.

function res = stk_isequal_tolrel(a, b, tolrel)

DEFAULT_TOLREL = 1e-8;

if nargin == 2
    tolrel = DEFAULT_TOLREL;
end

if isstruct(a) && isstruct(b)
    
    L = fieldnames(a);
    if ~isequal(fieldnames(b), L)
        res = false;
        return;
    end
    res = true;
    for k = 1:length(L)
        if ~ isfield(b, L{k})
            res = false;
            return;
        end
        res = stk_isequal_tolrel(a.(L{k}), b.(L{k}), tolrel);
        if ~ res, return; end
    end
    
elseif isnumeric(a) && isnumeric(b)
    
    if ~ isequal (size (a), size (b))
        res = false;
    else
        aa = a(:);
        bb = b(:);
        tolabs = max(abs(aa), abs(bb)) * tolrel;
        res = all(abs(bb - aa) <= tolabs);
    end
    
elseif ischar (a) && ischar (b)
    
    res = strcmp (a, b);
    
elseif iscell (a) && iscell (b)
    
    for i = 1:numel(a)
        if ~ stk_isequal_tolrel (a{i}, b{i}, tolrel)
            res = false;
            return;
        end
    end
    
    res = true;
    
elseif isa (a, 'stk_dataframe') && (strcmp (class (a), class (b)))
    
    res = stk_isequal_tolrel (struct (a), struct (b), tolrel);
    
else
    
    res = false;
    
end

end % function


%!shared r1, r2, r3, a, b, tolrel
%! a = 1.01; b = 1.02; tolrel = 0.1;

%!error rr = stk_isequal_tolrel();
%!error rr = stk_isequal_tolrel(a);
%!test  r1 = stk_isequal_tolrel(a, b);
%!test  r2 = stk_isequal_tolrel(a, b, tolrel);
%!test  r3 = stk_isequal_tolrel(a, [b b]);

%!test assert (isequal (r1, false));
%!test assert (isequal (r2, true));
%!test assert (isequal (r3, false));

%!test
%! a = struct('u', []); b = struct('v', []);
%! assert(~ stk_isequal_tolrel(a, b))
%!test
%! a = struct('u', 1.01); b = struct('u', 1.02);
%! assert(stk_isequal_tolrel(a, b, tolrel))