changeset 4945:4bd917f8a4a7

[project @ 2004-08-31 15:09:17 by jwe]
author jwe
date Tue, 31 Aug 2004 15:09:17 +0000
parents 44046bbaa52c
children 48a39e2b2ab7
files scripts/ChangeLog scripts/general/repmat.m scripts/plot/axis.m src/ChangeLog src/data.cc
diffstat 5 files changed, 202 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Aug 31 05:30:47 2004 +0000
+++ b/scripts/ChangeLog	Tue Aug 31 15:09:17 2004 +0000
@@ -1,3 +1,11 @@
+2004-08-31  2004-08-30 David Bateman  <dbateman@free.fr>
+
+	* (general/repmat.m): Adapt to allow integer types.
+
+2004-08-31  Paul Kienzle  <pkienzle@users.sf.net>
+
+	* plot/axis.m: Don't reset axes when querying them.
+
 2004-08-27  David Bateman  <dbateman@free.fr>
 
 	* statistics/base/ranks.m: Handle non-consecutive ties.
--- a/scripts/general/repmat.m	Tue Aug 31 05:30:47 2004 +0000
+++ b/scripts/general/repmat.m	Tue Aug 31 15:09:17 2004 +0000
@@ -55,13 +55,17 @@
     if (isstr (a))
       x = setstr (toascii (a) * ones (idx));
     else
-      x = a * ones(idx);
+      x = a * ones(idx, class(a));
     endif
   elseif (ndims (a) == 2 && length (idx) < 3)
     if (isstr (a))
       x = setstr (kron (ones (idx), toascii (a)));
+    elseif (strcmp (class(a), "double")) 
+      x = kron (ones (idx), a);
     else
-      x = kron (ones (idx), a);
+      aidx = size(a);
+      x = a (kron (ones (1, idx(1)), 1:aidx(1)),  
+	     kron (ones (1, idx(2)), 1:aidx(2)));
     endif
   else
     aidx = size(a);
@@ -70,7 +74,7 @@
     elseif (length(aidx) < length(idx))
       aidx = [aidx, ones(1,length(idx)-length(aidx))];
     endif
-    cidx = cell ();
+    cidx = cell (1, length (aidx));
     for i=1:length(aidx)
       cidx{i} = kron (ones (1, idx(i)), 1:aidx(i));
     endfor
--- a/scripts/plot/axis.m	Tue Aug 31 05:30:47 2004 +0000
+++ b/scripts/plot/axis.m	Tue Aug 31 15:09:17 2004 +0000
@@ -31,7 +31,10 @@
 ## automatically by setting the built-in variable @code{automatic_replot}
 ## to a nonzero value.
 ##
-## Without any arguments, @code{axis} turns autoscaling on.
+## Without any arguments, @code{axis} turns autoscaling on.  
+##
+## With one output argument, @code{x=axis} returns the current axes 
+## (this is not yet implemented for automatic axes).
 ##
 ## The vector argument specifying limits is optional, and additional
 ## string arguments may be used to specify various axis properties.  For
@@ -110,6 +113,7 @@
 ## @item "xy" 
 ## Restore y-axis, so higher values are nearer the top. 
 ## @end table
+## 
 ## @end deftypefn
 
 ## Author: jwe
@@ -129,8 +133,11 @@
   ## limits.
 
   if (nargin == 0)
-    gset autoscale;
-    curr_axis = __current_axis__;
+    if (nargout == 0)
+      gset autoscale;
+    else
+      curr_axis = __current_axis__;
+    endif
 
   elseif (isstr (ax))
     ax = tolower (ax);
--- a/src/ChangeLog	Tue Aug 31 05:30:47 2004 +0000
+++ b/src/ChangeLog	Tue Aug 31 15:09:17 2004 +0000
@@ -1,3 +1,14 @@
+2004-08-31  David Bateman  <dbateman@free.fr>
+
+	* data.cc (Fzeros, Fones, Feye): Update help text for optional
+	class argument.
+	(static octave_value identity_matrix (int, int, const std::string&)):
+	New function to create identity matrices with an arbitrary type.
+	(Feye): Call new version of identity matrix function, even for scalars.
+	(static octave_value fill_matrix (const octave_value_list&, double,
+	const char *)): Update to allow arbitrary classes of matrices to be
+	created.
+
 2004-08-31  John W. Eaton  <jwe@octave.org>
 
 	* ls-mat5.cc (read_int): New function.
--- a/src/data.cc	Tue Aug 31 05:30:47 2004 +0000
+++ b/src/data.cc	Tue Aug 31 15:09:17 2004 +0000
@@ -1208,8 +1208,9 @@
 
   int nargin = args.length ();
 
+  std::string nm = "double";
+
   int ndim = 0;
-  int type = 0;
 
   dim_vector dims;
   
@@ -1217,15 +1218,13 @@
 
   if (nargin > 0 && args(nargin-1).is_string ())
     {
+      nm = args(nargin-1).string_value();
       nargin--;
 
-      // XXX FIXME XXX -- allow type of the resulting matrix to be
-      // specified, e.g.
-      //
-      //   zeros(n1, n2, ..., 'real')
-      //   zeros(n1, n2, ..., 'complex')
-      //
-      // type = get_type (args(nargin).string_value ());
+      if (nm != "int8" && nm != "int16" && nm != "int32" && nm != "int64" &&
+	  nm != "uint8" && nm != "uint16" && nm != "uint32" && nm != "uint64"
+	  && nm != "double")
+	error ("%s: Unrecognized or illegal classname", fcn);
     }
 
   // determine matrix dimension
@@ -1234,7 +1233,6 @@
     {
     case 0:
       ndim = 0;
-      type = 0;
       break;
 
     case 1:
@@ -1277,26 +1275,62 @@
 
       if (! error_state)
 	{
-	  // Construct either scalar, matrix or N-d array.
 
-	  switch (ndim)
-	    {
-	    case 0:
-	      retval = val;
-	      break;
+#define INT_FILL_MATRIX(TYPE) \
+	  { \
+	    switch (ndim)	\
+	      { \
+	      case 0: \
+	        retval = octave_ ## TYPE (val); \
+	        break; \
+		  \
+	      default: \
+	        retval = TYPE ## NDArray (dims, val); \
+	        break; \
+	      } \
+	  }
 
-	    case 1:
-	      retval = Matrix (dims(0), dims(0), val);
-	      break;
+	  if (nm == "int8")
+	    INT_FILL_MATRIX (int8)
+	  else if (nm == "int16")
+	    INT_FILL_MATRIX (int16)
+	  else if (nm == "int32")
+	    INT_FILL_MATRIX (int32)
+	  else if (nm == "int64")
+	    INT_FILL_MATRIX (int64)
+	  else if (nm == "uint8")
+	    INT_FILL_MATRIX (uint8)
+	  else if (nm == "uint16")
+	    INT_FILL_MATRIX (uint16)
+	  else if (nm == "uint32")
+	    INT_FILL_MATRIX (uint32)
+	  else if (nm == "uint64")
+	    INT_FILL_MATRIX (uint64)
+	  else
+	    {
+	      // Construct either scalar, matrix or N-d array.
+	      switch (ndim)
+		{
+		case 0:
+		  retval = val;
+		  break;
 
-	    case 2:
-	      retval = Matrix (dims(0), dims(1), val);
-	      break;
+		case 1:
+		  retval = Matrix (dims(0), dims(0), val);
+		  break;
+
+		case 2:
+		  retval = Matrix (dims(0), dims(1), val);
+		  break;
 
-	    default:
-	      retval = NDArray (dims, val);
-	      break;
+		default:
+		  retval = NDArray (dims, val);
+		  break;
+		}
 	    }
+
+#undef INT_FILL_MATRIX
+
 	}
     }
 
@@ -1308,6 +1342,7 @@
 @deftypefn {Built-in Function} {} ones (@var{x})\n\
 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\
 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k},...)\n\
+@deftypefnx {Built-in Function} {} ones (..., @var{class})\n\
 Return a matrix or N-dimensional array whose elements are all 1.\n\
 The arguments are handled the same as the arguments for @code{eye}.\n\
 \n\
@@ -1317,6 +1352,13 @@
 @example\n\
 val_matrix = val * ones (n, m)\n\
 @end example\n\
+\n\
+The optional argument @var{class}, allows @code{ones} to return an array of\n\
+the specified type, like\n\
+\n\
+@example\n\
+val = ones (n,m, \"uint8\")\n\
+@end example\n\
 @end deftypefn")
 {
   return fill_matrix (args, 1.0, "ones");
@@ -1327,17 +1369,92 @@
 @deftypefn {Built-in Function} {} zeros (@var{x})\n\
 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\
 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k},...)\n\
+@deftypefnx {Built-in Function} {} zeros (..., @var{class})\n\
 Return a matrix or N-dimensional array whose elements are all 0.\n\
 The arguments are handled the same as the arguments for @code{eye}.\n\
+\n\
+The optional argument @var{class}, allows @code{zeros} to return an array of\n\
+the specified type, like\n\
+\n\
+@example\n\
+val = zeros (n,m, \"uint8\")\n\
+@end example\n\
 @end deftypefn")
 {
   return fill_matrix (args, 0.0, "zeros");
 }
 
+static octave_value
+identity_matrix (int nr, int nc, const std::string& nm)
+{
+  octave_value retval;
+
+#define INT_EYE_MATRIX(TYPE) \
+  { \
+    if (nr == 1 && nc == 1) \
+      retval = octave_ ## TYPE (1); \
+    else \
+      { \
+	dim_vector dims (nr, nc); \
+	TYPE ## NDArray m (dims, octave_ ## TYPE (0));\
+	if (nr > 0 && nc > 0) \
+	  { \
+	    int n = std::min (nr, nc); \
+		\
+	    for (int i = 0; i < n; i++)	\
+	      m (i, i) = octave_ ## TYPE (1); \
+	  } \
+	retval = m; \
+      } \
+  }
+
+  if (nm == "int8")
+    INT_EYE_MATRIX (int8)
+  else if (nm == "int16")
+    INT_EYE_MATRIX (int16)
+  else if (nm == "int32")
+    INT_EYE_MATRIX (int32)
+  else if (nm == "int64")
+    INT_EYE_MATRIX (int64)
+  else if (nm == "uint8")
+    INT_EYE_MATRIX (uint8)
+  else if (nm == "uint16")
+    INT_EYE_MATRIX (uint16)
+  else if (nm == "uint32")
+    INT_EYE_MATRIX (uint32)
+  else if (nm == "uint64")
+    INT_EYE_MATRIX (uint64)
+  else
+    {
+      if (nr == 1 && nc == 1)
+	retval = 1.0;
+      else
+	{
+
+	  Matrix m (nr, nc, 0.0);
+      
+	  if (nr > 0 && nc > 0)
+	    {
+	      int n = std::min (nr, nc);
+
+	      for (int i = 0; i < n; i++)
+		m (i, i) = 1.0;
+	    }
+
+	  retval = m;
+	}
+    }
+
+#undef INT_EYE_MATRIX
+
+  return retval;
+}
+
 DEFUN (eye, args, ,
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} eye (@var{x})\n\
 @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})\n\
+@deftypefnx {Built-in Function} {} eye (..., @var{class})\n\
 Return an identity matrix.  If invoked with a single scalar argument,\n\
 @code{eye} returns a square matrix with the dimension specified.  If you\n\
 supply two scalar arguments, @code{eye} takes them to be the number of\n\
@@ -1366,18 +1483,40 @@
 @end group\n\
 @end example\n\
 \n\
+The optional argument @var{class}, allows @code{eye} to return an array of\n\
+the specified type, like\n\
+\n\
+@example\n\
+val = zeros (n,m, \"uint8\")\n\
+@end example\n\
+\n\
 For compatibility with @sc{Matlab}, calling @code{eye} with no arguments\n\
 is equivalent to calling it with an argument of 1.\n\
 @end deftypefn")
 {
   octave_value retval;
 
+  std::string nm = "double";
+
   int nargin = args.length ();
 
+  // Check for type information.
+
+  if (nargin > 0 && args(nargin-1).is_string ())
+    {
+      nm = args(nargin-1).string_value();
+      nargin--;
+
+      if (nm != "int8" && nm != "int16" && nm != "int32" && nm != "int64" &&
+	  nm != "uint8" && nm != "uint16" && nm != "uint32" && nm != "uint64"
+	  && nm != "double")
+	error ("eye: Unrecognized or illegal classname");
+    }
+
   switch (nargin)
     {
     case 0:
-      retval = 1.0;
+      retval = identity_matrix (1, 1, nm);
       break;
 
     case 1:
@@ -1386,7 +1525,7 @@
 	get_dimensions (args(0), "eye", nr, nc);
 
 	if (! error_state)
-	  retval = identity_matrix (nr, nc);
+	  retval = identity_matrix (nr, nc, nm);
       }
       break;
 
@@ -1396,7 +1535,7 @@
 	get_dimensions (args(0), args(1), "eye", nr, nc);
 
 	if (! error_state)
-	  retval = identity_matrix (nr, nc);
+	  retval = identity_matrix (nr, nc, nm);
       }
       break;