Commit 4e3c4cdc authored by Jakob Gabriel's avatar Jakob Gabriel
Browse files

quantity.Domain: isequidistant()

parent 53eb9f74
......@@ -307,7 +307,28 @@ classdef (InferiorClasses = {?quantity.EquidistantDomain}) Domain < ...
else
newObj = obj;
end
end% sort()
end% sort()
function result = isequidistant(obj, AbsTol)
% ISEQUIDISTANT(obj) returns true, if the grid is homogenious, i.e. the
% grid points are equidistant and false else. Also works with
% quantity.Domain arrays.
% ISEQUIDISTANT(obj, AbsTol) allows perturbations in the spacing of
% absolute maximum defined by AbsTol. Default: 1e3*eps.
arguments
obj quantity.Domain;
AbsTol = 1e3*eps;
end % arguments
result = true(size(obj));
for it = 1 : numel(result)
spacing = diff(obj(it).grid);
if any(diff(obj(it).grid) - mean(spacing) > AbsTol)
result = false;
end
end % for it = 1 : numel(result)
end % ishomogenious
end % methods
methods (Access = protected)
......
......@@ -2,7 +2,7 @@ function [tests] = testDomain()
tests = functiontests(localfunctions);
end
function setupOnce(testCase)
function setupOnce(tc)
z = linspace(0, pi, 3);
a = quantity.Domain('a', z);
......@@ -10,32 +10,41 @@ b = quantity.Domain('b', z);
c = quantity.Domain('c', z);
d = [a b c];
testCase.TestData.z = z;
testCase.TestData.a = a;
testCase.TestData.b = b;
testCase.TestData.c = c;
testCase.TestData.d = d;
tc.TestData.z = z;
tc.TestData.a = a;
tc.TestData.b = b;
tc.TestData.c = c;
tc.TestData.d = d;
end
function testFind(testCase)
z = testCase.TestData.z;
a = testCase.TestData.a;
b = testCase.TestData.b;
c = testCase.TestData.c;
d = testCase.TestData.d;
function testIsequidistant(tc)
tc.verifyTrue(tc.TestData.a.isequidistant);
tc.verifyTrue(all(isequidistant(tc.TestData.d)));
tc.verifyFalse(isequidistant(quantity.Domain("z", [0, 0.2, 0.4, 0.8, 1.6])));
tc.verifyFalse(all(isequidistant(...
[tc.TestData.a, quantity.Domain("z", [0, 0.2, 0.4, 0.8, 1.6]), tc.TestData.b])));
end % testIsequidistant
testCase.verifyEqual( d.find('a'), a)
testCase.verifyEqual( d.find('b', 'c'), [b c])
testCase.verifyEqual( d.find({'b', 'c'}), [b c])
testCase.verifyEqual( d.find(b.name), b);
testCase.verifyEqual( d.find({a.name c.name}), [a c])
testCase.verifyEqual( d.find([a.name b.name]), [a b])
function testFind(tc)
z = tc.TestData.z;
a = tc.TestData.a;
b = tc.TestData.b;
c = tc.TestData.c;
d = tc.TestData.d;
tc.verifyEqual( d.find('a'), a)
tc.verifyEqual( d.find('b', 'c'), [b c])
tc.verifyEqual( d.find({'b', 'c'}), [b c])
tc.verifyEqual( d.find(b.name), b);
tc.verifyEqual( d.find({a.name c.name}), [a c])
tc.verifyEqual( d.find([a.name b.name]), [a b])
end
function testGet(testCase)
function testGet(tc)
z = linspace(0, pi, 3);
a = quantity.Domain('a', z);
......@@ -44,13 +53,13 @@ c = quantity.Domain('c', z);
d = [a b c];
testCase.verifyEqual(d.find('a'), a);
testCase.verifyEqual(d.find(b.name), b);
testCase.verifyEqual(d.find('c'), c);
tc.verifyEqual(d.find('a'), a);
tc.verifyEqual(d.find(b.name), b);
tc.verifyEqual(d.find('c'), c);
end
function testEquality(testCase)
function testEquality(tc)
d = quantity.Domain('d', 0);
e = quantity.Domain.empty();
......@@ -58,24 +67,24 @@ e = quantity.Domain.empty();
dd = d;
ee = e;
testCase.verifyNotEqual( d, e );
testCase.verifyEqual( dd, d );
testCase.verifyEqual( ee, e );
testCase.verifyNotEqual( e, d );
tc.verifyNotEqual( d, e );
tc.verifyEqual( dd, d );
tc.verifyEqual( ee, e );
tc.verifyNotEqual( e, d );
end
function testDomainInit(testCase)
function testDomainInit(tc)
Z = linspace(0,pi, 3);
d = quantity.Domain('z', Z);
D = [d d];
testCase.verifyEqual( ndims(D), 2);
testCase.verifyEqual( ndims(d), 1);
tc.verifyEqual( ndims(D), 2);
tc.verifyEqual( ndims(d), 1);
end
function testSort(testCase)
function testSort(tc)
z = linspace(0, pi, 3);
a = quantity.Domain('a', z);
......@@ -88,58 +97,58 @@ d = [a b c];
[d__, I__] = d.sort('ascend');
d___ = d.sort();
testCase.verifyEqual([d_.name], ["c", "b", "a"]);
testCase.verifyEqual({d__.name}, {d.name});
testCase.verifyEqual({d__.name}, {d___.name});
testCase.verifyEqual( I_, flip(I__) );
tc.verifyEqual([d_.name], ["c", "b", "a"]);
tc.verifyEqual({d__.name}, {d.name});
tc.verifyEqual({d__.name}, {d___.name});
tc.verifyEqual( I_, flip(I__) );
end
function testDomainGridLength(testCase)
function testDomainGridLength(tc)
Z = linspace(0,pi, 3);
d = quantity.Domain('z', Z);
D = [d d];
testCase.verifyEqual( cellfun(@(v) numel(v), {Z, Z}), D.gridLength)
tc.verifyEqual( cellfun(@(v) numel(v), {Z, Z}), D.gridLength)
end
function testDomainNumGridElements(testCase)
function testDomainNumGridElements(tc)
Z = linspace(0,pi, 3);
d = quantity.Domain('z', Z);
D = [d d];
testCase.verifyEqual( D.numGridElements, prod([length(Z), length(Z)]));
tc.verifyEqual( D.numGridElements, prod([length(Z), length(Z)]));
end
function testEmpty(testCase)
function testEmpty(tc)
d = quantity.Domain();
e = quantity.Domain.empty();
o = quantity.Domain('', 1);
testCase.verifyTrue( isempty(d) )
testCase.verifyTrue( isempty(e) )
testCase.verifyTrue( isempty([d, e]))
testCase.verifyTrue( isempty([d, d]))
testCase.verifyFalse( isempty(o) )
testCase.verifyFalse( isempty([o e]) )
testCase.verifyTrue( isempty([o d]) )
tc.verifyTrue( isempty(d) )
tc.verifyTrue( isempty(e) )
tc.verifyTrue( isempty([d, e]))
tc.verifyTrue( isempty([d, d]))
tc.verifyFalse( isempty(o) )
tc.verifyFalse( isempty([o e]) )
tc.verifyTrue( isempty([o d]) )
end
function testDefaultGrid(testCase)
function testDefaultGrid(tc)
g = quantity.Domain.defaultGrid([100, 42]);
testCase.verifyEqual(g(1).grid, linspace(0, 1, 100).');
testCase.verifyEqual(g(2).grid, linspace(0, 1, 42).');
tc.verifyEqual(g(1).grid, linspace(0, 1, 100).');
tc.verifyEqual(g(2).grid, linspace(0, 1, 42).');
end
function testGridJoin(testCase)
function testGridJoin(tc)
s = linspace(0, 1);
z = linspace(0, 1, 71)';
t = linspace(0, 1, 51);
......@@ -168,22 +177,22 @@ joinedDomainCC = sort( gridJoin(c, c) );
joinedDomainBC = sort( gridJoin(b, c) );
joinedDomain = sort( gridJoin(Z, [Z, T]) );
testCase.verifyEqual([joinedDomainAB.name], ["p", "s", "t", "z"]);
testCase.verifyEqual({joinedDomainAB.grid}, {s(:), s(:), t(:), z(:)});
testCase.verifyEqual(joinedDomainCC.name, "p");
testCase.verifyEqual({joinedDomainCC.grid}, {t(:)});
testCase.verifyEqual([joinedDomainBC.name], ["p", "s"]);
testCase.verifyEqual({joinedDomainBC.grid}, {s(:), t(:)});
testCase.verifyEqual({joinedDomain.grid}, {t(:), z(:)});
tc.verifyEqual([joinedDomainAB.name], ["p", "s", "t", "z"]);
tc.verifyEqual({joinedDomainAB.grid}, {s(:), s(:), t(:), z(:)});
tc.verifyEqual(joinedDomainCC.name, "p");
tc.verifyEqual({joinedDomainCC.grid}, {t(:)});
tc.verifyEqual([joinedDomainBC.name], ["p", "s"]);
tc.verifyEqual({joinedDomainBC.grid}, {s(:), t(:)});
tc.verifyEqual({joinedDomain.grid}, {t(:), z(:)});
d1 = [Z Z T];
d2 = quantity.Domain.empty;
testCase.verifyEqual( d1.join( d2 ), [Z T] )
tc.verifyEqual( d1.join( d2 ), [Z T] )
end
function testFind_Index(testCase)
function testFind_Index(tc)
z = linspace(0, 2*pi, 71)';
t = linspace(0, 3*pi, 51);
s = linspace(0, 1);
......@@ -196,17 +205,17 @@ a = [Z, T, S];
idx = a.gridIndex('z');
testCase.verifyEqual(idx, 1);
tc.verifyEqual(idx, 1);
idx = a.gridIndex({'z', 't'});
testCase.verifyEqual(idx, [1 2]);
tc.verifyEqual(idx, [1 2]);
idx = a.gridIndex({'z', 's'});
testCase.verifyEqual(idx, [1 3]);
tc.verifyEqual(idx, [1 3]);
idx = a.gridIndex('t');
testCase.verifyEqual(idx, 2);
tc.verifyEqual(idx, 2);
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