Symbolic.m 28.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classdef Symbolic < quantity.Function
	
	properties (SetAccess = protected)
		valueSymbolic sym;
		variable sym;
	end
	properties (Constant)
		defaultSymVar = sym('z', 'real');
	end
	
	methods
		
		function obj = Symbolic(valueContinuous, varargin)
			
			parentVarargin = {};
			
			if nargin > 0
				variableParser = misc.Parser();
				variableParser.addParameter('variable', quantity.Symbolic.getVariable(valueContinuous));
20
21
				variableParser.parse(varargin{:});
				variable = variableParser.Results.variable;
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
				numVar = numel(variable);
				defaultGrid = cell(1, numVar);
				for it = 1:numVar
					if it==1
						defaultGrid{it} = linspace(0, 1, 101).';
					else
						defaultGrid{it} = reshape(linspace(0, 1, 101), [ones(1, it-1), numel(linspace(0, 1, 101))]);
					end
				end
				gridParser = misc.Parser();
				gridParser.addParameter('grid', defaultGrid);
				gridParser.parse(varargin{:});
				
				s = size(valueContinuous);
				S = num2cell(s);
				
				fun = cell(S{:});
				symb = cell(S{:});
				
				for k = 1:numel(valueContinuous)
					
					if iscell(valueContinuous)
						fun{k} = valueContinuous{k};
					elseif isa(valueContinuous, 'sym') || isnumeric(valueContinuous)
						fun{k} = valueContinuous(k);
					else
						error('Type of valueContinuous not supported')
					end
					
					% input check for valueContinuous
					% FIXME: check that varargin does not contain valueSymbolic.
					% FIXME: initialize valueSymbolic with the correct symbolic
					% variable
					if isa(fun{k}, 'sym')
						symb{k} = fun{k};
						fun{k} = quantity.Symbolic.setValueContinuous(symb{k}, variable);
					elseif isa(fun{k}, 'double')
						symb{k} = sym(fun{k});
						fun{k} = quantity.Symbolic.setValueContinuous(symb{k}, variable);						
					elseif isa(fun{k}, 'function_handle')
						symb{k} = sym(fun{k});
					else
						error(['valueContinuous has to be a symbolic function or a function_handle but is a ', class(fun{k})]);
					end
				end
				
				gridName = cellstr(string(variable));
				% check if the reuqired gridName fits to the current
				% variables:
				if isfield(gridParser.Unmatched, 'gridName') ...
						&& ~all(strcmp(gridParser.Unmatched.gridName, gridName))
					error('If the gridName(s) are set explicitly, they have to be the same as the variable names!')
				end
				
76
77
				parentVarargin = [{fun}, varargin(:)', {'grid'}, {gridParser.Results.grid}, ...
					{'gridName'}, {gridName}];
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
			end
			% call parent class
			obj@quantity.Function(parentVarargin{:});
			
			if nargin > 0
				
				if isempty(obj)
					obj = quantity.Symbolic.empty(size(obj));
				end
				
				% special properties
				for k = 1:numel(valueContinuous)
					obj(k).variable = variable; %#ok<AGROW>
					obj(k).valueSymbolic = symb{k}; %#ok<AGROW>
				end
			end
		end
95
				
Jakob Gabriel's avatar
Jakob Gabriel committed
96
97
		function itIs = isConstant(obj)
			itIs = true;
Ferdinand Fischer's avatar
Ferdinand Fischer committed
98
			for k = 1:numel(obj)
Jakob Gabriel's avatar
Jakob Gabriel committed
99
100
				itIs = itIs && isempty(symvar(obj(k).sym));
				if ~itIs 
Ferdinand Fischer's avatar
Ferdinand Fischer committed
101
102
103
					break
				end
			end
104
105
106
		end
		
		function i = eq(A, B)
107
108
109
110
111
112
113
114
115
116
117
118
119
			%==  Equal.
			%   A == B does element by element comparisons between A and B and returns
			%   an array with elements set to logical 1 (TRUE) where the relation is
			%   true and elements set to logical 0 (FALSE) where it is not. A and B
			%   must have compatible sizes. In the simplest cases, they can be the same
			%   size or one can be a scalar. Two inputs have compatible sizes if, for
			%   every dimension, the dimension sizes of the inputs are either the same
			%   or one of them is 1.
			%	
			%	Two quantity.Symbolic objects are equal if they have the
			%	same symbolic expression, same gridSize and same
			%	variables.
			
120
121
122
123
124
125
			i = all(size(A) == size(B));
			
			if ~i
				return;
			end
			
126
127
128
129
			i = i && isequal([A.valueSymbolic], [B.valueSymbolic]);
			i = i && isequal(A.grid, B.grid);
			i = i && isequal(A.variable, B.variable);
			
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
197
198
199
200
201
		end
		
		function Q = quantity.Discrete(obj, varargin)
			% Cast of a quantity.Symbolic object into a quantity.Discrete
			% object.
			myParser = misc.Parser();
			myParser.addParameter('grid', obj(1).grid);
			myParser.addParameter('gridName', obj(1).gridName);
			myParser.addParameter('name', obj(1).name);
			myParser.parse(varargin{:});
			assert(isequal(myParser.Results.gridName, obj(1).gridName))
			Q = quantity.Discrete(obj.on(myParser.Results.grid), ...
				'grid', myParser.Results.grid, 'gridName', myParser.Results.gridName, ...
				'name', myParser.Results.name);
		end
		
		%-----------------------------
		% --- overloaded functions ---
		%-----------------------------
		function value = on(obj, myGrid, myGridName)
			% evaluates obj.valueContinuous function and returns an array
			% with the dimensions (n, m, ..., z_disc). n, m, ... are the
			% array dimensions of obj.valueContinuous and z_disc is the
			% dimensions of the grid.
			if nargin == 1
				value = [obj.valueDiscrete];
				gridSize = obj(1).gridSize;
				value = reshape(value, [gridSize, size(obj)]);
				return
			elseif nargin >= 2 && ~iscell(myGrid)
				myGrid = {myGrid};
			end
			gridPermuteIdx = 1:obj(1).nargin;
			if isempty(gridPermuteIdx)
				gridPermuteIdx = 1;
			end
			if nargin == 3
				if ~iscell(myGridName)
					myGridName = {myGridName};
				end
				assert(numel(myGrid) == numel(myGridName), ...
					['If on() is called by using gridNames as third input', ...
					', then the cell-array of grid and gridName must have ', ...
					'equal number of elements.']);
				assert(numel(myGridName) == obj(1).nargin, ...
					'All (or none) gridName must be specified');
				gridPermuteIdx = cellfun(@(v) obj(1).gridIndex(v), myGridName);
				myGrid = myGrid(gridPermuteIdx);
			end
			% at first the value has to be initialized by different
			% dimensions, to simplify the evaluation of each entry:

			gridSize = cellfun('length', myGrid);
			if isempty(gridSize), gridSize = 1; end

			value = nan([numel(obj), gridSize]);
			for k = 1:numel(obj)
				ndGrd = obj.ndgrid( myGrid );
				tmp = obj(k).valueContinuous( ndGrd{:} );
				% Replaced
				%   double(subs(obj(k).valueSymbolic, obj.variable, grid));
				% because this is very slow!
				value(k,:) = tmp(:);
			end
			value = reshape(permute(value, [1+(gridPermuteIdx), 1]), ...
				[gridSize(gridPermuteIdx), size(obj)]);
		end
		
	end
	
	%% mathematical operations
	methods (Access = public)
202
		function c = cat(dim, a, varargin)
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
			%CAT Concatenate arrays.
			%   CAT(DIM,A,B) concatenates the arrays of objects A and B
			%   from the class quantity.Discrete along the dimension DIM.
			%   CAT(2,A,B) is the same as [A,B].
			%   CAT(1,A,B) is the same as [A;B].
			%
			%   B = CAT(DIM,A1,A2,A3,A4,...) concatenates the input
			%   arrays A1, A2, etc. along the dimension DIM.
			%
			%   When used with comma separated list syntax, CAT(DIM,C{:}) or 
			%   CAT(DIM,C.FIELD) is a convenient way to concatenate a cell or
			%   structure array containing numeric matrices into a single matrix.
			%
			%   Examples:
			%     a = magic(3); b = pascal(3); 
			%     c = cat(4,a,b)
			%   produces a 3-by-3-by-1-by-2 result and
			%     s = {a b};
			%     for i=1:length(s), 
			%       siz{i} = size(s{i});
			%     end
			%     sizes = cat(1,siz{:})
			%   produces a 2-by-2 array of size vectors.
			%     
			%   See also NUM2CELL.

			%   Copyright 1984-2005 The MathWorks, Inc.
			%   Built-in function.
			if nargin == 1
				objArgin = {a};
			else
234
235
236
237
238
239
240
241
242
243
				% this function has the very special thing that it a does
				% not have to be an quantity.Discrete object. So it has to
				% be checked which of the input arguments is an
				% quantity.Discrete object. This is considered to be give
				% the basic values for the initialization of new
				% quantity.Discrete values
				objCell = [{a}, varargin(:)'];
				isAquantityDiscrete = cellfun(@(o) isa(o, 'quantity.Discrete'), objCell);
				objIdx = find(isAquantityDiscrete, 1);
				obj = objCell{objIdx};
244
				
Ferdinand Fischer's avatar
Ferdinand Fischer committed
245
246
247
248
249
250
251
252
				if ~isempty(obj)
					myVariable = obj(1).variable;
					myGrid = obj(1).grid;
				else
					myVariable = '';
					myGrid = {};
				end
				
253
				for k = 1:numel(objCell)
254
					
255
256
257
258
					if isa(objCell{k}, 'quantity.Symbolic')
						o = objCell{k};
					elseif isa(objCell{k}, 'double') || isa(objCell{k}, 'sym')
						o = quantity.Symbolic( objCell{k}, ...
Ferdinand Fischer's avatar
Ferdinand Fischer committed
259
260
							'variable', myVariable, ...
							'grid', myGrid);
261
					end
262
					objCell{k} = o;
263
264
				end
			end
265
			c = cat@quantity.Discrete(dim, objCell{:});
266
267
		end
		
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
		function solution = subs(obj, gridName, values)
			%% SUBS substitues gridName and values
			%   solution = subs(obj, gridName, values) TODO
			% This function substitutes the variables specified with
			% gradName with values. It can be used to rename grids or to
			% evaluate (maybe just some) grids.
			% GridName is cell-array of char-arrays
			% chosen from obj.gridName-property. Values is a cell-array of
			% the size of gridName. Each cell can contain arrays themself,
			% but those arrays must be of same size. Values can be
			% char-arrays standing for new gridName or or numerics.
			% In contrast to on() or at(), only some, but not necessarily
			% all variables are evaluated.
			if ~iscell(gridName)
				gridName = {gridName};
			end
			if ~iscell(values)
				values = {values};
			end
			isNumericValue = cellfun(@isnumeric, values);
			if any((cellfun(@(v) numel(v(:)), values)>1) & isNumericValue)
				error('only implemented for one value per grid');
			end
			numericValues = values(isNumericValue);
			if ~isempty(numericValues) && numel(obj(1).gridName) == numel(numericValues)
				% if all grids are evaluated, solution is a double array.
				solution = reshape(obj.on(numericValues), size(obj));
			else
				% evaluate numeric values
				subsGrid = obj(1).grid;
				selectRemainingGrid = false(1, numel(obj(1).grid));
				for currentGridName = gridName(isNumericValue)
					selectGrid = strcmp(obj(1).gridName, currentGridName);
					subsGrid{selectGrid} = values{strcmp(gridName, currentGridName)};
					selectRemainingGrid = selectRemainingGrid | selectGrid;
				end
				newGrid = obj(1).grid(~selectRemainingGrid);
				newGridName = obj(1).gridName(~selectRemainingGrid);
 				for it = 1 : numel(values)
 					if ~isNumericValue(it) && ~isempty(obj(1).gridName(~selectRemainingGrid)) 
						% check if there is a symbolic value and if this value exists in the object
						if ischar(values{it})
							newGridName{strcmp(obj(1).gridName(~selectRemainingGrid), gridName{it})} ...
								= values{it};
						elseif isa(values{it}, 'sym')
							gridNameTemp = symvar(values{it});
							assert(numel(gridNameTemp) == 1, 'replacing one gridName with 2 gridName is not supported');
							newGridName{strcmp(obj(1).gridName(~selectRemainingGrid), gridName{it})} ...
								= char(gridNameTemp);
							
						else
							error(['value{', num2str(it), '} is of unsupported type']);
						end
 					end
				end
								
				symbolicSolution = subs(obj.sym(), gridName, values);				
				
				if isempty(symvar(symbolicSolution(:)))
					% take the new values that are not numeric as the new
					% variables:
329
330
					newGridName = unique(newGridName);
					newGrid = cell(1, numel(newGridName));
331
332
333
334
335
336
					charValues = cell(0);
					for it = 1 : numel(values)
						if ischar(values{it})
							charValues{end+1} = values{it};
						end
					end
337
					for it = 1 : numel(newGridName)
338
						if ~isempty(charValues) && contains(charValues, newGridName{it})
339
340
341
342
343
							newGrid{it} = obj.gridOf(gridName{strcmp(values, newGridName{it})});
						else
							newGrid{it} = obj.gridOf(newGridName{it});
						end
					end
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
					solution = quantity.Symbolic(double(symbolicSolution), ...
						'grid', newGrid, 'variable', newGridName, 'name', obj(1).name);
				else
					% before creating a new quantity, it is checked that
					% newGridName is unique. If there are non-unique
					% gridName, multiple are removed and the finest grid
					% from newGrid is taken.
					uniqueGridName = unique(newGridName);
					if numel(newGridName) == numel(uniqueGridName)
						solution = quantity.Symbolic(symbolicSolution, ...
							'grid', newGrid, 'variable', newGridName, 'name', obj(1).name);
					else
						uniqueGrid = cell(1, numel(uniqueGridName));
						for it = 1 : numel(uniqueGrid)
							gridCandidatesTemp = newGrid(...
								strcmp(uniqueGridName{it}, newGridName));
							for jt = 1 : numel(gridCandidatesTemp)
								if ~isempty(uniqueGrid{it})
									assert(uniqueGrid{it}(1) == gridCandidatesTemp{jt}(1) ...
										&& uniqueGrid{it}(end) == gridCandidatesTemp{jt}(end), ...
										'grids must have same domain');
								end
								if isempty(uniqueGrid{it}) || ...
										numel(gridCandidatesTemp{jt}) > numel(uniqueGrid{it})
									uniqueGrid{it} = gridCandidatesTemp{jt};
								end
							end
						end
						solution = quantity.Symbolic(symbolicSolution, ...
							'grid', uniqueGrid, 'variable', uniqueGridName, 'name', obj(1).name);
					end
				end
				
			end
		end
		
		function solution = subsValueWithArray(obj, gridName, values)
			% This function substitutes the variables specified with
			% gradName with values. Values must be a numerical scalar for
			% each gridName.
			% This method works for value-arrays but not for obj-arrays.
			% In contrast to on() or at(), only some, but not necessarily
			% all variables are evaluated.
			if numel(obj) ~= 1
				error('Yet this method is not implemented for quanitity arrays');
			end
			if ~iscell(gridName)
				gridName = {gridName};
			end
			if ~iscell(values)
				values = {values};
			end
			if numel(values) ~= numel(gridName)
				error(['Number of variables to be substituted and number ', ...
					'of values do not coincide']);
			end
			if isa(values{1}, 'sym')
				error('subs is yet only implemented for numeric values');
			end
			valueSize = [cellfun(@numel, values), 1];
			if all(strcmp(obj(1).gridName, gridName))
				% if all grids are evaluated, solution is a double array.
				solution = obj.at(values);
			else
				symbolicSolution = reshape(subs(obj.valueSymbolic, ...
					obj(1).variable(strcmp(obj(1).gridName, gridName)), values{:}), valueSize);
				solution = quantity.Symbolic(symbolicSolution, ...
					'variable', obj(1).variable(~strcmp(obj(1).gridName, gridName)),...
					'gridName', obj(1).gridName(~strcmp(obj(1).gridName, gridName)), ...
					'grid', obj(1).grid(~strcmp(obj(1).gridName, gridName)));
			end
		end
		
		function solution = solveAlgebraic(obj, rhs, gridName, varargin)
			%% this method solves
			%	obj(gridName) == rhs
			% for the variable specified by gridName.
			% rhs must be of apropriate size and gridName must
			% be an gridName of obj. If the result is constant (i.e., if
			% obj only depends on variable, then a double array is
			% returned. Else the solution is of the type as obj.
			if ~isequal(size(rhs), size(obj))
				error('rhs has not the same size as quantity');
			end
			if ~iscell(gridName)
				gridName = {gridName};
			end
			if numel(gridName) ~= 1
				error('this function can only solve for one variable');
			end
			if isempty(strcmp(obj(1).gridName, gridName{1}))
				error('quantity does not depend on variable');
			end
			symbolicSolution = solve(obj(:).sym == rhs(:), ...
				obj(1).variable(strcmp(obj(1).gridName, gridName{1})));
			if isnumeric(rhs) && (numel(obj(1).variable) == 1)
				% solution will be a double array
				solution = double(symbolicSolution);
			else
				solution = quantity.Symbolic(symbolicSolution);
			end
		end
		
		function solution = solveDVariableEqualQuantity(obj, varargin)
			%% solves the first order ODE
			%	dvar / ds = obj(var(s))
			%	var(s=0) = ic
			% for var(s, ic). Herein, var is the (only) continuous variale
			% obj.variable. The initial condition of the IVP is a variable
			% of the result var(s, ic).
			assert(numel(obj(1).gridName) == 1, ...
				'this method is only implemented for quanitities with one gridName');
			
			myParser = misc.Parser();
			myParser.addParameter('initialValueGrid', obj(1).grid{1});
			myParser.addParameter('variableGrid', obj(1).grid{1});
			myParser.addParameter('newGridName', 's');
			myParser.parse(varargin{:});
			
			s = sym(myParser.Results.newGridName);
			syms ic varNew(s)
			symbolicSolution = sym(zeros(size(obj)));
			for it = 1:numel(obj)
				symbolicSolution(it) = ...
					dsolve(diff(varNew(s), s) == subs(obj(it).valueSymbolic, ...
					obj(it).variable, varNew(s)), varNew(0)==ic);
			end
			solution = quantity.Symbolic(symbolicSolution, ...
				'gridName', {myParser.Results.newGridName, 'ic'}, 'variable', {s, ic}, ...
				'grid', {myParser.Results.variableGrid, myParser.Results.initialValueGrid}, ...
				'name', ['solve(', obj(1).name, ')']);
		end
				
		function sym = sym(obj)
			sym = reshape([obj.valueSymbolic], size(obj));
		end
		
		function A = adj(obj)
			a = misc.adj(obj.valueSymbolic);
			argin = misc.struct2namevaluepair(obj.obj2struct());
			A = quantity.Symbolic(a, argin{:});
			warning('not tested')
		end
		
		function y = sqrt(x)
			% quadratic root for scalar and diagonal symbolic quantities
			y = quantity.Symbolic(sqrt(x.sym()), ...
				'grid', x(1).grid, 'variable', x(1).variable, ...
				'name', ['sqrt(', x(1).name, ')']);
		end
		
		function y = sqrtm(x)
			% quadratic root for matrices of symbolic quantities
			y = quantity.Symbolic(sqrtm(x.sym()), ...
				'grid', x(1).grid, 'variable', x(1).variable, ...
				'name', ['sqrtm(', x(1).name, ')']);
		end
		
Jakob Gabriel's avatar
Jakob Gabriel committed
502
503
504
505
506
507
		function result = diff(obj, k, diffGridName)
			% diff applies the kth-derivative for the variable specified with
			% the input gridName to the obj. If no gridName is specified, then diff
			% applies the derivative w.r.t. to all gridNames / variables.
			if nargin == 1 || isempty(k)
				k = 1; % by default, only one derivatve per diffGridName is applied
508
			end
Jakob Gabriel's avatar
Jakob Gabriel committed
509
510
511
			if nargin <= 2 % if no diffGridName is specified, then the derivative 
				% w.r.t. to all gridNames is applied
				diffGridName = obj(1).gridName;
512
513
			end
			
Jakob Gabriel's avatar
Jakob Gabriel committed
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
			result = obj.copy();
			if iscell(diffGridName)
				for it = 1 : numel(diffGridName)
					result = result.diff(k, diffGridName{it});
				end
			else
				diffVariable = obj.gridName2variable(diffGridName);
				[result.name] = deal(['(d_{' char(diffVariable) '} ' result(1).name, ')']);
				[result.valueDiscrete] = deal([]);
				for l = 1:numel(obj)
					result(l).valueSymbolic = diff(obj(l).valueSymbolic, diffVariable, k);
					result(l).valueContinuous = obj.setValueContinuous(result(l).valueSymbolic, obj(1).variable);
				end
			end
		end
		
		function thisVariable = gridName2variable(obj, thisGridName)
			% this method returns the variable thisVariable stored in obj(1).variable
			% in the order specified by thisGridName. If thisGridName is a char, then
			% only one variable is returned. If thisGridName is a cell-array, then a
			% cell array of variables is returned.
			if ischar(thisGridName)
				assert(any(strcmp(obj(1).gridName, thisGridName)), ...
					['The gridName ', thisGridName, ' is not a gridName of this Quantity']);
				variableNames = arrayfun(@(v) char(v), obj(1).variable, 'UniformOutput', false);
				selectVariable = strcmp(variableNames, thisGridName);
				assert(sum(selectVariable) == 1);
				thisVariable = obj(1).variable(selectVariable);
				
			elseif iscell(thisGridName)
				thisVariable = cell(size(thisGridName));
				for it = 1 : numel(thisGridName)
					thisVariable{it} = obj.gridName2variable(thisGridName{it});
				end
			else
				error(['The input gridName of gridName2variable() must be a char-array', ...
					' or a cell-array']);
551
552
553
554
			end
		end
		
		function mObj = uminus(obj)
555
556
557
558
559
560
561
			% unitary minus: C = -A
			mObj = quantity.Symbolic(-obj.sym, 'grid', obj(1).grid, ...
				'variable', obj(1).variable, 'name', ['-', obj(1).name]);
		end
		function mObj = uplus(obj)
			% unitary plus: C = +A
			mObj = copy(obj);
562
563
564
565
		end
		
		function C = mtimes(A, B)
			% if one input is ordinary matrix, this is very simple.
Ferdinand Fischer's avatar
Ferdinand Fischer committed
566
567
568
569
570
571
572
			if isempty(B) ||  isempty(A)
				% TODO: actually this only holds for multiplication of
				% matrices. If higher dimensional arrays are multiplied
				% this can lead to wrong results.
				C = quantity.Symbolic( mtimes@quantity.Discrete(A, B) );
				return
			end			
573
574
575
			if isnumeric(B)
				C = quantity.Symbolic(A.sym() * B, ...
					'grid', A(1).grid, 'variable', A(1).variable, ...
576
					'name', [A(1).name, ' c']);
577
578
579
580
581
				return
			end
			if isnumeric(A)
				C = quantity.Symbolic(A * B.sym(), ...
					'grid', B(1).grid, 'variable', B(1).variable, ...
582
					'name', ['c ', B(1).name]);
583
584
585
586
587
				return
			end
			
			idx = computePermutationVectors(A, B);
			parameters = struct(A(1));
588
			parameters.name = [A(1).name, ' ', B(1).name];
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
			parameters.grid = ...
				[A(1).grid(idx.A.common), ...
				A(1).grid(~idx.A.common), ...
				B(1).grid(~idx.B.common)];
			parameters.gridName = [A(1).gridName(idx.A.common), ...
				A(1).gridName(~idx.A.common), ...
				B(1).gridName(~idx.B.common)];
			parameters.variable = [A(1).variable(idx.A.common), ...
				A(1).variable(~idx.A.common), ...
				B(1).variable(~idx.B.common)];
			parameters = misc.struct2namevaluepair(parameters);
			
			C = quantity.Symbolic(A.sym() * B.sym(), parameters{:});
			
		end
		
		
% 		function C = mrdivide(A, B)
% 			%TODO es fehlt noch die Beschränkung der Funktion auf die Fälle
% 			%für welche diese Operation funktioniert!
609
%			%TODO only english comments please. ;)
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
% 			
% 			ftr = false;    % % flag for the special case a == const.
% 			
% 			if isa(B, 'double')
% 				B = quantity.Symbolic(sym(B), 'grid', {});
% 			end
% 			
% 			if isa(A, 'double')
% 				A_ = quantity.Symbolic(sym(A), 'grid', {});
% 				A = B';
% 				B = A_';
% 				ftr = true;
% 			end
% 			
% 			idx = computePermutationVectors(A, B);
% 			parameters = struct(A(1));
% 			parameters.name = [A(1).name B(1).name];
% 			parameters.grid = quantity.Discrete.normalizeGrid(...
% 				[A(1).grid(idx.A.common), ...
% 				A(1).grid(~idx.A.common), ...
% 				B(1).grid(~idx.B.common)]);
% 			parameters.gridName = [A(1).gridName(idx.A.common), ...
% 				A(1).gridName(~idx.A.common), ...
% 				B(1).gridName(~idx.B.common)];
% 			parameters.variable = [A(1).variable(idx.A.common), ...
% 				A(1).variable(~idx.A.common), ...
% 				B(1).variable(~idx.B.common)];
% 			parameters = misc.struct2namevaluepair(parameters);
% 			
% 			if ftr
% 				c = A.sym() \ B.sym(); % TODO check ob das wirklich stimmt
% 			else
% 				c = A.sym() / B.sym();
% 			end
% 			
% 			C = quantity.Symbolic(c, parameters{:});
% 			
% 			
% 			if ftr
% 				C = C';
% 			end
% 			
% 		end
		
		
		function C = minus(a, b)
656
			assert(isequal(size(a), size(b)), 'terms must be of same size');
657
658
659
660
661
662
663
664
			if ~isa(a, 'quantity.Discrete')
				a = quantity.Symbolic(a, 'name', 'c');
				parameters = struct(b(1));
			else
				if ~isa(b, 'quantity.Discrete')
					b = quantity.Symbolic(b, 'name', 'c');
				end
				parameters = struct(a(1));
665
			end
666
667
			
			parameters.name = [a(1).name, '-' , b(1).name];
668
669
670
671
672
			parameters = misc.struct2namevaluepair(parameters);
			C = quantity.Symbolic(a.sym() - b.sym(), parameters{:});
		end
		
		function C = plus(a, b)
673
			assert(isequal(size(a), size(b)), 'terms must be of same size');
674
675
676
677
678
679
680
681
			if ~isa(a, 'quantity.Discrete')
				a = quantity.Symbolic(a, 'name', 'c');
				parameters = struct(b(1));
			else
				if ~isa(b, 'quantity.Discrete')
					b = quantity.Symbolic(b, 'name', 'c');
				end
				parameters = struct(a(1));
682
			end
683
684
			
			parameters.name = [a(1).name, '+' , b(1).name];
685
686
687
688
689
690
691
			parameters = misc.struct2namevaluepair(parameters);
			C = quantity.Symbolic(a.sym() + b.sym(), parameters{:});
		end
		
		function absQuantity = abs(obj)
			% abs returns the absolut value of the quantity as a quantity
			absQuantity = quantity.Symbolic(abs(obj.sym()), ...
692
				'grid', obj(1).grid, 'variable', obj(1).variable, ...
693
694
695
696
697
698
699
				'name', ['|', obj(1).name, '|']);	
		end
		
		function y = inv(obj)
			% inv inverts the matrix obj.
			y = quantity.Symbolic(inv(obj.sym()), ...
				'name', ['(', obj(1).name, ')^{-1}'], ...
700
				'grid', obj(1).grid, 'variable', obj(1).variable, ...
701
702
703
704
705
706
707
				'variable', obj(1).variable);
		end
		
		function y = exp(obj)
			% exp() is the exponential function using obj as the exponent.
			y = quantity.Symbolic(exp(obj.sym()), ...
				'name', ['exp(', obj(1).name, ')'], ...
708
				'grid', obj(1).grid, 'variable', obj(1).variable, ...
709
710
711
712
713
714
715
				'variable', obj(1).variable);
		end
		
		function y = expm(obj)
			% exp() is the matrix-exponential function using obj as the exponent.
			y = quantity.Symbolic(expm(obj.sym()), ...
				'name', ['expm(', obj(1).name, ')'], ...
716
				'grid', obj(1).grid, 'variable', obj(1).variable, ...
717
718
719
				'variable', obj(1).variable);
		end
		
720
721
722
723
724
725

		
		%% TODO das verhalten von int und cumint an das von quantity.Discrete anpassen!
% 		function C = cumint(obj, z, a, b)
% 			z = obj(1).grid{1};
% 			C = obj.int(z, z(1), obj.variable);
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
% 		end
		
		function C = int(obj, z, a, b)
			% from help sym/int:
			% "int(S,v,a,b) is the definite integral of S with respect to v
			%  from a to b."
			
			if nargin == 2
				if ~iscell(z)
					z = {z};
				end				
				
				grdIdx = strcmp(obj(1).gridName, z);
				a = obj(1).grid{grdIdx}(1);
				b = obj(1).grid{grdIdx}(end);
			end
			
			if nargin == 3
				b = a;
				a = z;
				z = num2cell( obj(1).variable );
			end
			
			if nargin == 1
				assert(obj.nargin == 1, 'Not Implemented')
				z = obj(1).grid{1};
				a = z(1);
				b = obj.variable;
				z = num2cell( obj(1).variable );
			end
			
			if obj.nargin == numel(z) && isnumeric(a) && isnumeric(b)
				C = int@quantity.Function(obj, a, b);
				%                 for k = 1:numel(obj)
				%                    C(k) = double( int( sym(obj(k)), z, a, b) );
				%                 end
				%                 C = reshape(C, size(C));
			else
				% compute the symbolic integration
				I = int(obj.sym, z{:}, a, b);
766
767
768
769
770
				variableI = quantity.Symbolic.getVariable(I);
				if iscell(variableI)
					gridNameI = cellfun(@(v) char(v), variableI);
				else
					gridNameI = char(variableI);
771
				end
772
773
774
775
776
777
778
779
780
				C = quantity.Symbolic(I, ...
					'grid', obj.gridOf(gridNameI), 'variable', variableI, ...
					'name', ['int[', obj(1).name, ']']);
% 				obj.copy();
% 				for k = 1:numel(obj)
% 					C(k).valueSymbolic = I(k);
% 					C(k).valueDiscrete = [];
% 					C(k).valueContinuous = obj.setValueContinuous(I(k));
% 				end
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
			end
		end
		
	end
	
	methods (Access = protected)
		
		function p = getCopyParameters(obj)
			p = nameValuePair(obj, 'gridName');
		end
		
		% Override copyElement method:
		function cpObj = copyElement(obj)
			% Make a shallow copy of all properties
			cpObj = copyElement@quantity.Discrete(obj);
			% #TODO insert code here if some properties should not be copied.
		end
		
		
		%TODO change this function to have the possibility to compute it to a
		%certain accuracy
		function Phi = internPeanoBakerSeries(obj, z, z0, N)
			% PEANOBAKERSERIES_SYM Symbolic computation of (partial) peano baker series
			% Detailed description for The Peano-Baker Series can be found in:
			% The Peano-Baker Series, MICHAEL BAAKE AND ULRIKE SCHLAEGEL, 2012
			%
			% [ Phi ] = panobakerseries_sym( A, z0, N )
			%
			% * A:   System-Operator Matrix \in C[s]^(nxn); with n is the number of
			%      states.
			% * z0:  Lower limit of Integration
			% * N:  Number of iterations
			%
			% * Phi: First result of the state transition matrix.(n x n x 1)
			
			I = quantity.Symbolic( zeros(size(obj,1), size(obj,2), N ));
			I(:,:,1) = eye( size(obj,1) );
			Phi = I(:,:,1);
			for k = 1 : N - 1
				I(:, :, k + 1) = int( obj * I(:, :, k), z, z0, z );
				Phi = Phi + I(:, :, k + 1);
			end
		end
		
	end
	
	methods (Static)
		
		function P = zeros(valueSize, varargin)
			%ZEROS initializes an zero quantity.Discrete object of the size
			%specified by the input valueSize.
			P = quantity.Symbolic(zeros(valueSize), varargin{:});
		end

		function [p, q, parameters] = inputNormalizer(a, b)
			
			parameters = [];
			
			function s = innerInputNormalizer(a)
				if isa(a, 'double')
					s.name = num2str(a(:)');
					s.value = a;
					% 					s.variable = [];
				elseif isa(a, 'sym')
					s.name = char(a);
					s.value = a;
					% 					s.variable = [];
				elseif isa(a, 'quantity.Symbolic')
					s.name = a(1).name;
					s.value = a.sym();
					% 					s.variable = a.variable;
					parameters = struct(a);
					parameters = rmfield(parameters, 'gridName');
				end
			end
			
			p = innerInputNormalizer(a);
			q = innerInputNormalizer(b);
			
		end
	end
	
	methods (Static, Access = protected)
		function var = getVariable(symbolicFunction, nVar)
			
			if misc.issym(symbolicFunction(:).')
				var = symvar(symbolicFunction(:).');
			else
				var = [];
			end
			
			if isempty(var)
				% 				var = quantity.Symbolic.defaultSymVar;
			else
				
				if var(1) ~= quantity.Symbolic.defaultSymVar
					% fast solution to order the spatial and temporal variable
					% in the common order: (z, t)
					var = flip(var);
				end
			end
		end
		function doNotCopy = doNotCopyPropertiesName()
			parentalProperties = quantity.Discrete.doNotCopyPropertiesName;
			doNotCopy = {parentalProperties{:}, ...
				'valueSymbolic', 'isConstant'};
		end
		function f = setValueContinuous(f, symVar)
			
			if isa(f, 'sym')
				if nargin == 1
					symVar = quantity.Symbolic.getVariable(f);
				end
				
				if isempty(symvar(f))
					f = @(varargin) double(f) + quantity.Function.zero(varargin{:});
				else
					f = matlabFunction(f, 'Vars', symVar );
				end
			end
		end
	end
end