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

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


2010/7/23 Benoit Jacob <jacob.benoit.1@xxxxxxxxx>:
> 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)
> {
>  other.derived().evalTo(derived());
>  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.setZero();
>  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 :-)

Ah no, here we are actually safe because the template evalTo()
declaration in the derived class overrides the evalTo() from
DenseBase. ok. So no crash.

That said, since we normally allow assigning a matrix to an array, I
think we should allow it too for special matrices. So I'll still fix
it.

Benoit

>
> Benoit
>
>>
>> thanks,
>> Manoj
>>
>>
>>
>



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/