Mercurial > hg > octave-nkf
changeset 5148:9eab94f5fadf
[project @ 2005-02-18 19:04:34 by jwe]
author | jwe |
---|---|
date | Fri, 18 Feb 2005 19:04:34 +0000 |
parents | 47e4c91e5799 |
children | 8b7b4ffab39c |
files | liboctave/Array-util.cc liboctave/Array.cc liboctave/ChangeLog src/ChangeLog src/data.cc |
diffstat | 5 files changed, 53 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/Array-util.cc +++ b/liboctave/Array-util.cc @@ -461,9 +461,9 @@ for (int i = 0; i < n_el; i++) { if (inv) - retval(perm_vec(i)-1) = old_idx(i); + retval(perm_vec(i)) = old_idx(i); else - retval(i) = old_idx(perm_vec(i)-1); + retval(i) = old_idx(perm_vec(i)); } return retval;
--- a/liboctave/Array.cc +++ b/liboctave/Array.cc @@ -430,43 +430,54 @@ dim_vector dv = dims (); dim_vector dv_new; - int nd = dv.length (); - - dv_new.resize (nd); + int perm_vec_len = perm_vec.length (); + + if (perm_vec_len < dv.length ()) + (*current_liboctave_error_handler) + ("%s: invalid permutation vector", inv ? "ipermute" : "permute"); + + dv_new.resize (perm_vec_len); + + // Append singleton dimensions as needed. + dv.resize (perm_vec_len, 1); + + const Array<T> tmp = reshape (dv); // Need this array to check for identical elements in permutation array. - Array<bool> checked (nd, false); + Array<bool> checked (perm_vec_len, false); // Find dimension vector of permuted array. - for (int i = 0; i < nd; i++) + for (int i = 0; i < perm_vec_len; i++) { - int perm_el = perm_vec.elem (i); - - if (perm_el > dv.length () || perm_el < 1) + int perm_elt = perm_vec.elem (i); + + if (perm_elt >= perm_vec_len || perm_elt < 0) { (*current_liboctave_error_handler) - ("permutation vector contains an invalid element"); + ("%s: permutation vector contains an invalid element", + inv ? "ipermute" : "permute"); return retval; } - if (checked.elem(perm_el - 1)) + if (checked.elem(perm_elt)) { (*current_liboctave_error_handler) - ("PERM cannot contain identical elements"); + ("%s: permutation vector cannot contain identical elements", + inv ? "ipermute" : "permute"); return retval; } else - checked.elem(perm_el - 1) = true; - - dv_new (i) = dv (perm_el - 1); + checked.elem(perm_elt) = true; + + dv_new(i) = dv(perm_elt); } retval.resize (dv_new); // Index array to the original array. - Array<int> old_idx (nd, 0); + Array<int> old_idx (perm_vec_len, 0); // Number of elements in Array (should be the same for // both the permuted array and original array). @@ -478,7 +489,7 @@ // Get the idx of permuted array. Array<int> new_idx = calc_permutated_idx (old_idx, perm_vec, inv); - retval.elem (new_idx) = elem (old_idx); + retval.elem (new_idx) = tmp.elem (old_idx); increment_index (old_idx, dv); }
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,11 @@ +2005-02-18 John W. Eaton <jwe@octave.org> + + * Array.cc (Array<T>::permute): Allow permutation vector longer + than number of dimenensions of permuted matrix. + + * Array.cc (Array<T>::permute): Use zero-based indexing for perm_vec. + * Array-util.cc (calc_permutated_idx): Likewise. + 2005-02-10 David Bateman <dbateman@free.fr> * CNDArray.cc (ComplexNDarray::operator !): Change sense of test.
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2005-02-18 John W. Eaton <jwe@octave.org> + + * data.cc (do_permute): Use zero-based indexing for permutation + vector that is passed to octave_value::permute method. + Allow permutation vector longer than number of dimenensions of + permuted matrix. + 2005-02-16 John W. Eaton <jwe@octave.org> * ov-base-mat.h (octave_base_matrix::squeeze): Explicitly convert
--- a/src/data.cc +++ b/src/data.cc @@ -863,10 +863,18 @@ { octave_value retval; - if (args.length () == 2 && args(1).length () == args(0).dims ().length ()) + if (args.length () == 2 && args(1).length () >= args(1).ndims ()) { Array<int> vec = args(1).int_vector_value (); + // XXX FIXME XXX -- maybe we shoudl create an idx_vector object + // here and pass that to permute? + + int n = vec.length (); + + for (int i = 0; i < n; i++) + vec(i)--; + octave_value ret = args(0).permute (vec, inv); if (! error_state)