itaValue.m 10.9 KB
Newer Older
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
classdef itaValue
    
    % itaValue class groups values and corresponding physical unit of a
    % variable. itaValues can be used for solving of calculation tasks
    % obtaining the resulting physical unit in the end.
    %
    % itaValue works in conjunction with itaAudio and itaResult.
    % e.g. a = itaAudio; b = itaValue(); c = a * b;
    
    % <ITA-Toolbox>
    % This file is part of the ITA-Toolbox. Some rights reserved.
    % You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
    % </ITA-Toolbox>
    
    properties
        value = 1;  % Value (double) of your variable
        unit  = ''; % Physical unit (string) of your variable
    end
    
    methods
        %% constructor
        function A = itaValue(varargin)
            % Create an itaValue object
            %     a = itaValue()  - empty object
            %     a = itaValue(2) - without physical unit
            %     a = itaValue(2,'kg') - with physical unit
            %     a = itaValue('2 m/s') - as one single string
            switch nargin
                case 0
                    % do nothing
                    
                case 1
                    if isa(varargin{1},'itaValue')
                        A = varargin{1};
                    elseif isempty(varargin{1})
                        A.value = 1;
                        A.unit = '';
                        
                    elseif ischar(varargin{1})
                        token = varargin{1}(:).';
                        
42
43
                        if ~isnan(str2double(token)) %pre check if only a value inside, pdi
                            A.value = str2double(token);
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
                        else
                            %token_new = token; token_new(token_new == '.') = ' ';
                            unit_start = find(isstrprop(token,'alpha'));
                            unit_start2 = find(token == ' ');
                            if isstrprop(token(1),'alpha') || ( length(token) > 2 && strcmpi(token(2),'/')) % if first element is alpha, this is already a unit string!
                                A.value = 1;
                                unit_start = 1;
                            else
                                if ~isempty(unit_start2) % take the space as division
                                    unit_start = unit_start2;
                                end
                            end
                            try
                                if strcmpi(token(unit_start(1)) ,'e')
                                    unit_start = unit_start(2);
                                else
                                    unit_start = unit_start(1);
                                end
62
63
							catch
								% do nothing
64
65
66
                            end
                            value_token = token(1:unit_start-1);
                            value_token = value_token(value_token ~= ' ');
67
                            A.value = sscanf(value_token,'%f');
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
                            if isempty(A.value)
                                A.value = 1;
                            end
                            unit_str = token(unit_start:end);
                            
                            if strcmpi(unit_str,'dB')
                                if ~isempty(value_token)
                                    A.value = 20*log10(A.value);
                                end
                            else
                                A.unit = ita_deal_units(unit_str);
                            end
                        end
                    elseif isnumeric(varargin{1})
                        A.value = varargin{1}(:).';
                    else
                        disp('sorry, please see syntax')
                    end
                case 2
                    A.value = varargin{1};
                    A.unit  = ita_deal_units(varargin{2});
                otherwise
                    disp('itaValue.strange things happened. we appologize.')
            end
        end
        
        %% *********************** conversions *****************************
        function res = num2str(a,varargin)
            % convert to string with value followed by unit string
            if length(a) > 1
                res = [];
                for idx = 1:length(a)
100
                    res = [res ' '  num2str(a(idx),varargin{:})]; %#ok<AGROW>
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
140
141
142
143
144
145
146
147
148
149
                end
            else
                res = num2str(a.value(:),varargin{:});
                if ~isempty(a.unit)
                    res = [res , repmat([' ' a.unit],numel(a.value),1)];
                end
            end
        end
        
        
        function [res, symbol] = simplify(this)
            [res, symbol] = ita_deal_units_symbolic(this.num2str);
            res = itaValue(res);
        end
        
        
        function res = diag(this)
            % diagonal of matrix
            if size(this,1) ~= size(this,2)
                error('itaValue:diag:: Matrix is not square')
            end
            res = repmat(itaValue,1,size(this,1));
            for idx = 1:size(this,1)
                res(idx) = this(idx,idx);
            end
        end
        
        
        function res = det(a)
            % determinant including physical units
            values = det(double(a));
            res = ita_unit_det(a);
            res.value = values;
        end
        
        
        function res = prod(a)
            % product of entries in a vector
            res = a(1,1);
            for idx = 2:numel(a)
                res = a(idx) * res;
            end
        end
        
        
        function res = inv(a)
            % invert a matrix
            res    = ita_unit_inv(a);
            values = inv(double(a));
150
            for idx = 1:size(a,1)
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
                for jdx = 1:size(a,2)
                    res(idx,jdx).value = values(idx,jdx);
                end
            end
        end
        
        
        function res = double(a)
            %type cast to double
            if size(a,1) > 1 || size(a,2) > 1
                res = zeros(size(a));
                for idx = 1:size(a,1)
                    for jdx = 1:size(a,2)
                        res(idx,jdx) = a(idx,jdx).value;
                    end
                end
            else
                % get the double value without unit. Same as Obj.value
                res = double(a.value(:));
            end
        end
        
        % *****************************************************************
        
        %% math
        function this = ceil(this)
            this.value = ceil(this.value);
        end
        
        function this = floor(this)
            this.value = floor(this.value);
        end
        
        function this = abs(this)
            this.value = abs(this.value);
        end
        
        function res = isempty(this)
            res = isempty(this.value);
        end
        
        %% disp
        function disp(a)
            disp(num2str(a))
        end
        
197
        function display(a) %#ok<DISPLAY>
198
199
            % show the variable value and string
            x = ver('matlab');
200
            if isempty(javachk('desktop')) && sscanf(x.Version,'%f') < 7.13    % check if we are in desktop mode
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
                spacing = 15;
                if size(a,1) == 1 && size(a,2) == 1 %normal 1D disp
                    
                    cprintf('blue',[' ' num2str(a.value(:).') ])
                    cprintf('red',[' ' a.unit '\n'])
                    
                else
                    %matrix disp
                    for idx = 1:size(a,1)
                        for jdx = 1:size(a,2)
                            
                            if isempty(a(idx,jdx).unit), a(idx,jdx).unit = '[]'; end %avoid strange behavior of cprintf
                            tmpUnit = a(idx,jdx).unit;
                            a(idx,jdx).unit = '';
                            res = num2str(a(idx,jdx));
                            for i=1:numel(a(idx,jdx).value)
                                numStr = num2str(res(i,:));
                                fprintf([' ' numStr ]) %no color in here, this is much faster
                                fprintf([' ' tmpUnit repmat(' ',1,max(spacing-length(numStr) - length(tmpUnit) - 2,1)) ''])
                            end
                        end
                        fprintf('\n')
                    end
                end
            else
                disp(a);
            end
        end
        
    end
    
    methods (Static = true)
        function varargout = log_reference(unit)
            % reference value used for this unit to obtain dB
            if iscell(unit)
                val = ones(numel(unit),1);
                log_prefix = 20.*ones(numel(unit),1);
                res = cell(numel(unit),1);
                % call recursively
                uniqueVals = unique(unit);
                if numel(uniqueVals) == 1
                    [res1,val1,log_prefix1] = itaValue.log_reference(uniqueVals{1});
                    res(:)          = {res1};
                    val(:)          = val1;
                    log_prefix(:)   = log_prefix1;
                else
                    for i = 1:numel(uniqueVals)
                        [tmpRes,tmpVal,tmpLogPrefix] = itaValue.log_reference(uniqueVals{i});
                        res(strcmpi(unit,uniqueVals{i})) = {tmpRes};
                        val(strcmpi(unit,uniqueVals{i})) = tmpVal;
                        log_prefix(strcmpi(unit,uniqueVals{i})) = tmpLogPrefix;
                    end
                end
            else
                val        = 1;
                log_prefix = 20;
                if isempty(unit)
                    res = '1';
                else
                    switch unit
                        case 'W'
                            res = '1pW';
                            val = 1e-12;
                            log_prefix = 10;
265
266
267
268
                        case {'W/m^2','kg/s^3'}
                            res = 'W/m^2';
                            val = 1e-12;
                            log_prefix = 10;
269
                        case 'm/s' % ISO 1683
270
271
                            res = '1nm/s';
                            val = 1e-9;
272
273
274
                        case 'm/s^2' % ISO 1683
                            res = '1um/s^2';
                            val = 1e-6;
275
276
277
278
279
280
281
282
283
284
285
286
287
                        case 'Pa'
                            res = '20uPa';
                            val = 20e-6;
                        case {'Pa^2','kg/(s m^2)'}
                            res = unit;
                            log_prefix = 10;
                        otherwise
                            res = unit;
                    end
                end
            end
            
            varargout{1} = res; %return string for e.g. legend
288
            if nargout >= 2
289
290
                varargout{2} = val; %return double for division
            end
291
            if nargout >= 3
292
293
294
295
296
                varargout{3} = log_prefix; %scaling factor for log10
            end
        end
    end
    
297
end