简体   繁体   中英

How to convert static 2d array into Eigen

Excuse me, I am a beginner in C++. So I do not use pointers. I need to convert a static 2d array of C++ into Eigen library format. I need to compute eigenvalues and eigenvectors of a large matrix since it is my applied problem.

My code is something like

double matr1[100][100];

MatrixXd copy_matr1;

for (int i = 0; i < 100; i++)
   for (int j = 0; j < 100; j++)
      matr1[i][j] = i + j;

copy_matr1 = Map<MatrixXd>(matr1);

or (with replacement of the last line with the next one)

copy_matr1 = Map<MatrixXd>(matr1, 100, 100);

But the last line is wrong. What is the correct notation?

But the code below (that converts a static 1d array of C++ into Eigen library format) is correct. I cannot understand where is a mistake in the previous snippet.

double arr1[100];

MatrixXd copy_arr1;

for (int i = 0; i < 100; i++)
   arr1[i] = i + 10;

copy_arr1 = Map<MatrixXd>(arr1);

or (with replacement of the last line with the next one)

copy_arr1 = Map<MatrixXd>(arr1, 100);

Thank you very much in advance!

The semantics of the constructor overloads of Eigen::Map<> allows only for mapping a "raw" array to its Eigen equivalent, but does not allow for mapping a raw array of raw arrays (ie, a 2D raw array).

To convert your 2D array into your Eigen equivalent of choice, you need to represent the former as a 1D raw array,

double matr1[100 * 100];

See eg the following Q&A for how to easily map the logic of your 2D array into a 1D array:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM