From 1c78d6f2a6c7be509c1a190e1cfe42bce1916230 Mon Sep 17 00:00:00 2001 From: Deanna Hood Date: Tue, 17 Mar 2015 08:29:57 +1000 Subject: [PATCH] Add boolean not operator (!) array support --- Eigen/src/Core/functors/UnaryFunctors.h | 18 ++++++++++++++++++ Eigen/src/plugins/ArrayCwiseUnaryOps.h | 20 ++++++++++++++++++++ doc/snippets/Cwise_boolean_not.cpp | 5 +++++ test/array.cpp | 2 ++ 4 files changed, 45 insertions(+) create mode 100644 doc/snippets/Cwise_boolean_not.cpp diff --git a/Eigen/src/Core/functors/UnaryFunctors.h b/Eigen/src/Core/functors/UnaryFunctors.h index fa74aeae0..a7cd3b7ca 100644 --- a/Eigen/src/Core/functors/UnaryFunctors.h +++ b/Eigen/src/Core/functors/UnaryFunctors.h @@ -626,6 +626,24 @@ struct functor_traits > }; }; +/** \internal + * \brief Template functor to compute the logical not of a boolean + * + * \sa class CwiseUnaryOp, ArrayBase::operator! + */ +template struct scalar_boolean_not_op { + EIGEN_EMPTY_STRUCT_CTOR(scalar_boolean_not_op) + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator() (const bool& a) const { return !a; } +}; +template +struct functor_traits > { + enum { + Cost = NumTraits::AddCost, + PacketAccess = false + }; +}; + + } // end namespace internal } // end namespace Eigen diff --git a/Eigen/src/plugins/ArrayCwiseUnaryOps.h b/Eigen/src/plugins/ArrayCwiseUnaryOps.h index 6e6c86401..1d5c9a6c1 100644 --- a/Eigen/src/plugins/ArrayCwiseUnaryOps.h +++ b/Eigen/src/plugins/ArrayCwiseUnaryOps.h @@ -5,6 +5,7 @@ typedef CwiseUnaryOp, const Derived> ArgReturnTy typedef CwiseUnaryOp, const Derived> Abs2ReturnType; typedef CwiseUnaryOp, const Derived> SqrtReturnType; typedef CwiseUnaryOp, const Derived> InverseReturnType; +typedef CwiseUnaryOp, const Derived> BooleanNotReturnType; typedef CwiseUnaryOp, const Derived> ExpReturnType; typedef CwiseUnaryOp, const Derived> LogReturnType; @@ -404,6 +405,25 @@ isFinite() const return IsFiniteReturnType(derived()); } +/** \returns an expression of the coefficient-wise ! operator of *this + * + * \warning this operator is for expression of bool only. + * + * Example: \include Cwise_boolean_not.cpp + * Output: \verbinclude Cwise_boolean_not.out + * + * \sa operator!=() + */ +EIGEN_DEVICE_FUNC +inline const BooleanNotReturnType +operator!() const +{ + EIGEN_STATIC_ASSERT((internal::is_same::value), + THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_OF_BOOL); + return BooleanNotReturnType(derived()); +} + + #define EIGEN_MAKE_SCALAR_CWISE_UNARY_OP(METHOD_NAME,FUNCTOR) \ EIGEN_DEVICE_FUNC \ inline const CwiseUnaryOp >, const Derived> \ diff --git a/doc/snippets/Cwise_boolean_not.cpp b/doc/snippets/Cwise_boolean_not.cpp new file mode 100644 index 000000000..8b8e6fc95 --- /dev/null +++ b/doc/snippets/Cwise_boolean_not.cpp @@ -0,0 +1,5 @@ +Array3d v(1,2,3); +v(1) *= 0.0/0.0; +v(2) /= 0.0; +cout << v << endl << endl; +cout << !isFinite(v) << endl; diff --git a/test/array.cpp b/test/array.cpp index a17fe9b4a..254af2ee9 100644 --- a/test/array.cpp +++ b/test/array.cpp @@ -221,6 +221,8 @@ template void array_real(const ArrayType& m) VERIFY_IS_APPROX(m1.square().sqrt(), sqrt(square(m1))); VERIFY_IS_APPROX(cube(m1.cube()), pow((m1),3*3)); + VERIFY(!(m1>m2),(m1<=m2)); + VERIFY_IS_APPROX(cos(m1+RealScalar(3)*m2), cos((m1+RealScalar(3)*m2).eval())); VERIFY_IS_APPROX(m1.abs().sqrt(), sqrt(abs(m1)));