changeset 4569:01e4957409a4

[project @ 2003-10-30 05:48:17 by jwe]
author jwe
date Thu, 30 Oct 2003 05:48:17 +0000
parents 03c053808a7c
children 32b491743d40
files liboctave/CNDArray.cc liboctave/CNDArray.h liboctave/ChangeLog liboctave/boolNDArray.cc liboctave/chNDArray.cc liboctave/dNDArray.cc liboctave/dNDArray.h liboctave/mx-inlines.cc src/ChangeLog src/data.cc src/ov-bool-mat.h src/ov-complex.cc src/ov-complex.h src/ov-scalar.h src/ov-str-mat.h
diffstat 15 files changed, 268 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/CNDArray.cc	Thu Oct 30 00:22:41 2003 +0000
+++ b/liboctave/CNDArray.cc	Thu Oct 30 05:48:17 2003 +0000
@@ -77,15 +77,91 @@
 boolNDArray
 ComplexNDArray::all (int dim) const
 {
-  MX_ND_ANY_ALL (MX_ND_ALL_EVAL (real (elem (iter_idx)) == 0
-				 && imag (elem (iter_idx)) == 0), true);
+  MX_ND_ANY_ALL_REDUCTION
+    (MX_ND_ALL_EVAL (elem (iter_idx) == Complex (0, 0)), true);
 }
 
 boolNDArray
 ComplexNDArray::any (int dim) const
 {
-  MX_ND_ANY_ALL (MX_ND_ANY_EVAL (real (elem (iter_idx)) != 0
-				 || imag (elem (iter_idx)) != 0), false);
+  MX_ND_ANY_ALL_REDUCTION
+    (MX_ND_ANY_EVAL (elem (iter_idx) != Complex (0, 0)), false);
+}
+
+ComplexMatrix
+ComplexNDArray::cumprod (int dim) const
+{
+  if (dims () . length () == 2)
+    {
+      MX_CUMULATIVE_OP (ComplexMatrix, Complex, *=);
+    }
+  else
+    {
+      (*current_liboctave_error_handler)
+	("cumsum is not yet implemented for N-d arrays");
+
+      return ComplexMatrix ();
+    }
+}
+
+ComplexMatrix
+ComplexNDArray::cumsum (int dim) const
+{
+  if (dims () . length () == 2)
+    {
+      MX_CUMULATIVE_OP (ComplexMatrix, Complex, +=);
+    }
+  else
+    {
+      (*current_liboctave_error_handler)
+	("cumsum is not yet implemented for N-d arrays");
+
+      return ComplexMatrix ();
+    }
+}
+
+ComplexNDArray
+ComplexNDArray::prod (int dim) const
+{
+  MX_ND_COMPLEX_OP_REDUCTION (*= elem (iter_idx), Complex (1, 0));
+}
+
+ComplexNDArray
+ComplexNDArray::sumsq (int dim) const
+{
+  MX_ND_COMPLEX_OP_REDUCTION
+    (+= imag (elem (iter_idx))
+     ? elem (iter_idx) * conj (elem (iter_idx))
+     : std::pow (elem (iter_idx), 2), Complex (0, 0));
+}
+
+ComplexNDArray 
+ComplexNDArray::sum (int dim) const
+{
+  MX_ND_COMPLEX_OP_REDUCTION (+= elem (iter_idx), Complex (0, 0));
+}
+
+Matrix 
+ComplexNDArray::abs (void) const
+{
+  Matrix retval;
+
+  if (dims () . length () == 2)
+    {
+      int nr = rows ();
+      int nc = cols ();
+
+      retval.resize (nr, nc);
+      
+      for (int j = 0; j < nc; j++)
+	for (int i = 0; i < nr; i++)
+	  retval (i, j) = ::abs (elem (i, j));
+    }
+  else
+    (*current_liboctave_error_handler)
+      ("abs is not yet implemented for N-d arrays");
+      
+  return retval;
 }
 
 ComplexMatrix
--- a/liboctave/CNDArray.h	Thu Oct 30 00:22:41 2003 +0000
+++ b/liboctave/CNDArray.h	Thu Oct 30 05:48:17 2003 +0000
@@ -77,6 +77,14 @@
   boolNDArray all (int dim = -1) const;
   boolNDArray any (int dim = -1) const;
 
+  ComplexMatrix cumprod (int dim = -1) const;
+  ComplexMatrix cumsum (int dim = -1) const;
+  ComplexNDArray prod (int dim = -1) const;
+  ComplexNDArray sum (int dim = -1) const;
+  ComplexNDArray sumsq (int dim = -1) const;
+
+  Matrix abs (void) const;
+
   ComplexMatrix matrix_value (void) const;
 
   ComplexNDArray squeeze (void) const { return ArrayN<Complex>::squeeze (); }
--- a/liboctave/ChangeLog	Thu Oct 30 00:22:41 2003 +0000
+++ b/liboctave/ChangeLog	Thu Oct 30 05:48:17 2003 +0000
@@ -1,3 +1,18 @@
+2003-10-29  Petter Risholm  <risholm@stud.ntnu.no>
+
+	* dNDArray.cc (NDArray::cumprod, NDArray::cumsum, NDArray::prod,
+	NDArray::sum, NDArray::sumsq, NDArray::abs): New functions.
+	* dNDArray.h: Provide decls.
+	* CNDArray.cc (ComplexNDArray::cumprod, ComplexNDArray::cumsum,
+	ComplexNDArray::prod, ComplexNDArray::sum, ComplexNDArray::sumsq,
+	ComplexNDArray::abs): New functions.
+	* CNDArray.h: Provide decls.
+
+	* mx-inlines.cc (MX_ND_REDUCTION): Rename from MX_ND_ANY_ALL.
+	Generalize to handle other reduction operations.
+	(MX_ND_REAL_OP_REDUCTION, MX_ND_COMPLEX_OP_REDUCTION,
+	MX_ND_ALL_ANY_REDUCTION): New macros.
+
 2003-10-29  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* Array.cc (Array<T>::reshape): New function.
--- a/liboctave/boolNDArray.cc	Thu Oct 30 00:22:41 2003 +0000
+++ b/liboctave/boolNDArray.cc	Thu Oct 30 05:48:17 2003 +0000
@@ -53,13 +53,13 @@
 boolNDArray
 boolNDArray::all (int dim) const
 {
-  MX_ND_ANY_ALL (MX_ND_ALL_EVAL (MX_ND_ALL_EXPR), true);
+  MX_ND_ANY_ALL_REDUCTION (MX_ND_ALL_EVAL (MX_ND_ALL_EXPR), true);
 }
 
 boolNDArray
 boolNDArray::any (int dim) const
 {
-  MX_ND_ANY_ALL (MX_ND_ANY_EVAL (MX_ND_ANY_EXPR), false);
+  MX_ND_ANY_ALL_REDUCTION (MX_ND_ANY_EVAL (MX_ND_ANY_EXPR), false);
 }
 
 boolMatrix
--- a/liboctave/chNDArray.cc	Thu Oct 30 00:22:41 2003 +0000
+++ b/liboctave/chNDArray.cc	Thu Oct 30 05:48:17 2003 +0000
@@ -40,13 +40,13 @@
 boolNDArray
 charNDArray::all (int dim) const
 {
-  MX_ND_ANY_ALL (MX_ND_ALL_EVAL (elem (iter_idx) == ' '), true);
+  MX_ND_ANY_ALL_REDUCTION (MX_ND_ALL_EVAL (elem (iter_idx) == ' '), true);
 }
 
 boolNDArray
 charNDArray::any (int dim) const
 {
-  MX_ND_ANY_ALL (MX_ND_ANY_EVAL (elem (iter_idx) != ' '), false);
+  MX_ND_ANY_ALL_REDUCTION (MX_ND_ANY_EVAL (elem (iter_idx) != ' '), false);
 }
 
 charMatrix
--- a/liboctave/dNDArray.cc	Thu Oct 30 00:22:41 2003 +0000
+++ b/liboctave/dNDArray.cc	Thu Oct 30 05:48:17 2003 +0000
@@ -68,15 +68,89 @@
 boolNDArray
 NDArray::all (int dim) const
 {
-  MX_ND_ANY_ALL (MX_ND_ALL_EVAL (MX_ND_ALL_EXPR), true);
+  MX_ND_ANY_ALL_REDUCTION (MX_ND_ALL_EVAL (MX_ND_ALL_EXPR), true);
 }
 
 boolNDArray
 NDArray::any (int dim) const
 {
-  MX_ND_ANY_ALL (MX_ND_ANY_EVAL (MX_ND_ANY_EXPR), false);
+  MX_ND_ANY_ALL_REDUCTION (MX_ND_ANY_EVAL (MX_ND_ANY_EXPR), false);
+}
+
+Matrix
+NDArray::cumprod (int dim) const
+{
+ if (dims () . length () == 2)
+   {
+     MX_CUMULATIVE_OP (Matrix, double, *=);
+   }
+ else
+   {
+     (*current_liboctave_error_handler)
+       ("cumprod is not yet implemented for N-d arrays");
+
+     return Matrix ();
+   }
+}
+
+Matrix
+NDArray::cumsum (int dim) const
+{
+  if (dims () . length () == 2)
+    {
+      MX_CUMULATIVE_OP (Matrix, double, +=);
+    }
+  else
+    {
+      (*current_liboctave_error_handler)
+	("cumprod is not yet implemented for N-d arrays");
+
+      return Matrix ();
+    }
 }
 
+NDArray
+NDArray::prod (int dim) const
+{
+  MX_ND_REAL_OP_REDUCTION (*= elem (iter_idx), 1);
+}
+
+NDArray
+NDArray::sumsq (int dim) const
+{
+  MX_ND_REAL_OP_REDUCTION (+= std::pow (elem (iter_idx), 2), 0);
+}
+
+NDArray 
+NDArray::sum (int dim) const
+{
+  MX_ND_REAL_OP_REDUCTION (+= elem (iter_idx), 0);
+}
+
+Matrix
+NDArray::abs (void) const
+{
+  Matrix retval;
+
+  if (dims () . length () == 2)
+    {
+      int nr = rows ();
+      int nc = cols ();
+
+      retval.resize (nr, nc);
+      
+      for (int j = 0; j < nc; j++)
+	for (int i = 0; i < nr; i++)
+	  retval(i,j) = fabs (elem (i, j));
+    }
+  else
+    (*current_liboctave_error_handler)
+      ("abs is not yet implemented for N-d arrays");
+
+  return retval;
+}
+
+
 Matrix
 NDArray::matrix_value (void) const
 {
--- a/liboctave/dNDArray.h	Thu Oct 30 00:22:41 2003 +0000
+++ b/liboctave/dNDArray.h	Thu Oct 30 05:48:17 2003 +0000
@@ -75,6 +75,14 @@
   boolNDArray all (int dim = -1) const;
   boolNDArray any (int dim = -1) const;
 
+  Matrix cumprod (int dim = -1) const;
+  Matrix cumsum (int dim = -1) const;
+  NDArray prod (int dim = -1) const;
+  NDArray sum (int dim = -1) const;  
+  NDArray sumsq (int dim = -1) const;
+ 
+  Matrix abs (void) const;
+
   Matrix matrix_value (void) const;
 
   NDArray squeeze (void) const { return ArrayN<double>::squeeze (); }
--- a/liboctave/mx-inlines.cc	Thu Oct 30 00:22:41 2003 +0000
+++ b/liboctave/mx-inlines.cc	Thu Oct 30 05:48:17 2003 +0000
@@ -377,10 +377,10 @@
      retval (iter_idx) = 1; \
      break; \
    } 
-
-#define MX_ND_ANY_ALL(EVAL_EXPR, VAL) \
+ 
+#define MX_ND_REDUCTION(EVAL_EXPR, END_EXPR, VAL, ACC_DECL, RET_TYPE) \
  \
-  boolNDArray retval; \
+  RET_TYPE retval; \
  \
   dim_vector dv = dims (); \
  \
@@ -474,6 +474,7 @@
  \
   for (int j = 0; j < num_iter; j++) \
     { \
+      ACC_DECL;\
       for (int i = 0; i < dim_length; i++) \
 	{ \
 	  if (dim > -1) \
@@ -485,11 +486,23 @@
       if (dim > -1) \
         iter_idx (dim) = 0; \
  \
+      END_EXPR;\
+ \
       increment_index (iter_idx, dv); \
     } \
  \
   return retval
- 
+
+#define MX_ND_REAL_OP_REDUCTION(ASN_EXPR, INIT_VAL) \
+  MX_ND_REDUCTION (acc ASN_EXPR, retval.elem (iter_idx) = acc, \
+                   INIT_VAL, double acc = INIT_VAL, NDArray)
+
+#define MX_ND_COMPLEX_OP_REDUCTION(ASN_EXPR, INIT_VAL) \
+  MX_ND_REDUCTION (acc ASN_EXPR, retval.elem (iter_idx) = acc, \
+                   INIT_VAL, Complex acc = INIT_VAL, ComplexNDArray)
+
+#define MX_ND_ANY_ALL_REDUCTION(EVAL_EXPR, VAL) \
+  MX_ND_REDUCTION (EVAL_EXPR, , VAL, , boolNDArray)
 
 #endif
 
--- a/src/ChangeLog	Thu Oct 30 00:22:41 2003 +0000
+++ b/src/ChangeLog	Thu Oct 30 05:48:17 2003 +0000
@@ -1,5 +1,26 @@
 2003-10-29  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
+	* ov-str-mat.h (octave_char_matrix_str::reshape): New function.
+	Force result to be a string.
+
+2003-10-29  Petter Risholm  <risholm@stud.ntnu.no>
+
+	* data.cc (DATA_REDUCTION): Work on NDArray and ComplexNDArray
+	objects instead of Matrix and ComplexMatrix objects.
+
+2003-10-29  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* ov-bool-mat.h (octave_bool_matrix::NDArray,
+	octave_bool_matrix::ComplexNDArray): New functions.
+
+	* ov-complex.cc (octave_complex::array_value,
+	octave_complex::complex_array_value): New functions.
+	* ov-complex.h: Provide decls.
+
+	* ov-scalar.cc (octave_scalar::array_value,
+	octave_scalar::complex_array_value): New functions.
+	* ov-scalar.h: Provide decls.
+
 	* oct-map.cc (Octave_map::reshape): New function.
 	* oct-map.h: Provide decl.
 
--- a/src/data.cc	Thu Oct 30 00:22:41 2003 +0000
+++ b/src/data.cc	Thu Oct 30 05:48:17 2003 +0000
@@ -370,14 +370,14 @@
 	    { \
 	      if (arg.is_real_type ()) \
 		{ \
-		  Matrix tmp = arg.matrix_value (); \
+		  NDArray tmp = arg.array_value (); \
  \
 		  if (! error_state) \
 		    retval = tmp.FCN (dim); \
 		} \
 	      else if (arg.is_complex_type ()) \
 		{ \
-		  ComplexMatrix tmp = arg.complex_matrix_value (); \
+		  ComplexNDArray tmp = arg.complex_array_value (); \
  \
 		  if (! error_state) \
 		    retval = tmp.FCN (dim); \
--- a/src/ov-bool-mat.h	Thu Oct 30 00:22:41 2003 +0000
+++ b/src/ov-bool-mat.h	Thu Oct 30 05:48:17 2003 +0000
@@ -96,11 +96,17 @@
   Matrix matrix_value (bool = false) const
     { return Matrix (matrix.matrix_value ()); }
 
+  NDArray array_value (bool = false) const
+    { return NDArray (matrix.matrix_value ()); }
+
   Complex complex_value (bool = false) const;
 
   ComplexMatrix complex_matrix_value (bool = false) const
     { return ComplexMatrix (matrix.matrix_value ( )); }
 
+  ComplexNDArray complex_array_value (bool = false) const
+    { return ComplexNDArray (matrix.matrix_value ()); }
+
   boolMatrix bool_matrix_value (void) const
     { return matrix.matrix_value (); }
 
--- a/src/ov-complex.cc	Thu Oct 30 00:22:41 2003 +0000
+++ b/src/ov-complex.cc	Thu Oct 30 05:48:17 2003 +0000
@@ -113,6 +113,19 @@
   return retval;
 }
 
+NDArray
+octave_complex::array_value (bool force_conversion) const
+{
+  NDArray retval;
+
+  if (! force_conversion && Vwarn_imag_to_real)
+    gripe_implicit_conversion ("complex scalar", "real matrix");
+
+  retval = NDArray (dim_vector (1, 1), std::real (scalar));
+
+  return retval;
+}
+
 Complex
 octave_complex::complex_value (bool) const
 {
@@ -126,6 +139,12 @@
   return ComplexMatrix (1, 1, scalar);
 }
 
+ComplexNDArray
+octave_complex::complex_array_value (bool force_conversion) const
+{
+  return ComplexNDArray (dim_vector (1, 1), scalar);
+}
+
 /*
 ;;; Local Variables: ***
 ;;; mode: C++ ***
--- a/src/ov-complex.h	Thu Oct 30 00:22:41 2003 +0000
+++ b/src/ov-complex.h	Thu Oct 30 05:48:17 2003 +0000
@@ -86,10 +86,14 @@
 
   Matrix matrix_value (bool = false) const;
 
+  NDArray array_value (bool = false) const;
+
   Complex complex_value (bool = false) const;
 
   ComplexMatrix complex_matrix_value (bool = false) const;
 
+  ComplexNDArray complex_array_value (bool = false) const;
+
   void increment (void) { scalar += 1.0; }
 
   void decrement (void) { scalar -= 1.0; }
--- a/src/ov-scalar.h	Thu Oct 30 00:22:41 2003 +0000
+++ b/src/ov-scalar.h	Thu Oct 30 05:48:17 2003 +0000
@@ -89,17 +89,17 @@
   Matrix matrix_value (bool = false) const
     { return Matrix (1, 1, scalar); }
 
-  NDArray double_nd_array_value (bool = false) const
-    {
-      dim_vector temp (1, 1);
-      return NDArray (temp, double_value ());
-    }
+  NDArray array_value (bool = false) const
+    { return NDArray (dim_vector (1, 1), double_value ()); }
 
   Complex complex_value (bool = false) const { return scalar; }
 
   ComplexMatrix complex_matrix_value (bool = false) const
     { return  ComplexMatrix (1, 1, Complex (scalar)); }
 
+  ComplexNDArray complex_array_value (bool = false) const
+    { return ComplexNDArray (dim_vector (1, 1), double_value ()); }
+
   octave_value convert_to_str_internal (bool pad, bool force) const;
 
   void increment (void) { ++scalar; }
--- a/src/ov-str-mat.h	Thu Oct 30 00:22:41 2003 +0000
+++ b/src/ov-str-mat.h	Thu Oct 30 05:48:17 2003 +0000
@@ -91,6 +91,9 @@
 
   void assign (const octave_value_list& idx, const charMatrix& rhs);
 
+  octave_value reshape (const dim_vector& new_dims) const
+    { return octave_value (charNDArray (matrix.reshape (new_dims)), true); }
+
   bool is_string (void) const { return true; }
 
   bool is_real_type (void) const { return false; }