# HG changeset patch # User Jaroslav Hajek # Date 1250835496 -7200 # Node ID 19d298e6f7e5c421792b2cb7aa08a7efbbf0ce34 # Parent 3d6a9aea2aeae1b31d33b7c36b87ea2f99d0ae98 make ! operator check for NaNs, simplify implementations in liboctave diff --git a/liboctave/CMatrix.cc b/liboctave/CMatrix.cc --- a/liboctave/CMatrix.cc +++ b/liboctave/CMatrix.cc @@ -3058,16 +3058,8 @@ boolMatrix ComplexMatrix::operator ! (void) const { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - boolMatrix b (nr, nc); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = 0; i < nr; i++) - b.elem (i, j) = elem (i, j) == 0.0; - - return b; + ND_LOGICAL_NAN_CHECK (*this); + return do_mx_unary_op (*this, mx_inline_iszero); } // other operations diff --git a/liboctave/CNDArray.cc b/liboctave/CNDArray.cc --- a/liboctave/CNDArray.cc +++ b/liboctave/CNDArray.cc @@ -497,12 +497,8 @@ boolNDArray ComplexNDArray::operator ! (void) const { - boolNDArray b (dims ()); - - for (octave_idx_type i = 0; i < length (); i++) - b.elem (i) = elem (i) == 0.0; - - return b; + ND_LOGICAL_NAN_CHECK (*this); + return do_mx_unary_op (*this, mx_inline_iszero); } // FIXME -- this is not quite the right thing. diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,14 @@ +2009-08-20 Jaroslav Hajek + + * dMatrix.cc (Matrix::operator!): Simplify & check for NaNs. + * fMatrix.cc (FloatMatrix::operator!): Ditto. + * CMatrix.cc (ComplexMatrix::operator!): Ditto. + * fCMatrix.cc (FloatComplexMatrix::operator!): Ditto. + * dNDArray.cc (NDArray::operator!): Ditto. + * fNDArray.cc (FloatNDArray::operator!): Ditto. + * CNDArray.cc (ComplexNDArray::operator!): Ditto. + * fCNDArray.cc (FloatComplexNDArray::operator!): Ditto. + 2009-08-20 Jaroslav Hajek * mx-inlines.cc (mx_inline_add, mx_inline_sub, mx_inline_mul, diff --git a/liboctave/dMatrix.cc b/liboctave/dMatrix.cc --- a/liboctave/dMatrix.cc +++ b/liboctave/dMatrix.cc @@ -2567,16 +2567,8 @@ boolMatrix Matrix::operator ! (void) const { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - boolMatrix b (nr, nc); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = 0; i < nr; i++) - b.elem (i, j) = ! elem (i, j); - - return b; + ND_LOGICAL_NAN_CHECK (*this); + return do_mx_unary_op (*this, mx_inline_iszero); } // column vector by row vector -> matrix operations diff --git a/liboctave/dNDArray.cc b/liboctave/dNDArray.cc --- a/liboctave/dNDArray.cc +++ b/liboctave/dNDArray.cc @@ -538,12 +538,8 @@ boolNDArray NDArray::operator ! (void) const { - boolNDArray b (dims ()); - - for (octave_idx_type i = 0; i < length (); i++) - b.elem (i) = ! elem (i); - - return b; + ND_LOGICAL_NAN_CHECK (*this); + return do_mx_unary_op (*this, mx_inline_iszero); } bool diff --git a/liboctave/fCMatrix.cc b/liboctave/fCMatrix.cc --- a/liboctave/fCMatrix.cc +++ b/liboctave/fCMatrix.cc @@ -3051,16 +3051,8 @@ boolMatrix FloatComplexMatrix::operator ! (void) const { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - boolMatrix b (nr, nc); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = 0; i < nr; i++) - b.elem (i, j) = elem (i, j) == static_cast (0.0); - - return b; + ND_LOGICAL_NAN_CHECK (*this); + return do_mx_unary_op (*this, mx_inline_iszero); } // other operations diff --git a/liboctave/fCNDArray.cc b/liboctave/fCNDArray.cc --- a/liboctave/fCNDArray.cc +++ b/liboctave/fCNDArray.cc @@ -492,12 +492,8 @@ boolNDArray FloatComplexNDArray::operator ! (void) const { - boolNDArray b (dims ()); - - for (octave_idx_type i = 0; i < length (); i++) - b.elem (i) = elem (i) == static_cast (0.0); - - return b; + ND_LOGICAL_NAN_CHECK (*this); + return do_mx_unary_op (*this, mx_inline_iszero); } // FIXME -- this is not quite the right thing. diff --git a/liboctave/fMatrix.cc b/liboctave/fMatrix.cc --- a/liboctave/fMatrix.cc +++ b/liboctave/fMatrix.cc @@ -2566,16 +2566,8 @@ boolMatrix FloatMatrix::operator ! (void) const { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - boolMatrix b (nr, nc); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = 0; i < nr; i++) - b.elem (i, j) = ! elem (i, j); - - return b; + ND_LOGICAL_NAN_CHECK (*this); + return do_mx_unary_op (*this, mx_inline_iszero); } // column vector by row vector -> matrix operations diff --git a/liboctave/fNDArray.cc b/liboctave/fNDArray.cc --- a/liboctave/fNDArray.cc +++ b/liboctave/fNDArray.cc @@ -496,12 +496,8 @@ boolNDArray FloatNDArray::operator ! (void) const { - boolNDArray b (dims ()); - - for (octave_idx_type i = 0; i < length (); i++) - b.elem (i) = ! elem (i); - - return b; + ND_LOGICAL_NAN_CHECK (*this); + return do_mx_unary_op (*this, mx_inline_iszero); } bool diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2009-08-21 Jaroslav Hajek + + * OPERATORS/op-s-s.cc: Check for NaN in ! operator. + * OPERATORS/op-fs-fs.cc: Ditto. + * OPERATORS/op-cs-cs.cc: Ditto. + * OPERATORS/op-fcs-fcs.cc: Ditto. + 2009-08-17 Jaroslav Hajek * ops.h (DEFNDASSIGNOP_FNOP): New macro. diff --git a/src/OPERATORS/op-cs-cs.cc b/src/OPERATORS/op-cs-cs.cc --- a/src/OPERATORS/op-cs-cs.cc +++ b/src/OPERATORS/op-cs-cs.cc @@ -25,6 +25,8 @@ #include #endif +#include "Array-util.h" + #include "gripes.h" #include "oct-obj.h" #include "ov.h" @@ -42,8 +44,10 @@ DEFUNOP (not, complex) { CAST_UNOP_ARG (const octave_complex&); - - return octave_value (v.complex_value () == 0.0); + Complex x = v.complex_value (); + if (xisnan (x)) + gripe_nan_to_logical_conversion (); + return octave_value (x == 0.0); } DEFUNOP_OP (uplus, complex, /* no-op */) diff --git a/src/OPERATORS/op-fcs-fcs.cc b/src/OPERATORS/op-fcs-fcs.cc --- a/src/OPERATORS/op-fcs-fcs.cc +++ b/src/OPERATORS/op-fcs-fcs.cc @@ -42,8 +42,10 @@ DEFUNOP (not, float_complex) { CAST_UNOP_ARG (const octave_float_complex&); - - return octave_value (v.float_complex_value () == 0.0); + FloatComplex x = v.float_complex_value (); + if (xisnan (x)) + gripe_nan_to_logical_conversion (); + return octave_value (x == 0.0f); } DEFUNOP_OP (uplus, float_complex, /* no-op */) diff --git a/src/OPERATORS/op-fs-fs.cc b/src/OPERATORS/op-fs-fs.cc --- a/src/OPERATORS/op-fs-fs.cc +++ b/src/OPERATORS/op-fs-fs.cc @@ -25,6 +25,8 @@ #include #endif +#include "Array-util.h" + #include "gripes.h" #include "oct-obj.h" #include "ov.h" @@ -39,7 +41,15 @@ // scalar unary ops. -DEFUNOP_OP (not, float_scalar, !) +DEFUNOP (not, float_scalar) +{ + CAST_UNOP_ARG (const octave_float_scalar&); + float x = v.float_value (); + if (xisnan (x)) + gripe_nan_to_logical_conversion (); + return octave_value (x == 0.0f); +} + DEFUNOP_OP (uplus, float_scalar, /* no-op */) DEFUNOP_OP (uminus, float_scalar, -) DEFUNOP_OP (transpose, float_scalar, /* no-op */) diff --git a/src/OPERATORS/op-s-s.cc b/src/OPERATORS/op-s-s.cc --- a/src/OPERATORS/op-s-s.cc +++ b/src/OPERATORS/op-s-s.cc @@ -25,6 +25,8 @@ #include #endif +#include "Array-util.h" + #include "gripes.h" #include "oct-obj.h" #include "ov.h" @@ -40,7 +42,15 @@ // scalar unary ops. -DEFUNOP_OP (not, scalar, !) +DEFUNOP (not, scalar) +{ + CAST_UNOP_ARG (const octave_scalar&); + double x = v.scalar_value (); + if (xisnan (x)) + gripe_nan_to_logical_conversion (); + return octave_value (x == 0.0); +} + DEFUNOP_OP (uplus, scalar, /* no-op */) DEFUNOP_OP (uminus, scalar, -) DEFUNOP_OP (transpose, scalar, /* no-op */)