From bug-octave-request at bevo dot che dot wisc dot edu Wed Dec 10 10:11:34 2003 Subject: Incorrect dimensions of subarrays of ND Arrays From: "John W. Eaton" To: Quentin Spencer cc: bug-octave at bevo dot che dot wisc dot edu Date: Wed, 10 Dec 2003 10:11:32 -0600 On 9-Dec-2003, I wrote: | Please try the following patch. | | Thanks, | | jwe | | liboctave/ChangeLog: | | 2003-12-09 John W. Eaton | | * dim-vector.h (dim_vector::chop_trailing_singleton_dims, | dim_vector::dim_vector_rep::chop_trailing_singleton_dims): | New functions. | * Array.cc (ArrayN::indexN): Use it. | (ArrayN::index (Array&, int, const T&)): Likewise. Unfortunately, this caused some other trouble. An improved version is appended below. It should be used instead of the previous patch, not in addition to it. jwe liboctave/ChangeLog: 2003-12-10 John W. Eaton * Array-util.cc (get_zero_len_size): Delete. * Array.cc (Array::index (Array&, int, const T&)): Handle zero-length result dimensions the same as empty original indices. 2003-12-09 John W. Eaton * dim-vector.h (dim_vector::chop_trailing_singleton_dims, dim_vector::dim_vector_rep::chop_trailing_singleton_dims): New functions. * Array.cc (ArrayN::indexN): Use it. (ArrayN::index (Array&, int, const T&)): Likewise. Index: liboctave/dim-vector.h =================================================================== RCS file: /usr/local/cvsroot/octave/liboctave/dim-vector.h,v retrieving revision 1.9 diff -u -r1.9 dim-vector.h --- liboctave/dim-vector.h 24 Nov 2003 21:24:37 -0000 1.9 +++ liboctave/dim-vector.h 10 Dec 2003 16:09:21 -0000 at @ -111,6 +111,17 @@ return dims[i]; } + void chop_trailing_singletons (void) + { + for (int i = ndims - 1; i > 1; i--) + { + if (dims[i] == 1) + ndims--; + else + break; + } + } + private: // No assignment! at @ -200,7 +211,6 @@ } } - std::string str (char sep = 'x') const { OSSTREAM buf; at @ -287,6 +297,12 @@ return retval; } + + void chop_trailing_singletons (void) + { + make_unique (); + rep->chop_trailing_singletons (); + } }; static inline bool Index: liboctave/Array.cc =================================================================== RCS file: /usr/local/cvsroot/octave/liboctave/Array.cc,v retrieving revision 1.78 diff -u -r1.78 Array.cc --- liboctave/Array.cc 25 Nov 2003 05:41:35 -0000 1.78 +++ liboctave/Array.cc 10 Dec 2003 16:09:19 -0000 at @ -1859,6 +1859,7 @@ else { dim_vector new_dims; + new_dims.resize (n_dims); for (int i = 0; i < n_dims; i++) at @ -1867,6 +1868,8 @@ new_dims(i) = 1; } + new_dims.chop_trailing_singletons (); + retval = Array (new_dims); } } at @ -1885,6 +1888,8 @@ new_dims(i) = 1; } + new_dims.chop_trailing_singletons (); + retval = Array (tmp, new_dims); } else at @ -1917,6 +1922,8 @@ result_dims(1) = (ntot > 0 ? 1 : 0); } + result_dims.chop_trailing_singletons (); + retval.resize (result_dims); int n = number_of_elements (result_dims); at @ -2022,14 +2029,12 @@ { if (all_ok (ra_idx)) { - if (any_orig_empty (ra_idx)) + if (any_orig_empty (ra_idx) || any_zero_len (frozen_lengths)) { + frozen_lengths.chop_trailing_singletons (); + retval.resize (frozen_lengths); } - else if (any_zero_len (frozen_lengths)) - { - retval.resize (get_zero_len_size (frozen_lengths, dimensions)); - } else if (all_colon_equiv (ra_idx, dimensions) && frozen_lengths.length () == n_dims) { at @ -2037,9 +2042,13 @@ } else { - retval.resize (frozen_lengths); + dim_vector frozen_lengths_for_resize = frozen_lengths; + + frozen_lengths_for_resize.chop_trailing_singletons (); - int n = number_of_elements (frozen_lengths); + retval.resize (frozen_lengths_for_resize); + + int n = retval.length (); Array result_idx (ra_idx.length (), 0); at @ -2051,17 +2060,13 @@ { elt_idx = get_elt_idx (ra_idx, result_idx); - int numelem_result = - get_scalar_idx (result_idx, frozen_lengths); - int numelem_elt = get_scalar_idx (elt_idx, this_dims); - if (numelem_result > length () || numelem_result < 0 - || numelem_elt > length () || numelem_elt < 0) + if (numelem_elt > length () || numelem_elt < 0) (*current_liboctave_error_handler) - ("attempt to grow array along ambiguous dimension"); + ("invalid N-d array index"); else - retval.checkelem (numelem_result) = checkelem (numelem_elt); + retval.elem (i) = elem (numelem_elt); increment_index (result_idx, frozen_lengths); ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------