# HG changeset patch # User jwe # Date 1067629656 0 # Node ID f7697d703481c2bf556f37d66f0c103603821336 # Parent 70da2b8c91dd60ae24d9f13de57ede6cdd72c6ac [project @ 2003-10-31 19:47:35 by jwe] diff --git a/liboctave/CNDArray.cc b/liboctave/CNDArray.cc --- a/liboctave/CNDArray.cc +++ b/liboctave/CNDArray.cc @@ -88,36 +88,16 @@ (MX_ND_ANY_EVAL (elem (iter_idx) != Complex (0, 0)), false); } -ComplexMatrix +ComplexNDArray ComplexNDArray::cumprod (int dim) const { - if (dims () . length () == 2) - { - MX_CUMULATIVE_OP (ComplexMatrix, Complex, *=); - } - else - { - (*current_liboctave_error_handler) - ("cumsum is not yet implemented for N-d arrays"); - - return ComplexMatrix (); - } + MX_ND_CUMULATIVE_OP (ComplexNDArray, Complex, Complex (1, 0), *); } -ComplexMatrix +ComplexNDArray ComplexNDArray::cumsum (int dim) const { - if (dims () . length () == 2) - { - MX_CUMULATIVE_OP (ComplexMatrix, Complex, +=); - } - else - { - (*current_liboctave_error_handler) - ("cumsum is not yet implemented for N-d arrays"); - - return ComplexMatrix (); - } + MX_ND_CUMULATIVE_OP (ComplexNDArray, Complex, Complex (0, 0), +); } ComplexNDArray diff --git a/liboctave/CNDArray.h b/liboctave/CNDArray.h --- a/liboctave/CNDArray.h +++ b/liboctave/CNDArray.h @@ -77,8 +77,8 @@ boolNDArray all (int dim = -1) const; boolNDArray any (int dim = -1) const; - ComplexMatrix cumprod (int dim = -1) const; - ComplexMatrix cumsum (int dim = -1) const; + ComplexNDArray cumprod (int dim = -1) const; + ComplexNDArray cumsum (int dim = -1) const; ComplexNDArray prod (int dim = -1) const; ComplexNDArray sum (int dim = -1) const; ComplexNDArray sumsq (int dim = -1) const; diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,12 @@ +2003-10-31 Petter Risholm + + * mx-inlines.cc (MX_ND_CUMULATIVE_OP): New macro. + + * CNDArray.cc, CNDArray.h (ComplexNDArray::cumsum, + ComplexNDArray::cumprod): Return ComplexNDArray. Handle N-d arrays. + * dNDArray.cc, dNDArray.h (NDArray::cumsum, NDArray::cumprod): + Return NDArray. Handle N-d arrays. + 2003-10-31 John W. Eaton * LSODE.cc (LSODE::do_integrate): Avoid name conflict on systems diff --git a/liboctave/dNDArray.cc b/liboctave/dNDArray.cc --- a/liboctave/dNDArray.cc +++ b/liboctave/dNDArray.cc @@ -65,6 +65,7 @@ // XXX FIXME XXX -- this is not quite the right thing. + boolNDArray NDArray::all (int dim) const { @@ -77,36 +78,16 @@ MX_ND_ANY_ALL_REDUCTION (MX_ND_ANY_EVAL (MX_ND_ANY_EXPR), false); } -Matrix +NDArray NDArray::cumprod (int dim) const { - if (dims () . length () == 2) - { - MX_CUMULATIVE_OP (Matrix, double, *=); - } - else - { - (*current_liboctave_error_handler) - ("cumprod is not yet implemented for N-d arrays"); - - return Matrix (); - } + MX_ND_CUMULATIVE_OP (NDArray, double, 1, *); } -Matrix +NDArray NDArray::cumsum (int dim) const { - if (dims () . length () == 2) - { - MX_CUMULATIVE_OP (Matrix, double, +=); - } - else - { - (*current_liboctave_error_handler) - ("cumprod is not yet implemented for N-d arrays"); - - return Matrix (); - } + MX_ND_CUMULATIVE_OP (NDArray, double, 0, +); } NDArray diff --git a/liboctave/dNDArray.h b/liboctave/dNDArray.h --- a/liboctave/dNDArray.h +++ b/liboctave/dNDArray.h @@ -75,8 +75,8 @@ boolNDArray all (int dim = -1) const; boolNDArray any (int dim = -1) const; - Matrix cumprod (int dim = -1) const; - Matrix cumsum (int dim = -1) const; + NDArray cumprod (int dim = -1) const; + NDArray cumsum (int dim = -1) const; NDArray prod (int dim = -1) const; NDArray sum (int dim = -1) const; NDArray sumsq (int dim = -1) const; diff --git a/liboctave/mx-inlines.cc b/liboctave/mx-inlines.cc --- a/liboctave/mx-inlines.cc +++ b/liboctave/mx-inlines.cc @@ -504,6 +504,128 @@ #define MX_ND_ANY_ALL_REDUCTION(EVAL_EXPR, VAL) \ MX_ND_REDUCTION (EVAL_EXPR, , VAL, , boolNDArray) +#define MX_ND_CUMULATIVE_OP(RET_TYPE, ACC_TYPE, VAL, OP) \ + RET_TYPE retval; \ + \ + dim_vector dv = dims (); \ + \ + int empty = true; \ + \ + /* If dim is larger then number of dims, return array as is */ \ + if (dim > dv.length ()) \ + { \ + retval = RET_TYPE (*this); \ + \ + return retval; \ + } \ + \ + /* Check if all dims are empty */ \ + for (int i = 0; i < dv.length (); i++) \ + { \ + if (dv(i) > 0) \ + { \ + empty = false; \ + break; \ + } \ + } \ + \ + if (empty) \ + { \ + retval.resize (dv); \ + \ + return retval; \ + } \ + \ + /* We need to find first non-singleton dim */ \ + if (dim == -1) \ + { \ + for (int i = 0; i < dv.length (); i++) \ + { \ + if (dv (i) != 1) \ + { \ + dim = i; \ + break; \ + } \ + } \ + \ + if (dim == -1) \ + dim = 0; \ + } \ + \ + /* Check to see if we have an empty array */ \ + /* ie 1x2x0x3. */ \ + int squeezed = 0; \ + \ + for (int i = 0; i < dv.length (); i++) \ + { \ + if (dv(i) == 0) \ + { \ + squeezed = 1; \ + break; \ + } \ + } \ + \ + if (squeezed) \ + { \ + retval.resize (dv); \ + \ + return retval; \ + } \ + \ + /* Make sure retval has correct dimensions */ \ + retval.resize (dv, VAL); \ + \ + /* Length of Dimension */ \ + int dim_length = 1; \ + \ + dim_length = dv (dim); \ + \ + 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 */ \ + \ + /* This could be done faster */ \ + 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; \ + \ + 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 (i > 0) \ + { \ + iter_idx (dim) = i - 1; \ + \ + ACC_TYPE prev_sum = retval (iter_idx); \ + \ + iter_idx (dim) = i; \ + \ + retval (iter_idx) = elem (iter_idx) OP prev_sum; \ + } \ + else \ + retval (iter_idx) = elem (iter_idx); \ + } \ + \ + if (dim > -1) \ + iter_idx (dim) = 0; \ + \ + increment_index (iter_idx, dv); \ + } \ +\ + return retval + #endif /* diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,7 @@ 2003-10-31 John W. Eaton + * ov.cc (octave_value::length): If any dim is zero, return 0. + * ov-cell.cc (octave_cell::subsref): When indexing with '{', quit early if an error occurs in do_index_op. diff --git a/src/ov.cc b/src/ov.cc --- a/src/ov.cc +++ b/src/ov.cc @@ -839,7 +839,7 @@ int retval = 0; dim_vector dv = dims (); - + for (int i = 0; i < dv.length (); i++) { if (dv(i) < 0) @@ -848,6 +848,12 @@ break; } + if (dv(i) == 0) + { + retval = 0; + break; + } + if (dv(i) > retval) retval = dv(i); }