[EIGEN] How to get in and out data from Eigen matrix

Eigen is a C++ template library [WWW] for linear algebra. You can write expressions involving matrices and vectors as in Matlab [WWW]. No more need to create your own home-made code for matrix addition, multiplication, linear equation solving etc.

There are multiple libraries out on the web doing matrix operations and around. The well known are in OpenCV [WWW], Armadillo [WWW] and Eigen of our choice. A nice comparative study is given in [WWW], showing relative performances on different matrix sizes and operations (add, multiply, transpose, inverse and SVD). In this blog post we will stick with Eigen.

All is great once you have your data inside the Eigen data structures. You can multiply them, add, use in solvers, invert, work on rows, columns, blocks etc etc etc. In practice, two questions inevitably arises:

  1. I have an array of doubles. How to put it into the Eigen matrix?
  2. I have an Eigen matrix. How to get the values out into a plain array without loops?

The good news that you don’t need to do loopy code to get your data out from the Eigen objects. You can map the existing memory occupied by a double array to an Eigen object and vice versa! Great, eh? How it works? How to do this? Gimme some code!

Suppose you have an array with double values of size nRows x nCols.

double *X; // non-NULL pointer to some data

You can create an nRows x nCols size double matrix using the Map functionality like this:

MatrixXd eigenX = Map<MatrixXd>( X, nRows, nCols );

The Map operation maps the existing memory region into the Eigen’s data structures. A single line like this allows to avoid to write ugly code of matrix creation, for loop with copying each element in good order etc.

Now what if you have got an Eigen matrix with some result and you want to get out a plain double array? Guess what, you can use the Map once again!

MatrixXd resultEigen;   // Eigen matrix with some result (non NULL!)

double *resultC;                // NULL pointer

Map<MatrixXd>( resultC, resultEigen.rows(), resultEigen.cols() ) = resultEigen;

After the Map operation, the resultC pointer will now point to a beginning of a memory region with your computation result. Just make sure you pass by also the size information of the array. By default the layout is column-major, that is the elements are wrote out column by column from the Eigen matrix.

That’s all folks! Don’t forget to look and even print out the great tutorial [WWW]. It shows many typical use cases on small examples.

Matlab users: print out and check out the correspondence between Matlab code and Eigen code [HERE].

This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

2 Responses to [EIGEN] How to get in and out data from Eigen matrix

  1. Pedro says:

    How to pass resultC to matlab output ??

    plhs[0] = resultC; // ????????????

    error C2440:
    ‘=’: cannot convert from ‘double *’ to ‘mxArray *’

    Please complete your article.

  2. Pingback: Convert Eigen Matrix to C array - ExceptionsHub

Leave a Reply

Your email address will not be published. Required fields are marked *