# HG changeset patch # User Jaroslav Hajek # Date 1258966829 -3600 # Node ID aabf7a8c2e5747fdf6c6f50635cd234bd831773d # Parent 1fac51c5f83fbca4fbe7d26093208c01f57312d8 implement sparse logical conversion diff --git a/liboctave/dSparse.cc b/liboctave/dSparse.cc --- a/liboctave/dSparse.cc +++ b/liboctave/dSparse.cc @@ -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 (); diff --git a/liboctave/dSparse.h b/liboctave/dSparse.h --- a/liboctave/dSparse.h +++ b/liboctave/dSparse.h @@ -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; diff --git a/src/ls-oct-ascii.h b/src/ls-oct-ascii.h --- a/src/ls-oct-ascii.h +++ b/src/ls-oct-ascii.h @@ -115,6 +115,14 @@ return status; } +template +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. diff --git a/src/ov-bool-mat.cc b/src/ov-bool-mat.cc --- a/src/ov-bool-mat.cc +++ b/src/ov-bool-mat.cc @@ -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" diff --git a/src/ov-re-sparse.cc b/src/ov-re-sparse.cc --- a/src/ov-re-sparse.cc +++ b/src/ov-re-sparse.cc @@ -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 { diff --git a/src/ov-re-sparse.h b/src/ov-re-sparse.h --- a/src/ov-re-sparse.h +++ b/src/ov-re-sparse.h @@ -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 diff --git a/src/ov.h b/src/ov.h --- a/src/ov.h +++ b/src/ov.h @@ -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); }