changeset 277:678477bf617c

(clumsily) make slice functions handle out-of-range arguments properly
author Chris Cannam
date Sat, 25 May 2013 18:41:53 +0100
parents 39b5f3fed12d
children 704c58ab4598
files yetilab/matrix.yeti yetilab/matrix/test/test_matrix.yeti
diffstat 2 files changed, 36 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/yetilab/matrix.yeti	Sat May 25 18:31:10 2013 +0100
+++ b/yetilab/matrix.yeti	Sat May 25 18:41:53 2013 +0100
@@ -641,20 +641,40 @@
 concat direction mm =
     concat' direction (filter do mat: not (isZeroSize? mat) done mm);
 
+//!!! next two v. clumsy
+
 //!!! doc note: argument order chosen for consistency with std module slice
+//!!! NB always returns dense matrix, should have sparse version
 rowSlice m start end = //!!! doc: storage order same as input
-    if isRowMajor? m then
-        DenseRows (array (map ((flip getRow) m) [start .. end - 1]))
-    else 
-        DenseCols (array (map do v: vec.slice v start end done (asColumns m)))
+    if start < 0 then rowSlice m 0 end
+    elif start > height m then rowSlice m (height m) end
+    else
+        if end < start then rowSlice m start start
+        elif end > height m then rowSlice m start (height m)
+        else
+            if isRowMajor? m then
+                DenseRows (array (map ((flip getRow) m) [start .. end - 1]))
+            else 
+                DenseCols (array (map do v: vec.slice v start end done (asColumns m)))
+            fi;
+        fi;
     fi;
 
 //!!! doc note: argument order chosen for consistency with std module slice
+//!!! NB always returns dense matrix, should have sparse version
 columnSlice m start end = //!!! doc: storage order same as input
-    if not isRowMajor? m then
-        DenseCols (array (map ((flip getColumn) m) [start .. end - 1]))
-    else 
-        DenseRows (array (map do v: vec.slice v start end done (asRows m)))
+    if start < 0 then columnSlice m 0 end
+    elif start > width m then columnSlice m (width m) end
+    else
+        if end < start then columnSlice m start start
+        elif end > width m then columnSlice m start (width m)
+        else
+            if not isRowMajor? m then
+                DenseCols (array (map ((flip getColumn) m) [start .. end - 1]))
+            else 
+                DenseRows (array (map do v: vec.slice v start end done (asRows m)))
+            fi;
+        fi;
     fi;
 
 resizedTo newsize m =
--- a/yetilab/matrix/test/test_matrix.yeti	Sat May 25 18:31:10 2013 +0100
+++ b/yetilab/matrix/test/test_matrix.yeti	Sat May 25 18:41:53 2013 +0100
@@ -448,13 +448,19 @@
 "rowSlice-\(name)": \(
     compareMatrices
        (mat.rowSlice (newMatrix (RowMajor ()) [[1,0],[3,4],[0,6],[7,8]]) 1 3)
-       (newMatrix (RowMajor ()) [[3,4],[0,6]])
+       (newMatrix (RowMajor ()) [[3,4],[0,6]]) and
+        compareMatrices
+           (mat.rowSlice (newMatrix (RowMajor ()) [[1,0],[3,4],[0,6],[7,8]]) 3 6)
+           (newMatrix (RowMajor ()) [[7,8]])
 ),
 
 "columnSlice-\(name)": \(
     compareMatrices
        (mat.columnSlice (newMatrix (RowMajor ()) [[1,0,3,4],[0,6,7,8]]) 1 3)
-       (newMatrix (RowMajor ()) [[0,3],[6,7]])
+       (newMatrix (RowMajor ()) [[0,3],[6,7]]) and
+        compareMatrices
+           (mat.columnSlice (newMatrix (RowMajor ()) [[1,0,3,4],[0,6,7,8]]) 2 5)
+           (newMatrix (RowMajor ()) [[3,4],[7,8]])
 ),
 
 "density-\(name)": \(