Mercurial > hg > octave-terminal
changeset 9853:8d9e4752441a
implement complex built-in logical conversions
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 23 Nov 2009 10:48:31 +0100 |
parents | aabf7a8c2e57 |
children | 5d5dc23d6c56 |
files | src/ChangeLog src/ov-complex.h src/ov-cx-mat.cc src/ov-cx-mat.h src/ov-cx-sparse.cc src/ov-cx-sparse.h src/ov-flt-complex.h src/ov-flt-cx-mat.cc src/ov-flt-cx-mat.h |
diffstat | 9 files changed, 96 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,15 @@ +2009-11-23 Jaroslav Hajek <highegg@gmail.com> + + * ov-complex.h (octave_complex::bool_value, + octave_complex::bool_array_value): New methods. + * ov-flt-complex.h (octave_float_complex::bool_value, + octave_float_complex::bool_array_value): New methods. + * ov-cx-mat.cc (octave_complex_matrix::bool_array_value): New method. + * ov-cx-mat.h: Declare it. + * ov-flt-cx-mat.cc (octave_float_complex_matrix::bool_array_value): + New method. + * ov-flt-cx-mat.h: Declare it. + 2009-11-23 Jaroslav Hajek <highegg@gmail.com> * ov-struct.cc (octave_struct::save_ascii): Save dimensions for
--- a/src/ov-complex.h +++ b/src/ov-complex.h @@ -34,6 +34,7 @@ #include "oct-alloc.h" #include "str-vec.h" +#include "gripes.h" #include "error.h" #include "ov-base.h" #include "ov-cx-mat.h" @@ -135,6 +136,26 @@ FloatComplexNDArray float_complex_array_value (bool = false) const; + bool bool_value (bool warn = false) const + { + if (xisnan (scalar)) + error ("invalid conversion from NaN to logical"); + else if (warn && scalar != 0.0 && scalar != 1.0) + gripe_logical_conversion (); + + return scalar != 0.0; + } + + boolNDArray bool_array_value (bool warn = false) const + { + if (xisnan (scalar)) + error ("invalid conversion from NaN to logical"); + else if (warn && scalar != 0.0 && scalar != 1.0) + gripe_logical_conversion (); + + return boolNDArray (dim_vector (1, 1), scalar != 0.0); + } + void increment (void) { scalar += 1.0; } void decrement (void) { scalar -= 1.0; }
--- a/src/ov-cx-mat.cc +++ b/src/ov-cx-mat.cc @@ -221,6 +221,18 @@ return FloatComplexMatrix (matrix.matrix_value ()); } +boolNDArray +octave_complex_matrix::bool_array_value (bool warn) const +{ + if (matrix.any_element_is_nan ()) + error ("invalid conversion from NaN to logical"); + else if (warn && (! matrix.all_elements_are_real () + || real (matrix).any_element_not_one_or_zero ())) + gripe_logical_conversion (); + + return mx_el_ne (matrix, Complex (0.0)); +} + charNDArray octave_complex_matrix::char_array_value (bool frc_str_conv) const {
--- a/src/ov-cx-mat.h +++ b/src/ov-cx-mat.h @@ -126,6 +126,8 @@ FloatComplexNDArray float_complex_array_value (bool = false) const; + boolNDArray bool_array_value (bool warn = false) const; + charNDArray char_array_value (bool frc_str_conv = false) const; SparseMatrix sparse_matrix_value (bool = false) const;
--- a/src/ov-cx-sparse.cc +++ b/src/ov-cx-sparse.cc @@ -230,6 +230,18 @@ return retval; } +SparseBoolMatrix +octave_sparse_complex_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.all_elements_are_real () + || real (matrix).any_element_not_one_or_zero ())) + gripe_logical_conversion (); + + return mx_el_ne (matrix, Complex (0.0)); +} + bool octave_sparse_complex_matrix::save_binary (std::ostream& os, bool&save_as_floats)
--- a/src/ov-cx-sparse.h +++ b/src/ov-cx-sparse.h @@ -127,6 +127,8 @@ SparseComplexMatrix sparse_complex_matrix_value (bool = false) const { return matrix; } + SparseBoolMatrix sparse_bool_matrix_value (bool warn = false) const; + #if 0 int write (octave_stream& os, int block_size, oct_data_conv::data_type output_type, int skip,
--- a/src/ov-flt-complex.h +++ b/src/ov-flt-complex.h @@ -34,6 +34,7 @@ #include "oct-alloc.h" #include "str-vec.h" +#include "gripes.h" #include "error.h" #include "ov-base.h" #include "ov-flt-cx-mat.h" @@ -133,6 +134,26 @@ FloatComplexNDArray float_complex_array_value (bool = false) const; + bool bool_value (bool warn = false) const + { + if (xisnan (scalar)) + error ("invalid conversion from NaN to logical"); + else if (warn && scalar != 0.0f && scalar != 1.0f) + gripe_logical_conversion (); + + return scalar != 0.0f; + } + + boolNDArray bool_array_value (bool warn = false) const + { + if (xisnan (scalar)) + error ("invalid conversion from NaN to logical"); + else if (warn && scalar != 0.0f && scalar != 1.0f) + gripe_logical_conversion (); + + return boolNDArray (dim_vector (1, 1), scalar != 1.0f); + } + void increment (void) { scalar += 1.0; } void decrement (void) { scalar -= 1.0; }
--- a/src/ov-flt-cx-mat.cc +++ b/src/ov-flt-cx-mat.cc @@ -210,6 +210,18 @@ return FloatComplexMatrix (matrix.matrix_value ()); } +boolNDArray +octave_float_complex_matrix::bool_array_value (bool warn) const +{ + if (matrix.any_element_is_nan ()) + error ("invalid conversion from NaN to logical"); + else if (warn && (! matrix.all_elements_are_real () + || real (matrix).any_element_not_one_or_zero ())) + gripe_logical_conversion (); + + return mx_el_ne (matrix, FloatComplex (0.0)); +} + charNDArray octave_float_complex_matrix::char_array_value (bool frc_str_conv) const {
--- a/src/ov-flt-cx-mat.h +++ b/src/ov-flt-cx-mat.h @@ -124,6 +124,8 @@ FloatComplexNDArray float_complex_array_value (bool = false) const; + boolNDArray bool_array_value (bool warn = false) const; + charNDArray char_array_value (bool frc_str_conv = false) const; SparseMatrix sparse_matrix_value (bool = false) const;