changeset 591:eb27901664cd

Check for consistent lengths in fromRows/fromColumns; test for same in mapRows/mapColumns
author Chris Cannam
date Thu, 11 Sep 2014 11:25:50 +0100
parents 7c33308cebf8
children e7c5b55aab9a
files src/may/matrix.yeti src/may/matrix/test/test_matrix.yeti
diffstat 2 files changed, 40 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/may/matrix.yeti	Thu Sep 11 11:11:11 2014 +0100
+++ b/src/may/matrix.yeti	Thu Sep 11 11:25:50 2014 +0100
@@ -444,6 +444,9 @@
     equal' (==) vec.equal;
 
 fromRows rows =
+   (if any do r: vec.length r != vec.length (head rows) done rows then
+        failWith "Inconsistent row lengths in fromRows (\(map vec.length rows))";
+    fi;
     {
         size = { 
             rows = length rows, 
@@ -453,9 +456,12 @@
                 fi,
         },
         data = DenseRows (array rows)
-    };
+    });
 
 fromColumns cols =
+   (if any do c: vec.length c != vec.length (head cols) done cols then
+        failWith "Inconsistent column lengths in fromColumns (\(map vec.length cols))";
+    fi;
     {
         size = { 
             columns = length cols, 
@@ -465,7 +471,7 @@
                 fi,
         },
         data = DenseCols (array cols)
-    };
+    });
 
 fromLists data = 
     case data of
--- a/src/may/matrix/test/test_matrix.yeti	Thu Sep 11 11:11:11 2014 +0100
+++ b/src/may/matrix/test/test_matrix.yeti	Thu Sep 11 11:25:50 2014 +0100
@@ -284,6 +284,22 @@
     compareMatrices m' (fromRows [[1],[0],[3]]);
 ),
 
+"mapRows-fail-\(name)": \(
+    try
+        m = fromRows [[1,4],[0,5],[3,6]];
+        \() (mat.mapRows
+            do r: 
+                if vec.at r 0 == 1
+                then vec.concat [r, r] 
+                else vec.resizedTo 1 r
+                fi 
+            done m);
+        false
+    catch FailureException e:
+        true
+    yrt
+),
+
 "mapColumns-\(name)": \(
     m = fromRows [[1,4],[0,5],[3,6]];
     m' = fromColumns [[1,0,3],[4,5,6]];
@@ -304,6 +320,22 @@
     compareMatrices m' (fromColumns [[1],[0],[3]]);
 ),
 
+"mapColumns-fail-\(name)": \(
+    try
+        m = fromColumns [[1,4],[0,5],[3,6]];
+        \() (mat.mapColumns
+            do r: 
+                if vec.at r 0 == 1
+                then vec.concat [r, r] 
+                else vec.resizedTo 1 r
+                fi 
+            done m);
+        false
+    catch FailureException e:
+        true
+    yrt
+),
+
 "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