Search code examples
c++linear-algebraeigeneigen3

What do the different Initializations of Eigen::RowVectorXf do?


I am reading through some docs, and found two different initializations and usages of new with Eigen::RowVectorXf. I could not find what the difference was through the documentation, or even that this was a possibility, and was hoping for some clarifications.

Here is the first version:

std::vector<Eigen::RowVectorXf*> V;
std::vector<float> data = {0,1,2,3,4};


V.push_back(new Eigen::RowVectorXf(5));

for (int i = 0; i<5; i++)
     V[0]->coeffRef(1, i) = data[i];

Here is the second version:

std::vector<Eigen::RowVectorXf*> V;
std::vector<float> data = {0,1,2,3,4};


V.push_back(new Eigen::RowVectorXf(1, 5));

for (int i = 0; i<5; i++)
     V[0]->coeffRef(i) = data[i];

I could not find any reference to this in the documentation, and I don't believe that they should be different--is this just a deprecated access feature? If someone could give me an overview of what is meant to be happening in both cases mathematically, and let me know if both are just creating a 1D vector with 5 columns, and filling it, I would be really grateful. Thank you!


Solution

  • Yes, Eigen::RowVectorXf(5) and Eigen::RowVectorXf(1, 5) both declare a 1x5 row vector.

    Neither constructor is deprecated. To clarify, RowVectorXf is just a typedef for Matrix<float, 1, Dynamic>, that is, a float-valued matrix with 1 row. As listed in the Eigen::Matrix documentation, Matrix (and hence also RowVectorXf) has multiple constructors:

    • The Matrix(Index dim) constructor makes a row or column vector of size dim. It is a compile error to call this constructor for a Matrix type that is not known at compile time to have one row or one column.

    • The Matrix(Index rows, Index cols) constructor makes a matrix of size rows by cols, and is valid for any Matrix type, including RowVectorXf. As you'd expect, there is an assertion at run time that rows is 1 if calling this form of constructor for RowVectorXf.

    Yes, the Matrix(Index dim) constructor is more useful for RowVectorXf. But it can be handy in generic code that the two-arg constructor may always be used.