Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Eigen matrix from an array with row-major order

Tags:

c++

matrix

eigen

I have an array of doubles, and I want to create a 4-by-4 matrix using the Eigen library. I also want to specify that the data is stored in row-major order. How can I do this?

I have tried the following, but it does not compile:

double data[16];
Eigen::Matrix4d M = Eigen::Map<Eigen::Matrix4d>(data, 4, 4, Eigen::RowMajor);
like image 631
Karnivaurus Avatar asked Feb 25 '15 15:02

Karnivaurus


People also ask

Is Eigen matrix 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 row-major order in array?

Row Major Order: Row major ordering assigns successive elements, moving across the rows and then down the next row, to successive memory locations. In simple language, the elements of an array are stored in a Row-Wise fashion.

What is the formula for row-major order?

By Row Major Order If array is declared by a[m][n] where m is the number of rows while n is the number of columns, then address of an element a[i][j] of the array stored in row major order is calculated as, Address(a[i][j]) = B. A. + (i * n + j) * size.


1 Answers

You need to pass a row-major matrix type to Map, e.g.:

Map<Matrix<double,4,4,RowMajor> > M(data);

then you can use M as an Eigen matrix, and the values of data will be modified, e.g.:

M = M.inverse();

If you want to copy the data to a true column-major Eigen matrix, then do:

Matrix4d M = Map<Matrix<double,4,4,RowMajor> >(data);

Of course, you can also copy to a row-major matrix by using the right type for M.

like image 126
ggael Avatar answered Sep 19 '22 10:09

ggael