changeset 8799:f6dc6eb57045

improve resize & resize on assignment
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 18 Feb 2009 11:26:56 +0100
parents 53f8fca6dd69
children 19a3769fe716
files liboctave/Array.cc liboctave/ChangeLog src/ChangeLog src/data.cc
diffstat 4 files changed, 46 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/Array.cc
+++ b/liboctave/Array.cc
@@ -1093,7 +1093,7 @@
     resize (dv(0), dv(1), rfv);
   else if (dimensions != dv)
     {
-      if (dimensions.length () <= dvl)
+      if (dimensions.length () <= dvl && ! dv.any_neg ())
         {
           Array<T> tmp (dv);
           // Prepare for recursive resizing.
--- a/liboctave/ChangeLog
+++ b/liboctave/ChangeLog
@@ -1,3 +1,8 @@
+2009-02-18  Jaroslav Hajek  <highegg@gmail.com>
+
+	* Array.cc (Array<T>::resize (const dim_vector&)): Check for negative
+	dimensions.
+
 2009-02-18  Jaroslav Hajek  <highegg@gmail.com>
 
 	* oct-inttypes.cc (pow (const octave_int<T>&, const octave_int<T>&)):
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2009-02-18  Jaroslav Hajek  <highegg@gmail.com>
+
+	* data.cc (Fresize): Allow arbitrary number of parameters. Improve
+	documentation.
+
 2009-02-17  Benjamin Lindner  <lindnerb@users.sourceforge.net>
 
 	* file-io.cc: (Fmkstemp): Use mkstemps if it is available and
--- a/src/data.cc
+++ b/src/data.cc
@@ -4385,16 +4385,41 @@
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} resize (@var{x}, @var{m})\n\
 @deftypefnx {Built-in Function} {} resize (@var{x}, @var{m}, @var{n})\n\
-Destructively resize @var{x}.\n\
+@deftypefnx {Built-in Function} {} resize (@var{x}, @var{m}, @var{n}, @dots{})\n\
+Resize @var{x} cutting off elements as necessary.\n\
+\n\
+In the result, element with certain indices is equal to the corresponding\n\
+element of @var{x} if the indices are within the bounds of @var{x};\n\
+otherwise, the element is set to zero.\n\
+\n\
+In other words, the statement\n\
+\n\
+@example\n\
+  y = resize (x, dv);\n\
+@end example\n\
 \n\
-@strong{Values in @var{x} are not preserved as they are with\n\
-@code{reshape}.}\n\
+@noindent\n\
+is equivalent to the following code:\n\
+\n\
+@example\n\
+  y = zeros (dv, class (x));\n\
+  sz = min (dv, size (x));\n\
+  for i = 1:length (sz), idx@{i@} = 1:sz(i); endfor\n\
+  y(idx@{:@}) = x(idx@{:@});\n\
+@end example\n\
+\n\
+@noindent\n\
+but is performed more efficiently.\n\
 \n\
 If only @var{m} is supplied and it is a scalar, the dimension of the\n\
 result is @var{m}-by-@var{m}.  If @var{m} is a vector, then the\n\
 dimensions of the result are given by the elements of @var{m}.\n\
 If both @var{m} and @var{n} are scalars, then the dimensions of\n\
 the result are @var{m}-by-@var{n}.\n\
+\n\
+An object can be resized to more dimensions than it has;\n\
+in such case the missing dimensions are assumed to be 1.\n\
+Resizing an object to fewer dimensions is not possible.\n\
 @seealso{reshape, postpad}\n\
 @end deftypefn")
 {
@@ -4421,17 +4446,18 @@
 	  retval = retval.resize (dv, true);
 	}
     }
-  else if (nargin == 3)
+  else if (nargin > 2)
     {
-      octave_idx_type m = static_cast<octave_idx_type> 
-	(args(1).scalar_value());
-      octave_idx_type n = static_cast<octave_idx_type> 
-	(args(2).scalar_value());
+      dim_vector dv;
+      dv.resize (nargin - 1);
+      for (octave_idx_type i = 1; i < nargin; i++)
+        dv(i-1) = static_cast<octave_idx_type> (args(i).scalar_value ());
       if (!error_state)
 	{
 	  retval = args(0);
-	  retval = retval.resize (dim_vector (m, n), true);
+	  retval = retval.resize (dv, true);
 	}
+
     }
   else
     print_usage ();