changeset 4735:24c7bc6354ba

[project @ 2004-02-05 17:26:28 by jwe]
author jwe
date Thu, 05 Feb 2004 17:26:28 +0000
parents f0939599fb7f
children 4179c59d82da
files liboctave/Array.cc liboctave/ChangeLog liboctave/dim-vector.h
diffstat 3 files changed, 89 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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  <risholm@stud.ntnu.no>
+
+	* Array.cc (Array<T>::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  <jwe@bevo.che.wisc.edu>
 
+	* dim-vector.h (dim_vector::squeeze): New function.
+	(Array<T>::assignN): Use it instead of chop_trailing_singltons for
+	deciding whether the assignment conforms.
+
 	* Array.cc (Array<T>::assignN): Simplify dimension check by
 	comparing rhs_dims and frozen_len sans trailing singletons.
 
--- 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