changeset 241:e777a713027b sparse

Fix comparator for sparse matrices; sprinkle some zeros in test data to exercise sparsity
author Chris Cannam
date Mon, 20 May 2013 16:01:43 +0100
parents f22ae9bfe49f
children 0ac8672d12b2
files yetilab/matrix/matrix.yeti yetilab/matrix/test/test_matrix.yeti
diffstat 2 files changed, 37 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/yetilab/matrix/matrix.yeti	Mon May 20 15:08:14 2013 +0100
+++ b/yetilab/matrix/matrix.yeti	Mon May 20 16:01:43 2013 +0100
@@ -260,13 +260,12 @@
 
 equal'' comparator vecComparator m1 m2 =
     // Prerequisite: m1 and m2 have same sparse-p and storage order
-   (compareLists l1 l2 = all id (map2 comparator l1 l2);
-    compareVecLists vv1 vv2 = all id (map2 vecComparator vv1 vv2);
+   (compareVecLists vv1 vv2 = all id (map2 vecComparator vv1 vv2);
     compareSparse d1 d2 =
         d1.extent == d2.extent and
         vecComparator d1.values d2.values and
-        compareLists d1.indices d2.indices and
-        compareLists d1.pointers d2.pointers;
+        d1.indices == d2.indices and
+        d1.pointers == d2.pointers;
     case m1 of
     DenseRows d1:
         case m2 of DenseRows d2: compareVecLists d1 d2; _: false; esac;
@@ -335,6 +334,7 @@
 abs' m =
     generate do row col: abs (getAt row col m) done (size m);
 
+//!!! todo: proper sparse multiply
 product m1 m2 =
     if (size m1).columns != (size m2).rows
     then failWith "Matrix dimensions incompatible: \(size m1), \(size m2) (\((size m1).columns != (size m2).rows)";
@@ -459,7 +459,6 @@
     equalUnder,
     transposed,
     flipped,
-    enumerateSparse,
     toRowMajor,
     toColumnMajor,
     toSparse,
@@ -501,7 +500,6 @@
     equalUnder is (number -> number -> boolean) -> matrix -> matrix -> boolean,
     transposed is matrix -> matrix,
     flipped is matrix -> matrix, 
-    enumerateSparse is matrix -> list<{ i is number, j is number, v is number }>,
     toRowMajor is matrix -> matrix, 
     toColumnMajor is matrix -> matrix,
     toSparse is number -> matrix -> matrix,
--- a/yetilab/matrix/test/test_matrix.yeti	Mon May 20 15:08:14 2013 +0100
+++ b/yetilab/matrix/test/test_matrix.yeti	Mon May 20 16:01:43 2013 +0100
@@ -90,11 +90,11 @@
 ),
 
 "equal-\(name)": \(
-    m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]];
+    m = newMatrix (ColumnMajor ()) [[1,4],[0,5],[3,6]];
     n = m;
-    p = newMatrix (RowMajor ()) [[1,2,3],[4,5,6]];
-    q = newMatrix (ColumnMajor ()) [[1,2,3],[4,5,6]];
-    r = newMatrix (ColumnMajor ()) [[1,4],[2,5]];
+    p = newMatrix (RowMajor ()) [[1,0,3],[4,5,6]];
+    q = newMatrix (ColumnMajor ()) [[1,0,3],[4,5,6]];
+    r = newMatrix (ColumnMajor ()) [[1,4],[0,5]];
     compareMatrices m n and
         compareMatrices m p and
         compareMatrices n p and
@@ -106,8 +106,8 @@
     p = newMatrix (ColumnMajor ()) [[1,2,3],[4,5,6]];
     q = newMatrix (ColumnMajor ()) [[1,2,3],[4,5,6]];
     r = newMatrix (ColumnMajor ()) [[4,3,1],[3,1,2]];
-    s = newMatrix (ColumnMajor ()) [[1,2,5],[6,7,8]];
-    t = newMatrix (ColumnMajor ()) [[1,2,5],[6,7,9]];
+    s = newMatrix (ColumnMajor ()) [[1,4,5],[6,7,8]];
+    t = newMatrix (ColumnMajor ()) [[1,4,5],[6,7,9]];
     mat.equalUnder (==) p p and
         mat.equalUnder (==) p q and
         mat.equalUnder (!=) p r and
@@ -123,30 +123,7 @@
            (map do col: mat.getAt row col m == generator row col done [0..2])
             done [0..1])
 ),
-/*!!!
-"setAt-\(name)": \(
-    generator row col = row * 10 + col;
-    m = generate generator { rows = 2, columns = 3 };
-    mat.setAt 1 2 16 m;
-    compare (mat.getAt 1 2 m) 16 and
-        compare (mat.getAt 1 1 m) 11 and
-        compare (mat.getAt 0 2 m) 2
-),
 
-"copyOfEqual-\(name)": \(
-    m = constMatrix 2 { rows = 3, columns = 4 };
-    m'' = mat.copyOf m;
-    compareMatrices m'' m
-),
-
-"copyOfAlias-\(name)": \(
-    m = constMatrix 2 { rows = 3, columns = 4 };
-    m' = m;
-    m'' = mat.copyOf m;
-    mat.setAt 0 0 6 m;
-    compareMatrices m' m and not mat.equal m m'';
-),
-*/
 "transposedEmpty-\(name)": \(
     compare (mat.size (mat.transposed (constMatrix 2 { rows = 0, columns = 0 }))) { columns = 0, rows = 0 } and
         compare (mat.size (mat.transposed (constMatrix 2 { rows = 0, columns = 4 }))) { columns = 0, rows = 0 } and
@@ -175,14 +152,14 @@
 ),
 
 "flipped-\(name)": \(
-    m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]];
+    m = newMatrix (ColumnMajor ()) [[1,4],[0,5],[3,6]];
     m' = mat.flipped m;
-    m'' = newMatrix (RowMajor ()) [[1,2,3],[4,5,6]];
+    m'' = newMatrix (RowMajor ()) [[1,0,3],[4,5,6]];
     compareMatrices m m' and compareMatrices m m'' and compareMatrices m' m'';
 ),
 
 "flipped-back-\(name)": \(
-    m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]];
+    m = newMatrix (ColumnMajor ()) [[1,4],[0,5],[3,6]];
     compareMatrices m (mat.flipped (mat.flipped m));
 ),
 
@@ -192,18 +169,18 @@
 ),
 
 "toRowMajor-\(name)": \(
-    m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]];
+    m = newMatrix (ColumnMajor ()) [[1,4],[0,5],[3,6]];
     m' = mat.toRowMajor m;
-    m'' = newMatrix (RowMajor ()) [[1,2,3],[4,5,6]];
+    m'' = newMatrix (RowMajor ()) [[1,0,3],[4,5,6]];
     m''' = mat.toRowMajor m'';
     compareMatrices m m' and compareMatrices m m'' and compareMatrices m' m''
         and compareMatrices m m''';
 ),
 
 "toColumnMajor-\(name)": \(
-    m = newMatrix (RowMajor ()) [[1,4],[2,5],[3,6]];
+    m = newMatrix (RowMajor ()) [[1,4],[0,5],[3,6]];
     m' = mat.toColumnMajor m;
-    m'' = newMatrix (ColumnMajor ()) [[1,2,3],[4,5,6]];
+    m'' = newMatrix (ColumnMajor ()) [[1,0,3],[4,5,6]];
     m''' = mat.toColumnMajor m'';
     compareMatrices m m' and compareMatrices m m'' and compareMatrices m' m''
         and compareMatrices m m''';
@@ -310,29 +287,29 @@
 "asRows-\(name)": \(
     compare
        (map vec.list
-           (mat.asRows (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]])))
-        [[1,2,3],[4,5,6]];
+           (mat.asRows (newMatrix (ColumnMajor ()) [[1,4],[0,5],[3,6]])))
+        [[1,0,3],[4,5,6]];
 ),
 
 "asColumns-\(name)": \(
     compare
        (map vec.list
-           (mat.asColumns (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]])))
-        [[1,4],[2,5],[3,6]];
+           (mat.asColumns (newMatrix (ColumnMajor ()) [[1,4],[0,5],[3,6]])))
+        [[1,4],[0,5],[3,6]];
 ),
 
 "concat-horiz-\(name)": \(
     compareMatrices
        (mat.concat (Horizontal ()) 
-          [(newMatrix (ColumnMajor ()) [[1,4],[2,5]]),
+          [(newMatrix (ColumnMajor ()) [[1,4],[0,5]]),
            (newMatrix (RowMajor ()) [[3],[6]])])
-       (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]])
+       (newMatrix (ColumnMajor ()) [[1,4],[0,5],[3,6]])
 ),
 
 "concatFail-horiz-\(name)": \(
     try
         \() (mat.concat (Horizontal ()) 
-          [(newMatrix (ColumnMajor ()) [[1,4],[2,5]]),
+          [(newMatrix (ColumnMajor ()) [[1,4],[0,5]]),
            (newMatrix (ColumnMajor ()) [[3],[6]])]);
         false
     catch FailureException e:
@@ -343,15 +320,15 @@
 "concat-vert-\(name)": \(
     compareMatrices
        (mat.concat (Vertical ()) 
-          [(newMatrix (ColumnMajor ()) [[1,4],[2,5]]),
+          [(newMatrix (ColumnMajor ()) [[1,4],[0,5]]),
            (newMatrix (RowMajor ()) [[3,6]])])
-       (newMatrix (ColumnMajor ()) [[1,4,3],[2,5,6]])
+       (newMatrix (ColumnMajor ()) [[1,4,3],[0,5,6]])
 ),
 
 "concatFail-vert-\(name)": \(
     try
         \() (mat.concat (Vertical ()) 
-          [(newMatrix (ColumnMajor ()) [[1,4],[2,5]]),
+          [(newMatrix (ColumnMajor ()) [[1,4],[0,5]]),
            (newMatrix (RowMajor ()) [[3],[6]])]);
         false
     catch FailureException e:
@@ -361,14 +338,14 @@
 
 "rowSlice-\(name)": \(
     compareMatrices
-       (mat.rowSlice 1 2 (newMatrix (RowMajor ()) [[1,2],[3,4],[5,6],[7,8]]))
-       (newMatrix (RowMajor ()) [[3,4],[5,6]])
+       (mat.rowSlice 1 2 (newMatrix (RowMajor ()) [[1,0],[3,4],[0,6],[7,8]]))
+       (newMatrix (RowMajor ()) [[3,4],[0,6]])
 ),
 
 "columnSlice-\(name)": \(
     compareMatrices
-       (mat.columnSlice 1 2 (newMatrix (RowMajor ()) [[1,2,3,4],[5,6,7,8]]))
-       (newMatrix (RowMajor ()) [[2,3],[6,7]])
+       (mat.columnSlice 1 2 (newMatrix (RowMajor ()) [[1,0,3,4],[0,6,7,8]]))
+       (newMatrix (RowMajor ()) [[0,3],[6,7]])
 ),
 
 ]);
@@ -376,11 +353,14 @@
 colhash = makeTests "column-dense" id;
 rowhash = makeTests "row-dense" mat.flipped;
 sparsecolhash = makeTests "column-sparse" (mat.toSparse 0);
-sparserowhash = makeTests "row-sparse" ((mat.toSparse 0) . (mat.flipped));
-sparserowhash2 = makeTests "row-sparse2" ((mat.flipped) . (mat.toSparse 0));
+
+// there are two possible orders for constructing a sparse row-major
+// matrix from a dense col-major one, so test them both:
+sparserowhash1 = makeTests "row-sparse-a" ((mat.toSparse 0) . (mat.flipped));
+sparserowhash2 = makeTests "row-sparse-b" ((mat.flipped) . (mat.toSparse 0));
 
 all = [:];
-for [ colhash, rowhash, sparsecolhash, sparserowhash, sparserowhash2 ] do h:
+for [ colhash, rowhash, sparsecolhash, sparserowhash1, sparserowhash2 ] do h:
     for (keys h) do k: all[k] := h[k] done;
 done;