Mercurial > repos > public > sbplib
changeset 168:ba1ae5b2c45e feature/grids
grid.Cartesian: Added methods and test to fulfilll abstract class.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 24 Feb 2016 15:01:54 +0100 |
parents | 15baeb35f74e |
children | ba8adcaf4681 |
files | +grid/Cartesian.m +grid/CartesianTest.m |
diffstat | 2 files changed, 127 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/+grid/Cartesian.m Tue Feb 23 13:25:43 2016 +0100 +++ b/+grid/Cartesian.m Wed Feb 24 15:01:54 2016 +0100 @@ -31,6 +31,10 @@ o = obj.d; end + function m = size(obj) + m = obj.m; + end + % points returns a n x d matrix containing the coordianets for all points. % points are ordered according to the kronecker product with X*Y*Z function X = points(obj) @@ -71,5 +75,39 @@ X{i} = repmat(t,s); end end + + % Restricts the grid function gf on obj to the subgrid g. + % Only works for even multiples + function gf = restrictFunc(obj, gf, g) + m1 = obj.m; + m2 = g.m; + + % Check the input + if prod(m1) ~= numel(gf) + error('grid:Cartesian:restrictFunc:NonMatchingFunctionSize', 'The grid function has to few or too many points.'); + end + + if ~all(mod(m1-1,m2-1) == 0) + error('grid:Cartesian:restrictFunc:NonMatchingGrids', 'Only integer downsamplings are allowed'); + end + + % Calculate stride for each dimension + stride = (m1-1)./(m2-1); + + % Create downsampling indecies + I = {}; + for i = 1:length(m1) + I{i} = 1:stride(i):m1(i); + end + + gf = reshapeRowMaj(gf, m1); + gf = gf(I{:}); + gf = reshapeRowMaj(gf, prod(m2)); + end + + % Projects the grid function gf on obj to the grid g. + function gf = projectFunc(obj, gf, g) + error('grid:Cartesian:NotImplemented') + end end end \ No newline at end of file
--- a/+grid/CartesianTest.m Tue Feb 23 13:25:43 2016 +0100 +++ b/+grid/CartesianTest.m Wed Feb 24 15:01:54 2016 +0100 @@ -50,6 +50,28 @@ end end +function testSize(testCase) + in = { + {[1 2 3]}, + {[1 2 3],[1 2]}, + {[1 2 3],[1 2 3]}, + {[1 2 3],[1 2 3], [1]}, + {[1 2 3],[1 2 3], [1 3 4]}, + }; + + out = { + [3], + [3 2], + [3 3], + [3 3 1], + [3 3 3], + }; + + for i = 1:length(in) + g = grid.Cartesian(in{i}{:}); + testCase.verifyEqual(g.size(),out{i}); + end +end function testPoints(testCase) in = { @@ -95,4 +117,70 @@ g = grid.Cartesian(in{i}{:}); testCase.verifyEqual(g.matrices(),out{i}); end -end \ No newline at end of file +end + + +function testRestrictFuncInvalidInput(testCase) + inG1 = { + {[1 2 3 4 5]}, + {[1 2 3],[4 5 6 7 8]}, + {[1 2 3],[4 5 6 7 8]}, + {[1 2 3],[4 5 6 7 8]}, + }; + + inG2 = { + {[1 3 4 5]}, + {[1 3],[4 5 6 8]}, + {[1 3],[4 6 8]}, + {[1 3],[4 6 8]}, + }; + + inGf = { + [1; 2; 3; 4; 5], + [14; 15; 16; 17; 18; 24; 25; 26; 27; 28; 34; 35; 36; 37; 38]; + [14; 15; 16; 17; 18; 24; 25; 26; 27; 28; 34; 35; 36]; + [14; 15; 16; 17; 18; 24; 25; 26; 27; 28; 34; 35; 36; 37; 38; 39; 40]; + }; + + out = { + 'grid:Cartesian:restrictFunc:NonMatchingGrids', + 'grid:Cartesian:restrictFunc:NonMatchingGrids', + 'grid:Cartesian:restrictFunc:NonMatchingFunctionSize', + 'grid:Cartesian:restrictFunc:NonMatchingFunctionSize', + }; + + for i = 1:length(inG1) + g1 = grid.Cartesian(inG1{i}{:}); + g2 = grid.Cartesian(inG2{i}{:}); + testCase.verifyError(@()g1.restrictFunc(inGf{i},g2),out{i}); + end +end + +function testRestrictFunc(testCase) + inG1 = { + {[1 2 3 4 5]}, + {[1 2 3],[4 5 6 7 8]}, + }; + + inG2 = { + {[1 3 5]}, + {[1 3],[4 6 8]}, + }; + + inGf = { + [1; 2; 3; 4; 5], + [14; 15; 16; 17; 18; 24; 25; 26; 27; 28; 34; 35; 36; 37; 38]; + }; + + outGf = { + [1; 3; 5], + [14; 16; 18; 34; 36; 38]; + }; + + for i = 1:length(inG1) + g1 = grid.Cartesian(inG1{i}{:}); + g2 = grid.Cartesian(inG2{i}{:}); + testCase.verifyEqual(g1.restrictFunc(inGf{i}, g2), outGf{i}); + end +end +