Re: [eigen] Is it really impossible to modify a column of a matrix with in function?

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


On 30.08.2011 11:58, Helmut Jarausch wrote:
void set_Col(VectorXi&  V) {
   V[0]=1;
}

int main() {
   MatrixXi A(2,3);
   A<<  1,2,3,
        4,5,6;
   set_Col(A.col(1));
   std::cout<<  "A=\n"<<  A<<  std::endl;

}


It looks as if a column cannot be passed by a non-const reference.
Why doesn't  the col-method return a non-const reference for a non-
const object (the col-method could be overloaded with/without the
const attribute)?

That's because A.col(1) does not return a VectorXi but a view on the corresponding column. Unfortunately passing this to another function is a bit hackish. To stay generic you need to pass a MatrixBase<Derived>& and because A.col() returns a temporary it needs to be a const-ref, which you need to const-cast within your method.
Look here for details:
http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html#TopicPlainFunctionsFailing

Christoph


--
----------------------------------------------
Dipl.-Inf. Christoph Hertzberg
Cartesium 0.051
Universität Bremen
Enrique-Schmidt-Straße 5
28359 Bremen

Tel: (+49) 421-218-64252
----------------------------------------------



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