diff options
author | 2011-04-12 22:34:16 +0100 | |
---|---|---|
committer | 2011-04-12 22:34:16 +0100 | |
commit | 12a30a982feab745d36d647ab88dfb0a51da2213 (patch) | |
tree | 5042422112605946ecd39dfbb0b5453f70d8a97d | |
parent | 88b3116b9932ac8fecb5f72bf590e2b44fc9d0ff (diff) |
Implement evaluator for Select.
-rw-r--r-- | Eigen/src/Core/CoreEvaluators.h | 39 | ||||
-rw-r--r-- | Eigen/src/Core/Select.h | 15 | ||||
-rw-r--r-- | test/evaluators.cpp | 3 |
3 files changed, 57 insertions, 0 deletions
diff --git a/Eigen/src/Core/CoreEvaluators.h b/Eigen/src/Core/CoreEvaluators.h index bd41bf405..756ebde1e 100644 --- a/Eigen/src/Core/CoreEvaluators.h +++ b/Eigen/src/Core/CoreEvaluators.h @@ -576,6 +576,45 @@ struct evaluator_impl<Block<XprType, BlockRows, BlockCols, InnerPanel, /* HasDir }; +// -------------------- Select -------------------- + +template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType> +struct evaluator_impl<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> > +{ + typedef Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> SelectType; + + evaluator_impl(const SelectType& select) + : m_conditionImpl(select.conditionMatrix()), + m_thenImpl(select.thenMatrix()), + m_elseImpl(select.elseMatrix()) + { } + + typedef typename SelectType::Index Index; + typedef typename SelectType::CoeffReturnType CoeffReturnType; + + CoeffReturnType coeff(Index row, Index col) const + { + if (m_conditionImpl.coeff(row, col)) + return m_thenImpl.coeff(row, col); + else + return m_elseImpl.coeff(row, col); + } + + CoeffReturnType coeff(Index index) const + { + if (m_conditionImpl.coeff(index)) + return m_thenImpl.coeff(index); + else + return m_elseImpl.coeff(index); + } + +protected: + typename evaluator<ConditionMatrixType>::type m_conditionImpl; + typename evaluator<ThenMatrixType>::type m_thenImpl; + typename evaluator<ElseMatrixType>::type m_elseImpl; +}; + + } // namespace internal #endif // EIGEN_COREEVALUATORS_H diff --git a/Eigen/src/Core/Select.h b/Eigen/src/Core/Select.h index d0cd66a26..87a071fc7 100644 --- a/Eigen/src/Core/Select.h +++ b/Eigen/src/Core/Select.h @@ -101,6 +101,21 @@ class Select : internal::no_assignment_operator, return m_else.coeff(i); } + const ConditionMatrixType& conditionMatrix() const + { + return m_condition; + } + + const ThenMatrixType& thenMatrix() const + { + return m_then; + } + + const ElseMatrixType& elseMatrix() const + { + return m_else; + } + protected: const typename ConditionMatrixType::Nested m_condition; const typename ThenMatrixType::Nested m_then; diff --git a/test/evaluators.cpp b/test/evaluators.cpp index bd4e89806..fc6fda557 100644 --- a/test/evaluators.cpp +++ b/test/evaluators.cpp @@ -164,4 +164,7 @@ void test_evaluators() matXcd_ref.real() = mat1; matXcd_ref.imag() = mat2; VERIFY_IS_APPROX(matXcd, matXcd_ref); + + // test Select + VERIFY_IS_APPROX_EVALUATOR(aX, (aXsrc > 0).select(aXsrc, -aXsrc)); } |