changeset 205:f0ef314e2070 feature/grids

blockmatrix: added function to calculate the block division for a given blockmatrix.
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 15 Jun 2016 16:28:22 +0200
parents 4ce0af75d2f4
children 50a323da7c7f
files +blockmatrix/getDivision.m +blockmatrix/getDivisionTest.m +blockmatrix/isBlockmatrixTest.m
diffstat 3 files changed, 106 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
diff -r 4ce0af75d2f4 -r f0ef314e2070 +blockmatrix/getDivision.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+blockmatrix/getDivision.m	Wed Jun 15 16:28:22 2016 +0200
@@ -0,0 +1,37 @@
+function div = getDivision(bm)
+    if ~blockmatrix.isBlockmatrix(bm)
+        error('blockmatrix:getDivision:NotABlockmatrix', 'Input is not a blockmatrix');
+    end
+
+    if isempty(bm)
+        div = {[],[]};
+        return
+    end
+
+    div = {row_height(bm),col_width(bm)};
+end
+
+
+function m = col_width(C)
+    m = zeros(1,size(C,2));
+    for j = 1:size(C,2)
+        for i = 1:size(C,1)
+            if isempty(C{i,j})
+                continue
+            end
+            m(j) = size(C{i,j},2);
+        end
+    end
+end
+
+function n = row_height(C)
+    n = zeros(1,size(C,1));
+    for i = 1:size(C,1)
+        for j = 1:size(C,2)
+            if isempty(C{i,j})
+                continue
+            end
+            n(i) = size(C{i,j},1);
+        end
+    end
+end
\ No newline at end of file
diff -r 4ce0af75d2f4 -r f0ef314e2070 +blockmatrix/getDivisionTest.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+blockmatrix/getDivisionTest.m	Wed Jun 15 16:28:22 2016 +0200
@@ -0,0 +1,69 @@
+function tests = getDivisionTest()
+    tests = functiontests(localfunctions);
+end
+
+function testError(testCase)
+    cases = {
+        magic(3),
+        {[2 2 2];{1,2}},
+        {[2 2 2];[1 2]},
+        {[2; 2; 2], [1; 2]},
+    };
+
+    for i =1:length(cases)
+        testCase.verifyError(@()blockmatrix.getDivision(cases{i}), 'blockmatrix:getDivision:NotABlockmatrix')
+    end
+end
+
+function testGetDivision(testCase)
+    cases = {
+        {
+            {},
+            {[],[]};
+        },
+        {
+            {
+            [2 2; 2 1], [1; 2];
+            [2 2], [1]
+            },
+            {[2 1], [2 1]}
+        },
+        {
+            {
+            [2 2; 2 1], [];
+            [2 2], [1]
+            },
+            {[2 1], [2 1]}
+        },
+        {
+            {
+            [2 2; 2 1], [];
+            [2 2], []
+            },
+            {[2 1], [2 0]}
+        },
+        {
+            {
+            [2 2; 2 1], [1; 2];
+            [], []
+            },
+            {[2 0], [2 1]}
+        },
+        {
+            {
+            [2 2; 2 1];
+            [2 2]
+            },
+            {[2 1], 2}
+        },
+    };
+
+    for i = 1:length(cases)
+        in = cases{i}{1};
+        out = blockmatrix.getDivision(in);
+        expected = cases{i}{2};
+        testCase.verifyEqual(out, expected);
+    end
+end
+
+
diff -r 4ce0af75d2f4 -r f0ef314e2070 +blockmatrix/isBlockmatrixTest.m
--- a/+blockmatrix/isBlockmatrixTest.m	Wed Jun 15 15:54:53 2016 +0200
+++ b/+blockmatrix/isBlockmatrixTest.m	Wed Jun 15 16:28:22 2016 +0200
@@ -52,8 +52,6 @@
             },
             true % Empty blocks allowed.
         },
-
-
     };
 
     for i = 1:length(cases)