changeset 4554:78e34346f6fd

[project @ 2003-10-27 22:01:49 by jwe]
author jwe
date Mon, 27 Oct 2003 22:01:49 +0000
parents c7eb767505e9
children fe70d8074644
files 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.h
diffstat 7 files changed, 83 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Mon Oct 27 21:39:55 2003 +0000
+++ b/src/ChangeLog	Mon Oct 27 22:01:49 2003 +0000
@@ -1,3 +1,14 @@
+2003-10-27  Petter Risholm  <risholm@stud.ntnu.no>
+
+	* ov.h (octave_value::ndims): New function.
+	* ov-base.h (octave_base_value::ndims): Likewise.
+	* ov-base-scalar.h (octave_base_scalar<T>::ndims): Likewise.
+	* ov-base-mat.cc (octave_base_matrix<MT>::ndims): New function.
+	(octave_base_matrix<MT>::length): Move here from ov-base-mat.h.
+	Make it work for N-d arrays.
+	* ov-base-mat.h (octave_base_matrix<MT>::ndims): Provide decl.
+	* data.cc (Fndims): New built-in function.
+
 2003-10-27  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* DLD-FUNCTIONS/balance.cc, DLD-FUNCTIONS/qz.cc:
--- a/src/data.cc	Mon Oct 27 21:39:55 2003 +0000
+++ b/src/data.cc	Mon Oct 27 22:01:49 2003 +0000
@@ -688,6 +688,29 @@
   return retval;
 }
 
+DEFUN (ndims, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {} ndims (@var{a})\n\
+Returns the number of dimensions of array @var{a}.\n\
+For any array, the result will always be larger than or equal to 2.\n\
+Trailing singleton dimensions are not counted.\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  if (args.length () == 1)
+    {
+      int n_dims = args(0).ndims ();
+
+      if (! error_state)
+	retval = n_dims;
+    }
+  else
+    print_usage ("ndims");
+
+  return retval;
+}
+
 DEFUN (size, args, nargout,
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} size (@var{a}, @var{n})\n\
--- a/src/ov-base-mat.cc	Mon Oct 27 21:39:55 2003 +0000
+++ b/src/ov-base-mat.cc	Mon Oct 27 22:01:49 2003 +0000
@@ -187,6 +187,45 @@
 }
 
 template <class MT>
+int
+octave_base_matrix<MT>::length (void) const
+{
+  int retval = 0;
+
+  dim_vector dv = dims();
+      
+  for (int i = 0; i < dv.length (); i++)
+    if (dv(i) > retval)
+      retval = dv(i);
+
+  return retval;
+}
+
+template <class MT>
+int
+octave_base_matrix<MT>::ndims (void) const
+{
+  dim_vector dv = dims ();
+
+  int n_dims = dv.length ();
+     
+   // Remove trailing singleton dimensions
+   for (int i = n_dims; i > 2; i--)
+     {
+       if (dv(i-1) == 1)
+	 n_dims--;
+       else
+	 break;
+     }
+   
+   // The result is always >= 2
+   if (n_dims < 2)
+     n_dims = 2;
+
+   return n_dims;
+}
+
+template <class MT>
 bool
 octave_base_matrix<MT>::print_as_scalar (void) const
 {
--- a/src/ov-base-mat.h	Mon Oct 27 21:39:55 2003 +0000
+++ b/src/ov-base-mat.h	Mon Oct 27 22:01:49 2003 +0000
@@ -95,18 +95,12 @@
 
   dim_vector dims (void) const { return matrix.dims (); }
 
-  // XXX FIXME XXX 
-  int length (void) const
-    {
-      int r = rows ();
-      int c = columns ();
-
-      return (r == 0 || c == 0) ? 0 : ((r > c) ? r : c);
-    }
-
   octave_value all (int dim = 0) const { return matrix.all (dim); }
   octave_value any (int dim = 0) const { return matrix.any (dim); }
 
+  int length (void) const;
+  int ndims (void) const;
+
   bool is_matrix_type (void) const { return true; }
 
   bool is_numeric_type (void) const { return true; }
--- a/src/ov-base-scalar.h	Mon Oct 27 21:39:55 2003 +0000
+++ b/src/ov-base-scalar.h	Mon Oct 27 22:01:49 2003 +0000
@@ -80,6 +80,8 @@
 
   int length (void) const { return 1; }
 
+  int ndims (void) const { return 2; }
+
   bool is_constant (void) const { return true; }
 
   bool is_defined (void) const { return true; }
--- a/src/ov-base.h	Mon Oct 27 21:39:55 2003 +0000
+++ b/src/ov-base.h	Mon Oct 27 22:01:49 2003 +0000
@@ -102,6 +102,8 @@
 
   int length (void) const { return -1; }
 
+  int ndims (void) const { return -1; }
+
   bool is_defined (void) const { return false; }
 
   bool is_cell (void) const { return false; }
--- a/src/ov.h	Mon Oct 27 21:39:55 2003 +0000
+++ b/src/ov.h	Mon Oct 27 22:01:49 2003 +0000
@@ -328,6 +328,9 @@
   virtual int length (void) const
     { return rep->length (); }
 
+  virtual int ndims (void) const
+    { return rep->ndims (); }
+
   // 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().