changeset 17682:93e272018df2

Correct dimesion of return values form sparse min/max function with empty matrices (bug #40295) * dSparse.cc (SparseMatrix SparseMatrix::max (Array<octave_idx-type>&, int)) : Specialize return values if called with an empty matrix (SparseMatrix SparseMatrix::min (Array<octave_idx-type>&, int)) : ditto * CSparse.cc (SparseComplexMatrix SparseComplexMatrix::max ( Array<octave_idx-type>&, int)) : Specialize return values if called with an empty matrix (SparseComplexMatrix SparseComplexMatrix::min (Array<octave_idx-type>&, int)) : ditto
author David Bateman <dbateman@free.fr>
date Fri, 18 Oct 2013 01:36:31 +0200
parents f6fded839513
children 279d4bde41d5
files liboctave/array/CSparse.cc liboctave/array/dSparse.cc
diffstat 2 files changed, 59 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/array/CSparse.cc	Thu Oct 17 16:05:05 2013 -0700
+++ b/liboctave/array/CSparse.cc	Fri Oct 18 01:36:31 2013 +0200
@@ -261,15 +261,23 @@
   octave_idx_type nr = dv(0);
   octave_idx_type nc = dv(1);
 
-  if (nr == 0 || nc == 0 || dim >= dv.length ())
-    return result;
+
+  if (dim >= dv.length ())
+    {
+      idx_arg.resize (dim_vector (nr, nc), 0);
+      return *this;
+    }
 
   if (dim < 0)
     dim = dv.first_non_singleton ();
 
   if (dim == 0)
     {
-      idx_arg.clear (1, nc);
+      idx_arg.resize (dim_vector (nr == 0 ? 0 : 1, nc), 0);
+
+      if (nr == 0 || nc == 0 || dim >= dv.length ())
+        return SparseComplexMatrix (nr == 0 ? 0 : 1, nc);
+
       octave_idx_type nel = 0;
       for (octave_idx_type j = 0; j < nc; j++)
         {
@@ -329,6 +337,12 @@
     }
   else
     {
+      idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0);
+
+      if (nr == 0 || nc == 0 || dim >= dv.length ())
+        return SparseComplexMatrix (nr, nc == 0 ? 0 : 1);
+
+
       idx_arg.resize (dim_vector (nr, 1), 0);
 
       for (octave_idx_type i = cidx (0); i < cidx (1); i++)
@@ -415,15 +429,22 @@
   octave_idx_type nr = dv(0);
   octave_idx_type nc = dv(1);
 
-  if (nr == 0 || nc == 0 || dim >= dv.length ())
-    return result;
+  if (dim >= dv.length ())
+    {
+      idx_arg.resize (dim_vector (nr, nc), 0);
+      return *this;
+    }
 
   if (dim < 0)
     dim = dv.first_non_singleton ();
 
   if (dim == 0)
     {
-      idx_arg.clear (1, nc);
+      idx_arg.resize (dim_vector (nr == 0 ? 0 : 1, nc), 0);
+
+      if (nr == 0 || nc == 0 || dim >= dv.length ())
+        return SparseComplexMatrix (nr == 0 ? 0 : 1, nc);
+
       octave_idx_type nel = 0;
       for (octave_idx_type j = 0; j < nc; j++)
         {
@@ -483,7 +504,10 @@
     }
   else
     {
-      idx_arg.resize (dim_vector (nr, 1), 0);
+      idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0);
+
+      if (nr == 0 || nc == 0 || dim >= dv.length ())
+        return SparseComplexMatrix (nr, nc == 0 ? 0 : 1);
 
       for (octave_idx_type i = cidx (0); i < cidx (1); i++)
         idx_arg.elem (ridx (i)) = -1;
--- a/liboctave/array/dSparse.cc	Thu Oct 17 16:05:05 2013 -0700
+++ b/liboctave/array/dSparse.cc	Fri Oct 18 01:36:31 2013 +0200
@@ -279,15 +279,22 @@
   octave_idx_type nr = dv(0);
   octave_idx_type nc = dv(1);
 
-  if (nr == 0 || nc == 0 || dim >= dv.length ())
-    return result;
+  if (dim >= dv.length ())
+    {
+      idx_arg.resize (dim_vector (nr, nc), 0);
+      return *this;
+    }
 
   if (dim < 0)
     dim = dv.first_non_singleton ();
 
   if (dim == 0)
     {
-      idx_arg.clear (1, nc);
+      idx_arg.resize (dim_vector (nr == 0 ? 0 : 1, nc), 0);
+
+      if (nr == 0 || nc == 0 || dim >= dv.length ())
+        return SparseMatrix (nr == 0 ? 0 : 1, nc);
+
       octave_idx_type nel = 0;
       for (octave_idx_type j = 0; j < nc; j++)
         {
@@ -341,7 +348,10 @@
     }
   else
     {
-      idx_arg.resize (dim_vector  (nr, 1), 0);
+      idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0);
+
+      if (nr == 0 || nc == 0 || dim >= dv.length ())
+        return SparseMatrix (nr, nc == 0 ? 0 : 1);
 
       for (octave_idx_type i = cidx (0); i < cidx (1); i++)
         idx_arg.elem (ridx (i)) = -1;
@@ -427,15 +437,22 @@
   octave_idx_type nr = dv(0);
   octave_idx_type nc = dv(1);
 
-  if (nr == 0 || nc == 0 || dim >= dv.length ())
-    return result;
+  if (dim >= dv.length ())
+    {
+      idx_arg.resize (dim_vector (nr, nc), 0);
+      return *this;
+    }
 
   if (dim < 0)
     dim = dv.first_non_singleton ();
 
   if (dim == 0)
     {
-      idx_arg.clear (1, nc);
+      idx_arg.resize (dim_vector (nr == 0 ? 0 : 1, nc), 0);
+
+      if (nr == 0 || nc == 0 || dim >= dv.length ())
+        return SparseMatrix (nr == 0 ? 0 : 1, nc);
+
       octave_idx_type nel = 0;
       for (octave_idx_type j = 0; j < nc; j++)
         {
@@ -489,7 +506,10 @@
     }
   else
     {
-      idx_arg.resize (dim_vector (nr, 1), 0);
+      idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0);
+
+      if (nr == 0 || nc == 0 || dim >= dv.length ())
+        return SparseMatrix (nr, nc == 0 ? 0 : 1);
 
       for (octave_idx_type i = cidx (0); i < cidx (1); i++)
         idx_arg.elem (ridx (i)) = -1;