changeset 9852:aabf7a8c2e57

implement sparse logical conversion
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 23 Nov 2009 10:00:29 +0100
parents 1fac51c5f83f
children 8d9e4752441a
files liboctave/dSparse.cc liboctave/dSparse.h src/ls-oct-ascii.h src/ov-bool-mat.cc src/ov-re-sparse.cc src/ov-re-sparse.h src/ov.h
diffstat 7 files changed, 40 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/dSparse.cc	Mon Nov 23 08:43:42 2009 +0100
+++ b/liboctave/dSparse.cc	Mon Nov 23 10:00:29 2009 +0100
@@ -7348,6 +7348,21 @@
 }
 
 bool
+SparseMatrix::any_element_not_one_or_zero (void) const
+{
+  octave_idx_type nel = nnz ();
+
+  for (octave_idx_type i = 0; i < nel; i++)
+    {
+      double val = data (i);
+      if (val != 0.0 && val != 1.0)
+	return true;
+    }
+
+  return false;
+}
+
+bool
 SparseMatrix::all_elements_are_zero (void) const
 {
   octave_idx_type nel = nnz ();
--- a/liboctave/dSparse.h	Mon Nov 23 08:43:42 2009 +0100
+++ b/liboctave/dSparse.h	Mon Nov 23 10:00:29 2009 +0100
@@ -386,6 +386,7 @@
   bool any_element_is_negative (bool = false) const;
   bool any_element_is_nan (void) const;
   bool any_element_is_inf_or_nan (void) const;
+  bool any_element_not_one_or_zero (void) const;
   bool all_elements_are_zero (void) const;
   bool all_elements_are_int_or_inf_or_nan (void) const;
   bool all_integers (double& max_val, double& min_val) const;
--- a/src/ls-oct-ascii.h	Mon Nov 23 08:43:42 2009 +0100
+++ b/src/ls-oct-ascii.h	Mon Nov 23 10:00:29 2009 +0100
@@ -115,6 +115,14 @@
   return status;
 }
 
+template <class T>
+bool
+extract_keyword (std::istream& is, const std::string& kw, T& value, 
+		 const bool next_only = false)
+{
+  return extract_keyword (is, kw.c_str (), value, next_only);
+}
+
 // Match one of the elements in KEYWORDS on stream IS, placing the
 // matched keyword in KW and the associated value in VALUE,
 // returning TRUE if successful and FALSE otherwise.
--- a/src/ov-bool-mat.cc	Mon Nov 23 08:43:42 2009 +0100
+++ b/src/ov-bool-mat.cc	Mon Nov 23 10:00:29 2009 +0100
@@ -32,6 +32,7 @@
 #include "mx-base.h"
 #include "oct-locbuf.h"
 
+#include "defun.h"
 #include "gripes.h"
 #include "oct-obj.h"
 #include "ops.h"
--- a/src/ov-re-sparse.cc	Mon Nov 23 08:43:42 2009 +0100
+++ b/src/ov-re-sparse.cc	Mon Nov 23 10:00:29 2009 +0100
@@ -184,6 +184,17 @@
   return NDArray (matrix.matrix_value ());
 }
 
+SparseBoolMatrix 
+octave_sparse_matrix::sparse_bool_matrix_value (bool warn) const
+{
+  if (matrix.any_element_is_nan ())
+    error ("invalid conversion from NaN to logical");
+  else if (warn && matrix.any_element_not_one_or_zero ())
+    gripe_logical_conversion ();
+
+  return mx_el_ne (matrix, 0.0);
+}
+
 octave_value
 octave_sparse_matrix::convert_to_str_internal (bool, bool, char type) const
 {
--- a/src/ov-re-sparse.h	Mon Nov 23 08:43:42 2009 +0100
+++ b/src/ov-re-sparse.h	Mon Nov 23 10:00:29 2009 +0100
@@ -128,6 +128,8 @@
   SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
     { return SparseComplexMatrix (matrix); }
 
+  SparseBoolMatrix sparse_bool_matrix_value (bool warn = false) const;
+
   octave_value convert_to_str_internal (bool pad, bool force, char type) const;
 
 #if 0
--- a/src/ov.h	Mon Nov 23 08:43:42 2009 +0100
+++ b/src/ov.h	Mon Nov 23 10:00:29 2009 +0100
@@ -764,8 +764,8 @@
   SparseComplexMatrix sparse_complex_matrix_value (bool frc_str_conv = false) const
     { return rep->sparse_complex_matrix_value (frc_str_conv); }
 
-  SparseBoolMatrix sparse_bool_matrix_value (bool frc_str_conv = false) const
-    { return rep->sparse_bool_matrix_value (frc_str_conv); }
+  SparseBoolMatrix sparse_bool_matrix_value (bool warn = false) const
+    { return rep->sparse_bool_matrix_value (warn); }
 
   DiagMatrix diag_matrix_value (bool force = false) const
     { return rep->diag_matrix_value (force); }