Make operator=(EigenBase<>) uses the new assignment mechanism and introduce a generic EigenBase to EigenBase assignment kind based on the previous evalTo mechanism.

This commit is contained in:
Gael Guennebaud
2014-06-25 17:23:52 +02:00
parent 3b19b466a7
commit b868bfb84a
10 changed files with 111 additions and 15 deletions

View File

@@ -167,8 +167,10 @@ struct storage_kind_to_shape<Sparse> {
};
struct Sparse2Sparse {};
struct Sparse2Dense {};
template<> struct AssignmentKind<SparseShape,SparseShape> { typedef Sparse2Sparse Kind; };
template<> struct AssignmentKind<DenseShape,SparseShape> { typedef Sparse2Dense Kind; };
template<typename DstXprType, typename SrcXprType>
@@ -242,6 +244,24 @@ struct Assignment<DstXprType, SrcXprType, Functor, Sparse2Sparse, Scalar>
}
};
// Sparse to Dense assignment
template< typename DstXprType, typename SrcXprType, typename Functor, typename Scalar>
struct Assignment<DstXprType, SrcXprType, Functor, Sparse2Dense, Scalar>
{
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<typename DstXprType::Scalar> &/*func*/)
{
eigen_assert(dst.rows() == src.rows() && dst.cols() == src.cols());
typedef typename SrcXprType::Index Index;
dst.setZero();
typename internal::evaluator<SrcXprType>::type srcEval(src);
typename internal::evaluator<DstXprType>::type dstEval(dst);
for (Index j=0; j<src.outerSize(); ++j)
for (typename internal::evaluator<SrcXprType>::InnerIterator i(srcEval,j); i; ++i)
dstEval.coeffRef(i.row(),i.col()) = i.value();
}
};
} // end namespace internal
#endif // EIGEN_TEST_EVALUATORS

View File

@@ -67,7 +67,7 @@ public:
Index nonZeros() const
{
#ifndef EIGEN_TEST_EVALUATORS
typedef typename internal::evaluator<XprType>::type EvaluatorType;
Index nnz = 0;
Index end = m_outerStart + m_outerSize.value();
for(int j=m_outerStart; j<end; ++j)
for(typename XprType::InnerIterator it(m_matrix, j); it; ++it)
@@ -641,7 +641,7 @@ public:
template<typename ArgType, int BlockRows, int BlockCols, int InnerPanel>
class unary_evaluator<Block<ArgType,BlockRows,BlockCols,InnerPanel>, IteratorBased>::OuterVectorInnerIterator
{
const XprType& m_block;
const unary_evaluator& m_eval;
Index m_outerPos;
Index m_innerIndex;
Scalar m_value;
@@ -649,10 +649,10 @@ class unary_evaluator<Block<ArgType,BlockRows,BlockCols,InnerPanel>, IteratorBas
public:
EIGEN_STRONG_INLINE OuterVectorInnerIterator(const unary_evaluator& aEval, Index outer)
: m_block(aEval.m_block),
m_outerPos( (IsRowMajor ? aEval.startCol() : aEval.startRow()) - 1), // -1 so that operator++ finds the first non-zero entry
m_innerIndex(IsRowMajor ? aEval.startRow() : aEval.startCol()),
m_end(IsRowMajor ? aEval.startCol()+aEval.blockCols() : aEval.startRow()+aEval.blockRows())
: m_eval(aEval),
m_outerPos( (IsRowMajor ? aEval.m_block.startCol() : aEval.m_block.startRow()) - 1), // -1 so that operator++ finds the first non-zero entry
m_innerIndex(IsRowMajor ? aEval.m_block.startRow() : aEval.m_block.startCol()),
m_end(IsRowMajor ? aEval.m_block.startCol()+aEval.m_block.blockCols() : aEval.m_block.startRow()+aEval.m_block.blockRows())
{
EIGEN_UNUSED_VARIABLE(outer);
eigen_assert(outer==0);
@@ -660,7 +660,7 @@ public:
++(*this);
}
inline Index index() const { return m_outerPos - (IsRowMajor ? m_block.startCol() : m_block.startRow()); }
inline Index index() const { return m_outerPos - (IsRowMajor ? m_eval.m_block.startCol() : m_eval.m_block.startRow()); }
inline Index outer() const { return 0; }
inline Index row() const { return IsRowMajor ? 0 : index(); }
inline Index col() const { return IsRowMajor ? index() : 0; }
@@ -673,7 +673,7 @@ public:
while(m_outerPos<m_end)
{
m_outerPos++;
typename XprType::InnerIterator it(m_block.m_matrix, m_outerPos);
EvalIterator it(m_eval.m_argImpl, m_outerPos);
// search for the key m_innerIndex in the current outer-vector
while(it && it.index() < m_innerIndex) ++it;
if(it && it.index()==m_innerIndex)

View File

@@ -337,6 +337,7 @@ template<typename Derived> class SparseMatrixBase : public EigenBase<Derived>
Block<Derived,Dynamic,Dynamic,true> innerVectors(Index outerStart, Index outerSize);
const Block<const Derived,Dynamic,Dynamic,true> innerVectors(Index outerStart, Index outerSize) const;
#ifndef EIGEN_TEST_EVALUATORS
/** \internal use operator= */
template<typename DenseDerived>
void evalTo(MatrixBase<DenseDerived>& dst) const
@@ -346,6 +347,7 @@ template<typename Derived> class SparseMatrixBase : public EigenBase<Derived>
for (typename Derived::InnerIterator i(derived(),j); i; ++i)
dst.coeffRef(i.row(),i.col()) = i.value();
}
#endif
Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> toDense() const
{