Implement evaluator for Select.

This commit is contained in:
Jitse Niesen 2011-04-12 22:34:16 +01:00
parent 88b3116b99
commit 12a30a982f
3 changed files with 57 additions and 0 deletions

View File

@ -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

View File

@ -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;

View File

@ -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));
}