Bug#384195: [Pkg-octave-devel]

John W. Eaton jwe at bevo.che.wisc.edu
Tue Aug 22 18:33:16 UTC 2006


On 22-Aug-2006, Nicolas Guilbert wrote:

| Package: octave2.9
| Version: 2.9.7-2
| 
| Steps to reproduce:
| octave:1> cumsum(zeros(1,0))
| panic: Floating point exception -- stopping myself...

Please try the following patch.  It also fixes a similar problem with
the sum and prod functions.

| Would expect ans==0

I think the result should be an empty matrix of the same size as the
argument, which also has the advantage of being compatible with
Matlab.

Thanks,

jwe


liboctave/ChangeLog:

2006-08-22  John W. Eaton  <jwe at octave.org>

	* mx-inlines.cc (MX_ND_CUMULATIVE_OP): Correctly detect empty arrays.
	If array is empty, return value is same size as array.
	(MX_ND_REDUCTION): Correctly detect empty arrays.
	If array is empty, produce correctly sized return value.


Index: liboctave/mx-inlines.cc
===================================================================
RCS file: /cvs/octave/liboctave/mx-inlines.cc,v
retrieving revision 1.48
diff -u -u -r1.48 mx-inlines.cc
--- liboctave/mx-inlines.cc	10 Feb 2006 21:30:42 -0000	1.48
+++ liboctave/mx-inlines.cc	22 Aug 2006 18:24:17 -0000
@@ -372,24 +372,17 @@
   dim_vector dv = this->dims (); \
   int nd = this->ndims (); \
  \
-  int empty = true; \
+  int empty = false; \
  \
   for (int i = 0; i < nd; i++) \
     { \
-      if (dv(i) > 0) \
+      if (dv(i) == 0) \
         { \
-          empty = false; \
+          empty = true; \
           break; \
         } \
     } \
  \
-  if (empty) \
-    { \
-      dim_vector retval_dv (1, 1); \
-      retval.resize (retval_dv, INIT_VAL); \
-      return retval; \
-    } \
- \
   /* We need to find first non-singleton dim.  */ \
  \
   if (dim == -1) \
@@ -435,36 +428,39 @@
  \
   retval.resize (dv, INIT_VAL); \
  \
-  octave_idx_type nel = dv.numel (); \
- \
-  octave_idx_type k = 1; \
- \
-  for (octave_idx_type result_idx = 0; result_idx < nel; result_idx++) \
+  if (! empty) \
     { \
-      OCTAVE_QUIT; \
+      octave_idx_type nel = dv.numel (); \
  \
-      for (octave_idx_type j = 0; j < n_elts; j++) \
+      octave_idx_type k = 1; \
+ \
+      for (octave_idx_type result_idx = 0; result_idx < nel; result_idx++) \
 	{ \
-          OCTAVE_QUIT; \
+	  OCTAVE_QUIT; \
  \
-	  EVAL_EXPR; \
+          for (octave_idx_type j = 0; j < n_elts; j++) \
+	    { \
+              OCTAVE_QUIT; \
  \
-	  iter_idx += incr; \
-	} \
+	      EVAL_EXPR; \
  \
-      if (k == reset_at) \
-        { \
-	  base = next_base; \
-	  next_base += base_incr; \
-	  iter_idx = base; \
-	  k = 1; \
-        } \
-      else \
-	{ \
-	  base++; \
-	  iter_idx = base; \
-	  k++; \
-         } \
+	      iter_idx += incr; \
+	    } \
+ \
+          if (k == reset_at) \
+	    { \
+	      base = next_base; \
+	      next_base += base_incr; \
+	      iter_idx = base; \
+	      k = 1; \
+	    } \
+	  else \
+	    { \
+	      base++; \
+	      iter_idx = base; \
+	      k++; \
+	     } \
+	} \
     } \
  \
   retval.chop_trailing_singletons (); \
@@ -487,24 +483,17 @@
   dim_vector dv = this->dims (); \
   int nd = this->ndims (); \
  \
-  int empty = true; \
+  bool empty = false; \
  \
   for (int i = 0; i < nd; i++) \
     { \
-      if (dv(i) > 0) \
+      if (dv(i) == 0) \
         { \
-          empty = false; \
+          empty = true; \
           break; \
         } \
     } \
  \
-  if (empty) \
-    { \
-      dim_vector retval_dv (1, 1); \
-      retval.resize (retval_dv, INIT_VAL); \
-      return retval; \
-    } \
- \
   /* We need to find first non-singleton dim.  */ \
  \
   if (dim == -1) \
@@ -548,47 +537,50 @@
  \
   retval.resize (dv, INIT_VAL); \
  \
-  octave_idx_type nel = dv.numel () / n_elts; \
- \
-  octave_idx_type k = 1; \
- \
-  for (octave_idx_type i = 0; i < nel; i++) \
+  if (! empty) \
     { \
-      OCTAVE_QUIT; \
+      octave_idx_type nel = dv.numel () / n_elts; \
  \
-      ACC_TYPE prev_val = INIT_VAL; \
+      octave_idx_type k = 1; \
  \
-      for (octave_idx_type j = 0; j < n_elts; j++) \
+      for (octave_idx_type i = 0; i < nel; i++) \
 	{ \
-          OCTAVE_QUIT; \
+	  OCTAVE_QUIT; \
  \
-	  if (j == 0) \
+          ACC_TYPE prev_val = INIT_VAL; \
+ \
+	  for (octave_idx_type j = 0; j < n_elts; j++) \
 	    { \
-	      retval(iter_idx) = elem (iter_idx); \
-	      prev_val = retval(iter_idx); \
+	      OCTAVE_QUIT; \
+ \
+	      if (j == 0) \
+		{ \
+		  retval(iter_idx) = elem (iter_idx); \
+		  prev_val = retval(iter_idx); \
+		} \
+	      else \
+		{ \
+		  prev_val = prev_val OP elem (iter_idx); \
+		  retval(iter_idx) = prev_val; \
+		} \
+ \
+	      iter_idx += incr; \
 	    } \
-	  else \
+ \
+	  if (k == reset_at) \
 	    { \
-	      prev_val = prev_val OP elem (iter_idx); \
-	      retval(iter_idx) = prev_val; \
+	      base = next_base; \
+	      next_base += base_incr; \
+	      iter_idx = base; \
+	      k = 1; \
 	    } \
- \
-	  iter_idx += incr; \
+	  else \
+	    { \
+	      base++; \
+	      iter_idx = base; \
+	      k++; \
+	     } \
 	} \
- \
-      if (k == reset_at) \
-        { \
-	  base = next_base; \
-	  next_base += base_incr; \
-	  iter_idx = base; \
-	  k = 1; \
-        } \
-      else \
-	{ \
-	  base++; \
-	  iter_idx = base; \
-	  k++; \
-         } \
     } \
  \
   retval.chop_trailing_singletons (); \




More information about the Pkg-octave-devel mailing list