Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping row-major array to column-majored Eigen Matrix

Tags:

eigen

eigen3

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?

like image 745
lorniper Avatar asked Dec 06 '16 10:12

lorniper


People also ask

Is Eigen column major or row-major?

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.

What is array explain row-major and column major representations with example?

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.

What is the difference between row-major order and column-major order?

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.

Is Fortran row or column major?

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 ) .


1 Answers

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";
like image 145
Avi Ginsburg Avatar answered Sep 21 '22 20:09

Avi Ginsburg