From 048910caae33dbf6b2359d5264a8be828084906a Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Thu, 3 Apr 2008 18:13:27 +0000 Subject: * added cwise comparisons * added "all" and "any" special redux operators * added support bool matrices * added support for cost model of STL functors via ei_functor_traits (By default ei_functor_traits query the functor member Cost) --- Eigen/src/Core/AssociativeFunctors.h | 38 ++++++++++++++ Eigen/src/Core/CwiseBinaryOp.h | 74 ++++++++++++++++++++++++++- Eigen/src/Core/CwiseUnaryOp.h | 2 +- Eigen/src/Core/ForwardDeclarations.h | 9 +++- Eigen/src/Core/MatrixBase.h | 38 +++++++++++++- Eigen/src/Core/NumTraits.h | 19 +++++-- Eigen/src/Core/Redux.h | 98 ++++++++++++++++++++++++++++++++++++ 7 files changed, 271 insertions(+), 7 deletions(-) (limited to 'Eigen/src') diff --git a/Eigen/src/Core/AssociativeFunctors.h b/Eigen/src/Core/AssociativeFunctors.h index 3b3a09ed9..cfd1ddbd9 100644 --- a/Eigen/src/Core/AssociativeFunctors.h +++ b/Eigen/src/Core/AssociativeFunctors.h @@ -65,4 +65,42 @@ template struct ei_scalar_max_op EIGEN_EMPTY_STRUCT { enum { Cost = ConditionalJumpCost + NumTraits::AddCost }; }; +// default ei_functor_traits for STL functors: + +template +struct ei_functor_traits > +{ enum { Cost = 10 }; }; + +template +struct ei_functor_traits > +{ enum { Cost = 5 }; }; + +template +struct ei_functor_traits > +{ enum { Cost = 5 }; }; + +template +struct ei_functor_traits > +{ enum { Cost = 5 }; }; + +template +struct ei_functor_traits > +{ enum { Cost = 1 }; }; + +template +struct ei_functor_traits > +{ enum { Cost = 1 }; }; + +template +struct ei_functor_traits > +{ enum { Cost = 1 }; }; + +template +struct ei_functor_traits > +{ enum { Cost = 1 }; }; + +template +struct ei_functor_traits > +{ enum { Cost = 1 }; }; + #endif // EIGEN_ASSOCIATIVE_FUNCTORS_H diff --git a/Eigen/src/Core/CwiseBinaryOp.h b/Eigen/src/Core/CwiseBinaryOp.h index 12da94a6c..12560284d 100644 --- a/Eigen/src/Core/CwiseBinaryOp.h +++ b/Eigen/src/Core/CwiseBinaryOp.h @@ -61,7 +61,7 @@ struct ei_traits > MaxRowsAtCompileTime = Lhs::MaxRowsAtCompileTime, MaxColsAtCompileTime = Lhs::MaxColsAtCompileTime, Flags = Lhs::Flags | Rhs::Flags, - CoeffReadCost = Lhs::CoeffReadCost + Rhs::CoeffReadCost + BinaryOp::Cost + CoeffReadCost = Lhs::CoeffReadCost + Rhs::CoeffReadCost + ei_functor_traits::Cost }; }; @@ -230,4 +230,76 @@ MatrixBase::cwise(const MatrixBase &other, const CustomBi return CwiseBinaryOp(derived(), other.derived(), func); } +/** \returns an expression of the coefficient-wise \< operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template +template +const CwiseBinaryOp::Scalar>, Derived, OtherDerived> +MatrixBase::cwiseLessThan(const MatrixBase &other) const +{ + return cwise(other, std::less()); +} + +/** \returns an expression of the coefficient-wise \<= operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template +template +const CwiseBinaryOp::Scalar>, Derived, OtherDerived> +MatrixBase::cwiseLessEqual(const MatrixBase &other) const +{ + return cwise(other, std::less_equal()); +} + +/** \returns an expression of the coefficient-wise \> operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template +template +const CwiseBinaryOp::Scalar>, Derived, OtherDerived> +MatrixBase::cwiseGreaterThan(const MatrixBase &other) const +{ + return cwise(other, std::greater()); +} + +/** \returns an expression of the coefficient-wise \>= operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template +template +const CwiseBinaryOp::Scalar>, Derived, OtherDerived> +MatrixBase::cwiseGreaterEqual(const MatrixBase &other) const +{ + return cwise(other, std::greater_equal()); +} + +/** \returns an expression of the coefficient-wise == operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template +template +const CwiseBinaryOp::Scalar>, Derived, OtherDerived> +MatrixBase::cwiseEqualTo(const MatrixBase &other) const +{ + return cwise(other, std::equal_to()); +} + +/** \returns an expression of the coefficient-wise != operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template +template +const CwiseBinaryOp::Scalar>, Derived, OtherDerived> +MatrixBase::cwiseNotEqualTo(const MatrixBase &other) const +{ + return cwise(other, std::not_equal_to()); +} + #endif // EIGEN_CWISE_BINARY_OP_H diff --git a/Eigen/src/Core/CwiseUnaryOp.h b/Eigen/src/Core/CwiseUnaryOp.h index 548d500a1..ae879a3f5 100644 --- a/Eigen/src/Core/CwiseUnaryOp.h +++ b/Eigen/src/Core/CwiseUnaryOp.h @@ -51,7 +51,7 @@ struct ei_traits > MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime, MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime, Flags = MatrixType::Flags, - CoeffReadCost = MatrixType::CoeffReadCost + UnaryOp::Cost + CoeffReadCost = MatrixType::CoeffReadCost + ei_functor_traits::Cost }; }; diff --git a/Eigen/src/Core/ForwardDeclarations.h b/Eigen/src/Core/ForwardDeclarations.h index e26704c07..b4aaaa005 100644 --- a/Eigen/src/Core/ForwardDeclarations.h +++ b/Eigen/src/Core/ForwardDeclarations.h @@ -82,7 +82,7 @@ struct ei_copy_unless_matrix struct ei_xpr_copy { - typedef typename ei_meta_if::type >::ret type; @@ -115,4 +115,11 @@ template struct ei_eval_if_needed_before_nesting typedef typename ei_meta_if::type, T>::ret type; }; + +template struct ei_functor_traits +{ + enum { Cost = T::Cost }; +}; + + #endif // EIGEN_FORWARDDECLARATIONS_H diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index d0187df8f..57fc2bdbb 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -252,7 +252,7 @@ template class MatrixBase */ //@{ template - const Product::type, + const Product::type, typename ei_eval_if_needed_before_nesting::ColsAtCompileTime>::type> operator*(const MatrixBase &other) const; @@ -354,6 +354,15 @@ template class MatrixBase RealScalar prec = precision()) const; bool isOrtho(RealScalar prec = precision()) const; + template + bool operator==(const MatrixBase& other) const + { return derived().cwiseEqualTo(other.derived()).all(); } + + template + bool operator!=(const MatrixBase& other) const + { return derived().cwiseNotEqualTo(other.derived()).all(); } + //@} + /// \name Special functions //@{ template @@ -390,6 +399,30 @@ template class MatrixBase const CwiseBinaryOp::Scalar>, Derived, OtherDerived> cwiseMax(const MatrixBase &other) const; + template + const CwiseBinaryOp::Scalar>, Derived, OtherDerived> + cwiseLessThan(const MatrixBase &other) const; + + template + const CwiseBinaryOp::Scalar>, Derived, OtherDerived> + cwiseLessEqual(const MatrixBase &other) const; + + template + const CwiseBinaryOp::Scalar>, Derived, OtherDerived> + cwiseGreaterThan(const MatrixBase &other) const; + + template + const CwiseBinaryOp::Scalar>, Derived, OtherDerived> + cwiseGreaterEqual(const MatrixBase &other) const; + + template + const CwiseBinaryOp::Scalar>, Derived, OtherDerived> + cwiseEqualTo(const MatrixBase &other) const; + + template + const CwiseBinaryOp::Scalar>, Derived, OtherDerived> + cwiseNotEqualTo(const MatrixBase &other) const; + const CwiseUnaryOp::Scalar>, Derived> cwiseAbs() const; const CwiseUnaryOp::Scalar>, Derived> cwiseAbs2() const; const CwiseUnaryOp::Scalar>, Derived> cwiseSqrt() const; @@ -419,6 +452,9 @@ template class MatrixBase typename ei_traits::Scalar minCoeff(int* row, int* col = 0) const; typename ei_traits::Scalar maxCoeff(int* row, int* col = 0) const; + bool all(void) const; + bool any(void) const; + template const PartialRedux verticalRedux(const BinaryOp& func) const; diff --git a/Eigen/src/Core/NumTraits.h b/Eigen/src/Core/NumTraits.h index cb601b160..5ce7e0fca 100644 --- a/Eigen/src/Core/NumTraits.h +++ b/Eigen/src/Core/NumTraits.h @@ -5,12 +5,12 @@ // // Eigen is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either +// License as published by the Free Software Foundation; either // version 3 of the License, or (at your option) any later version. // // Alternatively, you can redistribute it and/or // modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of +// published by the Free Software Foundation; either version 2 of // the License, or (at your option) any later version. // // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY @@ -18,7 +18,7 @@ // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the // GNU General Public License for more details. // -// You should have received a copy of the GNU Lesser General Public +// You should have received a copy of the GNU Lesser General Public // License and a copy of the GNU General Public License along with // Eigen. If not, see . @@ -125,4 +125,17 @@ template<> struct NumTraits }; }; +template<> struct NumTraits +{ + typedef bool Real; + typedef float FloatingPoint; + enum { + IsComplex = 0, + HasFloatingPoint = 0, + ReadCost = 1, + AddCost = 1, + MulCost = 1 + }; +}; + #endif // EIGEN_NUMTRAITS_H diff --git a/Eigen/src/Core/Redux.h b/Eigen/src/Core/Redux.h index 05d73afc2..32d1eda49 100644 --- a/Eigen/src/Core/Redux.h +++ b/Eigen/src/Core/Redux.h @@ -232,4 +232,102 @@ MatrixBase::maxCoeff() const return this->redux(Eigen::ei_scalar_max_op()); } + + +template +struct ei_all_unroller +{ + enum { + col = (UnrollCount-1) / Derived::RowsAtCompileTime, + row = (UnrollCount-1) % Derived::RowsAtCompileTime + }; + + static bool run(const Derived &mat) + { + return ei_all_unroller::run(mat) && mat.coeff(row, col); + } +}; + +template +struct ei_all_unroller +{ + static bool run(const Derived &mat) { return mat.coeff(0, 0); } +}; + +template +struct ei_all_unroller +{ + static bool run(const Derived &) { return false; } +}; + +template +struct ei_any_unroller +{ + enum { + col = (UnrollCount-1) / Derived::RowsAtCompileTime, + row = (UnrollCount-1) % Derived::RowsAtCompileTime + }; + + static bool run(const Derived &mat) + { + return ei_any_unroller::run(mat) || mat.coeff(row, col); + } +}; + +template +struct ei_any_unroller +{ + static bool run(const Derived &mat) { return mat.coeff(0, 0); } +}; + +template +struct ei_any_unroller +{ + static bool run(const Derived &) { return false; } +}; + +/** \returns true if all coefficients are true + * + * \sa MatrixBase::any() + */ +template +bool MatrixBase::all(void) const +{ + if(EIGEN_UNROLLED_LOOPS + && SizeAtCompileTime != Dynamic + && SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) + return ei_all_unroller0 && SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) ? + SizeAtCompileTime : Dynamic>::run(derived()); + else + { + for(int j = 0; j < cols(); j++) + for(int i = 0; i < rows(); i++) + if (!coeff(i, j)) return false; + return true; + } +} + +/** \returns true if at least one coefficient is true + * + * \sa MatrixBase::any() + */ +template +bool MatrixBase::any(void) const +{ + if(EIGEN_UNROLLED_LOOPS + && SizeAtCompileTime != Dynamic + && SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) + return ei_any_unroller0 && SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) ? + SizeAtCompileTime : Dynamic>::run(derived()); + else + { + for(int j = 0; j < cols(); j++) + for(int i = 0; i < rows(); i++) + if (coeff(i, j)) return true; + return false; + } +} + #endif // EIGEN_REDUX_H -- cgit v1.2.3