Mercurial > hg > octave-lojdl
changeset 4584:f7697d703481
[project @ 2003-10-31 19:47:35 by jwe]
author | jwe |
---|---|
date | Fri, 31 Oct 2003 19:47:36 +0000 |
parents | 70da2b8c91dd |
children | a1ac9ab474a1 |
files | liboctave/CNDArray.cc liboctave/CNDArray.h liboctave/ChangeLog liboctave/dNDArray.cc liboctave/dNDArray.h liboctave/mx-inlines.cc src/ChangeLog src/ov.cc |
diffstat | 8 files changed, 153 insertions(+), 53 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- 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;
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,12 @@ +2003-10-31 Petter Risholm <risholm@stud.ntnu.no> + + * 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 <jwe@bevo.che.wisc.edu> * LSODE.cc (LSODE::do_integrate): Avoid name conflict on systems
--- 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
--- 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;
--- 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<int> 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<int> 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 /*