Basile Perrenoud
Basile Perrenoud

Reputation: 4112

Eigen initialize from Matrix

I am currently working with Eigen in c++ and there is a few things about initialization that I couldn't find answers for.

Is it possible to initialize a Dynamic matrix using another dynamic matrix of the same size:

MyClass::MyClass(Eigen::Matrix<T, Eigen::Dynamic, Eigen Dynamic> sourceMatrix)
{
    Eigen::Matrix<T, Eigen::Dynamic, Eigen Dynamic> destMatrix(sourceMatrix)
}

The above seems to compile but my project is currently filled with other compile errors so I can't test it and I would like to be sure what it will do before using it. Will this make destMatrix a deep copy of sourceMatrix ? Or a shallow copy?

Is it any different than doing:

MyClass::MyClass(Eigen::Matrix<T, Eigen::Dynamic, Eigen Dynamic> sourceMatrix)
{
    Eigen::Matrix<T, Eigen::Dynamic, Eigen Dynamic> destMatrix(sourceMatrix.data())
}

Upvotes: 0

Views: 819

Answers (1)

ggael
ggael

Reputation: 29205

It will do a deep copy, also sourceMatrix should probably be declared as a const reference. If you want a shallow copy, then you have to request for it explicitly using, e.g., a Ref object:

Ref<MatrixXd> dest(source);

See the documentation of Eigen::Ref for more details and examples.

Upvotes: 3

Related Questions