# HG changeset patch # User jwe # Date 1067368423 0 # Node ID 802818bfac91e9f23e606bd523180f34399e3588 # Parent 239df9c618ef94f7aeb654e1c5f2b8b0c8ccab7c [project @ 2003-10-28 19:13:43 by jwe] diff --git a/liboctave/Array.h b/liboctave/Array.h --- a/liboctave/Array.h +++ b/liboctave/Array.h @@ -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 transpose (void) const; const T *data (void) const { return rep->data; } diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,10 @@ +2003-10-28 John W. Eaton + + * dim-vector.h (dim_vector::any_zero): New function. + (dim_vector::str): New default arg, sep. + + * Array.h (Array::numel): New function. + 2003-10-27 Petter Risholm * mx-inlines.cc (MX_ND_ALL_EXPR, MX_ND_ANY_EXPR, diff --git a/liboctave/dim-vector.h b/liboctave/dim-vector.h --- a/liboctave/dim-vector.h +++ b/liboctave/dim-vector.h @@ -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: *** */ - diff --git a/liboctave/mx-inlines.cc b/liboctave/mx-inlines.cc --- a/liboctave/mx-inlines.cc +++ b/liboctave/mx-inlines.cc @@ -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 */ \ diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,7 @@ +2003-10-28 John W. Eaton + + * general/numel.m: Delete. + 2003-10-15 John W. Eaton * miscellaneous/horzcat.m, miscellaneous/vartcat.m: New files. diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,18 @@ +2003-10-28 John W. Eaton + + * 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::numel): Likewise. + * ov-base-scalar.h (octave_base_scalar::numel): Likewise. + 2003-10-27 Petter Risholm * ov-base-mat.cc (octave_base_matrix::is_true): diff --git a/src/data.cc b/src/data.cc --- a/src/data.cc +++ b/src/data.cc @@ -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"); diff --git a/src/ov-base-mat.cc b/src/ov-base-mat.cc --- a/src/ov-base-mat.cc +++ b/src/ov-base-mat.cc @@ -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; diff --git a/src/ov-base-mat.h b/src/ov-base-mat.h --- a/src/ov-base-mat.h +++ b/src/ov-base-mat.h @@ -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; } diff --git a/src/ov-base-scalar.h b/src/ov-base-scalar.h --- a/src/ov-base-scalar.h +++ b/src/ov-base-scalar.h @@ -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; } diff --git a/src/ov-base.h b/src/ov-base.h --- a/src/ov-base.h +++ b/src/ov-base.h @@ -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); } diff --git a/src/ov-list.h b/src/ov-list.h --- a/src/ov-list.h +++ b/src/ov-list.h @@ -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; } diff --git a/src/ov.h b/src/ov.h --- a/src/ov.h +++ b/src/ov.h @@ -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 diff --git a/src/pr-output.cc b/src/pr-output.cc --- a/src/pr-output.cc +++ b/src/pr-output.cc @@ -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 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 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 (ndims); \ + nm += OSSTREAM_STR (buf); \ + \ + OSSTREAM_FREEZE (buf); \ + } \ \ - idx(0) = idx_vector (':'); \ - idx(1) = idx_vector (':'); \ + Array 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 (nda.index (idx), nr, nc)); \ + octave_value page \ + = MAT_T (Array2 (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)