Mercurial > repos > public > sbplib
changeset 203:764438b52541 feature/grids
blockmatrix: Added functions to test for block matrcies and divisions.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 15 Jun 2016 14:29:56 +0200 |
parents | e2fefb6f0746 |
children | 4ce0af75d2f4 |
files | +blockmatrix/isBlockmatrix.m +blockmatrix/isBlockmatrixTest.m +blockmatrix/isDivision.m +blockmatrix/isDivisionTest.m |
diffstat | 4 files changed, 177 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
diff -r e2fefb6f0746 -r 764438b52541 +blockmatrix/isBlockmatrix.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+blockmatrix/isBlockmatrix.m Wed Jun 15 14:29:56 2016 +0200 @@ -0,0 +1,59 @@ +function b = isBlockmatrix(bm) + if ~iscell(bm) + b = false; + return + end + + % Make sure all blocks are numerica matrices + for i = 1:length(bm) + if ~isnumeric(bm{i}) + b = false; + return + end + end + + [N,M] = size(bm); + % Make sure column dimensions agree + for i = 1:N + d = []; + for j = 1:M + d_ij = size(bm{i,j},1); + if d_ij == 0 + continue + end + + if isempty(d) + d = d_ij; + continue + end + + if d ~= d_ij + b = false; + return + end + end + end + + % Make sure row dimensions agree + for j = 1:M + d = []; + for i = 1:N + d_ij = size(bm{i,j},2); + if d_ij == 0 + continue + end + + if isempty(d) + d = d_ij; + continue + end + + if d ~= d_ij + b = false; + return + end + end + end + + b = true; +end
diff -r e2fefb6f0746 -r 764438b52541 +blockmatrix/isBlockmatrixTest.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+blockmatrix/isBlockmatrixTest.m Wed Jun 15 14:29:56 2016 +0200 @@ -0,0 +1,61 @@ +function tests = isBlockmatrixTest() + tests = functiontests(localfunctions); +end + +function testIsBlockmatrix(testCase) + cases = { + { + magic(3), + false % Must be a cell array + } + { + {[2 2 2];{1,2}}, + false % All elements of the cell matrix must be regular matrices + }, + { + {[2 2 2];[1 2]}, + false % Row dimensions must match + }, + { + {[2; 2; 2], [1; 2]}, + false % Column dimensions must match + }, + { + { + [2 2; 2 1], [1; 2]; + [2 2], [1] + }, + true % A simple valid one + }, + { + { + [2 2; 2 1], []; + [2 2], [1] + }, + true % Empty blocks assumed to be zero and match dimensions + }, + { + { + [2 2; 2 1], []; + [2 2], [] + }, + true % Empty blocks allowed. + }, + { + { + [2 2; 2 1], [1; 2]; + [], [] + }, + true % Empty blocks allowed. + }, + + + }; + + for i = 1:length(cases) + in = cases{i}{1}; + out = blockmatrix.isBlockmatrix(in); + expected = cases{i}{2}; + testCase.verifyEqual(out, expected, sprintf('Should return %d for %s', expected, toString(in))); + end +end \ No newline at end of file
diff -r e2fefb6f0746 -r 764438b52541 +blockmatrix/isDivision.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+blockmatrix/isDivision.m Wed Jun 15 14:29:56 2016 +0200 @@ -0,0 +1,34 @@ +function b = isDivision(div) + % Make sure it is a cellarray + if ~iscell(div) + b = false; + return + end + + % Make sure it has the right shape + if numel(div) ~= 2 + b = false; + return + end + + if ~isDivisionVector(div{1}) || ~isDivisionVector(div{2}) + b = false; + return + end + + b = true; +end + +function b = isDivisionVector(v) + if isempty(v) + b = false; + return + end + + if any(v <= 0) + b = false; + return + end + + b = true; +end
diff -r e2fefb6f0746 -r 764438b52541 +blockmatrix/isDivisionTest.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+blockmatrix/isDivisionTest.m Wed Jun 15 14:29:56 2016 +0200 @@ -0,0 +1,23 @@ +function tests = isDivisionTest() + tests = functiontests(localfunctions); +end + +function testIsDivision(testCase) + cases = { + {{[2 2 2],[1 2]} ,true}, + {{[1 2],[1 0]} ,false}, + {{[0 2],[1 1]} ,false}, + {{[1 2],[]} ,false}, + {{[1 2],[1]} ,true}, + {{[1 2],[1], [1 2 3]} ,false}, + {{[1 2 3]} ,false}, + {[1 2] ,false}, + }; + + for i = 1:length(cases) + in = cases{i}{1}; + out = blockmatrix.isDivision(in); + expected = cases{i}{2}; + testCase.verifyEqual(out, expected, sprintf('Should return %d for %s', expected, toString(in))); + end +end \ No newline at end of file