Discrete.m 91.8 KB
Newer Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
			assert(numel(p) == 2);
			assert(numel(q) == 1);
			q = p(2);
			p = p(1);
			
			A = a.on();
			B = repmat(b.on(), 1, 1, 1, q);
			B = permute(B, [1, 4, 2, 3]);
			
			% dimensions
			n = size(a, 1);
			m = size(b, 2);
			o = size(b, 1);
			
			P = zeros(p, q, n, m);
			
			for k = 1 : n % rows of P
				for l = 1 : m % columns of P
					for r = 1 : o % rows of B or columns of A
						P(:, :, k, l) = P( :, :, k, l ) + A( :, :, k, r) .* B( :, :, r, l );
					end
				end
			end
			
2025
2026
2027
			objOptArgList = a(1).optArgList();
			P = quantity.Discrete(quantity.Discrete.value2cell(P, [p, q], [n, m]), a(1).domain, ...
				objOptArgList{:});
2028
2029
		end
		
Jakob Gabriel's avatar
Jakob Gabriel committed
2030
2031
		function myGrid = gridOf(obj, myGridName)
			if ~iscell(myGridName)
2032
				gridIdx = obj(1).domain.gridIndex(myGridName);
2033
2034
2035
2036
2037
				if gridIdx > 0
					myGrid = obj(1).grid{gridIdx};
				else
					myGrid = [];
				end
Jakob Gabriel's avatar
Jakob Gabriel committed
2038
2039
2040
			else
				myGrid = cell(size(myGridName));
				for it = 1 : numel(myGrid)
2041
					gridIdx = obj(1).domain.gridIndex(myGridName{it});
2042
2043
2044
2045
2046
					if gridIdx > 0
						myGrid{it} = obj(1).grid{gridIdx};
					else
						myGrid{it} = [];
					end
2047
2048
2049
2050
				end
			end
		end
		
2051
		function result = diff(obj, diffGridName, k)
2052
			% DIFF computation of the derivative
Jakob Gabriel's avatar
Jakob Gabriel committed
2053
			%	result = DIFF(obj, diffGridName, k) applies the
2054
2055
			% 'k'th-derivative for the variable specified with the input
			% 'diffGridName' to the obj. If no 'diffGridName' is specified,
2056
			% then diff applies the derivative w.r.t. all gridNames.
2057
2058
2059
2060
			arguments
				obj;
				diffGridName (1,1) string = obj(1).gridName;
				k uint64 = 1;
2061
2062
2063
			end
			
			if obj.isConstant && isempty(obj(1).gridName)
2064
2065
				result = quantity.Discrete(zeros(size(obj)), obj(1).domain, ...
					'size', size(obj), 'name', "(d_{.}" + obj(1).name + ")");
2066
				return
2067
			end
2068
			
2069
			if isa(diffGridName, 'quantity.Domain')
2070
				% a quantity.Domain is used instead of a grid name
2071
				%	-> get the grid name from the domain object
2072
				%
2073
2074
2075
2076
2077
2078
				% #todo@domain: make the default case to call with a
				% quantity.Domain instead of a grid name. Then, the
				% section about the grid selection and so can be simplified
				%
				diffGridName = {diffGridName.name};
			end
2079
			
2080
			result = obj.diff_inner(k, diffGridName);
2081
		end % diff()
2082
		
2083
		function I = int(obj, varargin)
2084
2085
			% INT integrate
			% INT(obj) is the definite integral of obj with respect to
Jakob Gabriel's avatar
Jakob Gabriel committed
2086
			%   all of its independent variables over the grid obj(1).grid.
2087
2088
			% INT(obj, gridName) is the definite integral of obj with
			%	respect to the spatial coordinate grid name, that has to
Jakob Gabriel's avatar
Jakob Gabriel committed
2089
2090
			%	be one of obj(1).gridName. The names must be a string or a
			%   array of strings.
Jakob Gabriel's avatar
Jakob Gabriel committed
2091
			% INT(obj, a, b) (question: on which domain? what if numel(gridName)>1?)
2092
			% INT(obj, gridName, a, b)
2093
2094
2095
2096
			if isempty(obj)
				I = obj.copy();
				return
			end
2097
			
2098
2099
2100
2101
			if nargin == 1 || nargin == 3
				% obj.int() -> integrate over all dimensions of this
				% quantity.
				intDomain = obj(1).domain;
2102
			elseif nargin == 2
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
				% obj.int(z) OR obj.int(z, a, b)
				% integrate over the domain
				if isa(varargin{1}, 'quantity.Domain')
					intDomain = varargin{1};
				else
					intDomain = obj(1).domain.find( varargin{1} );
				end
			elseif nargin == 4
				% obj.int(z, <domain>, lowerBound, upperBOund)
				%
				I = cumInt( obj, varargin{:});
				return;
			end
2116
			
2117
			if nargin == 3
2118
2119
2120
				% (obj, a, b)
				a = varargin{1};
				b = varargin{2};
2121
2122
2123
			elseif nargin == 1 || nargin == 2
				a = [intDomain.lower];
				b = [intDomain.upper];
2124
2125
			end
			
2126
			I = obj.cumInt(intDomain(1), a(1), b(1));
2127
			
2128
2129
2130
			if numel(intDomain) > 1
				I = I.int(intDomain(2:end), a(2:end), b(2:end));
				return
2131
			end
2132
2133
		end
		
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
		function result = cumInt(obj, domain, lowerBound, upperBound)
			% CUMINT cumulative integration
			%	result = cumInt(obj, domain, lowerBound, upperBound)
			%	performes the integration over 'obj' for the 'domain' and
			%	the specified 'bounds'. 'domain' must be a gridName of the
			%	object. 'lowerBound' and 'upperBound' define the boundaries
			%	of the integration domain. These can be either doubles or
			%	gridNames. If it is double, the bound is constant. If it is
			%	a gridName, the bound is variable and the result is a
			%	function dependent of this variable.
2144
			
Jakob Gabriel's avatar
Jakob Gabriel committed
2145
2146
			% input parser since some default values depend on intGridName.
			myParser = misc.Parser;
2147
			myParser.addRequired('domain', @(d) obj(1).domain.gridIndex(d) ~= 0);
Jakob Gabriel's avatar
Jakob Gabriel committed
2148
			myParser.addRequired('lowerBound', ...
2149
				@(l) isnumeric(l) || ischar(l) || isstring(l));
Jakob Gabriel's avatar
Jakob Gabriel committed
2150
			myParser.addRequired('upperBound', ...
2151
				@(l) isnumeric(l) || ischar(l) || isstring(l));
Jakob Gabriel's avatar
Jakob Gabriel committed
2152
			myParser.parse(domain, lowerBound, upperBound)
2153
2154
2155
			
			% get grid
			myGrid = obj(1).grid;
2156
			intGridIdx = obj(1).domain.gridIndex(domain);
2157
2158
			
			% integrate
2159
2160
			F = numeric.cumtrapz_fast_nDim(myGrid{intGridIdx}, ...
				obj.on(), intGridIdx);
2161
			result = quantity.Discrete(F, obj(1).domain);
2162
			
2163
			% int_lowerBound^upperBound f(.) =
2164
			%	F(upperBound) - F(lowerBound)
2165
2166
			result = result.subs(domain, upperBound) ...
				- result.subs(domain, lowerBound);
2167
			if isa(result, 'quantity.Discrete')
2168
				result.setName("int(" + obj(1).name + ")");
2169
			end
2170
		end
2171
		
2172
2173
		function C = plus(A, B)
			%% PLUS is the sum of two quantities.
Jakob Gabriel's avatar
Jakob Gabriel committed
2174
			assert(isequal(size(A), size(B)), 'plus() not supports mismatching sizes')
2175
			
Ferdinand Fischer's avatar
Ferdinand Fischer committed
2176
2177
2178
2179
2180
			if isempty(A) || isempty(B)
				C = quantity.Discrete.empty(size(A));
				return
			end
			
2181
2182
			% for support of numeric inputs:
			if ~isa(A, 'quantity.Discrete')
2183
				if isnumeric(A)
2184
					A = quantity.Discrete(A, quantity.Domain.empty(), 'name', "c");
2185
				else
2186
					error('Not yet implemented')
2187
				end
2188
			elseif ~isa(B, 'quantity.Discrete')
2189
				if isnumeric(B)
2190
					B = quantity.Discrete(B, quantity.Domain.empty(), 'name', "c");
2191
				else
2192
					B = quantity.Discrete(B, A(1).domain, 'name', "c");
2193
				end
2194
2195
			end
			
2196
			% combine both domains with finest grid
2197
			joinedDomain = join(A(1).domain, B(1).domain);
2198
			
2199
2200
			[aDiscrete] = A.expandValueDiscrete(joinedDomain);
			[bDiscrete] = B.expandValueDiscrete(joinedDomain);
2201
			
2202
			% create result object
2203
			C = quantity.Discrete(aDiscrete + bDiscrete, joinedDomain, ...
2204
				'name', A(1).name + "+" + B(1).name);
2205
2206
2207
2208
		end
		
		function C = minus(A, B)
			% minus uses plus()
2209
			C = A + (-B);
2210
			if isnumeric(A)
2211
				[C.name] = deal("c-" + B(1).name);
2212
2213
				
			elseif isnumeric(B)
2214
				[C.name] = deal(A(1).name + "-c");
2215
			else
2216
				[C.name] = deal(A(1).name + "-" + B(1).name);
2217
			end
2218
2219
		end
		
2220
2221
2222
2223
2224
2225
		
		function C = uplus(A)
			% unitary plus: C = +A
			C = copy(A);
		end
		
2226
		function C = uminus(A)
2227
			% unitary plus: C = -A
2228
			C = (-1) * A;
2229
			[C.name] = deal("-" + A(1).name);
2230
2231
		end
		
2232
		function [P, supremum] = relativeErrorSupremum(A, B)
2233
2234
2235
2236
2237
			%% RELATIVEERRRORSUPREMUM compute the relative error of the supremum
			% [P, SUPREMUM] = relativeErrorSupremum(A, B) computes the supremum of the absolute
			% error of A and B and returns the relative error of A - B with respect to this
			% supremum. The relative error is returned as P and the supremum is returned as
			% SUPREMUM.
2238
2239
2240
2241
			assert(numel(A) == numel(B), 'Not implemented')
			
			P = A.copy();
			
Ferdinand Fischer's avatar
Ferdinand Fischer committed
2242
			if ~isa(B, 'quantity.Discrete')
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
				B = quantity.Discrete(B);
			end
			supremum = nan(size(A));
			for k = 1:numel(A)
				supremum(k) = max(max(abs(A(k).valueDiscrete), abs(B(k).valueDiscrete)));
				P(k).valueDiscrete = (A(k).valueDiscrete - B(k).valueDiscrete) ./ supremum(k);
				P(k).name = sprintf('%.2g', supremum(k));
			end
			supremum = reshape(supremum, size(A));
		end
		
2254
		function [i, m, s] = near(obj, B, varargin)
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
			%% NEAR comparer with numerical tolerance
			% [i, m, s] = near(obj, B) compares the numerical
			%	values of this object with the numerical values of B. B has
			%	to be a quantity.Discrete object.
			% [i, m, s] = near(obj, B, tolerance) compares the numerical
			% values with respect to the given tolerance. Default is 10eps.
			% [i, m, s] = near(obj, B, tolerance, relative) compares the
			% numerical values with respect to the given tolerance and if
			% relative is true, the values are compared, relativley to the
			% maximal value of obj.on(). If relative is a numerical value.
			% The values are compared reltivley to this.
			%
			% The output i is a logical value that is true if the values
			% are inside the given tolerance. m is the maximal derivation
			% of both numerical values. s is the text that is printed if
			% the function is called without output.
2271
2272
2273
2274
2275
2276
2277
2278
2279
			
			if nargin == 1
				b = 0;
			elseif isnumeric(B)
				b = B;
			else
				b = B.on(obj(1).grid, obj(1).gridName);
			end
			
2280
2281
			[i, m, s] = numeric.near(...
				obj.on(obj(1).grid, obj(1).gridName), ...
2282
				b, varargin{:});
2283
2284
2285
2286
			if nargout == 0
				i = s;
			end
		end
Ferdinand Fischer's avatar
Ferdinand Fischer committed
2287
		function maxValue = MAX(obj)
2288
2289
2290
2291
2292
2293
2294
			maxValue = max(obj.max(), [], 'all');
		end
		
		function maxValue = max(obj)
			% max returns the maximal value of all elements of A over all
			% variables as a double array.
			maxValue = reshape(max(obj.on(), [], 1:obj(1).nargin), size(obj));
Ferdinand Fischer's avatar
Ferdinand Fischer committed
2295
2296
		end
		
2297
2298
2299
		function minValue = min(obj)
			% max returns the minimum value of all elements of A over all
			% variables as a double array.
2300
			minValue = reshape(min(obj.on(), [], 1:obj(1).nargin), size(obj));
2301
		end % min()
2302
2303
2304
		
		function absQuantity = abs(obj)
			% abs returns the absolut value of the quantity as a quantity
2305
2306
			if isempty(obj)
				absQuantity = quantity.Discrete.empty(size(obj));
2307
			else
2308
				absQuantity = quantity.Discrete(abs(obj.on()), obj(1).domain, ...
2309
					'size', size(obj), 'name', "|" + obj(1).name + "|");
2310
			end
2311
2312
		end % abs()
		
2313
		function d = det(obj, setName)
2314
			% det(X) returns the the determinant of the squre matrix X
2315
2316
2317
2318
			arguments
				obj;
				setName = true;
			end
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
			if isempty(obj)
				d = quantity.Discrete.empty(1);
			elseif numel(obj) == 1
				d = copy(obj);
			elseif ismatrix(obj) && (size(obj, 1) == size(obj, 2))
				if size(obj, 1) == 2
					d = obj(1, 1) * obj(2, 2) - obj(2, 1) * obj(1, 2);
				elseif size(obj, 1) == 3
					d = prod(obj(logical([1, 0, 0; 0, 1, 0; 0, 0, 1]))) ...
						+ prod(obj(logical([0, 1, 0; 0, 0, 1; 1, 0, 0]))) ...
						+ prod(obj(logical([0, 0, 1; 1, 0, 0; 0, 1, 0]))) ...
						- prod(obj(logical([0, 0, 1; 0, 1, 0; 1, 0, 0]))) ...
						- prod(obj(logical([1, 0, 0; 0, 0, 1; 0, 1, 0]))) ...
						- prod(obj(logical([0, 1, 0; 1, 0, 0; 0, 0, 1])));
				else
					% maybe a more sophisticated implementation would be nice.
2335
2336
2337
2338
2339
2340
2341
					d = 0;
					for it = 1 : size(obj, 1)
						selector = true(size(obj));
						selector(:, 1) = false;
						selector(it, :) = false;
						d = d + (-1)^(it-1)*obj(it, 1) * det(reshape(obj(selector), size(selector)-1), false);
					end % for it = 1 : size(obj, 1)
2342
2343
2344
2345
				end
			else
				error("det is only defined for quadratic matrices");
			end
2346
2347
2348
			if setName
				d.setName("det(" + obj(1).name + ")");
			end
2349
2350
		end % det()
		
2351
2352
		function y = real(obj)
			% real() returns the real part of the obj.
2353
2354
			y = quantity.Discrete(real(obj.on()), obj(1).domain, ...
				'name', "real(" + obj(1).name + ")",  ...
2355
2356
2357
2358
2359
				'size', size(obj));
		end % real()
		
		function y = imag(obj)
			% real() returns the imaginary part of the obj.
2360
2361
			y = quantity.Discrete(imag(obj.on()), obj(1).domain, ...
				'name', "imag(" + obj(1).name + ")", 'size', size(obj));
2362
		end % imag()
2363
		
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
		function meanValue = mean(obj, dim)
			% mean(dim) returns the mean value over all domain as a double array 
			% of the size of obj.
			%
			% mean(obj, 'all') returns the mean value of all elements of A over all
			% domain as a scalar double.
			arguments
				obj;
				dim = [];
			end
			if strcmp(dim, "all")
				meanValue = mean(obj.on(), 'all');
			else
				meanValue = reshape(mean(obj.on(), 1:obj(1).nargin), size(obj));
			end
2379
		end % mean()
Jakob Gabriel's avatar
Jakob Gabriel committed
2380
		
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
		function medianValue = median(obj, dim)
			% median(dim) returns the median value over all domain as a double array 
			% of the size of obj.
			%
			% median(obj, 'all') returns the median value of all elements of A over all
			% domain as a scalar double.
			arguments
				obj;
				dim = [];
			end
			if strcmp(dim, "all")
				medianValue = median(obj.on(), 'all');
			else
				medianValue = reshape(median(obj.on(), 1:obj(1).nargin), size(obj));
			end
		end % mean()
2397
		
2398
2399
2400
2401
2402
2403
2404
		function value = obj2value(obj, myDomain)
			% OBJ2VALUE make the stored data in valueDiscrete available for
			% in the output format
			%	value = obj2value(obj) returns the valueDiscrete in the
			%	form size(value) = [gridLength, objSize]
			%	obj2value(obj, myDomain) returns the valueDiscrete in the
			%	form size(value) = [myDomain.gridLength objSize]
2405
			
2406
2407
			value = reshape(cat(numel(obj(1).domain)+1, obj(:).valueDiscrete), ...
				[obj(1).domain.gridLength(), size(obj)]);
2408
				
2409
			if nargin >= 2 && ~isequal( myDomain, obj(1).domain )
2410
				% if a new domain is specified for the evaluation of
Ferdinand Fischer's avatar
tmp    
Ferdinand Fischer committed
2411
2412
2413
2414
				% the quantity, ...
				if obj.isConstant
					% ... duplicate the constant value on the desired
					% domain
2415
					value = repmat(value(:).', [myDomain.n, ones(1, ndims(obj))]);
Ferdinand Fischer's avatar
tmp    
Ferdinand Fischer committed
2416
2417
				else
					%... do an interpolation based on the old data.
2418
					indexGrid = misc.indexGrid(size(obj));
2419
2420
2421
2422
					tempInterpolant = numeric.interpolant(...
						[obj(1).grid, indexGrid{:}], value);
					tempGrid = {myDomain.grid};
					value = tempInterpolant.evaluate(tempGrid{:}, indexGrid{:});
Ferdinand Fischer's avatar
tmp    
Ferdinand Fischer committed
2423
				end
2424
			end
2425
			
2426
		end % obj2value()
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
		
		function result = diag2vec(obj)
			% This method creates a vector of quantities by selecting the
			% diagonal elements of the quantity array obj.
			assert(ndims(obj) <= 2, 'quantity.diag2vec is only implemented for quantity matrices');
			
			for it = 1:min(size(obj, 1), size(obj, 2))
				result(it, 1) = copy(obj(it, it));
			end
		end
		
		function result = vec2diag(vec)
			% This method creates a diagonal matrix of quantities which
			% carries the elements of vec on its diagonal
			assert(isvector(vec), 'quantity.vec2diag is only implemented for quantity vectors');
2442
2443
2444
2445
2446
2447
			try
				result = zeros(numel(vec)) * (vec(:) * vec(:).');
			catch
				result = 0 * (vec(:) * vec(:).');
			end
			
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
			for it = 1 : numel(vec)
				result(it, it) = copy(vec(it));
			end
		end
		
		
	end% (Access = public)
	
	%%
	methods (Static)
		
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
		function P = ones(valueSize, domain, varargin)
			%ONES initializes an ones-quantity.Discrete object
			%	P = ones(VALUESIZE, DOMAIN) creates a matrix of size
			%	VALUESIZE on the DOMAIN with ones as entries.
			
			myParser = misc.Parser();
			myParser.addParameter('gridName', []);
			myParser.parse(varargin{:});
			
			if ~isa(domain, 'quantity.Domain')
				% if the input parameter DOMAIN is not a quantity.Domain
				% object. It is assumed that it is a grid.
				domain = quantity.Domain.gridCells2domain(...
					domain, myParser.Results.gridName);
			end
			
			if any( valueSize == 0)
				P = quantity.Discrete.empty(valueSize);
			else
				O = ones([domain.gridLength, valueSize(:)']);
2479
				P = quantity.Discrete(O, domain, 'size', valueSize, varargin{:});
2480
2481
2482
			end
		end
		
2483
		function P = zeros(valueSize, domain, varargin)
2484
			%ZEROS initializes an zero quantity.Discrete object
2485
2486
			%	P = zeros(VALUESIZE, DOMAIN) creates a matrix of size
			%	VALUESIZE on the DOMAIN with zero entries.
2487
2488
			
			myParser = misc.Parser();
2489
			myParser.addParameter('gridName', []);
2490
2491
2492
2493
2494
			myParser.parse(varargin{:});
			
			if ~isa(domain, 'quantity.Domain')
				% if the input parameter DOMAIN is not a quantity.Domain
				% object. It is assumed that it is a grid.
2495
2496
				domain = quantity.Domain.gridCells2domain(...
					domain, myParser.Results.gridName);
2497
			end
2498
			
2499
2500
2501
2502
			if any( valueSize == 0)
				P = quantity.Discrete.empty(valueSize);
			else
				O = zeros([domain.gridLength, valueSize(:)']);
2503
				P = quantity.Discrete(O, domain, 'size', valueSize, varargin{:});
2504
			end
2505
2506
2507
2508
2509
		end
		
		function q = value2cell(value, gridSize, valueSize)
			% VALUE2CELL
			
2510
2511
2512
2513
2514
			fullSize = size(value);
			
			myGridSize = num2cell( fullSize(1:length(gridSize)) );
			
			if nargin == 2			
2515
				valueSize = [fullSize(length(myGridSize)+1:length(fullSize)), 1, 1];
2516
			end
2517
2518
2519
2520
			
			myValueSize =  arrayfun(@(n) ones(n, 1), valueSize, 'UniformOutput', false);
			s = [myGridSize(:); myValueSize(:)]; %{gs{:}, vs{:}};%
			q = reshape(mat2cell(value, s{:}), valueSize);
2521
		end % value2cell()
2522
2523
		
	end %% (Static)
2524
	
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
	methods(Access = protected) 
		
		function newObj = changeGrid(obj, gridNew, gridNameNew)
			% CHANGEGRID change the grid of the quantity.
			%	newObj = CHANGEGRID(obj, gridNew, gridNameNew)
			% change the grid of the obj quantity. The order of grid and
			% gridName in the obj properties remains unchanged, only the
			% data points are exchanged.
			%
			%	newObj = CHANGEGRID(obj, domain) changes the domain of the
			%	object specified by the name of DOMAIN into the
			%	corresponding domain from DOMAIN.
			%
			%	example:
			%		q.changeGrid( linspace(0,1)', 't')
			%	will change the grid with the name 't' to the new grid
			%	linspace(0,1)'
			if isempty(obj)
				newObj = obj.copy();
				return;
			end
			
			if isa(gridNew, 'quantity.Domain')
				gridNameNew = [gridNew.name];
				gridNew = {gridNew.grid};
			else
				gridNameNew = misc.ensureString(gridNameNew);	
				gridNew  = misc.ensureIsCell(gridNew);
				for it = 1:numel(gridNew)
					assert( isnumeric( [gridNew{it}] ), "The gridNew parameter must be a cell array of numeric arrays." )
				end
			end
		
			if obj(1).isConstant
				newDomain(1:length( gridNew )) = quantity.Domain();
				for it = 1 : length(gridNew)
					newDomain(it) = ...
						quantity.Domain(gridNameNew(it), gridNew{it});
				end
			else
				gridIndexNew = obj(1).domain.gridIndex(gridNameNew);
				% initialization of the newDomain array as quantity.Domain
				% array. This is required in order to handle also
				% quantity.EquidistantDomains:
				newDomain(1:obj(1).nargin) = quantity.Domain();
				newDomain(:) = obj(1).domain;

				for it = 1 : length(gridIndexNew)
					newDomain(gridIndexNew(it)) = ...
						quantity.Domain(gridNameNew(it), gridNew{it});
				end
				assert(isequal([newDomain.name], obj(1).gridName), ...
					'rearranging grids failed');
			end
			
			newObj = obj.copy();
			[newObj.domain] = deal(newDomain);
			for it = 1 : numel(obj)
				newObj(it).valueDiscrete = obj(it).on(newDomain);
			end
		end % changeGrid()	
2586
		
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
		function [valDiscrete] = expandValueDiscrete(obj, newDomain)
			% EXPANDVALUEDISCRETE expand the discrete value on the
			% newDomain
			%	[valDiscrete] = ...
			%       expandValueDiscrete(obj, newDomain) expands the
			%       discrete values on a new domain. So that a function
			%			f(z,t) = f(z) + f(t)
			%	can be computed.
		
			gridJoinedLength = newDomain.gridLength;
			
			% get the index of obj.grid in the joined grid
2599
			[idx, logicalIdx] = newDomain.gridIndex([obj(1).domain.name]);
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
			% evaluate the 
			valDiscrete = obj.on( newDomain(logicalIdx) );
			oldDim = ndims(valDiscrete);
			valDiscrete = permute(valDiscrete, [(1:sum(~logicalIdx)) + oldDim, 1:oldDim] );
			valDiscrete = repmat(valDiscrete, [gridJoinedLength(~logicalIdx), ones(1, ndims(valDiscrete))]);
			%
			valDiscrete = reshape(valDiscrete, ...
				[gridJoinedLength(~logicalIdx), gridJoinedLength(logicalIdx), size(obj)]);
			
			% permute valDiscrete such that grids are in the order specified
			% by gridNameJoined.
			gridIndex = 1:numel(logicalIdx);
			gridOrder = [gridIndex(~logicalIdx), gridIndex(logicalIdx)];
			gridIndex(gridOrder) = 1:numel(logicalIdx);
			
			valDiscrete = permute(valDiscrete, [gridIndex, numel(logicalIdx)+(1:ndims(obj))]);
		end
		
2618
		function result = diff_inner(obj, k, diffGridName)
2619
			
2620
			gridIndex = obj(1).domain.gridIndex(diffGridName);
2621
2622
2623
2624

			permutationVector = 1 : (numel(obj(1).grid)+ndims(obj));

			objDiscrete = permute(obj.on(), ...
2625
2626
2627
				[permutationVector(gridIndex), ...
				permutationVector(permutationVector ~= gridIndex)]);
			[spacing, idx] = getSpacing(obj);
2628

2629
2630
2631
2632
			if iscolumn(objDiscrete)
				derivativeDiscrete = gradient(objDiscrete, ...
					spacing{idx == gridIndex}, ...
					spacing{idx ~= gridIndex});
2633
			else
2634
2635
2636
				spacing = [spacing(idx == gridIndex); spacing(idx ~= gridIndex)];
				[~, derivativeDiscrete] = gradient(objDiscrete, ...
					spacing{2}, spacing{idx~=2});
2637
2638
			end

2639
2640
			rePermutationVector = [2:(gridIndex), ...
				1, (gridIndex+1):ndims(derivativeDiscrete)];
2641
			result = quantity.Discrete(...
2642
2643
				permute(derivativeDiscrete, rePermutationVector), obj(1).domain, ...
				'size', size(obj), 'name', "(d_{" + diffGridName + "}" + obj(1).name + ")");
2644
2645

			if k > 1
2646
2647
2648
				% if a higher order derivative is requested, call the function
				% recursivly until the first-order derivative is reached
				result = result.diff(diffGridName, k-1);
2649
			end
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
		end % diff_inner()
		
			
		function [mySpace, idx] = getSpacing(obj)
			% getSpacing returns a cell array of the spacing needed for gradient(),
			% in diff_inner including a spacing for the array dimensions of obj.
			mySpace = cell(numel(obj(1).domain), 1);
			for it = 1 : numel(obj(1).domain)
				mySpace{it, 1} = obj(1).domain(it).grid;
			end
			if isscalar(obj)
				% do nothing
			elseif iscolumn(obj)
				mySpace{it+1, 1} = (1 : 1 : numel(obj)).';
			else % ismatrix(obj)
				for jt = 1 : ndims(obj)
					mySpace{it+jt, 1} = (1 : 1 : size(obj, jt)).';
				end	
			end
			idx = (1 : 1 : numel(mySpace)).';
		end % getSpacing(obj)
2671
		
2672
2673
2674
		function [idx, permuteGrid] = computePermutationVectors(a, b)
			% Computes the required permutation vectors to use
			% misc.multArray for multiplication of matrices
2675
			
2676
2677
2678
2679
2680
			% #todo this assertion should not be required!
% 			uA = unique(a(1).gridName, 'stable');
% 			assert(numel(uA) == numel(a(1).gridName), 'Gridnames have to be unique!');
% 			uB = unique(b(1).gridName, 'stable');
% 			assert(numel(uB) == numel(b(1).gridName), 'Gridnames have to be unique!');
2681
2682
			
			% 1) find common entries
2683
2684
2685
2686
2687
			if isempty( b(1).gridName ) || isempty( a(1).gridName )
				common = [];
			else
				common = intersect(a(1).gridName, b(1).gridName);
			end
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
			
			commonA = false(1, a.nargin());
			commonB = false(1, b.nargin());
			
			idxA0 = 1:a.nargin();
			idxB0 = 1:b.nargin();
			
			
			gridA = zeros(1, a.nargin());
			gridB = zeros(1, b.nargin());
			
			for k = 1:numel(common)
				c = common{k};
				
				% 2) find logical indices for the common entries
				cA = strcmp(c, a(1).gridName);
				commonA = commonA | cA;
				
				% 3) exchange the order of the logical indices
				gridA(k) = idxA0(cA);
				
				cB = strcmp(c, b(1).gridName);
				commonB = commonB | cB;
				
				gridB(k) = idxB0(cB);
			end
			
			gridA(numel(common)+1:end) = idxA0(~commonA);
			gridB(numel(common)+1:end) = idxB0(~commonB);
			
			valueA = a.nargin + (1:numel(size(a)));
			valueB = b.nargin + (1:numel(size(b)));
			
			idx.A.permute = [gridA, valueA];
			idx.A.grid = gridA;
			idx.A.value = valueA;
			idx.A.common = commonA;
			
			idx.B.permute = [gridB, valueB];
			idx.B.grid = gridB;
			idx.B.value = valueB;
			idx.B.common = commonB;
			
			idx.common = 1:numel(common);
			
			% permutation for the new grid
			nGridA = a.nargin();				% number of the grid dimensions of quantity a
			nValueA = ndims(a) - 1;				% number of the value dimensions of a minus the multiplied dimension
			nGridB = b.nargin() - numel(common);% number of the grid dimensions of quantity b minus the common dimensions that are part of nGridA
			nValueB = ndims(b) - 1;				% number of the value dimensions minus the multiplied dimensions
			
			idxGrid = 1:(nGridA + nGridB + nValueA + nValueB);
			
			lGridA = [true(1, nGridA), false(1, nValueA), false(1, nGridB), false(1, nValueB)];
			lGridVA = [false(1, nGridA), true(1, nValueA), false(1, nGridB), false(1, nValueB)];
			lGridB = [false(1, nGridA), false(1, nValueA), true(1, nGridB), false(1, nValueB)];
			lGridVB = [false(1, nGridA), false(1, nValueA), false(1, nGridB), true(1, nValueB)];
			
			% Creates the permutation vector to bring the result of
			% misc.multArray into the required form for the
			% quantity.Discrete class.
			permuteGrid = [idxGrid(lGridA), idxGrid(lGridB), idxGrid(lGridVA), idxGrid(lGridVB)];
		end
2751
		
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
		
		function f = setValueContinuous(obj, f)
		end
		function f = getValueContinuous(obj, f)
		end
		
		% Override copyElement method:
		function cpObj = copyElement(obj)
			% Make a shallow copy of all properties
			cpObj = copyElement@matlab.mixin.Copyable(obj);
2762
2763
			% #TODO insert code here if some properties should not be
			% copied.
2764
		end
Jakob Gabriel's avatar
Jakob Gabriel committed
2765
		
2766
		function s = getPropertyGroups(obj)
2767
2768
2769
2770
2771
2772
			% Function to display the correct values
			
			if isempty(obj)
				s = getPropertyGroups@matlab.mixin.CustomDisplay(obj);
				return;
			else
2773
				s = getPropertyGroups@matlab.mixin.CustomDisplay(obj(1));
2774
			end
2775
2776
2777
			
			if numel(obj) ~= 1
				s.PropertyList.valueDiscrete = ...
2778
					[sprintf('%ix', obj(1).domain.gridLength, size(obj)) sprintf('\b')];
2779
			end
Ferdinand Fischer's avatar
tmp    
Ferdinand Fischer committed
2780
		end % getPropertyGroups
2781
		
Jakob Gabriel's avatar
Jakob Gabriel committed
2782
	end % methods (Access = protected)
2783
	
2784
end % classdef
For faster browsing, not all history is shown. View entire blame