changeset 4559:802818bfac91

[project @ 2003-10-28 19:13:43 by jwe]
author jwe
date Tue, 28 Oct 2003 19:13:43 +0000
parents 239df9c618ef
children 661246155bbc
files liboctave/Array.h liboctave/ChangeLog liboctave/dim-vector.h liboctave/mx-inlines.cc scripts/ChangeLog src/ChangeLog src/data.cc src/ov-base-mat.cc src/ov-base-mat.h src/ov-base-scalar.h src/ov-base.h src/ov-list.h src/ov.h src/pr-output.cc
diffstat 14 files changed, 166 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/Array.h	Tue Oct 28 18:48:07 2003 +0000
+++ b/liboctave/Array.h	Tue Oct 28 19:13:43 2003 +0000
@@ -234,6 +234,7 @@
   int capacity (void) const { return rep->length (); }
   int length (void) const { return capacity (); }
   int nelem (void) const { return capacity (); }
+  int numel (void) const { return nelem (); }
 
   int dim1 (void) const { return dimensions(0); }
   int dim2 (void) const { return dimensions(1); }
@@ -441,6 +442,8 @@
 
   bool is_square (void) const { return (dim1 () == dim2 ()); }
 
+  bool is_empty (void) const { return numel () == 0; }
+
   Array<T> transpose (void) const;
 
   const T *data (void) const { return rep->data; }
--- a/liboctave/ChangeLog	Tue Oct 28 18:48:07 2003 +0000
+++ b/liboctave/ChangeLog	Tue Oct 28 19:13:43 2003 +0000
@@ -1,3 +1,10 @@
+2003-10-28  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* dim-vector.h (dim_vector::any_zero): New function.
+	(dim_vector::str): New default arg, sep.
+
+	* Array.h (Array<T>::numel): New function.
+
 2003-10-27  Petter Risholm  <risholm@stud.ntnu.no>
 
 	* mx-inlines.cc (MX_ND_ALL_EXPR, MX_ND_ANY_EXPR,
--- a/liboctave/dim-vector.h	Tue Oct 28 18:48:07 2003 +0000
+++ b/liboctave/dim-vector.h	Tue Oct 28 19:13:43 2003 +0000
@@ -199,7 +199,7 @@
   }
 
 
-  std::string str (void) const
+  std::string str (char sep = 'x') const
   {
     OSSTREAM buf;
 
@@ -208,7 +208,7 @@
 	buf << elem (i);
 
 	if (i < length () - 1)
-	  buf << "x";
+	  buf << sep;
       }
 
     buf << OSSTREAM_ENDS;
@@ -235,6 +235,22 @@
 
     return retval;
   }
+
+  bool any_zero (void) const
+  {
+    bool retval = false;
+
+    for (int i = 0; i < length (); i++)
+      {
+	if (elem (i) == 0)
+	  {
+	    retval = true;
+	    break;
+	  }
+      }
+
+    return retval;
+  }
 };
 
 static inline bool
@@ -275,4 +291,3 @@
 ;;; mode: C++ ***
 ;;; End: ***
 */
-
--- a/liboctave/mx-inlines.cc	Tue Oct 28 18:48:07 2003 +0000
+++ b/liboctave/mx-inlines.cc	Tue Oct 28 19:13:43 2003 +0000
@@ -384,12 +384,13 @@
  \
   dim_vector dv = dims (); \
  /* Check to see if we have a empty matrix: [] */ \
-  if (dv.length () == 2 && dv (0) == 0 && dv (1) == 0) \
-    { \
-      dim_vector dv_temp (1,1); \
-      retval.resize (dv_temp,false); \
-      return retval; \
-    } \
+  for (int i = 0; i < dv.length (); i++)\
+    if (dv (i) == 0) \
+      { \
+        dim_vector dv_temp (1, 1); \
+        retval.resize (dv_temp, false); \
+        return retval; \
+      } \
   if (dim == -1)/* We need to find first non-singleton dim */ \
     { \
       /* Be sure to return a scalar if we have a vector */ \
--- a/scripts/ChangeLog	Tue Oct 28 18:48:07 2003 +0000
+++ b/scripts/ChangeLog	Tue Oct 28 19:13:43 2003 +0000
@@ -1,3 +1,7 @@
+2003-10-28  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* general/numel.m: Delete.
+
 2003-10-15  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* miscellaneous/horzcat.m, miscellaneous/vartcat.m: New files.
--- a/src/ChangeLog	Tue Oct 28 18:48:07 2003 +0000
+++ b/src/ChangeLog	Tue Oct 28 19:13:43 2003 +0000
@@ -1,3 +1,18 @@
+2003-10-28  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* pr-output.cc 	(print_empty_nd_array): New static function.
+	(PRINT_ND_ARRAY): Use it.
+
+	* ov.h (octave_value::is_empty): No longer virtual.  Return value
+	based on numel.
+	* data.cc (Fisempty): Use it.
+	(Fnumel): New function.
+
+	* ov.h (octave_value::numel): New function.
+	* ov-base.h (octave_base_value::numel): Likewise.
+	* ov-base-mat.h (octave_base_matrix<T>::numel): Likewise.
+	* ov-base-scalar.h  (octave_base_scalar<T>::numel): Likewise.
+
 2003-10-27  Petter Risholm  <risholm@stud.ntnu.no>
 
 	* ov-base-mat.cc (octave_base_matrix<MT>::is_true):
--- a/src/data.cc	Tue Oct 28 18:48:07 2003 +0000
+++ b/src/data.cc	Tue Oct 28 19:13:43 2003 +0000
@@ -711,6 +711,32 @@
   return retval;
 }
 
+DEFUN (numel, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {} numel (@var{a})\n\
+Returns the number of elements in the object @var{a}.\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  if (args.length () == 1)
+    {
+      int numel = args(0).numel ();
+
+      if (! error_state)
+	{
+	  if (numel < 0)
+	    numel = 0;
+
+	  retval = numel;
+	}
+    }
+  else
+    print_usage ("numel");
+
+  return retval;
+}
+
 DEFUN (size, args, nargout,
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} size (@var{a}, @var{n})\n\
@@ -877,14 +903,7 @@
   octave_value retval = false;
 
   if (args.length () == 1)
-    {
-      octave_value arg = args(0);
-
-      if (arg.is_matrix_type ())
-	retval = (arg.rows () == 0 || arg.columns () == 0);
-      else if (arg.is_list () || arg.is_string ())
-	retval = (arg.length () == 0);
-    }
+    retval = args(0).is_empty ();
   else
     print_usage ("isempty");
 
--- a/src/ov-base-mat.cc	Tue Oct 28 18:48:07 2003 +0000
+++ b/src/ov-base-mat.cc	Tue Oct 28 19:13:43 2003 +0000
@@ -215,7 +215,8 @@
 
   int n_dims = dv.length ();
      
-   // Remove trailing singleton dimensions
+   // Remove trailing singleton dimensions.
+
    for (int i = n_dims; i > 2; i--)
      {
        if (dv(i-1) == 1)
@@ -224,7 +225,8 @@
 	 break;
      }
    
-   // The result is always >= 2
+   // The result is always >= 2.
+
    if (n_dims < 2)
      n_dims = 2;
 
--- a/src/ov-base-mat.h	Tue Oct 28 18:48:07 2003 +0000
+++ b/src/ov-base-mat.h	Tue Oct 28 19:13:43 2003 +0000
@@ -99,8 +99,11 @@
   octave_value any (int dim = 0) const { return matrix.any (dim); }
 
   int length (void) const;
+
   int ndims (void) const;
 
+  int numel (void) const { return matrix.numel (); }
+
   bool is_matrix_type (void) const { return true; }
 
   bool is_numeric_type (void) const { return true; }
--- a/src/ov-base-scalar.h	Tue Oct 28 18:48:07 2003 +0000
+++ b/src/ov-base-scalar.h	Tue Oct 28 19:13:43 2003 +0000
@@ -82,6 +82,8 @@
 
   int ndims (void) const { return 2; }
 
+  int numel (void) const { return 1; }
+
   bool is_constant (void) const { return true; }
 
   bool is_defined (void) const { return true; }
--- a/src/ov-base.h	Tue Oct 28 18:48:07 2003 +0000
+++ b/src/ov-base.h	Tue Oct 28 19:13:43 2003 +0000
@@ -104,6 +104,8 @@
 
   int ndims (void) const { return -1; }
 
+  int numel (void) const { return -1; }
+
   bool is_defined (void) const { return false; }
 
   bool is_cell (void) const { return false; }
@@ -160,9 +162,6 @@
 
   bool is_true (void) const { return false; }
 
-  bool is_empty (void) const
-    { return (rows () == 0 || columns () == 0); }
-
   bool is_zero_by_zero (void) const
     { return (rows () == 0 && columns () == 0); }
 
--- a/src/ov-list.h	Tue Oct 28 18:48:07 2003 +0000
+++ b/src/ov-list.h	Tue Oct 28 19:13:43 2003 +0000
@@ -87,6 +87,8 @@
 
   int length (void) const { return lst.length (); }
 
+  int nelem (void) const { return length (); }
+
   bool is_defined (void) const { return true; }
 
   bool is_constant (void) const { return true; }
--- a/src/ov.h	Tue Oct 28 18:48:07 2003 +0000
+++ b/src/ov.h	Tue Oct 28 19:13:43 2003 +0000
@@ -331,6 +331,9 @@
   virtual int ndims (void) const
     { return rep->ndims (); }
 
+  virtual int numel (void) const
+    { return rep->numel (); }
+
   // Does this constant have a type?  Both of these are provided since
   // it is sometimes more natural to write is_undefined() instead of
   // ! is_defined().
@@ -341,6 +344,9 @@
   bool is_undefined (void) const
     { return ! is_defined (); }
 
+  bool is_empty (void) const
+    { return numel () == 0; }
+
   virtual bool is_cell (void) const
     { return rep->is_cell (); }
 
@@ -425,11 +431,6 @@
   virtual bool is_true (void) const
     { return rep->is_true (); }
 
-  // Is at least one of the dimensions of this constant zero?
-
-  virtual bool is_empty (void) const
-    { return rep->is_empty (); }
-
   // Are the dimensions of this constant zero by zero?
 
   virtual bool is_zero_by_zero (void) const
--- a/src/pr-output.cc	Tue Oct 28 18:48:07 2003 +0000
+++ b/src/pr-output.cc	Tue Oct 28 19:13:43 2003 +0000
@@ -1273,12 +1273,30 @@
   else
     {
       os << "[]";
+
       if (Vprint_empty_dimensions)
 	os << "(" << nr << "x" << nc << ")";
     }
 }
 
 static void
+print_empty_nd_array (std::ostream& os, const dim_vector& dims,
+		      bool pr_as_read_syntax)
+{
+  assert (dims.any_zero ());
+
+  if (pr_as_read_syntax)
+    os << "zeros (" << dims.str (',') << ")";
+  else
+    {
+      os << "[]";
+
+      if (Vprint_empty_dimensions)
+	os << "(" << dims.str () << ")";
+    }
+}
+
+static void
 pr_scale_header (std::ostream& os, double scale)
 {
   if (Vfixed_point_format && ! print_g && scale != 1.0)
@@ -1488,63 +1506,69 @@
 #define PRINT_ND_ARRAY(os, nda, NDA_T, ELT_T, MAT_T) \
   do \
     { \
-      int ndims = nda.ndims (); \
- \
-      dim_vector dims = nda.dims (); \
- \
-      Array<int> ra_idx (ndims, 0); \
+      if (nda.is_empty ()) \
+        print_empty_nd_array (os, nda.dims (), pr_as_read_syntax); \
+      else \
+        { \
  \
-      int m = 1; \
+          int ndims = nda.ndims (); \
+ \
+          dim_vector dims = nda.dims (); \
  \
-      for (int i = 2; i < ndims; i++) \
-	m *= dims(i); \
+          Array<int> ra_idx (ndims, 0); \
  \
-      int nr = dims(0); \
-      int nc = dims(1); \
+          int m = 1; \
  \
-      for (int i = 0; i < m; i++) \
-	{ \
-	  std::string nm = "ans"; \
+          for (int i = 2; i < ndims; i++) \
+            m *= dims(i); \
+ \
+          int nr = dims(0); \
+          int nc = dims(1); \
  \
-	  if (m > 1) \
-	    { \
-	      nm += "(:,:,"; \
+          for (int i = 0; i < m; i++) \
+            { \
+              std::string nm = "ans"; \
  \
-	      OSSTREAM buf; \
+              if (m > 1) \
+                { \
+                  nm += "(:,:,"; \
  \
-	      for (int k = 2; k < ndims; k++) \
-		{ \
-		  buf << ra_idx(k) + 1; \
+                  OSSTREAM buf; \
  \
-		  if (k < ndims - 1) \
-		    buf << ","; \
-		  else \
-		    buf << ")"; \
-		} \
+                  for (int k = 2; k < ndims; k++) \
+                    { \
+                      buf << ra_idx(k) + 1; \
  \
-	      buf << OSSTREAM_ENDS; \
- \
-	      nm += OSSTREAM_STR (buf); \
+                      if (k < ndims - 1) \
+                        buf << ","; \
+                      else \
+                        buf << ")"; \
+                    } \
  \
-	      OSSTREAM_FREEZE (buf); \
-	    } \
+                  buf << OSSTREAM_ENDS; \
  \
-	  Array<idx_vector> idx (ndims); \
+                  nm += OSSTREAM_STR (buf); \
+ \
+                  OSSTREAM_FREEZE (buf); \
+                } \
  \
-	  idx(0) = idx_vector (':'); \
-	  idx(1) = idx_vector (':'); \
+              Array<idx_vector> idx (ndims); \
  \
-	  for (int k = 2; k < ndims; k++) \
-	    idx(k) = idx_vector (ra_idx(k) + 1); \
+              idx(0) = idx_vector (':'); \
+              idx(1) = idx_vector (':'); \
+ \
+              for (int k = 2; k < ndims; k++) \
+                idx(k) = idx_vector (ra_idx(k) + 1); \
  \
-          octave_value page \
-            = MAT_T (Array2<ELT_T> (nda.index (idx), nr, nc)); \
+              octave_value page \
+                = MAT_T (Array2<ELT_T> (nda.index (idx), nr, nc)); \
+ \
+              page.print_with_name (os, nm); \
  \
-	  page.print_with_name (os, nm); \
- \
-	  if (i < m) \
-	    NDA_T::increment_index (ra_idx, dims, 2); \
-	} \
+              if (i < m) \
+                NDA_T::increment_index (ra_idx, dims, 2); \
+            } \
+        } \
     } \
   while (0)