1

is possible to cast a dynamic matrix to a static one and if yes what woud be the best solution? Example:

Eigen::MatrixXd a = Eigen::Matrixxd::Zero(4,4);

to

Eigen::Matrix<double, a.rows(), a.cols()> b = a; //?

Cheers!

2
  • what do you get? does it compile? does it error in run-time? But I doubt it, the template parameters must be know at compile time, it doesn't seems likely to be the case here. Apr 12, 2021 at 15:52
  • I get an compile error due to the not static initialization of b. I can not define a matrix by a.rows() because it is not at compile time. I search a workaround.
    – huhu_there
    Apr 12, 2021 at 19:06

1 Answer 1

0

From documentation.

The three mandatory template parameters of Matrix are: Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>.

RowsAtCompileTime and ColsAtCompileTime are the number of rows and columns of the > matrix as known at compile time

When you use Eigen::MatrixXd,

The RowsAtCompileTime and ColsAtCompileTime template parameters can take the special value Dynamic which indicates that the size is unknown at compile time, so must be handled as a run-time variable

Given that size of a is unknown at compile time, you cannot get value of a.rows().

Given that you know size of a at compile time, you should use Eigen::Matrix and not Eigen::MatrixXd. However, if you don't know a.rows() or a.cols(), you have to declare b as Eigen::MatrixXd. You cannot use Eigen::Matrix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.