Re: [eigen] Passing result of block() as non-const reference-to-matrix?

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


Hi Sidney,

that is expected since a matrix block is not a matrix. It is a special
matrix expression which shares operations defined in MatrixBase and
DenseBase with matrices. So you need to declare your function as a
template function like this

template <typename Derived>
void incrementBottomRight(Eigen::MatrixBase<Derived> & m)
{
   m(m.rows() - 1, m.cols() - 1) += 1.0;
}

In case this is not an option you could probably do

void incrementBottomRight(Eigen::Block<MatrixXd> & m)
{
   m(m.rows() - 1, m.cols() - 1) += 1.0;
}

but then the function will be limited to blocks only.

Regards,
Hauke

On Fri, Jul 9, 2010 at 11:28 AM, Sidney Cadot <sidney.cadot@xxxxxxxxx> wrote:
> Hello,
>
> Today I found, to my surprise, that passing the result of a Matrix::block() method to a function that takes a reference to an Eigen::Matrix doesn't seem to work. The program (which doesn't compile) demonstrates the problem.
>
> I know that it /is/ possible to pass the result of a Matrix::block() const call to a const reference to an Eigen::Matrix. Is it intentional that the non-const version of this does not work?
>
> Any help would be greatly appreciated.
>
> Regards, Sidney
>
> /////////////////////// code fragment starts here
>
> #include <Eigen/Core>
>
> void incrementBottomRight(Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> & m)
> {
>    m(m.rows() - 1, m.cols() - 1) += 1.0;
> }
>
> int main()
> {
>    Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> testMatrix(3, 3);
>
>    // This works fine
>    incrementBottomRight(testMatrix);
>
>    // This gives a compiler error
>    incrementBottomRight(testMatrix.block(0, 0, 2, 2));
>
>    std::cout << testMatrix << std::endl;
>
>    return 0;
> }
>
>
>
>



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