# HG changeset patch # User jwe # Date 1067292109 0 # Node ID 78e34346f6fd0f45c9efc752ceaa015e0bd6abca # Parent c7eb767505e96c0e53c8bf4abbc0984c959cfc7f [project @ 2003-10-27 22:01:49 by jwe] diff -r c7eb767505e9 -r 78e34346f6fd src/ChangeLog --- 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 + + * ov.h (octave_value::ndims): New function. + * ov-base.h (octave_base_value::ndims): Likewise. + * ov-base-scalar.h (octave_base_scalar::ndims): Likewise. + * ov-base-mat.cc (octave_base_matrix::ndims): New function. + (octave_base_matrix::length): Move here from ov-base-mat.h. + Make it work for N-d arrays. + * ov-base-mat.h (octave_base_matrix::ndims): Provide decl. + * data.cc (Fndims): New built-in function. + 2003-10-27 John W. Eaton * DLD-FUNCTIONS/balance.cc, DLD-FUNCTIONS/qz.cc: diff -r c7eb767505e9 -r 78e34346f6fd src/data.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\ diff -r c7eb767505e9 -r 78e34346f6fd src/ov-base-mat.cc --- 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 +int +octave_base_matrix::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 +int +octave_base_matrix::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 bool octave_base_matrix::print_as_scalar (void) const { diff -r c7eb767505e9 -r 78e34346f6fd src/ov-base-mat.h --- 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; } diff -r c7eb767505e9 -r 78e34346f6fd src/ov-base-scalar.h --- 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; } diff -r c7eb767505e9 -r 78e34346f6fd src/ov-base.h --- 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; } diff -r c7eb767505e9 -r 78e34346f6fd src/ov.h --- 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().