diff yetilab/matrix/test/test_matrix.yeti @ 257:f00ab8baa6d7

Make sum, difference, scaled and abs use sparse operations and return sparse matrices if all inputs are sparse
author Chris Cannam
date Tue, 21 May 2013 22:37:28 +0100
parents 8043f7405eae
children f3b7b5d20f88
line wrap: on
line diff
--- a/yetilab/matrix/test/test_matrix.yeti	Tue May 21 22:36:39 2013 +0100
+++ b/yetilab/matrix/test/test_matrix.yeti	Tue May 21 22:37:28 2013 +0100
@@ -215,6 +215,32 @@
     yrt
 ),
 
+"sparseSum-\(name)": \(
+    s = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [
+        { i = 0, j = 0, v = 1 },
+        { i = 0, j = 2, v = 2 },
+        { i = 1, j = 1, v = 4 },
+    ];
+    t = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [
+        { i = 0, j = 1, v = 7 },
+        { i = 1, j = 0, v = 5 },
+        { i = 1, j = 1, v = 6 },
+    ];
+    tot = mat.sum s t;
+    mat.isSparse? tot and
+        compareMatrices tot (mat.sum (mat.toDense s) t) and
+        compareMatrices tot (mat.sum (mat.toDense s) (mat.toDense t)) and
+        compareMatrices tot (mat.sum s (mat.toDense t)) and
+        compareMatrices tot 
+           (mat.newSparseMatrix (RowMajor ()) { rows = 2, columns = 3 } [
+               { i = 0, j = 0, v = 1 },
+               { i = 0, j = 1, v = 7 },
+               { i = 0, j = 2, v = 2 },
+               { i = 1, j = 0, v = 5 },
+               { i = 1, j = 1, v = 10 },
+            ])
+),
+
 "difference-\(name)": \(
     compareMatrices
        (mat.difference (constMatrix 2 { rows = 3, columns = 4 })
@@ -232,6 +258,32 @@
     yrt
 ),
 
+"sparseDifference-\(name)": \(
+    s = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [
+        { i = 0, j = 0, v = 1 },
+        { i = 0, j = 2, v = 2 },
+        { i = 1, j = 1, v = 4 },
+    ];
+    t = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [
+        { i = 0, j = 1, v = 7 },
+        { i = 1, j = 0, v = 5 },
+        { i = 1, j = 1, v = 6 },
+    ];
+    diff = mat.difference s t;
+    mat.isSparse? diff and
+        compareMatrices diff (mat.difference (mat.toDense s) t) and
+        compareMatrices diff (mat.difference (mat.toDense s) (mat.toDense t)) and
+        compareMatrices diff (mat.difference s (mat.toDense t)) and
+        compareMatrices diff 
+           (mat.newSparseMatrix (RowMajor ()) { rows = 2, columns = 3 } [
+               { i = 0, j = 0, v = 1 },
+               { i = 0, j = 1, v = -7 },
+               { i = 0, j = 2, v = 2 },
+               { i = 1, j = 0, v = -5 },
+               { i = 1, j = 1, v = -2 },
+            ])
+),
+
 "abs-\(name)": \(
     compareMatrices
        (mat.abs (newMatrix (ColumnMajor ()) [[-1,4],[2,-5],[-3,0]]))