# HG changeset patch # User jwe # Date 1067317330 0 # Node ID 773a21e4fce8a52f909100cd36c71802741668ab # Parent fe70d807464479a444b8b710cbcc8608640deac9 [project @ 2003-10-28 05:02:10 by jwe] diff -r fe70d8074644 -r 773a21e4fce8 liboctave/CNDArray.cc --- a/liboctave/CNDArray.cc Mon Oct 27 22:17:36 2003 +0000 +++ b/liboctave/CNDArray.cc Tue Oct 28 05:02:10 2003 +0000 @@ -74,38 +74,18 @@ // XXX FIXME XXX -- this is not quite the right thing. -boolMatrix +boolNDArray ComplexNDArray::all (int dim) const { - boolMatrix retval; - - if (dimensions.length () == 2) - { - ComplexMatrix tmp = matrix_value (); - retval = tmp.all (dim); - } - else - (*current_liboctave_error_handler) - ("all is not yet implemented for N-d Arrays"); - - return retval; + MX_ND_ALL_ANY (MX_ND_ALL_EVAL (real (elem (iter_idx)) == 0 + && imag (elem (iter_idx)) == 0)); } -boolMatrix +boolNDArray ComplexNDArray::any (int dim) const { - boolMatrix retval; - - if (dimensions.length () == 2) - { - ComplexMatrix tmp = matrix_value (); - retval = tmp.any (dim); - } - else - (*current_liboctave_error_handler) - ("any is not yet implemented for N-d Arrays"); - - return retval; + MX_ND_ALL_ANY (MX_ND_ANY_EVAL (real (elem (iter_idx)) + || imag (elem (iter_idx)))); } ComplexMatrix @@ -143,6 +123,13 @@ ::increment_index (ra_idx, dimensions, start_dimension); } +int +ComplexNDArray::compute_index (Array& ra_idx, + const dim_vector& dimensions) +{ + return ::compute_index (ra_idx, dimensions); +} + NDS_CMP_OPS(ComplexNDArray, real, Complex, real) NDS_BOOL_OPS(ComplexNDArray, Complex, 0.0) diff -r fe70d8074644 -r 773a21e4fce8 liboctave/CNDArray.h --- a/liboctave/CNDArray.h Mon Oct 27 22:17:36 2003 +0000 +++ b/liboctave/CNDArray.h Tue Oct 28 05:02:10 2003 +0000 @@ -74,8 +74,8 @@ // XXX FIXME XXX -- this is not quite the right thing. - boolMatrix all (int dim = -1) const; - boolMatrix any (int dim = -1) const; + boolNDArray all (int dim = -1) const; + boolNDArray any (int dim = -1) const; ComplexMatrix matrix_value (void) const; @@ -85,6 +85,9 @@ const dim_vector& dimensions, int start_dimension = 0); + static int compute_index (Array& ra_idx, + const dim_vector& dimensions); + // i/o // friend std::ostream& operator << (std::ostream& os, const NDArray& a); diff -r fe70d8074644 -r 773a21e4fce8 liboctave/ChangeLog --- a/liboctave/ChangeLog Mon Oct 27 22:17:36 2003 +0000 +++ b/liboctave/ChangeLog Tue Oct 28 05:02:10 2003 +0000 @@ -1,3 +1,16 @@ +2003-10-27 Petter Risholm + + * mx-inlines.cc (MX_ND_ALL_EXPR, MX_ND_ANY_EXPR, + MX_ND_ALL_EVAL, MX_ND_ANY_EVAL, MX_ND_ALL_ANY): New macros. + * dNDArray.h (NDArray::all, NDArray::any): Return type now boolNDArray. + * CNDArray.h (ComplexNDArray::all, ComplexNDArray::any): Likewise. + * boolNDArray.h (boolNDArray::all, boolNDArray::any): Likewise. + * chNDArray.h (charNDArray::all, charNDArray::any): Likewise. + * dNDArray.cc (NDArray::all, NDArray::any): Make them work. + * CNDArray.cc (ComplexNDArray::all, ComplexNDArray::any): Likewise. + * boolNDArray.cc (boolNDArray::all, boolNDArray::any): Likewise. + * chNDArray.cc (charNDArray::all, charNDArray::any): Likewise. + 2003-10-27 John W. Eaton * Array.cc (Array::resize_and_fill): Allow number of dimensions diff -r fe70d8074644 -r 773a21e4fce8 liboctave/boolNDArray.cc --- a/liboctave/boolNDArray.cc Mon Oct 27 22:17:36 2003 +0000 +++ b/liboctave/boolNDArray.cc Tue Oct 28 05:02:10 2003 +0000 @@ -50,38 +50,16 @@ // XXX FIXME XXX -- this is not quite the right thing. -boolMatrix +boolNDArray boolNDArray::all (int dim) const { - boolMatrix retval; - - if (dimensions.length () == 2) - { - boolMatrix tmp = matrix_value (); - retval = tmp.all (dim); - } - else - (*current_liboctave_error_handler) - ("all is not yet implemented for N-d Arrays"); - - return retval; + MX_ND_ALL_ANY (MX_ND_ALL_EVAL (MX_ND_ALL_EXPR)); } -boolMatrix +boolNDArray boolNDArray::any (int dim) const { - boolMatrix retval; - - if (dimensions.length () == 2) - { - boolMatrix tmp = matrix_value (); - retval = tmp.any (dim); - } - else - (*current_liboctave_error_handler) - ("any is not yet implemented for N-d Arrays"); - - return retval; + MX_ND_ALL_ANY (MX_ND_ANY_EVAL (MX_ND_ANY_EXPR)); } boolMatrix @@ -119,6 +97,13 @@ ::increment_index (ra_idx, dimensions, start_dimension); } +int +boolNDArray::compute_index (Array& ra_idx, + const dim_vector& dimensions) +{ + return ::compute_index (ra_idx, dimensions); +} + NDND_CMP_OPS (boolNDArray, , boolNDArray, ) /* diff -r fe70d8074644 -r 773a21e4fce8 liboctave/boolNDArray.h --- a/liboctave/boolNDArray.h Mon Oct 27 22:17:36 2003 +0000 +++ b/liboctave/boolNDArray.h Tue Oct 28 05:02:10 2003 +0000 @@ -68,8 +68,8 @@ // XXX FIXME XXX -- this is not quite the right thing. - boolMatrix all (int dim = -1) const; - boolMatrix any (int dim = -1) const; + boolNDArray all (int dim = -1) const; + boolNDArray any (int dim = -1) const; boolMatrix matrix_value (void) const; @@ -79,6 +79,9 @@ const dim_vector& dimensions, int start_dimension = 0); + static int compute_index (Array& ra_idx, + const dim_vector& dimensions); + // i/o // friend std::ostream& operator << (std::ostream& os, const NDArray& a); diff -r fe70d8074644 -r 773a21e4fce8 liboctave/chNDArray.cc --- a/liboctave/chNDArray.cc Mon Oct 27 22:17:36 2003 +0000 +++ b/liboctave/chNDArray.cc Tue Oct 28 05:02:10 2003 +0000 @@ -37,38 +37,16 @@ // XXX FIXME XXX -- this is not quite the right thing. -boolMatrix +boolNDArray charNDArray::all (int dim) const { - boolMatrix retval; - - if (dimensions.length () == 2) - { - charMatrix tmp = matrix_value (); - retval = tmp.all (dim); - } - else - (*current_liboctave_error_handler) - ("all is not yet implemented for N-d Arrays"); - - return retval; + MX_ND_ALL_ANY (MX_ND_ALL_EVAL (elem (iter_idx) == ' ')); } -boolMatrix +boolNDArray charNDArray::any (int dim) const { - boolMatrix retval; - - if (dimensions.length () == 2) - { - charMatrix tmp = matrix_value (); - retval = tmp.any (dim); - } - else - (*current_liboctave_error_handler) - ("any is not yet implemented for N-d Arrays"); - - return retval; + MX_ND_ALL_ANY (MX_ND_ANY_EVAL (elem (iter_idx) != ' ')); } charMatrix @@ -106,6 +84,13 @@ ::increment_index (ra_idx, dimensions, start_dimension); } +int +charNDArray::compute_index (Array& ra_idx, + const dim_vector& dimensions) +{ + return ::compute_index (ra_idx, dimensions); +} + /* ;;; Local Variables: *** ;;; mode: C++ *** diff -r fe70d8074644 -r 773a21e4fce8 liboctave/chNDArray.h --- a/liboctave/chNDArray.h Mon Oct 27 22:17:36 2003 +0000 +++ b/liboctave/chNDArray.h Tue Oct 28 05:02:10 2003 +0000 @@ -69,8 +69,8 @@ // XXX FIXME XXX -- this is not quite the right thing. - boolMatrix all (int dim = -1) const; - boolMatrix any (int dim = -1) const; + boolNDArray all (int dim = -1) const; + boolNDArray any (int dim = -1) const; charMatrix matrix_value (void) const; @@ -80,6 +80,9 @@ const dim_vector& dimensions, int start_dimension = 0); + static int compute_index (Array& ra_idx, + const dim_vector& dimensions); + // i/o // friend std::ostream& operator << (std::ostream& os, const charNDArray& a); diff -r fe70d8074644 -r 773a21e4fce8 liboctave/dNDArray.cc --- a/liboctave/dNDArray.cc Mon Oct 27 22:17:36 2003 +0000 +++ b/liboctave/dNDArray.cc Tue Oct 28 05:02:10 2003 +0000 @@ -65,38 +65,16 @@ // XXX FIXME XXX -- this is not quite the right thing. -boolMatrix +boolNDArray NDArray::all (int dim) const { - boolMatrix retval; - - if (dimensions.length () == 2) - { - Matrix tmp = matrix_value (); - retval = tmp.all (dim); - } - else - (*current_liboctave_error_handler) - ("all is not yet implemented for N-d Arrays"); - - return retval; + MX_ND_ALL_ANY (MX_ND_ALL_EVAL (MX_ND_ALL_EXPR)); } -boolMatrix +boolNDArray NDArray::any (int dim) const { - boolMatrix retval; - - if (dimensions.length () == 2) - { - Matrix tmp = matrix_value (); - retval = tmp.any (dim); - } - else - (*current_liboctave_error_handler) - ("any is not yet implemented for N-d Arrays"); - - return retval; + MX_ND_ALL_ANY (MX_ND_ANY_EVAL (MX_ND_ANY_EXPR)); } Matrix @@ -133,6 +111,13 @@ ::increment_index (ra_idx, dimensions, start_dimension); } +int +NDArray::compute_index (Array& ra_idx, + const dim_vector& dimensions) +{ + return ::compute_index (ra_idx, dimensions); +} + bool NDArray::any_element_is_negative (bool neg_zero) const { diff -r fe70d8074644 -r 773a21e4fce8 liboctave/dNDArray.h --- a/liboctave/dNDArray.h Mon Oct 27 22:17:36 2003 +0000 +++ b/liboctave/dNDArray.h Tue Oct 28 05:02:10 2003 +0000 @@ -72,8 +72,8 @@ // XXX FIXME XXX -- this is not quite the right thing. - boolMatrix all (int dim = -1) const; - boolMatrix any (int dim = -1) const; + boolNDArray all (int dim = -1) const; + boolNDArray any (int dim = -1) const; Matrix matrix_value (void) const; @@ -83,6 +83,9 @@ const dim_vector& dimensions, int start_dimension = 0); + static int compute_index (Array& ra_idx, + const dim_vector& dimensions); + // i/o // friend std::ostream& operator << (std::ostream& os, const NDArray& a); diff -r fe70d8074644 -r 773a21e4fce8 liboctave/mx-inlines.cc --- a/liboctave/mx-inlines.cc Mon Oct 27 22:17:36 2003 +0000 +++ b/liboctave/mx-inlines.cc Tue Oct 28 05:02:10 2003 +0000 @@ -350,6 +350,105 @@ #define MX_ANY_OP(DIM) MX_ANY_ALL_OP (DIM, false, !=, true) +#define MX_ND_ALL_EXPR elem (iter_idx) == 0 + +#define MX_ND_ANY_EXPR elem (iter_idx) + +#define MX_ND_ALL_EVAL(TEST_EXPR) \ + if (TEST_EXPR) \ + { \ + if (dim > -1) \ + iter_idx (dim) = 0; \ + retval (iter_idx) = 0; \ + break; \ + } \ + else \ + { \ + if (dim > -1) \ + iter_idx (dim) = 0; \ + retval (iter_idx) = 1; \ + } \ + +#define MX_ND_ANY_EVAL(TEST_EXPR) \ + if (TEST_EXPR) \ + { \ + if (dim > -1) \ + iter_idx (dim) = 0; \ + retval (iter_idx) = 1; \ + break; \ + } + +#define MX_ND_ALL_ANY(EVAL_EXPR) \ + \ + boolNDArray retval; \ + \ + dim_vector dv = dims (); \ + \ + if (dim == -1)/* We need to find first non-singleton dim */ \ + { \ + for (int i = 0; i < dv.length (); i++) \ + { \ + if (dv (i) > 1) \ + { \ + dim = i; \ + break; \ + } \ + } \ + if (dim == -1) \ + { \ + (*current_liboctave_error_handler) \ + ("all dimensions are singleton"); \ + return retval; \ + } \ + } \ + /* Length of Dimension */\ + int dim_length = 1; \ + /* dim = -1 means from here that the user specified a */ \ + /* dimension which is larger that the number of dimensions */ \ + /* of the array */ \ + if (dim >= dv.length ()) \ + dim = -1; \ + else \ + dim_length = dv (dim); \ + if (dim > -1) \ + dv (dim) = 1; \ + \ + /* We need to find the number of elements we need to */ \ + /* fill in retval. First we need to get last idx of */ \ + /* the dimension vector */ \ + Array temp_dv (dv.length (), 0); \ + for (int x = 0; x < dv.length (); x++) \ + temp_dv (x) = dv (x) - 1; \ + \ + /* This finds the number of elements in retval */ \ + int num_iter = compute_index (temp_dv, dv) + 1; \ + \ + /* Make sure retval has correct dimensions */ \ + retval.resize (dv, false); \ + \ + Array iter_idx (dv.length (), 0); \ + \ + /* Filling in values. */ \ + /* First loop finds new index */ \ + for (int j = 0; j < num_iter; j++) \ + { \ + for (int i = 0; i < dim_length; i++) \ + { \ + if (dim > -1) \ + iter_idx (dim) = i; \ + \ + EVAL_EXPR \ + } \ + \ + if (dim > -1) \ + iter_idx (dim) = 0; \ + \ + increment_index (iter_idx, dv); \ + } \ + \ + return retval + + #endif /* diff -r fe70d8074644 -r 773a21e4fce8 src/ChangeLog --- a/src/ChangeLog Mon Oct 27 22:17:36 2003 +0000 +++ b/src/ChangeLog Tue Oct 28 05:02:10 2003 +0000 @@ -1,5 +1,9 @@ 2003-10-27 Petter Risholm + * ov-base-mat.cc (octave_base_matrix::is_true): + Only work for 2-dimensional objects. + * data.cc (ANY_ALL (FCN)): Allow dim to take values large than 2. + * ov.h (octave_value::ndims): New function. * ov-base.h (octave_base_value::ndims): Likewise. * ov-base-scalar.h (octave_base_scalar::ndims): Likewise. diff -r fe70d8074644 -r 773a21e4fce8 src/data.cc --- a/src/data.cc Mon Oct 27 22:17:36 2003 +0000 +++ b/src/data.cc Tue Oct 28 05:02:10 2003 +0000 @@ -53,7 +53,7 @@ \ if (! error_state) \ { \ - if (dim <= 1 && dim >= -1) \ + if (dim >= -1) \ retval = args(0).FCN (dim); \ else \ error (#FCN ": invalid dimension argument = %d", dim + 1); \ @@ -366,7 +366,7 @@ \ if (! error_state) \ { \ - if (dim <= 1 && dim >= -1) \ + if (dim >= -1) \ { \ if (arg.is_real_type ()) \ { \ diff -r fe70d8074644 -r 773a21e4fce8 src/ov-base-mat.cc --- a/src/ov-base-mat.cc Mon Oct 27 22:17:36 2003 +0000 +++ b/src/ov-base-mat.cc Tue Oct 28 05:02:10 2003 +0000 @@ -176,13 +176,19 @@ { bool retval = false; - if (rows () > 0 && columns () > 0) + if (matrix.dims () . length () == 2) { - boolMatrix m = (matrix.all () . all ()); - - retval = (m.rows () == 1 && m.columns () == 1 && m(0,0)); + if (rows () > 0 && columns () > 0) + { + boolNDArray m = (matrix.all () . all ()); + + retval = (m.rows () == 1 && m.columns () == 1 && m(0,0)); + } } - + else + (*current_liboctave_error_handler) + ("is_true not yet implemented for N-d Arrays"); + return retval; } @@ -192,7 +198,7 @@ { int retval = 0; - dim_vector dv = dims(); + dim_vector dv = dims (); for (int i = 0; i < dv.length (); i++) if (dv(i) > retval)