changeset 18145:a86d608c413c

Return empty matrix rather than issuing error when requested diagonal is out of range. * data.cc: Add %!tests for new behavior. * Array.cc (diag): Return 0x1 empty matrix when diagonal is out of range. * DiagArray2.cc (extract_diag): Return 0x1 empty matrix when diagonal is out of range. * Sparse.cc (diag): Return 0x1 empty matrix when diagonal is out of range.
author Marco Caliari <marco.caliari@univr.it>
date Tue, 17 Dec 2013 09:00:34 -0800
parents b5970988ccff
children 190ef1764d30
files libinterp/corefcn/data.cc liboctave/array/Array.cc liboctave/array/DiagArray2.cc liboctave/array/Sparse.cc
diffstat 4 files changed, 11 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/data.cc	Tue Dec 17 13:28:31 2013 +0000
+++ b/libinterp/corefcn/data.cc	Tue Dec 17 09:00:34 2013 -0800
@@ -1354,6 +1354,11 @@
 %!assert (diag ({1}, 2, 3), {1,[],[]; [],[],[]});
 %!assert (diag ({1,2}, 3, 4), {1,[],[],[]; [],2,[],[]; [],[],[],[]});
 
+## Test out-of-range diagonals
+%!assert (diag (ones (3,3), 4), zeros (0, 1))
+%!assert (diag (cell (3,3), 4), cell (0, 1))
+%!assert (diag (sparse (ones (3,3)), 4), sparse (zeros (0, 1)))
+
 %% Test input validation
 %!error <Invalid call to diag> diag ()
 %!error <Invalid call to diag> diag (1,2,3,4)
--- a/liboctave/array/Array.cc	Tue Dec 17 13:28:31 2013 +0000
+++ b/liboctave/array/Array.cc	Tue Dec 17 09:00:34 2013 -0800
@@ -2571,9 +2571,8 @@
                     d.xelem (i) = elem (i, i);
                 }
             }
-          else
-            (*current_liboctave_error_handler)
-              ("diag: requested diagonal out of range");
+          else  // Matlab returns [] 0x1 for out-of-range diagonal
+            d.resize (dim_vector (0, 1));
         }
       else
         {
--- a/liboctave/array/DiagArray2.cc	Tue Dec 17 13:28:31 2013 +0000
+++ b/liboctave/array/DiagArray2.cc	Tue Dec 17 09:00:34 2013 -0800
@@ -66,9 +66,8 @@
     d = Array<T> (dim_vector (std::min (cols () - k, rows ()), 1), T ());
   else if (k < 0 && -k < rows ())
     d = Array<T> (dim_vector (std::min (rows () + k, cols ()), 1), T ());
-  else
-    (*current_liboctave_error_handler)
-      ("diag: requested diagonal out of range");
+  else  // Matlab returns [] 0x1 for out-of-range diagonal
+    d.resize (dim_vector (0, 1));
 
   return d;
 }
--- a/liboctave/array/Sparse.cc	Tue Dec 17 13:28:31 2013 +0000
+++ b/liboctave/array/Sparse.cc	Tue Dec 17 09:00:34 2013 -0800
@@ -2490,9 +2490,8 @@
                 }
             }
         }
-      else
-        (*current_liboctave_error_handler)
-          ("diag: requested diagonal out of range");
+      else  // Matlab returns [] 0x1 for out-of-range diagonal
+        d = Sparse<T> (0, 1, 0);
     }
   else if (nnr != 0 && nnc != 0)
     {