Re: [eigen] How does toDenseMatrix() work?

[ Thread Index | Date Index | More Archives ]

2010/7/23 Manoj Rajagopalan <rmanoj@xxxxxxxxx>:
> Hi eigen developers,
>  In some specialized classes like DiagonalMatrix,

It should be in _all_ special matrix classes. If we still have special
matrix classes without toDenseMatrix(), or with the old name
toDense(), we should fix that.

> I see the following member:
> DenseMatrixType toDenseMatrix() const { return derived(); }
>  where DenseMatrixType is a typedef for the full MxN container whereas
> derived() returns the "sparse" derived instance. Somewhere, the Eigen3
> machinery is ensuring an element-by-element "translation" from the derived
> matrix expression to the full MxN matrix. What is this mechanism?
>   Is this happening because the lvalue's EigenBase<> copy CTOR and copy
> assignment operator call the rvalue's evalTo() method? So if I write a custom
> matrix class for compact representation, I only need to inherit EigenBase<>
> and override the evalTo() method for this translation to work?

Yes, this is happening in DenseBase::operator=(EigenBase) which is
defined in Eigen/src/Core/EigenBase.h:

template<typename Derived>
template<typename OtherDerived>
Derived& DenseBase<Derived>::operator=(const EigenBase<OtherDerived> &other)
  return derived();

So yes, you got it right, see e.g. in Eigen/src/Core/DiagonalMatrix.h
for an example of a special matrix class implementation. Indeed evalTo
is implemented there as:

template<typename Derived>
template<typename DenseDerived>
void DiagonalBase<Derived>::evalTo(MatrixBase<DenseDerived> &other) const
  other.diagonal() = diagonal();

By the way... we have a discrepancy there, evalTo should be taking a
DenseBase not a MatrixBase, otherwise code trying to do that on an
Array will compile but will enter an infinite recursive call of
DenseBase::evalTo at runtime, let me fix this now :-)


> thanks,
> Manoj

Mail converted by MHonArc 2.6.19+