-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_eq.m
69 lines (61 loc) · 1.31 KB
/
assert_eq.m
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
function [status, msg] = assert_eq(a,b,varargin)
tol = 0.001;
type = 'abs';
if nargin > 2
tol = varargin{1};
end;
if nargin > 3
type = varargin{2};
end;
err = norm(a-b);
switch (type)
case 'rel'
if norm(a) > eps
err = norm(a-b)/norm(a);
else
err = 0;
end;
if err < tol
msg = 'PASS';
status = 0;
else
msg = sprintf('FAIL. %s err=%f',type, err);
status = 1;
end;
case 'abs'
err = norm(a-b);
if err < tol
msg = 'PASS';
status = 0;
else
msg = sprintf('FAIL. %s err=%f',type, err);
status = 1;
end;
case 'pairwise_rel'
errors = abs(a-b)./abs(a);
%are any of the errors > tol?
num_errors = sum(errors > tol);
if num_errors > 0
status = 1;
msg = sprintf('FAIL: %d compraisons failed. Relative residuals were:\n',num_errors);
msg = strcat(msg, '[', sprintf('%f,\n',errors), ']\n');
else
status = 0;
msg = 'PASS';
end;
case 'pairwise_abs'
errors = abs(a-b);
%are any of the errors > tol?
num_errors = sum(errors > tol);
if num_errors > 0
status = 1;
msg = sprintf('FAIL: %d compraisons failed. Residuals were:\n',num_errors);
msg = strcat(msg, '[', sprintf('%f,\n',errors), ']\n');
else
status = 0;
msg = 'PASS';
end;
otherwise
disp('WARNING: bad tolerance type, falling back to relative');
end;
end