Mercurial > hg > octave-lyh
changeset 4015:6476dd85a65f
[project @ 2002-08-02 07:59:19 by jwe]
author | jwe |
---|---|
date | Fri, 02 Aug 2002 07:59:20 +0000 |
parents | 8eaf7182d48a |
children | 139f5273d0f8 |
files | liboctave/CMatrix.cc liboctave/CMatrix.h liboctave/ChangeLog liboctave/boolMatrix.cc liboctave/boolMatrix.h liboctave/chMatrix.cc liboctave/chMatrix.h liboctave/dMatrix.cc liboctave/dMatrix.h liboctave/mx-inlines.cc src/Cell.h src/ChangeLog src/data.cc src/ov-base-mat.h src/ov-base.h src/ov.h |
diffstat | 16 files changed, 161 insertions(+), 457 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/CMatrix.cc +++ b/liboctave/CMatrix.cc @@ -2333,124 +2333,31 @@ return false; } +// XXX FIXME XXX Do these really belong here? Maybe they should be +// in a base class? + boolMatrix -ComplexMatrix::all (void) const +ComplexMatrix::all (int dim) const { - int nr = rows (); - int nc = cols (); - boolMatrix retval; - if (nr > 0 && nc > 0) - { - if (nr == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = true; - for (int j = 0; j < nc; j++) - { - if (elem (0, j) == 0.0) - { - retval.elem (0, 0) = false; - break; - } - } - } - else if (nc == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = true; - for (int i = 0; i < nr; i++) - { - if (elem (i, 0) == 0.0) - { - retval.elem (0, 0) = false; - break; - } - } - } - else - { - retval.resize (1, nc); - for (int j = 0; j < nc; j++) - { - retval.elem (0, j) = true; - for (int i = 0; i < nr; i++) - { - if (elem (i, j) == 0.0) - { - retval.elem (0, j) = false; - break; - } - } - } - } - } - return retval; + MX_ALL_OP (dim); } boolMatrix -ComplexMatrix::any (void) const +ComplexMatrix::any (int dim) const { - int nr = rows (); - int nc = cols (); - boolMatrix retval; - if (nr > 0 && nc > 0) - { - if (nr == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = false; - for (int j = 0; j < nc; j++) - { - if (elem (0, j) != 0.0) - { - retval.elem (0, 0) = true; - break; - } - } - } - else if (nc == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = false; - for (int i = 0; i < nr; i++) - { - if (elem (i, 0) != 0.0) - { - retval.elem (0, 0) = true; - break; - } - } - } - else - { - retval.resize (1, nc); - for (int j = 0; j < nc; j++) - { - retval.elem (0, j) = false; - for (int i = 0; i < nr; i++) - { - if (elem (i, j) != 0.0) - { - retval.elem (0, j) = true; - break; - } - } - } - } - } - return retval; + MX_ANY_OP (dim); } ComplexMatrix ComplexMatrix::cumprod (int dim) const { - MX_CUMMULATIVE_OP (ComplexMatrix, Complex, *=); + MX_CUMULATIVE_OP (ComplexMatrix, Complex, *=); } ComplexMatrix ComplexMatrix::cumsum (int dim) const { - MX_CUMMULATIVE_OP (ComplexMatrix, Complex, +=); + MX_CUMULATIVE_OP (ComplexMatrix, Complex, +=); } ComplexMatrix
--- a/liboctave/CMatrix.h +++ b/liboctave/CMatrix.h @@ -241,8 +241,8 @@ bool all_integers (double& max_val, double& min_val) const; bool too_large_for_float (void) const; - boolMatrix all (void) const; - boolMatrix any (void) const; + boolMatrix all (int dim = 0) const; + boolMatrix any (int dim = 0) const; ComplexMatrix cumprod (int dim = 0) const; ComplexMatrix cumsum (int dim = 0) const;
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,5 +1,35 @@ +2002-08-02 John W. Eaton <jwe@bevo.che.wisc.edu> + + * mx-inlines.cc (MX_ANY_ALL_OP_ROW_CODE, MX_ANY_ALL_OP_COL_CODE): + New macros. + (MX_ANY_ALL_OP): Define MX_ANY_ALL_OP using them and + MX_BASE_REDUCTION_OP. + (MX_CUMULATIVE_OP): Fix spelling. Change all uses. + 2002-08-01 John W. Eaton <jwe@bevo.che.wisc.edu> + * chMatrix.h, chMatrix.cc (charMatrix::any, charMatrix::all): + Return boolMatrix, not Matrix. + + * mx-inlines.cc (MX_ANY_ALL_OP, MX_ALL_OP, MX_ANY_OP): New macros. + * dMatrix.cc (Matrix::any): Replace guts with MX_ANY_OP. + (Matrix::all): Replace guts with MX_ALL_OP. + * CMatrix.cc (ComplexMatrix::any): Replace guts with MX_ANY_OP. + (ComplexMatrix::all): Replace guts with MX_ALL_OP. + * boolMatrix.cc (boolMatrix::any): Replace guts with MX_ANY_OP. + (boolMatrix::all): Replace guts with MX_ALL_OP. + * chMatrix.cc (charMatrix::any): Replace guts with MX_ANY_OP. + (charMatrix::all): Replace guts with MX_ALL_OP. + + * dMatrix.h (Matrix::any): New arg, dim. + (Matrix::all): Likewise. + * CMatrix.h (ComplexMatrix::any): Likewise. + (ComplexMatrix::all): Likewise. + * boolMatrix.h (boolMatrix::any): Likewise. + (boolMatrix::all): Likewise. + * chMatrix.h (charMatrix::any): Likewise. + (charMatrix::all): Likewise. + * Makefile.in: Use $@-t instead of $@.t. 2002-07-25 John W. Eaton <jwe@bevo.che.wisc.edu>
--- a/liboctave/boolMatrix.cc +++ b/liboctave/boolMatrix.cc @@ -79,112 +79,19 @@ // other operations +// XXX FIXME XXX Do these really belong here? Maybe they should be +// in a base class? + boolMatrix -boolMatrix::all (void) const +boolMatrix::all (int dim) const { - int nr = rows (); - int nc = cols (); - boolMatrix retval; - if (nr > 0 && nc > 0) - { - if (nr == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = true; - for (int j = 0; j < nc; j++) - { - if (! elem (0, j)) - { - retval.elem (0, 0) = false; - break; - } - } - } - else if (nc == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = true; - for (int i = 0; i < nr; i++) - { - if (! elem (i, 0)) - { - retval.elem (0, 0) = false; - break; - } - } - } - else - { - retval.resize (1, nc); - for (int j = 0; j < nc; j++) - { - retval.elem (0, j) = true; - for (int i = 0; i < nr; i++) - { - if (! elem (i, j)) - { - retval.elem (0, j) = false; - break; - } - } - } - } - } - return retval; + MX_ALL_OP (dim); } boolMatrix -boolMatrix::any (void) const +boolMatrix::any (int dim) const { - int nr = rows (); - int nc = cols (); - boolMatrix retval; - if (nr > 0 && nc > 0) - { - if (nr == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = false; - for (int j = 0; j < nc; j++) - { - if (elem (0, j)) - { - retval.elem (0, 0) = true; - break; - } - } - } - else if (nc == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = false; - for (int i = 0; i < nr; i++) - { - if (elem (i, 0)) - { - retval.elem (0, 0) = true; - break; - } - } - } - else - { - retval.resize (1, nc); - for (int j = 0; j < nc; j++) - { - retval.elem (0, j) = false; - for (int i = 0; i < nr; i++) - { - if (elem (i, j)) - { - retval.elem (0, j) = true; - break; - } - } - } - } - } - return retval; + MX_ANY_OP (dim); } MM_CMP_OPS(boolMatrix, , boolMatrix, )
--- a/liboctave/boolMatrix.h +++ b/liboctave/boolMatrix.h @@ -64,8 +64,8 @@ // other operations - boolMatrix all (void) const; - boolMatrix any (void) const; + boolMatrix all (int dim = 0) const; + boolMatrix any (int dim = 0) const; #if 0 // i/o
--- a/liboctave/chMatrix.cc +++ b/liboctave/chMatrix.cc @@ -192,115 +192,19 @@ return result; } -// XXX FIXME XXX -- these should probably return a boolMatrix type -// instead, but that will have to wait for a future version... +// XXX FIXME XXX Do these really belong here? Maybe they should be +// in a base class? -Matrix -charMatrix::all (void) const +boolMatrix +charMatrix::all (int dim) const { - int nr = rows (); - int nc = cols (); - Matrix retval; - if (nr > 0 && nc > 0) - { - if (nr == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = 1.0; - for (int j = 0; j < nc; j++) - { - if (elem (0, j) == 0) - { - retval.elem (0, 0) = 0.0; - break; - } - } - } - else if (nc == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = 1.0; - for (int i = 0; i < nr; i++) - { - if (elem (i, 0) == 0) - { - retval.elem (0, 0) = 0.0; - break; - } - } - } - else - { - retval.resize (1, nc); - for (int j = 0; j < nc; j++) - { - retval.elem (0, j) = 1.0; - for (int i = 0; i < nr; i++) - { - if (elem (i, j) == 0) - { - retval.elem (0, j) = 0.0; - break; - } - } - } - } - } - return retval; + MX_ALL_OP (dim); } -Matrix -charMatrix::any (void) const +boolMatrix +charMatrix::any (int dim) const { - int nr = rows (); - int nc = cols (); - Matrix retval; - if (nr > 0 && nc > 0) - { - if (nr == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = 0.0; - for (int j = 0; j < nc; j++) - { - if (elem (0, j) != 0) - { - retval.elem (0, 0) = 1.0; - break; - } - } - } - else if (nc == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = 0.0; - for (int i = 0; i < nr; i++) - { - if (elem (i, 0) != 0) - { - retval.elem (0, 0) = 1.0; - break; - } - } - } - else - { - retval.resize (1, nc); - for (int j = 0; j < nc; j++) - { - retval.elem (0, j) = 0.0; - for (int i = 0; i < nr; i++) - { - if (elem (i, j) != 0) - { - retval.elem (0, j) = 1.0; - break; - } - } - } - } - } - return retval; + MX_ANY_OP (dim); } /*
--- a/liboctave/chMatrix.h +++ b/liboctave/chMatrix.h @@ -71,8 +71,8 @@ charMatrix extract (int r1, int c1, int r2, int c2) const; - Matrix all (void) const; - Matrix any (void) const; + boolMatrix all (int dim = 0) const; + boolMatrix any (int dim = 0) const; #if 0 // i/o
--- a/liboctave/dMatrix.cc +++ b/liboctave/dMatrix.cc @@ -1876,128 +1876,31 @@ return false; } -// XXX FIXME XXX Do these really belong here? They should maybe be -// cleaned up a bit, no? What about corresponding functions for the -// Vectors? +// XXX FIXME XXX Do these really belong here? Maybe they should be +// in a base class? boolMatrix -Matrix::all (void) const +Matrix::all (int dim) const { - int nr = rows (); - int nc = cols (); - boolMatrix retval; - if (nr > 0 && nc > 0) - { - if (nr == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = true; - for (int j = 0; j < nc; j++) - { - if (elem (0, j) == 0.0) - { - retval.elem (0, 0) = false; - break; - } - } - } - else if (nc == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = true; - for (int i = 0; i < nr; i++) - { - if (elem (i, 0) == 0.0) - { - retval.elem (0, 0) = false; - break; - } - } - } - else - { - retval.resize (1, nc); - for (int j = 0; j < nc; j++) - { - retval.elem (0, j) = true; - for (int i = 0; i < nr; i++) - { - if (elem (i, j) == 0.0) - { - retval.elem (0, j) = false; - break; - } - } - } - } - } - return retval; + MX_ALL_OP (dim); } boolMatrix -Matrix::any (void) const +Matrix::any (int dim) const { - int nr = rows (); - int nc = cols (); - boolMatrix retval; - if (nr > 0 && nc > 0) - { - if (nr == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = false; - for (int j = 0; j < nc; j++) - { - if (elem (0, j) != 0.0) - { - retval.elem (0, 0) = true; - break; - } - } - } - else if (nc == 1) - { - retval.resize (1, 1); - retval.elem (0, 0) = false; - for (int i = 0; i < nr; i++) - { - if (elem (i, 0) != 0.0) - { - retval.elem (0, 0) = true; - break; - } - } - } - else - { - retval.resize (1, nc); - for (int j = 0; j < nc; j++) - { - retval.elem (0, j) = false; - for (int i = 0; i < nr; i++) - { - if (elem (i, j) != 0.0) - { - retval.elem (0, j) = true; - break; - } - } - } - } - } - return retval; + MX_ANY_OP (dim); } Matrix Matrix::cumprod (int dim) const { - MX_CUMMULATIVE_OP (Matrix, double, *=); + MX_CUMULATIVE_OP (Matrix, double, *=); } Matrix Matrix::cumsum (int dim) const { - MX_CUMMULATIVE_OP (Matrix, double, +=); + MX_CUMULATIVE_OP (Matrix, double, +=); } Matrix
--- a/liboctave/dMatrix.h +++ b/liboctave/dMatrix.h @@ -197,8 +197,8 @@ bool all_integers (double& max_val, double& min_val) const; bool too_large_for_float (void) const; - boolMatrix all (void) const; - boolMatrix any (void) const; + boolMatrix all (int dim = 0) const; + boolMatrix any (int dim = 0) const; Matrix cumprod (int dim = 0) const; //optional dimension argument Matrix cumsum (int dim = 0) const;
--- a/liboctave/mx-inlines.cc +++ b/liboctave/mx-inlines.cc @@ -229,7 +229,7 @@ // Avoid some code duplication. Maybe we should use templates. -#define MX_CUMMULATIVE_OP(RET_TYPE, ELT_TYPE, OP) \ +#define MX_CUMULATIVE_OP(RET_TYPE, ELT_TYPE, OP) \ \ int nr = rows (); \ int nc = cols (); \ @@ -303,11 +303,17 @@ } \ } \ } \ - else \ + else if (nr == 0 && nc == 0) \ { \ retval.resize (1, 1); \ retval.elem (0, 0) = MT_RESULT; \ } \ + else if (nr == 0 && (dim == 0 || dim == -1)) \ + retval.resize (1, nc, MT_RESULT); \ + else if (nc == 0 && dim == 1) \ + retval.resize (nr, 1, MT_RESULT); \ + else \ + retval.resize (nr, nc); \ \ return retval @@ -322,6 +328,31 @@ MX_REDUCTION_OP_ROW_EXPR (OP), \ MX_REDUCTION_OP_COL_EXPR (OP), \ INIT_VAL, MT_RESULT) + +#define MX_ANY_ALL_OP_ROW_CODE(TEST_OP, TEST_TRUE_VAL) \ + if (elem (i, j) TEST_OP 0.0) \ + { \ + retval.elem (i, 0) = TEST_TRUE_VAL; \ + break; \ + } + +#define MX_ANY_ALL_OP_COL_CODE(TEST_OP, TEST_TRUE_VAL) \ + if (elem (i, j) TEST_OP 0.0) \ + { \ + retval.elem (0, j) = TEST_TRUE_VAL; \ + break; \ + } + +#define MX_ANY_ALL_OP(DIM, INIT_VAL, TEST_OP, TEST_TRUE_VAL) \ + MX_BASE_REDUCTION_OP (boolMatrix, \ + MX_ANY_ALL_OP_ROW_CODE (TEST_OP, TEST_TRUE_VAL), \ + MX_ANY_ALL_OP_COL_CODE (TEST_OP, TEST_TRUE_VAL), \ + INIT_VAL, INIT_VAL) + +#define MX_ALL_OP(DIM) MX_ANY_ALL_OP (DIM, true, ==, false) + +#define MX_ANY_OP(DIM) MX_ANY_ALL_OP (DIM, false, !=, true) + #endif /*
--- a/src/Cell.h +++ b/src/Cell.h @@ -59,10 +59,10 @@ : Array2<octave_value> (c) { } // XXX FIXME XXX - boolMatrix all (void) const { return boolMatrix (); } + boolMatrix all (int dim = 0) const { return boolMatrix (); } // XXX FIXME XXX - boolMatrix any (void) const { return boolMatrix (); } + boolMatrix any (int dim = 0) const { return boolMatrix (); } // XXX FIXME XXX bool is_true (void) const { return false; }
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,19 @@ +2002-08-02 John W. Eaton <jwe@bevo.che.wisc.edu> + + * data.cc (ANY_ALL): New macro. + (Fany, Fall): Replace guts with ANY_ALL. + 2002-08-01 John W. Eaton <jwe@bevo.che.wisc.edu> + * data.cc (Fall, Fany): Handle optional DIM argument. + + * ov.h (octave_value::any): New arg, dim. + (octave_value::all): Likewise. + * ov-base.h (octave_base_value::any): Likewise. + (octave_base_value::all): Likewise. + * ov-base-mat.h (octave_base_matrix::any): Likewise. + (octave_base_matrix::all): Likewise. + * Makefile.in: Use $@-t instead of $@.t. (doc-files): Use move-if-change when creating doc-files.
--- a/src/data.cc +++ b/src/data.cc @@ -50,29 +50,43 @@ #define ABS(x) (((x) < 0) ? (-x) : (x)) #endif +#define ANY_ALL(FCN) \ + \ + octave_value_list retval; \ + \ + int nargin = args.length (); \ + \ + int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ + \ + if (! error_state) \ + { \ + if (dim <= 1 && dim >= -1) \ + { \ + if (nargin == 1 || nargin == 2) \ + retval = args(0).FCN (dim); \ + else \ + print_usage (#FCN); \ + } \ + else \ + error (#FCN ": invalid dimension argument = %d", dim + 1); \ + } \ + \ + return retval + DEFUN (all, args, , "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} all (@var{x})\n\ +@deftypefn {Built-in Function} {} all (@var{x}, @var{dim})\n\ The function @code{all} behaves like the function @code{any}, except\n\ that it returns true only if all the elements of a vector, or all the\n\ -elements in a column of a matrix, are nonzero.\n\ +elements along dimension @var{dim} of a matrix, are nonzero.\n\ @end deftypefn") { - octave_value_list retval; - - int nargin = args.length (); - - if (nargin == 1 && args(0).is_defined ()) - retval = args(0).all (); - else - print_usage ("all"); - - return retval; + ANY_ALL (all); } DEFUN (any, args, , "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} any (@var{x})\n\ +@deftypefn {Built-in Function} {} any (@var{x}, @var{dim})\n\ For a vector argument, return 1 if any element of the vector is\n\ nonzero.\n\ \n\ @@ -87,24 +101,18 @@ @end group\n\ @end example\n\ \n\ -To see if any of the elements of a matrix are nonzero, you can use a\n\ -statement like\n\ +If the optional argument @var{dim} is supplied, work along dimension\n\ +@var{dim}. For example,\n\ \n\ @example\n\ -any (any (a))\n\ +@group\n\ +any (eye (2, 4), 2)\n\ + @result{} [ 1; 1 ]\n\ +@end group\n\ @end example\n\ @end deftypefn") { - octave_value_list retval; - - int nargin = args.length (); - - if (nargin == 1 && args(0).is_defined ()) - retval = args(0).any (); - else - print_usage ("any"); - - return retval; + ANY_ALL (any); } // These mapping functions may also be useful in other places, eh?
--- a/src/ov-base-mat.h +++ b/src/ov-base-mat.h @@ -91,8 +91,8 @@ return (r == 0 || c == 0) ? 0 : ((r > c) ? r : c); } - octave_value all (void) const { return matrix.all (); } - octave_value any (void) const { return matrix.any (); } + octave_value all (int dim) const { return matrix.all (dim); } + octave_value any (int dim) const { return matrix.any (dim); } bool is_matrix_type (void) const { return true; }
--- a/src/ov-base.h +++ b/src/ov-base.h @@ -128,9 +128,9 @@ bool is_all_va_args (void) const { return false; } - octave_value all (void) const { return 0.0; } + octave_value all (int dim) const { return 0.0; } - octave_value any (void) const { return 0.0; } + octave_value any (int dim) const { return 0.0; } bool is_bool_type (void) const { return false; }
--- a/src/ov.h +++ b/src/ov.h @@ -350,11 +350,11 @@ // Are any or all of the elements in this constant nonzero? - virtual octave_value all (void) const - { return rep->all (); } + virtual octave_value all (int dim = 0) const + { return rep->all (dim); } - virtual octave_value any (void) const - { return rep->any (); } + virtual octave_value any (int dim = 0) const + { return rep->any (dim); } // Other type stuff.