changeset 3723:4c3774db5b3c

[project @ 2000-10-12 05:10:08 by jwe]
author jwe
date Thu, 12 Oct 2000 05:10:09 +0000
parents ecc97b03ec09
children 3dfae66ad67c
files liboctave/CMatrix.cc liboctave/CMatrix.h liboctave/ChangeLog liboctave/dMatrix.cc liboctave/dMatrix.h src/ChangeLog src/data.cc
diffstat 7 files changed, 268 insertions(+), 404 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/CMatrix.cc	Tue Oct 10 23:41:20 2000 +0000
+++ b/liboctave/CMatrix.cc	Thu Oct 12 05:10:09 2000 +0000
@@ -2320,38 +2320,28 @@
 }
 
 ComplexMatrix
-ComplexMatrix::cumprod (void) const
+ComplexMatrix::cumprod (int dim) const
 {
   int nr = rows ();
   int nc = cols ();
-  ComplexMatrix retval;
+  ComplexMatrix retval (nr, nc);
   if (nr > 0 && nc > 0)
     {
-      if (nr == 1)
+      if ((nr == 1 && dim == 0) || dim == 1)
 	{
-	  retval.resize (1, nc);
-	  Complex prod = elem (0, 0);
-	  for (int j = 0; j < nc; j++)
-	    {
-	      retval.elem (0, j) = prod;
-	      if (j < nc - 1)
-		prod *= elem (0, j+1);
-	    }
-	}
-      else if (nc == 1)
-	{
-	  retval.resize (nr, 1);
-	  Complex prod = elem (0, 0);
 	  for (int i = 0; i < nr; i++)
 	    {
-	      retval.elem (i, 0) = prod;
-	      if (i < nr - 1)
-		prod *= elem (i+1, 0);
+	      Complex prod = elem (0, 0);
+	      for (int j = 0; j < nc; j++)
+		{
+		  retval.elem (0, j) = prod;
+		  if (j < nc - 1)
+		    prod *= elem (i, j+1);
+		}
 	    }
 	}
       else
 	{
-	  retval.resize (nr, nc);
 	  for (int j = 0; j < nc; j++)
 	    {
 	      Complex prod = elem (0, j);
@@ -2368,38 +2358,28 @@
 }
 
 ComplexMatrix
-ComplexMatrix::cumsum (void) const
+ComplexMatrix::cumsum (int dim) const
 {
   int nr = rows ();
   int nc = cols ();
-  ComplexMatrix retval;
+  ComplexMatrix retval (nr, nc);
   if (nr > 0 && nc > 0)
     {
-      if (nr == 1)
+      if ((nr == 1 && dim == 0) || dim == 1)
 	{
-	  retval.resize (1, nc);
-	  Complex sum = elem (0, 0);
-	  for (int j = 0; j < nc; j++)
-	    {
-	      retval.elem (0, j) = sum;
-	      if (j < nc - 1)
-		sum += elem (0, j+1);
-	    }
-	}
-      else if (nc == 1)
-	{
-	  retval.resize (nr, 1);
-	  Complex sum = elem (0, 0);
 	  for (int i = 0; i < nr; i++)
 	    {
-	      retval.elem (i, 0) = sum;
-	      if (i < nr - 1)
-		sum += elem (i+1, 0);
+	      Complex sum = elem (0, 0);
+	      for (int j = 0; j < nc; j++)
+		{
+		  retval.elem (i, j) = sum;
+		  if (j < nc - 1)
+		    sum += elem (i, j+1);
+		}
 	    }
 	}
       else
 	{
-	  retval.resize (nr, nc);
 	  for (int j = 0; j < nc; j++)
 	    {
 	      Complex sum = elem (0, j);
@@ -2416,26 +2396,22 @@
 }
 
 ComplexMatrix
-ComplexMatrix::prod (void) const
+ComplexMatrix::prod (int dim) const
 {
   int nr = rows ();
   int nc = cols ();
   ComplexMatrix retval;
   if (nr > 0 && nc > 0)
     {
-      if (nr == 1)
+      if ((nr == 1 && dim == 0) || dim == 1)
 	{
-	  retval.resize (1, 1);
-	  retval.elem (0, 0) = 1.0;
-	  for (int j = 0; j < nc; j++)
-	    retval.elem (0, 0) *= elem (0, j);
-	}
-      else if (nc == 1)
-	{
-	  retval.resize (1, 1);
-	  retval.elem (0, 0) = 1.0;
+	  retval.resize(nr, 1);
 	  for (int i = 0; i < nr; i++)
-	    retval.elem (0, 0) *= elem (i, 0);
+	    {
+	      retval.elem (i, 0) = 1.0;
+	      for (int j = 0; j < nc; j++)
+		retval.elem (i, 0) *= elem (i, j);
+	    }
 	}
       else
 	{
@@ -2448,30 +2424,31 @@
 	    }
 	}
     }
+  else
+    {
+      retval.resize (1,1);
+      retval.elem (0,0) = 1.0;
+    }
   return retval;
 }
 
 ComplexMatrix
-ComplexMatrix::sum (void) const
+ComplexMatrix::sum (int dim) const
 {
   int nr = rows ();
   int nc = cols ();
   ComplexMatrix retval;
   if (nr > 0 && nc > 0)
     {
-      if (nr == 1)
+      if ((nr == 1 && dim == 0) || dim == 1)
 	{
-	  retval.resize (1, 1);
-	  retval.elem (0, 0) = 0.0;
-	  for (int j = 0; j < nc; j++)
-	    retval.elem (0, 0) += elem (0, j);
-	}
-      else if (nc == 1)
-	{
-	  retval.resize (1, 1);
-	  retval.elem (0, 0) = 0.0;
+	  retval.resize (nr, 1);
 	  for (int i = 0; i < nr; i++)
-	    retval.elem (0, 0) += elem (i, 0);
+	    {
+	      retval.elem (i, 0) = 0.0;
+	      for (int j = 0; j < nc; j++)
+		retval.elem (i, 0) += elem (i, j);
+	    }
 	}
       else
 	{
@@ -2484,35 +2461,33 @@
 	    }
 	}
     }
+  else
+    {
+      retval.resize (1, 1);
+      retval.elem (0, 0) = 0.0;
+    }
   return retval;
 }
 
 ComplexMatrix
-ComplexMatrix::sumsq (void) const
+ComplexMatrix::sumsq (int dim) const
 {
   int nr = rows ();
   int nc = cols ();
   ComplexMatrix retval;
   if (nr > 0 && nc > 0)
     {
-      if (nr == 1)
+      if ((nr == 1 && dim == 0) || dim == 1)
 	{
-	  retval.resize (1, 1);
-	  retval.elem (0, 0) = 0.0;
-	  for (int j = 0; j < nc; j++)
-	    {
-	      Complex d = elem (0, j);
-	      retval.elem (0, 0) += d * conj (d);
-	    }
-	}
-      else if (nc == 1)
-	{
-	  retval.resize (1, 1);
-	  retval.elem (0, 0) = 0.0;
+	  retval.resize (nr, 1);
 	  for (int i = 0; i < nr; i++)
 	    {
-	      Complex d = elem (i, 0);
-	      retval.elem (0, 0) += d * conj (d);
+	      retval.elem (i, 0) = 0.0;
+	      for (int j = 0; j < nc; j++)
+		{
+		  Complex d = elem (i, j);
+		  retval.elem (i, 0) += d * conj (d);
+		}
 	    }
 	}
       else
@@ -2529,6 +2504,12 @@
 	    }
 	}
     }
+  else
+    {
+      retval.resize (1, 1);
+      retval.elem (0, 0) = 0.0;
+    }
+
   return retval;
 }
 
--- a/liboctave/CMatrix.h	Tue Oct 10 23:41:20 2000 +0000
+++ b/liboctave/CMatrix.h	Thu Oct 12 05:10:09 2000 +0000
@@ -244,11 +244,11 @@
   boolMatrix all (void) const;
   boolMatrix any (void) const;
 
-  ComplexMatrix cumprod (void) const;
-  ComplexMatrix cumsum (void) const;
-  ComplexMatrix prod (void) const;
-  ComplexMatrix sum (void) const;
-  ComplexMatrix sumsq (void) const;
+  ComplexMatrix cumprod (int dim = 0) const;
+  ComplexMatrix cumsum (int dim = 0) const;
+  ComplexMatrix prod (int dim = 0) const;
+  ComplexMatrix sum (int dim = 0) const;
+  ComplexMatrix sumsq (int dim = 0) const;
 
   ComplexColumnVector diag (void) const;
   ComplexColumnVector diag (int k) const;
--- a/liboctave/ChangeLog	Tue Oct 10 23:41:20 2000 +0000
+++ b/liboctave/ChangeLog	Thu Oct 12 05:10:09 2000 +0000
@@ -1,3 +1,16 @@
+2000-10-12  Cai Jianming <caijianming@yahoo.co.uk>
+
+	dMatrix.cc (Matrix::cumprod (int) const): New arg, DIM.
+	(Matrix::cumsum (int) const): Likewise.
+	(Matrix::prod (int) const): Likewise.
+	(Matrix::sum (int) const): Likewise.
+	(Matrix::sumsq (int) const): Likewise.
+	* CMatrix.cc (ComplexMatrix::cumprod (int dim) const): Likewise.
+	(ComplexMatrix::cumsum (int) const): Likewise.
+	(ComplexMatrix::prod (int) const): Likewise.
+	(ComplexMatrix::sum (int) const): Likewise.
+	(ComplexMatrix::sumsq (int) const): Likewise.
+
 2000-10-10  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* Array2-idx.h (Array2<T>::index (idx_vector&)): Correctly set
--- a/liboctave/dMatrix.cc	Tue Oct 10 23:41:20 2000 +0000
+++ b/liboctave/dMatrix.cc	Thu Oct 12 05:10:09 2000 +0000
@@ -1873,45 +1873,28 @@
 }
 
 Matrix
-Matrix::cumprod (void) const
+Matrix::cumprod (int dim) const
 {
-  Matrix retval;
-
   int nr = rows ();
   int nc = cols ();
-
-  if (nr == 1)
+  Matrix retval (nr, nc);
+
+  if (nr > 0 && nc >0)
     {
-      retval.resize (1, nc);
-      if (nc > 0)
+      if ((nr == 1 && dim == 0) || dim == 1)
 	{
-	  double prod = elem (0, 0);
-	  for (int j = 0; j < nc; j++)
+	  for (int i = 0; i < nr; i++)
 	    {
-	      retval.elem (0, j) = prod;
-	      if (j < nc - 1)
-		prod *= elem (0, j+1);
+	      double prod = elem (i, 0);
+	      for (int j = 0; j < nc; j++)
+		{
+		  retval.elem (0, j) = prod;
+		  if (j < nc - 1)
+		    prod *= elem (i, j+1);
+		}
 	    }
 	}
-    }
-  else if (nc == 1)
-    {
-      retval.resize (nr, 1);
-      if (nr > 0)
-	{
-	  double prod = elem (0, 0);
-	  for (int i = 0; i < nr; i++)
-	    {
-	      retval.elem (i, 0) = prod;
-	      if (i < nr - 1)
-		prod *= elem (i+1, 0);
-	    }
-	}
-    }
-  else
-    {
-      retval.resize (nr, nc);
-      if (nr > 0 && nc > 0)
+      else
 	{
 	  for (int j = 0; j < nc; j++)
 	    {
@@ -1929,45 +1912,28 @@
 }
 
 Matrix
-Matrix::cumsum (void) const
+Matrix::cumsum (int dim) const
 {
-  Matrix retval;
-
   int nr = rows ();
   int nc = cols ();
-
-  if (nr == 1)
+  Matrix retval (nr, nc);
+
+  if (nr > 0 && nc > 0)
     {
-      retval.resize (1, nc);
-      if (nc > 0)
+      if ((nr == 1 && dim == 0) || dim == 1)
 	{
-	  double sum = elem (0, 0);
-	  for (int j = 0; j < nc; j++)
+	  for (int i = 0; i < nr; i++)
 	    {
-	      retval.elem (0, j) = sum;
-	      if (j < nc - 1)
-		sum += elem (0, j+1);
+	      double sum = elem (i, 0);
+	      for (int j = 0; j < nc; j++)
+		{
+		  retval.elem (i, j) = sum;
+		  if (j < nc - 1)
+		    sum += elem (i, j+1);
+		}
 	    }
 	}
-    }
-  else if (nc == 1)
-    {
-      retval.resize (nr, 1);
-      if (nr > 0)
-	{
-	  double sum = elem (0, 0);
-	  for (int i = 0; i < nr; i++)
-	    {
-	      retval.elem (i, 0) = sum;
-	      if (i < nr - 1)
-		sum += elem (i+1, 0);
-	    }
-	}
-    }
-  else
-    {
-      retval.resize (nr, nc);
-      if (nr > 0 && nc > 0)
+      else
 	{
 	  for (int j = 0; j < nc; j++)
 	    {
@@ -1985,129 +1951,125 @@
 }
 
 Matrix
-Matrix::prod (void) const
+Matrix::prod (int dim) const
 {
   Matrix retval;
 
   int nr = rows ();
   int nc = cols ();
 
-  if (nr == 1)
+  if (nr > 0 && nc > 0)
     {
-      retval.resize (1, 1);
-      retval.elem (0, 0) = 1.0;
-      for (int j = 0; j < nc; j++)
-	retval.elem (0, 0) *= elem (0, j);
-    }
-  else if (nc == 1)
-    {
-      retval.resize (1, 1);
-      retval.elem (0, 0) = 1.0;
-      for (int i = 0; i < nr; i++)
-	retval.elem (0, 0) *= elem (i, 0);
+      if ((nr == 1 && dim == 0) || dim == 1)
+	{
+	  retval.resize(nr, 1);
+	  for (int i = 0; i < nr; i++)
+	    {
+	      retval.elem (i, 0) = 1.0;
+	      for (int j = 0; j < nc; j++)
+		retval.elem (i, 0) *= elem (i, j);
+	    }
+	}
+      else
+	{
+	  retval.resize (1, nc);
+	  for (int j = 0; j < nc; j++)
+	    {
+	      retval.elem (0, j) = 1.0;
+	      for (int i = 0; i < nr; i++)
+		retval.elem (0, j) *= elem (i, j);
+	    }
+	}
     }
   else
     {
-      if (nc == 0)
-	{
-	  retval.resize (1, 1);
-	  retval.elem (0, 0) = 1.0;
-	}
-      else
-	retval.resize (1, nc);
-
-      for (int j = 0; j < nc; j++)
-	{
-	  retval.elem (0, j) = 1.0;
-	  for (int i = 0; i < nr; i++)
-	    retval.elem (0, j) *= elem (i, j);
-	}
+      retval.resize (1, 1);
+      retval.elem (0, 0) = 1.0;
     }
+
   return retval;
 }
 
 Matrix
-Matrix::sum (void) const
+Matrix::sum (int dim) const
 {
   Matrix retval;
 
   int nr = rows ();
   int nc = cols ();
 
-  if (nr == 1)
+  if (nr > 0 && nc > 0)
     {
-      retval.resize (1, 1);
-      retval.elem (0, 0) = 0.0;
-      for (int j = 0; j < nc; j++)
-	retval.elem (0, 0) += elem (0, j);
+      if ((nr == 1 && dim == 0) || dim == 1)
+	{
+	  retval.resize (nr, 1);
+	  for (int i = 0; i < nr; i++)
+	    {
+	      retval.elem (i, 0) = 0.0;
+	      for (int j = 0; j < nc; j++)
+		retval.elem (i, 0) += elem (i, j);
+	    }
+	}
+      else
+	{
+	  retval.resize (1, nc);
+	  for (int j = 0; j < nc; j++)
+	    {
+	      retval.elem (0, j) = 0.0;
+	      for (int i = 0; i < nr; i++)
+		retval.elem (0, j) += elem (i, j);
+	    }
+	}
     }
-  else if (nc == 1)
+  else
     {
       retval.resize (1, 1);
       retval.elem (0, 0) = 0.0;
-      for (int i = 0; i < nr; i++)
-	retval.elem (0, 0) += elem (i, 0);
-    }
-  else
-    {
-      if (nc == 0)
-	{
-	  retval.resize (1, 1);
-	  retval.elem (0, 0) = 0.0;
-	}
-      else
-	retval.resize (1, nc);
-
-      for (int j = 0; j < nc; j++)
-	{
-	  retval.elem (0, j) = 0.0;
-	  for (int i = 0; i < nr; i++)
-	    retval.elem (0, j) += elem (i, j);
-	}
     }
   return retval;
 }
 
 Matrix
-Matrix::sumsq (void) const
+Matrix::sumsq (int dim) const
 {
   Matrix retval;
 
   int nr = rows ();
   int nc = cols ();
 
-  if (nr == 1)
+  if (nr > 0 && nc > 0)
     {
-      retval.resize (1, 1);
-      retval.elem (0, 0) = 0.0;
-      for (int j = 0; j < nc; j++)
+      if ((nr == 1 && dim == 0) || dim == 1)
 	{
-	  double d = elem (0, j);
-	  retval.elem (0, 0) += d * d;
+	  retval.resize (nr, 1);
+	  for (int i = 0; i < nr; i++)
+	    {
+	      retval.elem (i, 0) = 0.0;
+	      for (int j = 0; j < nc; j++)
+		{
+		  double d = elem (i, j);
+		  retval.elem (i, 0) += d * d;
+		}
+	    }
 	}
-    }
-  else if (nc == 1)
-    {
-      retval.resize (1, 1);
-      retval.elem (0, 0) = 0.0;
-      for (int i = 0; i < nr; i++)
+      else
 	{
-	  double d = elem (i, 0);
-	  retval.elem (0, 0) += d * d;
+	  retval.resize (1, nc);
+	  for (int j = 0; j < nc; j++)
+	    {
+	      retval.elem (0, j) = 0.0;
+	      for (int i = 0; i < nr; i++)
+		{
+		  double d = elem (i, j);
+		  retval.elem (0, j) += d * d;
+		}
+	    }
 	}
     }
   else
     {
-      retval.resize (1, nc);
-      for (int j = 0; j < nc; j++)
-	{
-	  retval.elem (0, j) = 0.0;
-	  for (int i = 0; i < nr; i++)
-	    {
-	      double d = elem (i, j);
-	      retval.elem (0, j) += d * d;
-	    }
-	}
+      retval.resize (1, 1);
+      retval.elem (0, 0) = 0.0;
     }
   return retval;
 }
--- a/liboctave/dMatrix.h	Tue Oct 10 23:41:20 2000 +0000
+++ b/liboctave/dMatrix.h	Thu Oct 12 05:10:09 2000 +0000
@@ -200,11 +200,11 @@
   boolMatrix all (void) const;
   boolMatrix any (void) const;
 
-  Matrix cumprod (void) const;
-  Matrix cumsum (void) const;
-  Matrix prod (void) const;
-  Matrix sum (void) const;
-  Matrix sumsq (void) const;
+  Matrix cumprod (int dim = 0) const; //optional dimension argument
+  Matrix cumsum (int dim = 0) const;
+  Matrix prod (int dim = 0) const;
+  Matrix sum (int dim = 0) const;
+  Matrix sumsq (int dim = 0) const;
   Matrix abs (void) const;
 
   ColumnVector diag (void) const;
--- a/src/ChangeLog	Tue Oct 10 23:41:20 2000 +0000
+++ b/src/ChangeLog	Thu Oct 12 05:10:09 2000 +0000
@@ -1,3 +1,15 @@
+2000-10-12  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	Change patterned after patch by Cai Jianming
+	<caijianming@yahoo.co.uk> to support for DIM arg in 2.0.x sources.
+
+	* data.cc (DATA_REDUCTION): New macro.  Handle second DIM arg here.
+	(Fcumprod): Replace function body with DATA_REDUCTION.
+	(Fcumsum): Likewise.
+	(Fprod): Likewise.
+	(Fsum): Likewise.
+	(Fsumsq): Likewise.
+
 2000-10-10  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* error.cc (pr_where_2): New function.
--- a/src/data.cc	Tue Oct 10 23:41:20 2000 +0000
+++ b/src/data.cc	Thu Oct 12 05:10:09 2000 +0000
@@ -251,84 +251,70 @@
   return retval;
 }
 
+#define DATA_REDUCTION(FCN) \
+ \
+  octave_value_list retval; \
+ \
+  int nargin = args.length (); \
+ \
+  if (nargin == 1 || nargin == 2) \
+    { \
+      octave_value arg = args(0); \
+ \
+      int dim = (nargin == 1 ? 0 : args(1).int_value (true) - 1); \
+ \
+      if (! error_state) \
+	{ \
+	  if (dim == 0 || dim == 1) \
+	    { \
+	      if (arg.is_real_type ()) \
+		{ \
+		  Matrix tmp = arg.matrix_value (); \
+ \
+		  if (! error_state) \
+		    retval(0) = tmp.FCN (dim); \
+		} \
+	      else if (arg.is_complex_type ()) \
+		{ \
+		  ComplexMatrix tmp = arg.complex_matrix_value (); \
+ \
+		  if (! error_state) \
+		    retval(0) = tmp.FCN (dim); \
+		} \
+	      else \
+		{ \
+		  gripe_wrong_type_arg (#FCN, arg); \
+		  return retval; \
+		} \
+	    } \
+	  else \
+	    error (#FCN ": invalid dimension argument = %d", dim + 1); \
+	} \
+    } \
+  else \
+    print_usage (#FCN); \
+ \
+  return retval
+
 DEFUN (cumprod, args, ,
   "-*- texinfo -*-\n\
-@deftypefn {Built-in Function} {} cumprod (@var{x})\n\
-Cumulative products.\n\
+@deftypefn {Built-in Function} {} cumprod (@var{x}, @var{dim})\n\
+Cumulative product of elements along dimension @var{dim}.  If\n\
+@var{dim} is omitted, it defaults to 1 (column-wise cumulative\n\
+products).\n\
 @end deftypefn")
 {
-  octave_value_list retval;
-
-  int nargin = args.length ();
-
-  if (nargin == 1)
-    {
-      octave_value arg = args(0);
-
-      if (arg.is_real_type ())
-	{
-	  Matrix tmp = arg.matrix_value ();
-
-	  if (! error_state)
-	    retval(0) = tmp.cumprod ();
-	}
-      else if (arg.is_complex_type ())
-	{
-	  ComplexMatrix tmp = arg.complex_matrix_value ();
-
-	  if (! error_state)
-	    retval(0) = tmp.cumprod ();
-	}
-      else
-	{
-	  gripe_wrong_type_arg ("cumprod", arg);
-	  return retval;
-	}
-    }
-  else
-    print_usage ("cumprod");
-
-  return retval;
+  DATA_REDUCTION (cumprod);
 }
 
 DEFUN (cumsum, args, ,
   "-*- texinfo -*-\n\
-@deftypefn {Built-in Function} {} cumsum (@var{x})\n\
-Cumulative sums.\n\
+@deftypefn {Built-in Function} {} cumsum (@var{x}, @var{dim})\n\
+Cumulative sum of elements along dimension @var{dim}.  If @var{dim}\n\
+is omitted, it defaults to 1 (column-wise cumulative sums).\n\
 @end deftypefn")
 {
-  octave_value_list retval;
-
-  int nargin = args.length ();
-
-  if (nargin == 1)
-    {
-      octave_value arg = args(0);
-
-      if (arg.is_real_type ())
-	{
-	  Matrix tmp = arg.matrix_value ();
-
-	  if (! error_state)
-	    retval(0) = tmp.cumsum ();
-	}
-      else if (arg.is_complex_type ())
-	{
-	  ComplexMatrix tmp = arg.complex_matrix_value ();
-
-	  if (! error_state)
-	    retval(0) = tmp.cumsum ();
-	}
-      else
-	{
-	  gripe_wrong_type_arg ("cumsum", arg);
-	  return retval;
-	}
-    }
-  else
-    print_usage ("cumsum");
-
-  return retval;
+  DATA_REDUCTION (cumsum);
 }
 
 static octave_value
@@ -567,42 +553,12 @@
 
 DEFUN (prod, args, ,
   "-*- texinfo -*-\n\
-@deftypefn {Built-in Function} {} prod (@var{x})\n\
-Products.\n\
+@deftypefn {Built-in Function} {} prod (@var{x}, @var{dim})\n\
+Product of elements along dimension @var{dim}.  If @var{dim} is\n\
+omitted, it defaults to 1 (column-wise products).\n\
 @end deftypefn")
 {
-  octave_value_list retval;
-
-  int nargin = args.length ();
-
-  if (nargin == 1)
-    {
-      octave_value arg = args(0);
-
-      if (arg.is_real_type ())
-	{
-	  Matrix tmp = arg.matrix_value ();
-
-	  if (! error_state)
-	    retval(0) = tmp.prod ();
-	}
-      else if (arg.is_complex_type ())
-	{
-	  ComplexMatrix tmp = arg.complex_matrix_value ();
-
-	  if (! error_state)
-	    retval(0) = tmp.prod ();
-	}
-      else
-	{
-	  gripe_wrong_type_arg ("prod", arg);
-	  return retval;
-	}
-    }
-  else
-    print_usage ("prod");
-
-  return retval;
+  DATA_REDUCTION (prod);
 }
 
 DEFUN (length, args, ,
@@ -707,88 +663,28 @@
 
 DEFUN (sum, args, ,
   "-*- texinfo -*-\n\
-@deftypefn {Built-in Function} {} sum (@var{x})\n\
-Sum of elements.\n\
+@deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\
+Sum of elements along dimension @var{dim}.  If @var{dim} is\n\
+omitted, it defaults to 1 (column-wise sum).\n\
 @end deftypefn")
 {
-  octave_value_list retval;
-
-  int nargin = args.length ();
-
-  if (nargin == 1)
-    {
-      octave_value arg = args(0);
-
-      if (arg.is_real_type ())
-	{
-	  Matrix tmp = arg.matrix_value ();
-
-	  if (! error_state)
-	    retval(0) = tmp.sum ();
-	}
-      else if (arg.is_complex_type ())
-	{
-	  ComplexMatrix tmp = arg.complex_matrix_value ();
-
-	  if (! error_state)
-	    retval(0) = tmp.sum ();
-	}
-      else
-	{
-	  gripe_wrong_type_arg ("sum", arg);
-	  return retval;
-	}
-    }
-  else
-    print_usage ("sum");
-
-  return retval;
+  DATA_REDUCTION (sum);
 }
 
 DEFUN (sumsq, args, ,
   "-*- texinfo -*-\n\
-@deftypefn {Built-in Function} {} sumsq (@var{x})\n\
-Sum of squares of elements.\n\
+@deftypefn {Built-in Function} {} sumsq (@var{x}, @var{dim})\n\
+Sum of squares of elements along dimension @var{dim}.  If @var{dim}\n\
+is omitted, it defaults to 1 (column-wise sum of squares).\n\
 \n\
 This function is equivalent to computing\n\
-\n\
-  sum (X .* conj (X))\n\
-\n\
-but it uses less memory and avoids calling conj if X is real.\n\
+@example\n\
+sum (x .* conj (x), dim)\n\
+@end example\n\
+but it uses less memory and avoids calling conj if @var{x} is real.\n\
 @end deftypefn")
 {
-  octave_value_list retval;
-
-  int nargin = args.length ();
-
-  if (nargin == 1)
-    {
-      octave_value arg = args(0);
-
-      if (arg.is_real_type ())
-	{
-	  Matrix tmp = arg.matrix_value ();
-
-	  if (! error_state)
-	    retval(0) = tmp.sumsq ();
-	}
-      else if (arg.is_complex_type ())
-	{
-	  ComplexMatrix tmp = arg.complex_matrix_value ();
-
-	  if (! error_state)
-	    retval(0) = tmp.sumsq ();
-	}
-      else
-	{
-	  gripe_wrong_type_arg ("sumsq", arg);
-	  return retval;
-	}
-    }
-  else
-    print_usage ("sumsq");
-
-  return retval;
+  DATA_REDUCTION (sumsq);
 }
 
 DEFUN (is_bool, args, ,