changeset 7112:f90a8188c9c2

[project @ 2007-11-07 00:24:11 by dbateman]
author dbateman
date Wed, 07 Nov 2007 00:24:12 +0000
parents 9c73ef1819c7
children a018c140f8aa
files scripts/ChangeLog scripts/plot/hist.m src/ChangeLog src/data.cc
diffstat 4 files changed, 149 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Nov 06 22:47:35 2007 +0000
+++ b/scripts/ChangeLog	Wed Nov 07 00:24:12 2007 +0000
@@ -1,3 +1,8 @@
+2007-11-06  David Bateman  <dbateman@free.fr>
+
+	* plot/hist.m: Pass any additional arguments to bar for
+	treatment. Create a default x value that is always a vector.
+
 2007-11-06  Thomas Treichl  <Thomas.Treichl@gmx.net>
 
 	* pkg/pkg.m.m: Check for environment variables CC, CXX, AR, RANLIB
@@ -18,7 +23,7 @@
 
 	* startup/inputrc: Delete key bindings starting with \340 code.
 
-2007-11-06  David Bateman  <dbateman@free.fr>
+2007-11-05  David Bateman  <dbateman@free.fr>
 
 	* linear-algebra/__norm__.m: Scale frobenius norm by infinity norm
 	to avoid issues of over- and underflow.  From Rolf Fabian
--- a/scripts/plot/hist.m	Tue Nov 06 22:47:35 2007 +0000
+++ b/scripts/plot/hist.m	Wed Nov 07 00:24:12 2007 +0000
@@ -43,9 +43,9 @@
 
 ## Author: jwe
 
-function [nn, xx] = hist (y, x, norm)
+function [nn, xx] = hist (y, varargin)
 
-  if (nargin < 1 || nargin > 3)
+  if (nargin < 1)
     print_usage ();
   endif
 
@@ -56,18 +56,20 @@
   endif
 
   if (isreal (y))
-    max_val = max (y);
-    min_val = min (y);
+    max_val = max (y(:));
+    min_val = min (y(:));
   else
     error ("hist: first argument must be a vector");
   endif
 
-  if (nargin == 1)
+  iarg = 1;
+  if (nargin == 1 || ischar (varargin{iarg}))
     n = 10;
     x = [0.5:n]'/n;
     x = x * (max_val - min_val) + ones(size(x)) * min_val;
   else
     ## nargin is either 2 or 3
+    x = varargin {iarg++};
     if (isscalar (x))
       n = x;
       if (n <= 0)
@@ -113,8 +115,9 @@
 
   freq = diff (chist);
 
-  if (nargin == 3)
+  if (nargin > 2 && !ischar (varargin{iarg}))
     ## Normalise the histogram.
+    norm = varargin{iarg++};
     freq = freq / rows (y) * norm;
   endif
 
@@ -126,8 +129,10 @@
       nn = freq;
       xx = x;
     endif
+  elseif (size (freq, 2) != 1)
+    bar (x, freq, 0.8, varargin{iarg:end});
   else
-    bar (x, freq, 1.0);
+    bar (x, freq, 1.0, varargin{iarg:end});
   endif
 
 endfunction
@@ -146,7 +151,7 @@
 %!  assert(nn, [3,2,1]);
 %!test
 %!  [nn,xx]=hist([[1:4]',[1:4]'],3);
-%!  assert(xx, [[1.5,2.5,3.5]',[1.5,2.5,3.5]']);
+%!  assert(xx, [1.5;2.5;3.5]);
 %!  assert(nn, [[2,1,1]',[2,1,1]']);
 %!assert(hist(1,1),1);
 %!test
--- a/src/ChangeLog	Tue Nov 06 22:47:35 2007 +0000
+++ b/src/ChangeLog	Wed Nov 07 00:24:12 2007 +0000
@@ -9,6 +9,8 @@
 
 2007-11-06  David Bateman  <dbateman@free.fr>
 
+	* data.cc (DATA_REDUCTION): Handle the 'native' and 'double'
+	arguments of the Fsum function.
 	* OPERATORS/op-bm-bm.cc (matrix_to_bool_matrix,
 	scalar_to_bool_matrix): New type conversion functions.
 	(install_bm_bm_ops): Install new type conversions functions.
--- a/src/data.cc	Tue Nov 06 22:47:35 2007 +0000
+++ b/src/data.cc	Wed Nov 07 00:24:12 2007 +0000
@@ -370,6 +370,112 @@
   return retval;
 }
 
+#define NATIVE_REDUCTION_1(FCN, TYPE, DIM) \
+  (arg.is_ ## TYPE ## _type ()) \
+    { \
+      TYPE ## NDArray tmp = arg. TYPE ##_array_value (); \
+      \
+      if (! error_state) \
+        retval = tmp.FCN (DIM); \
+    }
+
+#define NATIVE_REDUCTION(FCN) \
+ \
+  octave_value retval; \
+ \
+  int nargin = args.length (); \
+ \
+  bool isnative = false; \
+  \
+  if (nargin > 1 && args(nargin - 1).is_string ()) \
+    { \
+      std::string str = args(nargin - 1).string_value (); \
+      \
+      if (! error_state) \
+	{ \
+	  if (str == "native") \
+	    isnative = true; \
+	  else if (str != "double") /* Ignore double as no single type */ \
+	    error ("sum: unrecognized string argument"); \
+          nargin --; \
+	} \
+    } \
+  \
+  if (nargin == 1 || nargin == 2) \
+    { \
+      octave_value arg = args(0); \
+ \
+      int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \
+ \
+      if (! error_state) \
+	{ \
+	  if (dim >= -1) \
+	    { \
+              if (isnative) \
+                { \
+                  if NATIVE_REDUCTION_1 (FCN, uint8, dim) \
+                  else if NATIVE_REDUCTION_1 (FCN, uint16, dim) \
+                  else if NATIVE_REDUCTION_1 (FCN, uint32, dim) \
+                  else if NATIVE_REDUCTION_1 (FCN, uint64, dim) \
+                  else if NATIVE_REDUCTION_1 (FCN, int8, dim) \
+                  else if NATIVE_REDUCTION_1 (FCN, int16, dim) \
+                  else if NATIVE_REDUCTION_1 (FCN, int32, dim) \
+                  else if NATIVE_REDUCTION_1 (FCN, int64, dim) \
+                  else if NATIVE_REDUCTION_1 (FCN, bool, dim) \
+                  else if (arg.is_char_matrix ()) \
+                    { \
+                       error (#FCN, ": invalid char type"); \
+                    } \
+	          else if (arg.is_complex_type ()) \
+		    { \
+		      ComplexNDArray tmp = arg.complex_array_value (); \
+                      \
+		      if (! error_state) \
+		        retval = tmp.FCN (dim); \
+		    } \
+	          else if (arg.is_real_type ()) \
+		    { \
+		      NDArray tmp = arg.array_value (); \
+                      \
+		      if (! error_state) \
+		        retval = tmp.FCN (dim); \
+		    } \
+                  else \
+		    { \
+		      gripe_wrong_type_arg (#FCN, arg); \
+		      return retval; \
+		    } \
+                } \
+	      else if (arg.is_real_type ()) \
+		{ \
+		  NDArray tmp = arg.array_value (); \
+                  \
+		  if (! error_state) \
+		    retval = tmp.FCN (dim); \
+		} \
+	      else if (arg.is_complex_type ()) \
+		{ \
+		  ComplexNDArray tmp = arg.complex_array_value (); \
+                  \
+		  if (! error_state) \
+		    retval = tmp.FCN (dim); \
+		} \
+	      else \
+		{ \
+		  gripe_wrong_type_arg (#FCN, arg); \
+		  return retval; \
+		} \
+	    } \
+	  else \
+	    error (#FCN ": invalid dimension argument = %d", dim + 1); \
+	} \
+      \
+    } \
+  else \
+    print_usage (); \
+ \
+  return retval
+
 #define DATA_REDUCTION(FCN) \
  \
   octave_value retval; \
@@ -1213,16 +1319,37 @@
 DEFUN (sum, args, ,
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\
+@deftypefnx {Built-in Function} {} sum (@dots{}, 'native')\n\
 Sum of elements along dimension @var{dim}.  If @var{dim} is\n\
 omitted, it defaults to 1 (column-wise sum).\n\
 \n\
 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\
 return the sum of the elements.\n\
+\n\
+If the optional argument 'native' is given, then the sum is performed\n\
+in the same type as the original argument, rather than in the default\n\
+double type. For example\n\
+\n\
+@example\n\
+sum ([true, true])\n\
+  @result{} 2\n\
+sum ([true, true], 'native')\n\
+  @result{} true\n\
+@end example\n\
 @end deftypefn")
 {
-  DATA_REDUCTION (sum);
+  NATIVE_REDUCTION (sum);
 }
 
+/*
+
+%!assert (sum([true,true]), 2)
+%!assert (sum([true,true],'native'), true)
+%!assert (sum(int8([127,10,-20])), 117);
+%!assert (sum(int8([127,10,-20]),'native'), int8(107));
+
+*/
+
 DEFUN (sumsq, args, ,
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} sumsq (@var{x}, @var{dim})\n\