Implement evaluator for Replicate.

This commit is contained in:
Jitse Niesen 2011-04-12 22:54:31 +01:00
parent 12a30a982f
commit 11164830f5
3 changed files with 63 additions and 0 deletions

View File

@ -615,6 +615,56 @@ protected:
};
// -------------------- Replicate --------------------
template<typename XprType, int RowFactor, int ColFactor>
struct evaluator_impl<Replicate<XprType, RowFactor, ColFactor> >
{
typedef Replicate<XprType, RowFactor, ColFactor> ReplicateType;
evaluator_impl(const ReplicateType& replicate)
: m_argImpl(replicate.nestedExpression()),
m_rows(replicate.nestedExpression().rows()),
m_cols(replicate.nestedExpression().cols())
{ }
typedef typename ReplicateType::Index Index;
typedef typename ReplicateType::CoeffReturnType CoeffReturnType;
typedef typename ReplicateType::PacketReturnType PacketReturnType;
CoeffReturnType coeff(Index row, Index col) const
{
// try to avoid using modulo; this is a pure optimization strategy
const Index actual_row = internal::traits<XprType>::RowsAtCompileTime==1 ? 0
: RowFactor==1 ? row
: row % m_rows;
const Index actual_col = internal::traits<XprType>::ColsAtCompileTime==1 ? 0
: ColFactor==1 ? col
: col % m_cols;
return m_argImpl.coeff(actual_row, actual_col);
}
template<int LoadMode>
PacketReturnType packet(Index row, Index col) const
{
const Index actual_row = internal::traits<XprType>::RowsAtCompileTime==1 ? 0
: RowFactor==1 ? row
: row % m_rows;
const Index actual_col = internal::traits<XprType>::ColsAtCompileTime==1 ? 0
: ColFactor==1 ? col
: col % m_cols;
return m_argImpl.template packet<LoadMode>(actual_row, actual_col);
}
protected:
typename evaluator<XprType>::type m_argImpl;
Index m_rows;
Index m_cols;
};
} // namespace internal
#endif // EIGEN_COREEVALUATORS_H

View File

@ -122,6 +122,10 @@ template<typename MatrixType,int RowFactor,int ColFactor> class Replicate
return m_matrix.template packet<LoadMode>(actual_row, actual_col);
}
const typename internal::remove_all<typename MatrixType::Nested>::type& nestedExpression() const
{
return m_matrix;
}
protected:
const typename MatrixType::Nested m_matrix;

View File

@ -167,4 +167,13 @@ void test_evaluators()
// test Select
VERIFY_IS_APPROX_EVALUATOR(aX, (aXsrc > 0).select(aXsrc, -aXsrc));
// test Replicate
mXsrc = MatrixXf::Random(6, 6);
VectorXf vX = VectorXf::Random(6);
mX.resize(6, 6);
VERIFY_IS_APPROX_EVALUATOR(mX, mXsrc.colwise() + vX);
matXcd.resize(12, 12);
VERIFY_IS_APPROX_EVALUATOR(matXcd, matXcd_ref.replicate(2,2));
VERIFY_IS_APPROX_EVALUATOR(matXcd, (matXcd_ref.replicate<2,2>()));
}