2

The following two posts are very helpful but not quite what I need:

However, I need to create a row-major C array (see 2) from an Eigen::MatrixXd, and I need to be able to specify the dimensions at run-time. Meaning, I'm making an interface that lets me pass an Eigen::MatrixXd to be converted along with it's dimensions and preallocated target C array.

This works but is column-major,

void eigen_matrixXd_to_double_array(const Eigen::MatrixXd& evector, double* destination, uint32_t nrow, uint32_t ncol)
{
    Eigen::Map<Eigen::MatrixXd>(destination,nrow,ncol) = evector;
}

I want something like:

    void eigen_matrixXd_to_double_array(const Eigen::MatrixXd& evector, double* destination, uint32_t nrow, uint32_t ncol)
{
    Eigen::Map<Eigen::Matrix<double, nrow, ncol, Eigen::RowMajor>>(destination,nrow,ncol) = evector;
}

or,

    void eigen_matrixXd_to_double_array(const Eigen::MatrixXd& evector, double* destination, uint32_t nrow, uint32_t ncol)
{
    Eigen::Map<Eigen::MatrixXd<Eigen::RowMajor>>(destination,nrow,ncol) = evector;
}

but neither of these work...

Any suggestions? Is it possible for me to make a row-major mapping with programmatic dimensions (realizing that the user of the C interface must get the dims correct)?

Thanks in advance...

  • @ggael, that worked! Thanks for the help. For the other noobs out there, here is a slight tweak that may be helpful for copy/paste, **typedef Eigen::Matrix RowMajMat;** – Southern.Cross Feb 06 '18 at 22:37

1 Answers1

3

You were very close:

typedef Matrix<double,Dynamic,Dynamic,RowMajor> RowMajMat;
RowMajMat::Map(destination, evector.rows(), evector.cols()) = evector;

Here, RowMajMat::Map is a static method returning a Map<RowMajMat>, so you can also write Map<RowMajMat>(destination, evector.rows(), evector.cols()).

ggael
  • 28,425
  • 2
  • 65
  • 71