diff options
author | Gael Guennebaud <g.gael@free.fr> | 2008-09-03 17:56:06 +0000 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2008-09-03 17:56:06 +0000 |
commit | e14aa8c8aa1646bbfdf501a84b0665bb17220229 (patch) | |
tree | 6e45f62be82a402bee82c25c28b8f26c1edd4394 /Eigen | |
parent | 59dc1da5bf6b5c57205ee8d695e0f6e852ca74ae (diff) |
Add coeff-wise comparisons to scalar operators. You can now write:
mat.cwise() < 2
instead of:
mat.cwise() < MatrixType::Constant(mat.rows(), mat.cols(), 2)
Diffstat (limited to 'Eigen')
-rw-r--r-- | Eigen/src/Array/CwiseOperators.h | 97 | ||||
-rw-r--r-- | Eigen/src/Core/Cwise.h | 24 |
2 files changed, 121 insertions, 0 deletions
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<ExpressionType>::operator!=(const MatrixBase<OtherDerived> &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<OtherDerived> &) const + */ +template<typename ExpressionType> +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::less) +Cwise<ExpressionType>::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<OtherDerived> &) const + */ +template<typename ExpressionType> +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::less_equal) +Cwise<ExpressionType>::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<OtherDerived> &) const + */ +template<typename ExpressionType> +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::greater) +Cwise<ExpressionType>::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<OtherDerived> &) const + */ +template<typename ExpressionType> +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::greater_equal) +Cwise<ExpressionType>::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<OtherDerived> &) const + */ +template<typename ExpressionType> +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::equal_to) +Cwise<ExpressionType>::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<OtherDerived> &) const + */ +template<typename ExpressionType> +inline const EIGEN_CWISE_COMP_TO_SCALAR_RETURN_TYPE(std::not_equal_to) +Cwise<ExpressionType>::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<OP<typename ei_traits<ExpressionType>::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<OP<typename ei_traits<ExpressionType>::Scalar>, ExpressionType, \ + NestByValue<typename ExpressionType::ConstantReturnType> > + /** \class Cwise * * \brief Pseudo expression providing additional coefficient-wise operations @@ -128,6 +134,24 @@ template<typename ExpressionType> class Cwise template<typename OtherDerived> const EIGEN_CWISE_BINOP_RETURN_TYPE(std::not_equal_to) operator!=(const MatrixBase<OtherDerived>& 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; |