changeset 5943:c038c2947ee1

[project @ 2006-08-18 21:27:03 by jwe]
author jwe
date Fri, 18 Aug 2006 21:27:04 +0000
parents a02a305e06ee
children d20c596b9b10
files liboctave/ChangeLog liboctave/dMatrix.cc liboctave/dMatrix.h liboctave/dNDArray.cc liboctave/dNDArray.h liboctave/intNDArray.cc liboctave/intNDArray.h src/ChangeLog src/OPERATORS/op-bm-b.cc src/OPERATORS/op-bm-bm.cc src/gripes.cc src/gripes.h src/ov-base.cc src/ov-base.h src/ov-intx.h src/ov-range.h src/ov-re-mat.cc src/ov-re-mat.h src/ov-re-sparse.cc src/ov-re-sparse.h src/ov-scalar.h src/ov.h
diffstat 22 files changed, 153 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/ChangeLog
+++ b/liboctave/ChangeLog
@@ -1,5 +1,13 @@
 2006-08-18  John W. Eaton  <jwe@octave.org>
 
+	* dMatrix.cc (Matrix::any_element_not_one_or_zero): New function.
+	* dMatrix.h: Provide decl.
+	* dNDArray.cc (NDArray::any_element_not_one_or_zero): New function.
+	* dNDArray.h: Provide decl.
+	* intNDArray.cc (intNDArray<T>::any_element_not_one_or_zero):
+	New function.
+	* intNDArray.h: Provide decl.
+
 	* Array.cc (Array<T>::permute): Only rearrange values if array is
 	not empty.
 
--- a/liboctave/dMatrix.cc
+++ b/liboctave/dMatrix.cc
@@ -2432,6 +2432,21 @@
 }
 
 bool
+Matrix::any_element_not_one_or_zero (void) const
+{
+  octave_idx_type nel = nelem ();
+
+  for (octave_idx_type i = 0; i < nel; i++)
+    {
+      double val = elem (i);
+      if (val != 0 && val != 1)
+	return true;
+    }
+
+  return false;
+}
+
+bool
 Matrix::all_elements_are_int_or_inf_or_nan (void) const
 {
   octave_idx_type nel = nelem ();
--- a/liboctave/dMatrix.h
+++ b/liboctave/dMatrix.h
@@ -242,6 +242,7 @@
 
   bool any_element_is_negative (bool = false) const;
   bool any_element_is_inf_or_nan (void) const;
+  bool any_element_not_one_or_zero (void) const;
   bool all_elements_are_int_or_inf_or_nan (void) const;
   bool all_integers (double& max_val, double& min_val) const;
   bool too_large_for_float (void) const;
--- a/liboctave/dNDArray.cc
+++ b/liboctave/dNDArray.cc
@@ -531,6 +531,21 @@
 }
 
 bool
+NDArray::any_element_not_one_or_zero (void) const
+{
+  octave_idx_type nel = nelem ();
+
+  for (octave_idx_type i = 0; i < nel; i++)
+    {
+      double val = elem (i);
+      if (val != 0 && val != 1)
+	return true;
+    }
+
+  return false;
+}
+
+bool
 NDArray::all_elements_are_int_or_inf_or_nan (void) const
 {
   octave_idx_type nel = nelem ();
--- a/liboctave/dNDArray.h
+++ b/liboctave/dNDArray.h
@@ -64,6 +64,7 @@
 
   bool any_element_is_negative (bool = false) const;
   bool any_element_is_inf_or_nan (void) const;
+  bool any_element_not_one_or_zero (void) const;
   bool all_elements_are_int_or_inf_or_nan (void) const;
   bool all_integers (double& max_val, double& min_val) const;
   bool too_large_for_float (void) const;
--- a/liboctave/intNDArray.cc
+++ b/liboctave/intNDArray.cc
@@ -44,6 +44,23 @@
   return b;
 }
 
+template <class T>
+bool
+intNDArray<T>::any_element_not_one_or_zero (void) const
+{
+  octave_idx_type nel = this->nelem ();
+
+  for (octave_idx_type i = 0; i < nel; i++)
+    {
+      T val = this->elem (i);
+
+      if (val != 0.0 && val != 1.0)
+	return true;
+    }
+
+  return false;
+}
+
 // FIXME -- this is not quite the right thing.
 
 template <class T>
--- a/liboctave/intNDArray.h
+++ b/liboctave/intNDArray.h
@@ -62,6 +62,8 @@
 
   boolNDArray operator ! (void) const;
 
+  bool any_element_not_one_or_zero (void) const;
+
   // FIXME -- this is not quite the right thing.
 
   boolNDArray all (int dim = -1) const;
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,25 @@
+2006-08-18  John W. Eaton  <jwe@octave.org>
+
+	* gripes.cc (gripe_logical_conversion): New function.
+	* gripes.h: Provide decl.
+	* ov.h (octave_value::bool_value, octave_value::bool_matrix_value,
+	octave_value::bool_array_value): New arg, warn.
+	* ov-base.cc, ov-base.h (octave_base_value::bool_value,
+	octave_base_value::bool_matrix_value, 
+	octave_base_value::bool_array_value): Likewise.
+	* ov-intx.h (OCTAVE_VALUE_INT_MATRIX_T::bool_array_value,
+	OCTAVE_VALUE_INT_SCALAR_T::bool_array_value): Likewise.
+	* ov-range.h (octave_range::bool_array_value): Likewise.
+	* ov-re-mat.cc, ov-re-mat.h (octave_matrix::bool_array_value):
+	Likewise.
+	* ov-re-sparse.cc, ov-re-sparse.h
+	(octave_sparse_matrix::bool_array_value): Likewise.
+	* ov-scalar.h (octave_scalar::bool_value,
+	octave_scalar::bool_array_value): Likewise.
+	* OPERATORS/op-bm-b.cc (oct_assignop_conv_and_assign):
+	Ask for warning from bool_array_value.
+	* OPERATORS/op-bm-bm.cc (oct_assignop_conv_and_assign): Likewise.
+
 2006-08-18  Benjamin Lindner  <lindnerben@gmx.net>
 
 	* ls-mat5.cc (read_mat5_tag): Declare type as int32_t, not int.
--- a/src/OPERATORS/op-bm-b.cc
+++ b/src/OPERATORS/op-bm-b.cc
@@ -67,7 +67,7 @@
   // FIXME -- perhaps add a warning for this conversion if the values
   // are not all 0 or 1?
 
-  boolNDArray v2 = a2.bool_array_value ();
+  boolNDArray v2 = a2.bool_array_value (true);
 
   if (! error_state)
     v1.assign (idx, v2);
--- a/src/OPERATORS/op-bm-bm.cc
+++ b/src/OPERATORS/op-bm-bm.cc
@@ -92,7 +92,7 @@
   // FIXME -- perhaps add a warning for this conversion if the values
   // are not all 0 or 1?
 
-  boolNDArray v2 = a2.bool_array_value ();
+  boolNDArray v2 = a2.bool_array_value (true);
 
   if (! error_state)
     v1.assign (idx, v2);
--- a/src/gripes.cc
+++ b/src/gripes.cc
@@ -184,6 +184,13 @@
   warning_with_id ("Octave:divide-by-zero", "division by zero");
 }
 
+extern void
+gripe_logical_conversion (void)
+{
+  warning_with_id ("Octave:logical-conversion",
+		   "value not equal to 1 or 0 converted to logical 1");
+}
+
 /*
 ;;; Local Variables: ***
 ;;; mode: C++ ***
--- a/src/gripes.h
+++ b/src/gripes.h
@@ -103,6 +103,9 @@
 extern void
 gripe_divide_by_zero (void);
 
+extern void
+gripe_logical_conversion (void);
+
 #endif
 
 /*
--- a/src/ov-base.cc
+++ b/src/ov-base.cc
@@ -480,7 +480,7 @@
 }
 
 bool
-octave_base_value::bool_value (void) const
+octave_base_value::bool_value (bool) const
 {
   bool retval = false;
   gripe_wrong_type_arg ("octave_base_value::bool_value()", type_name ());
@@ -488,7 +488,7 @@
 }
 
 boolMatrix
-octave_base_value::bool_matrix_value (void) const
+octave_base_value::bool_matrix_value (bool) const
 {
   boolMatrix retval;
   gripe_wrong_type_arg ("octave_base_value::bool_matrix_value()",
@@ -497,7 +497,7 @@
 }
 
 boolNDArray
-octave_base_value::bool_array_value (void) const
+octave_base_value::bool_array_value (bool) const
 {
   boolNDArray retval;
   gripe_wrong_type_arg ("octave_base_value::bool_array_value()",
--- a/src/ov-base.h
+++ b/src/ov-base.h
@@ -319,11 +319,11 @@
 
   virtual ComplexNDArray complex_array_value (bool = false) const;
 
-  virtual bool bool_value (void) const;
+  virtual bool bool_value (bool = false) const;
 
-  virtual boolMatrix bool_matrix_value (void) const;
+  virtual boolMatrix bool_matrix_value (bool = false) const;
 
-  virtual boolNDArray bool_array_value (void) const;
+  virtual boolNDArray bool_array_value (bool = false) const;
 
   virtual charMatrix char_matrix_value (bool force = false) const;
 
--- a/src/ov-intx.h
+++ b/src/ov-intx.h
@@ -126,12 +126,15 @@
     }
 
   boolNDArray
-  bool_array_value (void) const
+  bool_array_value (bool warn = false) const
   {
     boolNDArray retval (dims ());
 
     octave_idx_type nel = numel ();
-  
+
+    if (warn && matrix.any_element_not_one_or_zero ())
+      gripe_logical_conversion ();
+
     for (octave_idx_type i = 0; i < nel; i++)
       retval(i) = static_cast<bool>(matrix(i));
 
@@ -331,10 +334,15 @@
     }
 
   boolNDArray
-  bool_array_value (void) const
+  bool_array_value (bool warn = false) const
   {
     boolNDArray retval (dim_vector (1, 1));
+
+    if (warn && scalar != 0.0 && scalar != 1.0)
+      gripe_logical_conversion ();
+
     retval(0) = static_cast<bool>(scalar);
+
     return retval;
   }
 
--- a/src/ov-range.h
+++ b/src/ov-range.h
@@ -160,8 +160,15 @@
 
   Complex complex_value (bool = false) const;
 
-  boolNDArray bool_array_value (void) const
-    { return boolNDArray (range.matrix_value ()); }
+  boolNDArray bool_array_value (bool warn = false) const
+  {
+    Matrix m = range.matrix_value ();
+
+    if (warn && m.any_element_not_one_or_zero ())
+      gripe_logical_conversion ();
+
+    return boolNDArray (m);
+  }
 
   ComplexMatrix complex_matrix_value (bool = false) const
     { return ComplexMatrix (range.matrix_value ()); }
--- a/src/ov-re-mat.cc
+++ b/src/ov-re-mat.cc
@@ -146,8 +146,11 @@
 }
 
 boolNDArray
-octave_matrix::bool_array_value (void) const
+octave_matrix::bool_array_value (bool warn) const
 {
+  if (warn && matrix.any_element_not_one_or_zero ())
+    gripe_logical_conversion ();
+
   return boolNDArray (matrix);
 }
   
--- a/src/ov-re-mat.h
+++ b/src/ov-re-mat.h
@@ -135,7 +135,7 @@
 
   ComplexNDArray complex_array_value (bool = false) const;
    
-  boolNDArray bool_array_value (void) const;
+  boolNDArray bool_array_value (bool warn = false) const;
 
   charNDArray char_array_value (bool = false) const;
   
--- a/src/ov-re-sparse.cc
+++ b/src/ov-re-sparse.cc
@@ -137,9 +137,14 @@
 }
 
 boolNDArray
-octave_sparse_matrix::bool_array_value (void) const
+octave_sparse_matrix::bool_array_value (bool warn) const
 {
-  return boolNDArray (matrix.matrix_value ());
+  NDArray m = matrix.matrix_value ();
+
+  if (warn && m.any_element_not_one_or_zero ())
+    gripe_logical_conversion ();
+
+  return boolNDArray (m);
 }
 
 ComplexMatrix
--- a/src/ov-re-sparse.h
+++ b/src/ov-re-sparse.h
@@ -101,7 +101,7 @@
 
   Complex complex_value (bool = false) const;
 
-  boolNDArray bool_array_value (void) const;
+  boolNDArray bool_array_value (bool warn = false) const;
 
   ComplexMatrix complex_matrix_value (bool = false) const;
 
--- a/src/ov-scalar.h
+++ b/src/ov-scalar.h
@@ -36,6 +36,7 @@
 #include "oct-alloc.h"
 #include "str-vec.h"
 
+#include "gripes.h"
 #include "ov-base.h"
 #include "ov-re-mat.h"
 #include "ov-base-scalar.h"
@@ -159,10 +160,21 @@
     return retval;
   }
 
-  bool bool_value (void) const { return scalar; }
+  bool bool_value (bool warn = false) const
+  {
+    if (warn && scalar != 0 && scalar != 1)
+      gripe_logical_conversion ();
+
+    return scalar;
+  }
 
-  boolNDArray bool_array_value (void) const
-    { return boolNDArray (dim_vector (1, 1), scalar); }
+  boolNDArray bool_array_value (bool warn = false) const
+  {
+    if (warn && scalar != 0 && scalar != 1)
+      gripe_logical_conversion ();
+
+    return boolNDArray (dim_vector (1, 1), scalar);
+  }
 
   std::streamoff streamoff_value (void) const;
 
--- a/src/ov.h
+++ b/src/ov.h
@@ -591,14 +591,14 @@
   ComplexNDArray complex_array_value (bool frc_str_conv = false) const
     { return rep->complex_array_value (frc_str_conv); }
 
-  bool bool_value (void) const
-    { return rep->bool_value (); }
+  bool bool_value (bool warn = false) const
+    { return rep->bool_value (warn); }
 
-  boolMatrix bool_matrix_value (void) const
-    { return rep->bool_matrix_value (); }
+  boolMatrix bool_matrix_value (bool warn = false) const
+    { return rep->bool_matrix_value (warn); }
 
-  boolNDArray bool_array_value (void) const
-    { return rep->bool_array_value (); }
+  boolNDArray bool_array_value (bool warn = false) const
+    { return rep->bool_array_value (warn); }
 
   charMatrix char_matrix_value (bool frc_str_conv = false) const
     { return rep->char_matrix_value (frc_str_conv); }