changeset 543:8112db99ab50

Proper implementation of entryWiseProduct
author Chris Cannam
date Fri, 21 Mar 2014 16:37:28 +0000
parents acc244cab1b7
children 01863795221c
files src/may/matrix.yeti src/may/matrix/test/test_matrix.yeti
diffstat 2 files changed, 28 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/may/matrix.yeti	Fri Mar 21 10:33:04 2014 +0000
+++ b/src/may/matrix.yeti	Fri Mar 21 16:37:28 2014 +0000
@@ -677,10 +677,23 @@
     fi;
 
 entryWiseProduct m1 m2 = // or element-wise, or Hadamard product
-//!!! todo: faster, sparse version, unit tests
     if (size m1) != (size m2)
     then failWith "Matrices are not the same size: \(size m1), \(size m2)";
-    else generate do row col: at' m1 row col * at' m2 row col done (size m1);
+    else 
+        if isSparse? m1 then
+            newSparse (size m1)
+               ((taggerForTypeOf m1)
+                   (map do { i, j, v }: { i, j, v = v * (at' m2 i j) } done
+                       (enumerateSparse m1)))
+        elif isSparse? m2 then
+            entryWiseProduct m2 m1
+        else
+            if isRowMajor? m1 then
+                fromRows (array (map2 vec.multiply (asRows m1) (asRows m2)));
+            else
+                fromColumns (array (map2 vec.multiply (asColumns m1) (asColumns m2)));
+            fi
+        fi
     fi;
 
 concatAgainstGrain tagger getter counter mm =
--- a/src/may/matrix/test/test_matrix.yeti	Fri Mar 21 10:33:04 2014 +0000
+++ b/src/may/matrix/test/test_matrix.yeti	Fri Mar 21 16:37:28 2014 +0000
@@ -332,6 +332,19 @@
            (fromColumns [[58,139],[64,154]])
 ),
 
+"entryWiseProduct-\(name)": \(
+    compareMatrices
+       (mat.entryWiseProduct
+           (fromRows [[1,2,3],[4,5,0]])
+           (fromRows [[6,7,8],[0,1,2]]))
+       (fromRows [[6,14,24],[0,5,0]]) and
+    compareMatrices
+       (mat.entryWiseProduct
+           (fromRows [[1,2,3],[4,5,0]])
+           (fromColumns [[6,0],[7,1],[8,2]]))
+       (fromRows [[6,14,24],[0,5,0]])
+),
+
 "sparseProduct-\(name)": \(
     s = mat.newSparseMatrix { rows = 2, columns = 3 } (Columns [
         { i = 0, j = 0, v = 1 },