Commit 0b4c33a7 authored by Ferdinand Fischer's avatar Ferdinand Fischer
Browse files

Renamed quantity.Domain/gridIndex to index and quantity.Discrete|Symbolic/flipGrid -> flipDomain

parent 21a82ce5
......@@ -81,7 +81,7 @@ end
function mTimesPointWise(A, Vi)
z = Vi(1).grid{Vi.gridIndex('z')};
z = Vi(1).grid{Vi.index('z')};
c = zeros(size(A,1), size(Vi, 2), length(z));
......
......@@ -46,7 +46,7 @@ if isa(myOptions, "quantity.Discrete")
myOptions = [];
end
if isempty(myOptions)
if any(rope{1}(1).domain.gridIndex("t"))
if any(rope{1}(1).domain.index("t"))
myOptions = plotRopeOptionParser(rope{1}, "t", rope{1}(1).domain.find("t"));
else
myOptions = plotRopeOptionParser(rope{1});
......
......@@ -72,7 +72,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
% quantity.Discrete([]), which is useful for creating default
% values.
obj = quantity.Discrete.empty(size(valueOriginal));
end
end
elseif nargin > 1
%% input parser
......@@ -90,6 +90,11 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
valueOriginal = quantity.Discrete.value2cell(valueOriginal, gridLength);
end
if isempty( valueOriginal )
obj = quantity.Discrete.empty( size(valueOriginal) );
return
end
% Check if the grid fits to the values. In addition, catch
% the case if all values are empty. This is required for
% the initialization of quantity.Function and
......@@ -314,7 +319,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
end
assert( length( myCompositionDomain ) == 1 );
[idx, logOfDomain] = originalDomain.gridIndex(myCompositionDomain);
[idx, logOfDomain] = originalDomain.index(myCompositionDomain);
assert( isequal( originalDomain(idx), myCompositionDomain ), ...
'Composition of functions: The domains for the composition must be equal. A grid join is not implemented yet.');
assert( any( logOfDomain ) )
......@@ -382,7 +387,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
if ~isempty(intersectDomain)
idx = tmpDomain.gridIndex( intersectDomain );
idx = tmpDomain.index( intersectDomain );
% take the diagonal values of the common domain, i.e., z = zeta
% use the diag_nd function because it seems to be faster
......@@ -940,7 +945,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
assert(isscalar(obj), "invert is only implemented for scalars");
assert(numel(obj.nargin) == 1, "invert is only implemented for quantities on 1 domain");
inverse = quantity.Discrete(...
repmat(obj(1).grid{obj(1).domain.gridIndex(domainName)}(:), [1, size(obj)]), ...
repmat(obj(1).grid{obj(1).domain.index(domainName)}(:), [1, size(obj)]), ...
quantity.Domain([obj(1).name], obj.on()), ...
'name', domainName);
end % invert()
......@@ -1117,8 +1122,8 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
end
end % subsDomain()
function [idx, logicalIdx] = gridIndex(obj, varargin)
warning('DEPRICATED: use quantity.Domain.gridIndex method instead')
function [idx, logicalIdx] = index(obj, varargin)
warning('DEPRICATED: use quantity.Domain.index method instead')
[~, idx, logicalIdx] = obj(1).domain.find(varargin{:});
end
......@@ -1382,22 +1387,26 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
s = struct(obj);
end
function b = flipGrid(a, myGridName)
% flipGrid() implements a flip of the grids, for example with a(z)
% b(z) = a(1-z) = a.flipGrid('z');
function b = flipDomain(obj, myDomainName)
% flipDomain() implements a flip of the grids, for example with a(z)
% b(z) = a(1-z) = a.flipDomain('z');
% or for the multidimensional case with c(z, zeta)
% d(z, zeta) = c(1-z, 1-zeta) = c.flipGrid({'z', 'zeta'});
bMat = a.on();
if ~iscell(myGridName)
myGridName = {myGridName};
% d(z, zeta) = c(1-z, 1-zeta) = c.flipDomain(["z", "zeta"];
arguments
obj
myDomainName string
end
gridIdx = a(1).domain.gridIndex(myGridName);
for it = 1 : numel(myGridName)
bMat = flip(bMat, gridIdx(it));
bMat = obj.on();
idx = obj(1).domain.index(myDomainName);
for it = 1 : numel(myDomainName)
bMat = flip(bMat, idx(it));
end
b = quantity.Discrete(bMat, a(1).domain, ...
'name', "flip(" + a(1).name + ")");
end % flipGrid()
b = quantity.Discrete(bMat, obj(1).domain, ...
'name', "flip(" + obj(1).name + ")");
end % flipDomain()
function newObj = changeDomain(obj, domain)
% CHANGEDOMAIN change the grid of the quantity.
......@@ -1417,15 +1426,15 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
newDomain = domain;
else
% find the domains to be replaced:
gridIndexNew = obj(1).domain.gridIndex([domain.name]);
indexNew = obj(1).domain.index([domain.name]);
% 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)) = domain(it);
for it = 1 : length( indexNew )
newDomain(indexNew(it)) = domain(it);
end
assert(isequal([newDomain.name], obj(1).gridName), ...
'rearranging grids failed');
......@@ -1467,15 +1476,15 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
if obj(1).isNumber()
error("Not yet implemented")
else
gridIndexNew = obj(1).domain.gridIndex(optArgs.gridName);
indexNew = obj(1).domain.index(optArgs.gridName);
% 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)) = ...
for it = 1 : length(indexNew)
newDomain(indexNew(it)) = ...
quantity.Domain(myNewDomain(it).name, myNewDomain(it).grid);
end
end
......@@ -1773,8 +1782,8 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
% if there is a component of the original domain in the
% joined domain, it can happen the length of a domain has
% changed. Thus, it must be updated for a later evaluation.
idxA = domainA.gridIndex(joinedDomain(i).name);
idxB = domainB.gridIndex(joinedDomain(i).name);
idxA = domainA.index(joinedDomain(i).name);
idxB = domainB.index(joinedDomain(i).name);
if idxA
newDomainA = [newDomainA, joinedDomain(i)]; %#ok<AGROW>
......@@ -2150,7 +2159,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
function myGrid = gridOf(obj, myGridName)
if ~iscell(myGridName)
gridIdx = obj(1).domain.gridIndex(myGridName);
gridIdx = obj(1).domain.index(myGridName);
if gridIdx > 0
myGrid = obj(1).grid{gridIdx};
else
......@@ -2159,7 +2168,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
else
myGrid = cell(size(myGridName));
for it = 1 : numel(myGrid)
gridIdx = obj(1).domain.gridIndex(myGridName{it});
gridIdx = obj(1).domain.index(myGridName{it});
if gridIdx > 0
myGrid{it} = obj(1).grid{gridIdx};
else
......@@ -2275,7 +2284,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
% get grid
myGrid = obj(1).grid;
intGridIdx = obj(1).domain.gridIndex(domainName);
intGridIdx = obj(1).domain.index(domainName);
% integrate
F = numeric.cumtrapz_fast_nDim(myGrid{intGridIdx}, ...
......@@ -2750,8 +2759,8 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
% This is a helper function for subsDomain, hence it is protected.
% find domain to be merged
[idxOldDomain, idxOldDomainLogical] = gridIndex(obj(1).domain, oldDomainName);
[idxNewDomain, idxNewDomainLogical] = gridIndex(obj(1).domain, newDomain.name);
[idxOldDomain, idxOldDomainLogical] = index(obj(1).domain, oldDomainName);
[idxNewDomain, idxNewDomainLogical] = index(obj(1).domain, newDomain.name);
% pick finest grid.
newDomain = join(join(newDomain, obj(1).domain(idxNewDomain)), ...
......@@ -2797,7 +2806,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
gridJoinedLength = newDomain.gridLength;
% get the index of obj.grid in the joined grid
[~, logicalIdx] = newDomain.gridIndex([obj(1).domain.name]);
[~, logicalIdx] = newDomain.index([obj(1).domain.name]);
% evaluate the
valDiscrete = obj.on( newDomain(logicalIdx) );
oldDim = ndims(valDiscrete);
......@@ -2809,36 +2818,36 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
% 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);
index = 1:numel(logicalIdx);
gridOrder = [index(~logicalIdx), index(logicalIdx)];
index(gridOrder) = 1:numel(logicalIdx);
valDiscrete = permute(valDiscrete, [gridIndex, numel(logicalIdx)+(1:ndims(obj))]);
valDiscrete = permute(valDiscrete, [index, numel(logicalIdx)+(1:ndims(obj))]);
end % expandValueDiscrete()
function result = diff_inner(obj, k, diffGridName)
gridIndex = obj(1).domain.gridIndex(diffGridName);
index = obj(1).domain.index(diffGridName);
permutationVector = 1 : (numel(obj(1).grid)+ndims(obj));
objDiscrete = permute(obj.on(), ...
[permutationVector(gridIndex), ...
permutationVector(permutationVector ~= gridIndex)]);
[permutationVector(index), ...
permutationVector(permutationVector ~= index)]);
[spacing, idx] = getSpacing(obj);
if iscolumn(objDiscrete)
derivativeDiscrete = gradient(objDiscrete, ...
spacing{idx == gridIndex}, ...
spacing{idx ~= gridIndex});
spacing{idx == index}, ...
spacing{idx ~= index});
else
spacing = [spacing(idx == gridIndex); spacing(idx ~= gridIndex)];
spacing = [spacing(idx == index); spacing(idx ~= index)];
[~, derivativeDiscrete] = gradient(objDiscrete, ...
spacing{2}, spacing{idx~=2});
end
rePermutationVector = [2:(gridIndex), ...
1, (gridIndex+1):ndims(derivativeDiscrete)];
rePermutationVector = [2:(index), ...
1, (index+1):ndims(derivativeDiscrete)];
result = quantity.Discrete(...
permute(derivativeDiscrete, rePermutationVector), obj(1).domain, ...
"name", "(d_{" + diffGridName + "}" + obj(1).name + ")");
......
......@@ -185,7 +185,7 @@ classdef (InferiorClasses = {?quantity.EquidistantDomain}) Domain < ...
"number of new names must be equal to number of old names.");
d = copy(obj);
for it = 1 : numel(newName)
d(obj.gridIndex(oldName(it))).name = newName{it};
d(obj.index(oldName(it))).name = newName{it};
end % for it = 1 : numel(newName)
assert(misc.isunique([d.name]), "the resulting domain must contain unique names");
end % rename
......@@ -229,7 +229,7 @@ classdef (InferiorClasses = {?quantity.EquidistantDomain}) Domain < ...
searchName string;
end
idx = obj.gridIndex( searchName );
idx = obj.index( searchName );
if idx == 0
d = [];
......@@ -245,14 +245,14 @@ classdef (InferiorClasses = {?quantity.EquidistantDomain}) Domain < ...
% quantity.Domain array obj. toBeRemoved may be a string or a domain or a array of
% those.
[~, logicalIdxToBeRemoved] = obj.gridIndex(toBeRemoved);
[~, logicalIdxToBeRemoved] = obj.index(toBeRemoved);
rest = obj(~logicalIdxToBeRemoved);
end % remove()
function [idx, log] = gridIndex(obj, searchName, position)
function [idx, log] = index( obj, searchName, position)
%% GRIDINDEX returns the index of the grid
% [idx, log] = gridIndex(obj, names) searches in the name
% [idx, log] = index(obj, names) searches in the name
% properties of obj for the "names" and returns its index as
% "idx" and its logical index as "log"
arguments
......@@ -289,8 +289,12 @@ classdef (InferiorClasses = {?quantity.EquidistantDomain}) Domain < ...
if position == "first"
idx = idx(1);
end
end % gridIndex
end
function [idx, log] = gridIndex(obj, varargin)
warning("DEPRICATED: Use quantity.Domain/index instead")
[idx, log] = obj.index(varargin{:});
end % index
function l = ne(obj, obj2)
% ~= Not equal.
......@@ -326,8 +330,8 @@ classdef (InferiorClasses = {?quantity.EquidistantDomain}) Domain < ...
% both
for i = 1 : numel(joinedGrid)
currentGridName = joinedGrid(i);
[index1, logicalIndex1] = domain1.gridIndex(currentGridName, "first");
[index2, logicalIndex2] = domain2.gridIndex(currentGridName, "first");
[index1, logicalIndex1] = domain1.index(currentGridName, "first");
[index2, logicalIndex2] = domain2.index(currentGridName, "first");
% Check if a domain is in both domains:
% -> then take the finer one of both
......@@ -402,7 +406,7 @@ classdef (InferiorClasses = {?quantity.EquidistantDomain}) Domain < ...
error('the input parameter order must be a array of quantity.Domain objects or a string-array')
end
idx = cellfun(@(v) obj.gridIndex(v), names); % #todo@domainNameString
idx = cellfun(@(v) obj.index(v), names); % #todo@domainNameString
if isa(order, 'quantity.Domain')
newDomain = order;
......
......@@ -34,7 +34,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Piecewise < quantity.Discrete
'The quantities for the piecewise combination must have the same domain names' );
assert(length(quantities) >= 2, 'Only one quantity is given for the piecewise combination. At least 2 are required.');
assert( quantities{1}(1).domain.gridIndex(domainToJoin) == 1, ...
assert( quantities{1}(1).domain.index(domainToJoin) == 1, ...
'The domain to join must be the first domain!');
joinedGrid = quantities{1}(1).domain.find(domainToJoin.name).grid;
......
......@@ -402,19 +402,19 @@ classdef Symbolic < quantity.Function
'name', "sqrtm(" + x(1).name + ")");
end % sqrtm()
function b = flipGrid(a, myGridName)
if ~iscell(myGridName)
myGridName = {myGridName};
end
variableOld = cell(numel(myGridName), 1);
variableNew = cell(numel(myGridName), 1);
for it = 1 : numel(myGridName)
variableOld{it} = sym(myGridName{it});
variableNew{it} = 1-variableOld{it};
function b = flipDomain(obj, flipDomainName, optArgs)
arguments
obj
flipDomainName string;
optArgs.upper = 1;
end
b = quantity.Symbolic(subs(a.sym, variableOld, variableNew), a(1).domain, ...
'name', "flip(" + a(1).name + ")");
end % flipGrid()
flippedDomainName = optArgs.upper - sym( flipDomainName );
b = quantity.Symbolic(subs(obj.sym, num2cell( flipDomainName ), ...
num2cell( flippedDomainName ) ), obj(1).domain, ...
'name', "flip(" + obj(1).name + ")");
end % flipDomain()
function thisVariable = gridName2variable(obj, thisGridName)
% this method returns the variable thisVariable stored in obj(1).variable
......@@ -703,8 +703,8 @@ classdef Symbolic < quantity.Function
% This is a helper function for subsDomain, hence it is protected.
% find domain to be merged
[idxOldDomain, idxOldDomainLogical] = gridIndex(obj(1).domain, oldDomainName);
[idxNewDomain, idxNewDomainLogical] = gridIndex(obj(1).domain, newDomain.name);
[idxOldDomain, idxOldDomainLogical] = index(obj(1).domain, oldDomainName);
[idxNewDomain, idxNewDomainLogical] = index(obj(1).domain, newDomain.name);
% pick finest grid.
newDomain = join(join(newDomain, obj(1).domain(idxNewDomain)), ...
......
......@@ -50,7 +50,7 @@ c = a*b;
c.plot();
%% flip grid, i.e. d(z) = a(1-z)
d = a.flipGrid('z');
d = a.flipDomain('z');
[d.name] = deal('d');
plot([a; d]);
......
......@@ -76,6 +76,8 @@ classdef (InferiorClasses = {?quantity.Discrete, ?quantity.Function, ?quantity.S
elseif isnumeric(A)
obj(1).coefficient = quantity.Discrete(A, prsr.Results.domain);
A = {A};
else
error("Not yet implemented for objects of type " + class(A) + ".")
end
if isempty( obj(1).coefficient )
......@@ -144,25 +146,23 @@ classdef (InferiorClasses = {?quantity.Discrete, ?quantity.Function, ?quantity.S
I = signals.PolynomialOperator(II);
end
function value = applyTo(obj, y, varargin)
function value = applyTo(obj, y, optArgs)
% APPLYON evaluation of the differential parameterization
% VALUE = applyOn(OBJ, Y, varargin) applies the operator OBJ
% on the function Y, i.e.,
% value = A0(t) y(t) + A1(t) dt y(t) + A2(t) dz^2 y(t)...
% is evaluated.
myParser = misc.Parser();
myParser.addParameter('domain', y(1).domain);
myParser.addParameter('n', length(obj));
myParser.parse(varargin{:});
myDomain = myParser.Results.domain;
n = myParser.Results.n;
arguments
obj
y signals.BasicVariable
optArgs.domain (1,1) quantity.Domain = y(1).domain;
optArgs.n (1,1) double = min( length(obj), y.highestDerivative );
end
value = zeros(size(obj(1).coefficient));
for k = 1:n
value = value + obj(k).coefficient * y.diff(myDomain.name, k - 1);
for k = 1:optArgs.n
value = value + obj(k).coefficient * y.diff(optArgs.domain, k - 1);
end
end
......
......@@ -590,14 +590,14 @@ f = quantity.Discrete(quantity.Symbolic([1+z+zeta; 2*zeta+sin(z)] + zeros(2, 1)*
% % flip one grid
fReference = quantity.Discrete(quantity.Symbolic([1+z+(1-zeta); 2*(1-zeta)+sin(z)] + zeros(2, 1)*z*zeta, ...
[Z, Zeta]));
fFlipped = f.flipGrid('zeta');
fFlipped = f.flipDomain('zeta');
tc.verifyEqual(fReference.on(), fFlipped.on(), "AbsTol", 10*eps);
% flip both grids
fReference2 = quantity.Discrete(quantity.Symbolic([1+(1-z)+(1-zeta); 2*(1-zeta)+sin(1-z)] + zeros(2, 1)*z*zeta, ...
[Z, Zeta]));
fFlipped2 = f.flipGrid({'z', 'zeta'});
fFlipped3 = f.flipGrid({'zeta', 'z'});
fFlipped2 = f.flipDomain(["z", "zeta"]);
fFlipped3 = f.flipDomain({'zeta', 'z'});
tc.verifyEqual(fReference2.on(), fFlipped2.on(), "AbsTol", 10*eps);
tc.verifyEqual(fReference2.on(), fFlipped3.on(), "AbsTol", 10*eps);
end % testFlipGrid();
......
......@@ -293,18 +293,18 @@ T = quantity.Domain('t', t);
a = [Z, T, S];
idx = a.gridIndex('z');
idx = a.index('z');
tc.verifyEqual(idx, 1);
idx = a.gridIndex({'z', 't'});
idx = a.index({'z', 't'});
tc.verifyEqual(idx, [1 2]);
idx = a.gridIndex({'z', 's'});
idx = a.index({'z', 's'});
tc.verifyEqual(idx, [1 3]);
idx = a.gridIndex('t');
idx = a.index('t');
tc.verifyEqual(idx, 2);
end
......
......@@ -366,13 +366,13 @@ myDomain(2) = quantity.Domain("zeta", myGrid);
f = quantity.Symbolic([1+z+zeta; 2*zeta+sin(z)] + zeros(2, 1)*z*zeta, myDomain);
% flip zeta
fReference = quantity.Symbolic([1+z+(1-zeta); 2*(1-zeta)+sin(z)] + zeros(2, 1)*z*zeta, myDomain);
fFlipped = f.flipGrid("zeta");
fFlipped = f.flipDomain("zeta");
tc.verifyEqual(fReference.on, fFlipped.on, "AbsTol", 10*eps);
% flip z and zeta
fReference2 = quantity.Symbolic([1+(1-z)+(1-zeta); 2*(1-zeta)+sin(1-z)] + zeros(2, 1)*z*zeta, ...
myDomain);
fFlipped2 = f.flipGrid({'z', 'zeta'});
fFlipped2 = f.flipDomain(["z", "zeta"]);
tc.verifyEqual(fReference2.on, fFlipped2.on, "AbsTol", 10*eps);
end % testFlipGrid();
......@@ -588,7 +588,7 @@ testCase.verifyEqual(3+0*gridZ(:)*gridZeta, AOnPure(:,:,2,3));
%%
gridZ2 = linspace(0, 1, 15);
gridZeta2 = linspace(0, 1, 31);
AOnChangedGrid = A.on({gridZ2, gridZeta2}, {'z', 'zeta'});
AOnChangedGrid = A.on({gridZ2, gridZeta2}, ["z", "zeta"]);
testCase.verifyEqual(2+gridZ2(:)*ones(1, numel(gridZeta2)), AOnChangedGrid(:,:,1,1));
testCase.verifyEqual(2+gridZ2(:)*gridZeta2, AOnChangedGrid(:,:,2,2));
testCase.verifyEqual(3+0*gridZ2(:)*gridZeta2, AOnChangedGrid(:,:,2,3));
......@@ -601,7 +601,7 @@ testCase.verifyEqual((3+0*gridZ2(:)*gridZeta2).', AOnChangedGrid2(:,:,2,3));
% testCase.verifyEqual(ones([21, 31, 2, 3]), ...
% A.on({linspace(0, 1, 21), linspace(0, 1, 31)}));
% testCase.verifyEqual(ones([21, 31, 2, 3]), ...
% A.on({linspace(0, 1, 21), linspace(0, 1, 31)}, {'z', 'zeta'}));
% A.on({linspace(0, 1, 21), linspace(0, 1, 31)}, ["z", "zeta"]));
% testCase.verifyEqual(ones([21, 31, 2, 3]), ...
% A.on({linspace(0, 1, 21), linspace(0, 1, 31)}, {'zeta', 'z'}));
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment