Mercurial > hg > octave-terminal
changeset 4887:bbddd4339cf2
[project @ 2004-04-30 17:08:55 by jwe]
author | jwe |
---|---|
date | Fri, 30 Apr 2004 17:08:55 +0000 |
parents | 54b076a24718 |
children | 4a796c5fc7a3 |
files | liboctave/Array-util.cc liboctave/Array.cc liboctave/ChangeLog liboctave/dim-vector.h test/octave.test/index-wfi-f/index.exp test/octave.test/index-wfi-f/misc.m test/octave.test/stats/kurtosis-3.m test/octave.test/stats/skewness-3.m |
diffstat | 8 files changed, 169 insertions(+), 78 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/Array-util.cc +++ b/liboctave/Array-util.cc @@ -422,28 +422,24 @@ else { int last_ra_idx = ra_idx(n-1)(0); + for (int i = 1; i < ra_idx (n - 1).capacity (); i++) + last_ra_idx = (last_ra_idx > ra_idx(n-1)(i) ? last_ra_idx : + ra_idx(n-1)(i)); - if (last_ra_idx < dimensions(n - 1)) + if (last_ra_idx < size_left) { - retval(n - 1) = ra_idx(n - 1).freeze (dimensions(n-1), - "dimension", resize_ok); + retval(n-1) = ra_idx(n-1).freeze (size_left, + "dimension", resize_ok); } else { - if (size_left <= last_ra_idx) - { - // Make it larger than it should be to get an error - // later. - - retval.resize(n_dims + 1); + // Make it larger than it should be to get an error + // later. - (*current_liboctave_error_handler) - ("index exceeds N-d array dimensions"); - } - else - { - retval(n-1) = 1; - } + retval.resize (n_dims+1); + + (*current_liboctave_error_handler) + ("index exceeds N-d array dimensions"); } } }
--- a/liboctave/Array.cc +++ b/liboctave/Array.cc @@ -2286,78 +2286,82 @@ int ra_idx_len = ra_idx.length (); - while (ra_idx_len > n_dims) + bool trim_trailing_singletons = true; + for (int j = ra_idx_len; j > n_dims; j--) { - if (ra_idx(ra_idx_len-1) == 1) + idx_vector iidx = ra_idx (ra_idx_len-1); + if (iidx.capacity () == 1 && trim_trailing_singletons) ra_idx_len--; else - break; + trim_trailing_singletons = false; + + for (int i = 0; i < iidx.capacity (); i++) + if (iidx (i) != 0) + { + (*current_liboctave_error_handler) + ("index exceeds N-d array dimensions"); + + return retval; + } } ra_idx.resize (ra_idx_len); - if (n_dims < ra_idx.length ()) + dim_vector new_dims = dims (); + dim_vector frozen_lengths; + + if (! any_orig_empty (ra_idx) && ra_idx_len < n_dims) + frozen_lengths = short_freeze (ra_idx, dimensions, resize_ok); + else { - (*current_liboctave_error_handler) - ("index exceeds N-d array dimensions"); - - return retval; + new_dims.resize (ra_idx_len, 1); + frozen_lengths = freeze (ra_idx, new_dims, resize_ok); } - dim_vector frozen_lengths = short_freeze (ra_idx, dimensions, resize_ok); - - if (frozen_lengths.length () <= n_dims) + if (all_ok (ra_idx)) { - if (all_ok (ra_idx)) + if (any_orig_empty (ra_idx) || frozen_lengths.any_zero ()) + { + frozen_lengths.chop_trailing_singletons (); + + retval.resize (frozen_lengths); + } + else if (frozen_lengths.length () == n_dims + && all_colon_equiv (ra_idx, dimensions)) + { + retval = *this; + } + else { - if (any_orig_empty (ra_idx) || frozen_lengths.any_zero ()) - { - frozen_lengths.chop_trailing_singletons (); - - retval.resize (frozen_lengths); - } - else if (frozen_lengths.length () == n_dims - && all_colon_equiv (ra_idx, dimensions)) - { - retval = *this; - } - else + dim_vector frozen_lengths_for_resize = frozen_lengths; + + frozen_lengths_for_resize.chop_trailing_singletons (); + + retval.resize (frozen_lengths_for_resize); + + int n = retval.length (); + + Array<int> result_idx (ra_idx.length (), 0); + + Array<int> elt_idx; + + for (int i = 0; i < n; i++) { - dim_vector frozen_lengths_for_resize = frozen_lengths; - - frozen_lengths_for_resize.chop_trailing_singletons (); - - retval.resize (frozen_lengths_for_resize); - - int n = retval.length (); - - Array<int> result_idx (ra_idx.length (), 0); - - dim_vector this_dims = dims (); - - Array<int> elt_idx; - - for (int i = 0; i < n; i++) - { - elt_idx = get_elt_idx (ra_idx, result_idx); - - int numelem_elt = get_scalar_idx (elt_idx, this_dims); - - if (numelem_elt > length () || numelem_elt < 0) - (*current_liboctave_error_handler) - ("invalid N-d array index"); - else - retval.elem (i) = elem (numelem_elt); - - increment_index (result_idx, frozen_lengths); - - } + elt_idx = get_elt_idx (ra_idx, result_idx); + + int numelem_elt = get_scalar_idx (elt_idx, new_dims); + + if (numelem_elt > length () || numelem_elt < 0) + (*current_liboctave_error_handler) + ("invalid N-d array index"); + else + retval.elem (i) = elem (numelem_elt); + + increment_index (result_idx, frozen_lengths); + } } } - else - (*current_liboctave_error_handler) - ("invalid number of dimensions for N-dimensional array index"); return retval; }
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,17 @@ +2004-04-30 David Bateman <dbateman@free.fr> + + * dim_vector.h (dim_vector::dim_vector_rep::dim_vector_rep): + New arg, fill_value. + (dim_vector::resize): Allow optional fill_value argument. + + * Array.cc (Array<T>::index (Array<idx_vector>&, int, const T&)): + Don't chop trailing dimensions of Array<idx_vector> if there is + more than one element in idx_vector. Resize the return value to + the size of Array<idx_vector>. + + * Array-util.cc (short_freeze): Better freeze of last dimension of + idx_vector that is shorter than a dim_vector. + 2004-04-23 John W. Eaton <jwe@octave.org> * oct-sort.cc: Don't include oct-obj.h.
--- a/liboctave/dim-vector.h +++ b/liboctave/dim-vector.h @@ -77,7 +77,7 @@ } } - dim_vector_rep (int n, const dim_vector_rep *dv) + dim_vector_rep (int n, const dim_vector_rep *dv, int fill_value = 0) : dims ((dv && n > 0) ? new int [n] : 0), ndims (n > 0 ? n : 0), count (1) { @@ -91,7 +91,7 @@ dims[i] = dv->dims[i]; for (int i = dv_ndims; i < n; i++) - dims[i] = 0; + dims[i] = fill_value; } } @@ -196,7 +196,7 @@ int operator () (int i) const { return elem (i); } - void resize (int n) + void resize (int n, int fill_value = 0) { int len = length (); @@ -204,7 +204,7 @@ { dim_vector_rep *old_rep = rep; - rep = new dim_vector_rep (n, old_rep); + rep = new dim_vector_rep (n, old_rep, fill_value); if (--old_rep->count <= 0) delete old_rep;
--- a/test/octave.test/index-wfi-f/index.exp +++ b/test/octave.test/index-wfi-f/index.exp @@ -161,3 +161,7 @@ set test index-m-4 set prog_output "^ans = 3" do_test m-4.m + +set test misc +set prog_output "^ok" +do_test misc.m
new file mode 100644 --- /dev/null +++ b/test/octave.test/index-wfi-f/misc.m @@ -0,0 +1,73 @@ +a = [1,2;3,4]; +b = a; +b(:,:,2) = [5,6;7,8]; +a1 = [1; 3; 2; 4]; +a2 = [1, 3]; +a3 = [1, 2; 3, 4]; +a4 = [1; 3]; +a5 = 1; +a6 = [1; 3]; +a7 = [1, 2; 3, 4]; +a8(:,:,1) = [1, 2; 3, 4]; +a8(:,:,2) = [1, 2; 3, 4]; +a9(:,:,1,1) = [1, 2; 3, 4]; +a9(:,:,1,2) = [1, 2; 3, 4]; +a10(:,:,1,1) = [1, 2; 3, 4]; +a10(:,:,2,1) = [1, 2; 3, 4]; +a10(:,:,1,2) = [1, 2; 3, 4]; +a10(:,:,2,2) = [1, 2; 3, 4]; +a11 = zeros (1, 0); +a12 = zeros (1, 0, 2); +a13 = zeros (1, 1, 0); +b1 = [1; 3; 2; 4; 5; 7; 6; 8]; +b2 = [1, 2, 5, 6; 3, 4, 7, 8]; +b3 = [1; 3]; +b4(:,:,1) = [1, 2; 3, 4]; +b4(:,:,2) = [5, 6; 7, 8]; +b5 = [1; 3]; +b6(:,:,1,1) = [1; 3]; +b6(:,:,1,2) = [1; 3]; +b7 = 5; +b8 = [5, 6]; +b9 = [1, 2, 5, 6]; +b10 = zeros (1, 0, 2); +b11 = zeros (1, 0); + +function retval = eq (a, b) + sza = size (a); + szb = size (b); + retval = (length (sza) == length (szb) + && all (size (a) == size (b)) + && all (a(:) == b(:))); +endfunction + +r = [eq(a(:), a1); + eq(a(1:2), a2); + eq(a(:,:), a3); + eq(a(:,1), a4); + eq(a(1,1), a5); + eq(a(1:2,1), a6); + eq(a(:,:,1), a7); + eq(a(:,:,[1,1]), a8); + eq(a(:,:,1,[1,1]), a9); + eq(a(:,:,[1,1],[1,1]), a10); + eq(a(1,[]), a11); + eq(a(1,[],[1,1]), a12); + eq(a(1,1,[]), a13); + eq(b(:), b1); + eq(b(:,:), b2); + eq(b(:,1), b3); + eq(b(:,:,:), b4); + eq(b(:,1,1), b5); + eq(b(:,1,1,[1,1]), b6); + eq(b(1,3), b7); + eq(b(1,[3,4]), b8); + eq(b(1,1:4), b9); + eq(b(1,[],:), b10); + eq(b(1,[]), b11)]; + +if (all (r)) + printf ("ok\n"); +else + find (r == 0) +endif