Commit c6caf266 authored by Jakob Gabriel's avatar Jakob Gabriel Committed by Ferdinand Fischer
Browse files

quantity.Domain:

- new method remove()
- rename: renamed "copyAndRename"-method into "rename" and rewrote it
parent 6d129d46
......@@ -161,15 +161,29 @@ classdef (InferiorClasses = {?quantity.EquidistantDomain}) Domain < ...
end
end
function d = copyAndRename(obj, newName)
d = obj.copy();
function d = rename(obj, newName, oldName)
% RENAME renames the name of the domain OBJ.
%
% d = rename(obj, newName) replaces all names of obj with the names specified by the
% string array newName
%
% d = rename(obj, newName, oldName) replaces the names of obj specified by the string
% array oldName with the corresponding elements of the string array newName
newName = misc.ensureIsCell(newName);
assert(length(newName) == length(d), 'For the renaming of a copy of a domain, the numebr of new names must be equal the number of the domains');
for k = 1:length(newName)
d(k).name = newName{k};
end
end
arguments
obj quantity.Domain;
newName string;
oldName string = [obj.name];
end % arguments
assert(numel(newName) == numel(oldName), ...
"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};
end % for it = 1 : numel(newName)
assert(misc.isunique([d.name]), "the resulting domain must contain unique names");
end % rename
function nd = ndims(obj)
%NDIMS number of dimensions of the domain specified by the
......@@ -212,6 +226,19 @@ classdef (InferiorClasses = {?quantity.EquidistantDomain}) Domain < ...
d = obj( obj.gridIndex( searchName ) );
end
function rest = remove(obj, toBeRemoved)
% REMOVE removes the domain specified by toBeRemoved from the quantity.Domain array obj.
%
% rest = remove(obj, toBeRemoved) removes the domain specified by toBeRemoved from the
% quantity.Domain array obj. toBeRemoved may be a string or a domain or a array of
% those.
[~, logicalIdxToBeRemoved] = obj.gridIndex(toBeRemoved);
rest = obj(~logicalIdxToBeRemoved);
end % remove()
function [idx, log] = gridIndex(obj, searchName, position)
%% GRIDINDEX returns the index of the grid
% [idx, log] = gridIndex(obj, names) searches in the name
......
......@@ -18,19 +18,25 @@ tc.TestData.d = d;
end
function testDiscrete(tc)
tc.verifyEqual(tc.TestData.a.grid, on(tc.TestData.a.Discrete()));
tc.verifyClass(tc.TestData.a.Discrete, "quantity.Discrete");
end
function testRename(tc)
d = [quantity.Domain("a", linspace(0, 1, 3)), ...
quantity.Domain("b", linspace(0, 2, 5)), ...
quantity.Domain("ab", linspace(0, pi, 4))];
xyz = d.rename(["x", "y", "z"]);
xyz2 = d.rename(["z", "y", "x"], ["ab", "b", "a"]);
tc.verifyEqual([xyz.name], ["x", "y", "z"]);
tc.verifyEqual([xyz.name], [xyz2.name]);
function testSymbolic(tc)
tc.verifyEqual(tc.TestData.a.grid, on(tc.TestData.a.Symbolic()));
tc.verifyClass(tc.TestData.a.Symbolic, "quantity.Symbolic");
end
function testParser(tc)
cba = d.rename(["c", "b", "a"], ["a", "b", "ab"]);
tc.verifyEqual([cba.name], ["c", "b", "a"]);
tc.verifyEqual([cba.n], [d.n]);
end % testRename()
%profile on
function testParser(tc)%profile on
d = quantity.Domain.parser('blub', 1, 'grid', 1:3, 'gridName', 'test');
a = quantity.Domain.parser('domain', d);
b = quantity.Domain.parser('blabla', 0, 'domain', a);
......@@ -80,7 +86,6 @@ A = [quantity.Domain('a1', 0:10), quantity.Domain('a2', 0:10)];
tc.verifyTrue( A.contains(5) )
end
function testIsequidistant(tc)
......@@ -108,6 +113,15 @@ tc.verifyEqual( d.find([a.name b.name]), [a b])
end
function testRemove(tc)
d = tc.TestData.d;
dWithoutA = d.remove("a");
tc.verifyEqual([dWithoutA.name], ["b" "c"]);
a = d.remove(dWithoutA);
tc.verifyEqual(a.name, "a");
end
function testGet(tc)
z = linspace(0, pi, 3);
......
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