comparison m-toolbox/classes/@unit/eq.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % EQ overloads the == operator for ltpda unit objects.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: EQ overloads the == operator for ltpda unit objects.
5 %
6 % Two units are considered equal if each has the same unit
7 % components with the same exponents and prefixes. The order
8 % of the units doesn't matter.
9 %
10 % CALL: result = eq(u1,u2)
11 %
12 % INPUTS: u1, u2 - Input objects
13 %
14 % OUTPUTS: If the two objects are considered equal, result == true,
15 % otherwise, result == false.
16 %
17 % VERSION: $Id: eq.m,v 1.14 2011/02/18 16:48:55 ingo Exp $
18 %
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20
21 function result = eq(obj1, obj2, varargin)
22
23 import utils.const.*
24
25 % Check class
26 if ~strcmp(class(obj1), class(obj2))
27 utils.helper.msg(msg.PROC1, 'NOT EQUAL: The objects are not from the same class. [%s] <-> [%s]', class(obj1), class(obj2));
28 result = false;
29 return
30 end
31
32 % Check length of obj1 and obj2
33 if numel(obj1) ~= numel(obj2)
34 utils.helper.msg(msg.PROC1, 'NOT EQUAL: The size of the %s-object''s. [%d] <-> [%d]', class(obj1), numel(obj1), numel(obj2));
35 result = false;
36 return
37 end
38
39 for objNo = 1:numel(obj1)
40
41 % compare these two units
42 result = compare_units(obj1(objNo),obj2(objNo));
43
44 % If they are not equal, we simplify and check again
45 % - simplify() is expensive, so only do if necessary
46 if ~result
47 % and make sure we get copies so we don't modify the user's inputs
48 c1 = obj1(objNo).simplify();
49 c2 = obj2(objNo).simplify();
50 result = compare_units(c1,c2);
51 end
52
53 if ~result
54 return
55 end
56
57 end
58
59 end
60
61
62 function result = compare_units(u1,u2)
63
64 % simplify the input objects
65 matches = false(size(u1.strs));
66
67 % same length?
68 if numel(u1.strs) ~= numel(u2.strs)
69 result = false;
70 return
71 end
72
73 % Check all match
74 for oo=1:numel(u1.strs)
75 for ii=1:numel(u2.strs)
76 % Check that the strings and the values are the same
77 if strcmp(u1.strs{oo}, u2.strs{ii}) && u1.vals(oo) == u2.vals(ii)
78 % Check that the exponent are the same
79 % REMARK: It might be that there is a rounding problem. But then
80 % For example: 1/3 - ( 1 - 2/3 ) ~= 0
81 % In this example is the error equal the smalles error of (1/3)
82 if u1.exps(oo) == u2.exps(ii) || abs(u1.exps(oo) - u2.exps(ii)) == eps(u1.exps(oo))
83 matches(oo) = true;
84 end
85 end
86 end
87 end
88
89 result = all(matches);
90
91 end