changeset 97:d5fc902dcc3f

Initial matrix tests
author Chris Cannam
date Wed, 20 Mar 2013 22:49:41 +0000
parents a5b4d0f68ca8
children bd135a950af7
files yetilab/matrix/matrix.yeti yetilab/matrix/test/test_matrix.yeti yetilab/test/all.yeti
diffstat 3 files changed, 158 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/yetilab/matrix/matrix.yeti	Wed Mar 20 21:57:17 2013 +0000
+++ b/yetilab/matrix/matrix.yeti	Wed Mar 20 22:49:41 2013 +0000
@@ -59,7 +59,9 @@
     };
 
 newStorage rows cols = 
-    array (map \(vec.zeros rows) [1..cols]);
+    if rows < 1 then array []
+    else array (map \(vec.zeros rows) [1..cols])
+    fi;
 
 zeroMatrix rows cols = 
     make (ColM (newStorage rows cols));
@@ -80,6 +82,16 @@
 width m = m.size.columns;
 height m = m.size.rows;
 
+equal m1 m2 =
+   (compare d1 d2 =
+        all id (map2 vec.equal d1 d2);
+    case m1.data of
+    RowM d1:
+        case m2.data of RowM d2: compare d1 d2; _: false; esac;
+    ColM d1:
+        case m2.data of ColM d2: compare d1 d2; _: false; esac;
+    esac);
+
 copyOf m =
    (copyOfData d = (array (map vec.copyOf d));
     make
@@ -103,7 +115,7 @@
 
 newMatrix type data is RowMajor () | ColumnMajor () -> list?<list?<number>> -> 'a =
    (tagger = case type of RowMajor (): RowM; ColumnMajor (): ColM esac;
-    if empty? data
+    if empty? data or empty? (head data)
     then zeroMatrix 0 0
     else make (tagger (array (map vec.vector data)))
     fi);
@@ -115,9 +127,10 @@
     newMatrix (ColumnMajor ()) [data];
 
 {
+constMatrix, randomMatrix, zeroMatrix, identityMatrix,
 generate,
-constMatrix, randomMatrix, zeroMatrix, identityMatrix,
 width, height,
+equal,
 copyOf,
 transposed,
 flipped,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/yetilab/matrix/test/test_matrix.yeti	Wed Mar 20 22:49:41 2013 +0000
@@ -0,0 +1,141 @@
+
+module yetilab.matrix.test.test_matrix;
+
+mat = load yetilab.matrix.matrix;
+block = load yetilab.block.block;
+
+{ compare } = load yetilab.test.test;
+
+[
+
+"constMatrixEmpty": \(
+    m = mat.constMatrix 2 0 0;
+    compare m.size { columns = 0, rows = 0 }
+),
+
+"constMatrixEmpty2": \(
+    compare (mat.constMatrix 2 0 4).size { columns = 0, rows = 0 } and
+        compare (mat.constMatrix 2 4 0).size { columns = 0, rows = 0 }
+),
+
+"constMatrix": \(
+    m = mat.constMatrix 2 3 4;
+    compare m.size { columns = 4, rows = 3 } and
+        all id (map do row: compare (block.list (m.getRow row)) [2,2,2,2] done [0..2]) and
+        all id (map do col: compare (block.list (m.getColumn col)) [2,2,2] done [0..3])
+),
+
+"randomMatrixEmpty": \(
+    m = mat.randomMatrix 0 0;
+    compare m.size { columns = 0, rows = 0 }
+),
+
+"randomMatrix": \(
+    m = mat.randomMatrix 3 4;
+    compare m.size { columns = 4, rows = 3 }
+),
+
+"zeroMatrixEmpty": \(
+    m = mat.zeroMatrix 0 0;
+    compare m.size { columns = 0, rows = 0 }
+),
+
+"zeroMatrix": \(
+    m = mat.zeroMatrix 3 4;
+    compare m.size { columns = 4, rows = 3 } and
+        all id (map do row: compare (block.list (m.getRow row)) [0,0,0,0] done [0..2]) and
+        all id (map do col: compare (block.list (m.getColumn col)) [0,0,0] done [0..3])
+),
+
+"identityMatrixEmpty": \(
+    m = mat.identityMatrix 0 0;
+    compare m.size { columns = 0, rows = 0 }
+),
+
+"identityMatrix": \(
+    m = mat.identityMatrix 3 4;
+    compare m.size { columns = 4, rows = 3 } and
+        all id (map do row: compare (block.list (m.getRow row)) [1,1,1,1] done [0..2]) and
+        all id (map do col: compare (block.list (m.getColumn col)) [1,1,1] done [0..3])
+),
+
+"generateEmpty": \(
+    m = mat.generate do row col: 0 done 0 0;
+    compare m.size { columns = 0, rows = 0 }
+),
+
+"generate": \(
+    m = mat.generate do row col: row * 10 + col done 2 3;
+    compare (block.list (m.getRow 0)) [0,1,2] and
+        compare (block.list (m.getRow 1)) [10,11,12]
+),
+
+"widthAndHeight": \(
+    m = mat.constMatrix 2 3 4;
+    compare m.size { columns = mat.width m, rows = mat.height m }
+),
+
+"equal": \(
+    m = mat.constMatrix 2 3 4;
+    m' = m;
+    p = mat.constMatrix 2 4 3;
+    q = mat.constMatrix 3 3 4;
+    mat.equal m m' and mat.equal m m and
+       not mat.equal m p and not mat.equal m q and not mat.equal p q
+),
+
+"getAt": \(
+    generator row col = row * 10 + col;
+    m = mat.generate generator 2 3;
+    all id
+       (map do row: all id
+           (map do col: m.getAt row col == generator row col done [0..2])
+            done [0..1])
+),
+
+"setAt": \(
+    generator row col = row * 10 + col;
+    m = mat.generate generator 2 3;
+    m.setAt 1 2 16;
+    compare (m.getAt 1 2) 16 and
+        compare (m.getAt 1 1) 11 and
+        compare (m.getAt 0 2) 2
+),
+
+"copyOfEqual": \(
+    m = mat.constMatrix 2 3 4;
+    m'' = mat.copyOf m;
+    mat.equal m m''
+),
+
+"copyOfAlias": \(
+    m = mat.constMatrix 2 3 4;
+    m' = m;
+    m'' = mat.copyOf m;
+    m.setAt 0 0 6;
+    mat.equal m m' and not mat.equal m m'';
+),
+
+"transposedEmpty": \(
+    compare (mat.transposed (mat.constMatrix 2 0 0)).size { columns = 0, rows = 0 } and
+        compare (mat.transposed (mat.constMatrix 2 0 4)).size { columns = 0, rows = 0 } and
+        compare (mat.transposed (mat.constMatrix 2 4 0)).size { columns = 0, rows = 0 }
+),
+
+"transposedSize": \(
+    compare (mat.transposed (mat.constMatrix 2 3 4)).size { columns = 3, rows = 4 }
+),
+
+"transposed": \(
+    generator row col = row * 10 + col;
+    m = mat.generate generator 2 3;
+    m' = mat.transposed m;
+    all id
+       (map do row: all id
+           // like getAt test, but with col/row flipped
+           (map do col: m'.getAt col row == generator row col done [0..2])
+            done [0..1])
+),
+
+] is hash<string, () -> boolean>;
+
--- a/yetilab/test/all.yeti	Wed Mar 20 21:57:17 2013 +0000
+++ b/yetilab/test/all.yeti	Wed Mar 20 22:49:41 2013 +0000
@@ -10,6 +10,7 @@
 "framer"     : load yetilab.stream.test.test_framer,
 "fft"        : load yetilab.transform.test.test_fft,
 "vamppost"   : load yetilab.vamp.test.test_vamppost,
+"matrix"     : load yetilab.matrix.test.test_matrix
 ];
 
 bad = sum (mapHash do name testHash: runTests name testHash done tests);