changeset 551:25800b390b07

Work on minValue, maxValue, total
author Chris Cannam
date Fri, 28 Mar 2014 13:16:12 +0000
parents 5d6aeb765804
children 25b925cf3c98
files src/may/matrix.yeti src/may/matrix/test/test_matrix.yeti
diffstat 2 files changed, 50 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/may/matrix.yeti	Tue Mar 25 10:32:01 2014 +0000
+++ b/src/may/matrix.yeti	Fri Mar 28 13:16:12 2014 +0000
@@ -933,16 +933,37 @@
 
 minValue m =
     if width m == 0 or height m == 0 then 0
-    else 
+    elif isSparse? m then
         minv ll = fold min (head ll) (tail ll);
-        minv (map (.v) (enumerate m));
+        minnz = minv (map (.v) (enumerate m));
+        if minnz > 0 and nonZeroValues m < (width m * height m) then 0
+        else minnz fi;
+    elif isRowMajor? m then
+        vec.min (vec.fromList (map vec.min (asRows m)));
+    else
+        vec.min (vec.fromList (map vec.min (asColumns m)));
     fi;
 
 maxValue m =
     if width m == 0 or height m == 0 then 0
-    else 
+    elif isSparse? m then
         maxv ll = fold max (head ll) (tail ll);
-        maxv (map (.v) (enumerate m));
+        maxnz = maxv (map (.v) (enumerate m));
+        if maxnz < 0 and nonZeroValues m < (width m * height m) then 0
+        else maxnz fi;
+    elif isRowMajor? m then
+        vec.max (vec.fromList (map vec.max (asRows m)));
+    else
+        vec.max (vec.fromList (map vec.max (asColumns m)));
+    fi;
+
+total m = 
+    if isSparse? m then
+        fold (+) 0 (map (.v) (enumerateSparse m));
+    elif isRowMajor? m then
+        fold (+) 0 (map vec.sum (asRows m));
+    else
+        fold (+) 0 (map vec.sum (asColumns m));
     fi;
 
 mapRows rf m =
@@ -1007,6 +1028,7 @@
     scaled,
     minValue,
     maxValue,
+    total,
     asRows,
     asColumns,
     sum = sum',
@@ -1071,6 +1093,7 @@
     scaled is number -> matrix_t -> matrix_t,
     minValue is matrix_t -> number,
     maxValue is matrix_t -> number,
+    total is matrix_t -> number,
     asRows is matrix_t -> list<vec.vector_t>, 
     asColumns is matrix_t -> list<vec.vector_t>,
     sum is list?<matrix_t> -> matrix_t,
--- a/src/may/matrix/test/test_matrix.yeti	Tue Mar 25 10:32:01 2014 +0000
+++ b/src/may/matrix/test/test_matrix.yeti	Fri Mar 28 13:16:12 2014 +0000
@@ -227,6 +227,29 @@
         compareMatrices (mat.mapColumns (vec.scaled 2) m') m''
 ),
 
+"minValue-\(name)": \(
+    compare (mat.minValue (fromRows [[1,2],[3,4],[5,-1]])) (-1) and
+    compare (mat.minValue (fromRows [[1,2],[3,0],[5,-1]])) (-1) and
+    compare (mat.minValue (fromRows [[1,2],[3,0],[5,1]])) 0 and
+    compare (mat.minValue (fromRows [[],[],[]])) 0
+),    
+
+"maxValue-\(name)": \(
+    compare (mat.maxValue (fromRows [[1,2],[3,4],[5,-1]])) 5 and
+    compare (mat.maxValue (fromRows [[1,2],[3,0],[5,-1]])) 5 and
+    compare (mat.maxValue (fromRows [[-1,-2],[-3,0],[-5,-1]])) 0 and
+    compare (mat.maxValue (fromRows [[-1,-2],[-3,-4],[-5,-1]])) (-1) and
+    compare (mat.maxValue (fromRows [[],[],[]])) 0
+),    
+
+"total-\(name)": \(
+    compare (mat.total (fromRows [[1,2],[3,4],[5,-1]])) 14 and
+    compare (mat.total (fromRows [[1,2],[3,0],[5,-1]])) 10 and
+    compare (mat.total (fromRows [[-1,-2],[-3,0],[-5,-1]])) (-12) and
+    compare (mat.total (fromRows [[-1,-2],[-3,-4],[-5,-1]])) (-16) and
+    compare (mat.total (fromRows [[],[],[]])) 0
+),    
+
 "sum-\(name)": \(
     compareMatrices
        (mat.sum [constMatrix 2 { rows = 3, columns = 4 },