# HG changeset patch # User jwe # Date 1076001988 0 # Node ID 24c7bc6354bac8a3f090d05d0a8d2d44d30eca4c # Parent f0939599fb7fff34238c4312f8d0f41f8e6202cb [project @ 2004-02-05 17:26:28 by jwe] diff -r f0939599fb7f -r 24c7bc6354ba liboctave/Array.cc --- a/liboctave/Array.cc Wed Feb 04 21:23:08 2004 +0000 +++ b/liboctave/Array.cc Thu Feb 05 17:26:28 2004 +0000 @@ -2746,14 +2746,29 @@ { // RHS is matrix or higher dimension. - // Check that RHS dimensions are the same length as the - // corresponding LHS index dimensions. - - dim_vector t_rhs_dims = rhs_dims; - t_rhs_dims.chop_trailing_singletons (); - - dim_vector t_frozen_len = frozen_len; - t_frozen_len.chop_trailing_singletons (); + // Check that non-singleton RHS dimensions conform to + // non-singleton LHS index dimensions. + + dim_vector t_rhs_dims = rhs_dims.squeeze (); + dim_vector t_frozen_len = frozen_len.squeeze (); + + // If after sqeezing out singleton dimensions, RHS is vector + // and LHS is vector, force them to have the same orientation + // so that operations like + // + // a = zeros (3, 3, 3); + // a(1:3,1,1) = [1,2,3]; + // + // will work. + + if (t_rhs_dims.length () == 2 && t_frozen_len.length () == 2 + && (t_rhs_dims.elem(1) == 1 && t_frozen_len.elem(0) == 1 + || t_rhs_dims.elem(0) == 1 && t_frozen_len.elem(1) == 1)) + { + int t0 = t_rhs_dims.elem(0); + t_rhs_dims.elem(0) = t_rhs_dims.elem(1); + t_rhs_dims.elem(1) = t0; + } if (t_rhs_dims != t_frozen_len) (*current_liboctave_error_handler) diff -r f0939599fb7f -r 24c7bc6354ba liboctave/ChangeLog --- a/liboctave/ChangeLog Wed Feb 04 21:23:08 2004 +0000 +++ b/liboctave/ChangeLog Thu Feb 05 17:26:28 2004 +0000 @@ -1,5 +1,17 @@ +2004-02-05 Petter Risholm + + * Array.cc (Array::assignN): Accept assignment of a vector + oriented differently from the index. + + * dim-vector.h (dim_vector::squeeze): Return value always has at + least two dimensions. + 2004-02-04 John W. Eaton + * dim-vector.h (dim_vector::squeeze): New function. + (Array::assignN): Use it instead of chop_trailing_singltons for + deciding whether the assignment conforms. + * Array.cc (Array::assignN): Simplify dimension check by comparing rhs_dims and frozen_len sans trailing singletons. diff -r f0939599fb7f -r 24c7bc6354ba liboctave/dim-vector.h --- a/liboctave/dim-vector.h Wed Feb 04 21:23:08 2004 +0000 +++ b/liboctave/dim-vector.h Thu Feb 05 17:26:28 2004 +0000 @@ -303,6 +303,60 @@ make_unique (); rep->chop_trailing_singletons (); } + + dim_vector squeeze (void) const + { + dim_vector new_dims = *this; + + bool dims_changed = 1; + + int k = 0; + + for (int i = 0; i < length (); i++) + { + if (elem (i) == 1) + dims_changed = true; + else + new_dims(k++) = elem (i); + } + + if (dims_changed) + { + if (k == 0) + new_dims = dim_vector (1, 1); + else if (k == 1) + { + // There is one non-singleton dimension, so we need + // to decide the correct orientation. + + if (elem (0) == 1) + { + // The original dimension vector had a leading + // singleton dimension. + + int tmp = new_dims(0); + + new_dims.resize (2); + + new_dims(0) = 1; + new_dims(1) = tmp; + } + else + { + // The first element of the original dimension vector + // was not a singleton dimension. + + new_dims.resize (2); + + new_dims(1) = 1; + } + } + else + new_dims.resize(k); + } + + return new_dims; + } }; static inline bool