diff --git a/Eigen/src/Array/CwiseOperators.h b/Eigen/src/Array/CwiseOperators.h index 498bc86f2..4b6346daa 100644 --- a/Eigen/src/Array/CwiseOperators.h +++ b/Eigen/src/Array/CwiseOperators.h @@ -289,6 +289,103 @@ Cwise::operator!=(const MatrixBase &other) const return EIGEN_CWISE_BINOP_RETURN_TYPE(std::not_equal_to)(_expression(), other.derived()); } +// comparisons to scalar value + +/** \array_module + * + * \returns an expression of the coefficient-wise \< operator of *this and a scalar \a s + * + * \sa operator<(const MatrixBase &) const + */ +template +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::less) +Cwise::operator<(Scalar s) const +{ + return EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::less)(_expression(), + typename ExpressionType::ConstantReturnType(_expression().rows(), _expression().cols(), s)); +} + +/** \array_module + * + * \returns an expression of the coefficient-wise \<= operator of *this and a scalar \a s + * + * \sa operator<=(const MatrixBase &) const + */ +template +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::less_equal) +Cwise::operator<=(Scalar s) const +{ + return EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::less_equal)(_expression(), + typename ExpressionType::ConstantReturnType(_expression().rows(), _expression().cols(), s)); +} + +/** \array_module + * + * \returns an expression of the coefficient-wise \> operator of *this and a scalar \a s + * + * \sa operator>(const MatrixBase &) const + */ +template +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::greater) +Cwise::operator>(Scalar s) const +{ + return EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::greater)(_expression(), + typename ExpressionType::ConstantReturnType(_expression().rows(), _expression().cols(), s)); +} + +/** \array_module + * + * \returns an expression of the coefficient-wise \>= operator of *this and a scalar \a s + * + * \sa operator>=(const MatrixBase &) const + */ +template +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::greater_equal) +Cwise::operator>=(Scalar s) const +{ + return EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::greater_equal)(_expression(), + typename ExpressionType::ConstantReturnType(_expression().rows(), _expression().cols(), s)); +} + +/** \array_module + * + * \returns an expression of the coefficient-wise == operator of *this and a scalar \a s + * + * \warning this performs an exact comparison, which is generally a bad idea with floating-point types. + * In order to check for equality between two vectors or matrices with floating-point coefficients, it is + * generally a far better idea to use a fuzzy comparison as provided by MatrixBase::isApprox() and + * MatrixBase::isMuchSmallerThan(). + * + * \sa operator==(const MatrixBase &) const + */ +template +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::equal_to) +Cwise::operator==(Scalar s) const +{ + return EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::equal_to)(_expression(), + typename ExpressionType::ConstantReturnType(_expression().rows(), _expression().cols(), s)); +} + +/** \array_module + * + * \returns an expression of the coefficient-wise != operator of *this and a scalar \a s + * + * \warning this performs an exact comparison, which is generally a bad idea with floating-point types. + * In order to check for equality between two vectors or matrices with floating-point coefficients, it is + * generally a far better idea to use a fuzzy comparison as provided by MatrixBase::isApprox() and + * MatrixBase::isMuchSmallerThan(). + * + * \sa operator!=(const MatrixBase &) const + */ +template +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::not_equal_to) +Cwise::operator!=(Scalar s) const +{ + return EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::not_equal_to)(_expression(), + typename ExpressionType::ConstantReturnType(_expression().rows(), _expression().cols(), s)); +} + +// scalar addition /** \array_module * diff --git a/Eigen/src/Core/Cwise.h b/Eigen/src/Core/Cwise.h index b4c508ddd..bdf21260a 100644 --- a/Eigen/src/Core/Cwise.h +++ b/Eigen/src/Core/Cwise.h @@ -36,6 +36,12 @@ #define EIGEN_CWISE_UNOP_RETURN_TYPE(OP) \ CwiseUnaryOp::Scalar>, ExpressionType> +/** \internal + * convenient macro to defined the return type of a cwise comparison to a scalar */ +#define EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(OP) \ + CwiseBinaryOp::Scalar>, ExpressionType, \ + NestByValue > + /** \class Cwise * * \brief Pseudo expression providing additional coefficient-wise operations @@ -128,6 +134,24 @@ template class Cwise template const EIGEN_CWISE_BINOP_RETURN_TYPE(std::not_equal_to) operator!=(const MatrixBase& other) const; + // comparisons to a scalar value + const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::less) + operator<(Scalar s) const; + + const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::less_equal) + operator<=(Scalar s) const; + + const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::greater) + operator>(Scalar s) const; + + const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::greater_equal) + operator>=(Scalar s) const; + + const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::equal_to) + operator==(Scalar s) const; + + const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::not_equal_to) + operator!=(Scalar s) const; protected: ExpressionTypeNested m_matrix; diff --git a/test/array.cpp b/test/array.cpp index 0fa13b65b..977449365 100644 --- a/test/array.cpp +++ b/test/array.cpp @@ -89,6 +89,12 @@ template void comparisons(const MatrixType& m) VERIFY(! (m1.cwise() > m3).all() ); } + // comparisons to scalar + VERIFY( (m1.cwise() != (m1(r,c)+1) ).any() ); + VERIFY( (m1.cwise() > (m1(r,c)-1) ).any() ); + VERIFY( (m1.cwise() < (m1(r,c)+1) ).any() ); + VERIFY( (m1.cwise() == m1(r,c) ).any() ); + // test Select VERIFY_IS_APPROX( (m1.cwise()m2).select(m1,m2), m1.cwise().max(m2) ); @@ -98,10 +104,13 @@ template void comparisons(const MatrixType& m) m3(i,j) = ei_abs(m1(i,j))=MatrixType::Constant(rows,cols,mid)) .select(m1,0), m3); + // even shorter version: + VERIFY_IS_APPROX( (m1.cwise().abs().cwise()