I want to map from a C-type array to a Column majored Eigen matrix.
The mapping itself is using the RowMajor type,
so I tried
std::vector<double> a(9);
double *p= a.data();
Eigen::MatrixXd M=Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::RowMajor>>(p)
I got what I expected(the order of M.data()), however, if the dimension(3) in the template is not known at compile time, this method doesn't work... any solution?
The default in Eigen is column-major. Naturally, most of the development and testing of the Eigen library is thus done with column-major matrices. This means that, even though we aim to support column-major and row-major storage orders transparently, the Eigen library may well work best with column-major matrices.
The elements of an array can be stored in column-major layout or row-major layout. For an array stored in column-major layout, the elements of the columns are contiguous in memory. In row-major layout, the elements of the rows are contiguous. Array layout is also called order, format, and representation.
The difference between the orders lies in which elements of an array are contiguous in memory. In row-major order, the consecutive elements of a row reside next to each other, whereas the same holds true for consecutive elements of a column in column-major order.
Array storage in Fortran is column-major. That is to say, when stored in memory as a linear array, the matrix will be arranged as ( a 11 , a 21 , a 12 , a 22 ) .
I assume that you wrote:
Eigen::MatrixXd M=Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(p);
This doesn't let the map know what the dimensions should be. You have to add that in the constructor:
std::vector<double> a{1,2,3,4,5,6,7,8,9};
double *p = a.data();
std::cout << Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::RowMajor>>(p) << "\n\n";
std::cout << Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(p, 3, 3) << "\n\n";
std::cout << Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::ColMajor>>(p) << "\n\n";
std::cout << Eigen::Map<Eigen::MatrixXd>(p, 3, 3) << "\n\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With