changeset 590:7c33308cebf8

Make it permissible for mapRows to change the number of columns (if consistent) and mapColumns to change number of rows; test for same
author Chris Cannam
date Thu, 11 Sep 2014 11:11:11 +0100
parents 02fba8dc2a92
children eb27901664cd
files src/may/matrix.yeti src/may/matrix/test/test_matrix.yeti
diffstat 2 files changed, 26 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/may/matrix.yeti	Thu Sep 11 11:00:58 2014 +0100
+++ b/src/may/matrix.yeti	Thu Sep 11 11:11:11 2014 +0100
@@ -968,14 +968,10 @@
     fi;
 
 mapRows rf m =
-   (rows = map rf (asRows m);
-    if not empty? rows and vec.length (head rows) != width m then
-        failWith "Map function changes row length in mapRows: from \(width m) to \(vec.length (head rows))";
-    fi;
-    newMatrix (size m) (Rows (map rf (asRows m))));
+    fromRows (map rf (asRows m));
 
 mapColumns cf m =
-    newMatrix (size m) (Columns (map cf (asColumns m)));
+    fromColumns (map cf (asColumns m));
 
 format m =
     strJoin "\n"
--- a/src/may/matrix/test/test_matrix.yeti	Thu Sep 11 11:00:58 2014 +0100
+++ b/src/may/matrix/test/test_matrix.yeti	Thu Sep 11 11:11:11 2014 +0100
@@ -272,6 +272,18 @@
         compareMatrices (mat.mapRows (vec.scaled 2) m') m''
 ),
 
+"mapRows-extend-\(name)": \(
+    m = fromRows [[1,4],[0,5],[3,6]];
+    m' = mat.mapRows do r: vec.concat [r, r] done m;
+    compareMatrices m' (mat.concatHorizontal [ m, m ])
+),
+
+"mapRows-shrink-\(name)": \(
+    m = fromRows [[1,4],[0,5],[3,6]];
+    m' = mat.mapRows do r: vec.resizedTo 1 r done m;
+    compareMatrices m' (fromRows [[1],[0],[3]]);
+),
+
 "mapColumns-\(name)": \(
     m = fromRows [[1,4],[0,5],[3,6]];
     m' = fromColumns [[1,0,3],[4,5,6]];
@@ -280,6 +292,18 @@
         compareMatrices (mat.mapColumns (vec.scaled 2) m') m''
 ),
 
+"mapColumns-extend-\(name)": \(
+    m = fromColumns [[1,4],[0,5],[3,6]];
+    m' = mat.mapColumns do r: vec.concat [r, r] done m;
+    compareMatrices m' (mat.concatVertical [ m, m ])
+),
+
+"mapColumns-shrink-\(name)": \(
+    m = fromColumns [[1,4],[0,5],[3,6]];
+    m' = mat.mapColumns do r: vec.resizedTo 1 r done m;
+    compareMatrices m' (fromColumns [[1],[0],[3]]);
+),
+
 "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