This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Confused with lvalue of block operation assigned to a vector

Tags: None
(comma "," separated)
ngallegoortiz
Registered Member
Posts
1
Karma
0
Dear Eigen users,

I have been using Eigen 3 and I arrived to this question, I may be missing something on the assignment so I would appreciate your hints. Here is a piece of sample code that reproduces the problem.
//--------
Code: Select all
Eigen::MatrixXd m(2,3);
    m << 1, 2, 3, 4, 5, 6;
    std::cerr << "this is the matrix\n" << m << std::endl;
    Eigen::Vector3d v = m.block(1,0,1,3);
    std::cerr << "m.block(1,0,1,3)\n" << m.block(1,0,1,3) << std::endl;
    std::cerr << "this is the vector Eigen::Vector3d v = m.block(1,0,1,3)\n" << v << std::endl;
    Eigen::Vector3d r = m.row(1);
    std::cerr << "this is the vector r = m.row(1)\n" << r << std::endl;

//---------
here the output

this is the matrix
1 2 3
4 5 6
m.block(1,0,1,3)
4 5 6
this is the vector Eigen::Vector3d v = m.block(1,0,1,3)
4
2
5
this is the vector r = m.row(1)
4
5
6

Why are block and row commands here stored differently on Vector3d, I was expecting them to be equivalent.

Thanks,

Nicolas
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Vector3d is a column vector, use RowVector3d for a row vector.

m.block(1,0,1,3) or m.row(1) are row vectors, but Eigen allows implicit transposition when copying a row vector to a column one (and conversely).

So basically, when you do:

r = m.row(1)

Eigen actually performs:

r = m.row(1).transpose()
zhanxw
Registered Member
Posts
17
Karma
0
I tried this example (Eigen 3.3.0), and this program crashed (see below). So you won't see this confusing outputs.

eigen/Eigen/src/Core/PlainObjectBase.h:279: void Eigen::PlainObjectBase<Derived>::resize(Eigen::Index, Eigen::Index) [with Derived = Eigen::Matrix<double, 3, 1>; Eigen::Index = long int]: Assertion `(!(RowsAtCompileTime!=Dynamic) || (rows==RowsAtCompileTime)) && (!(ColsAtCompileTime!=Dynamic) || (cols==ColsAtCompileTime)) && (!(RowsAtCompileTime==Dynamic && MaxRowsAtCompileTime!=Dynamic) || (rows<=MaxRowsAtCompileTime)) && (!(ColsAtCompileTime==Dynamic && MaxColsAtCompileTime!=Dynamic) || (cols<=MaxColsAtCompileTime)) && rows>=0 && cols>=0 && "Invalid sizes when resizing a matrix or array."' failed.


Bookmarks



Who is online

Registered users: abc72656, Bing [Bot], daret, Google [Bot], Sogou [Bot], Yahoo [Bot]