0

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!

ak1215
  • 11
  • 1
  • 1
    This might be helpful: [Map two-dimensional array to Eigen::Matrix](https://stackoverflow.com/questions/40510135/map-two-dimensional-array-to-eigenmatrix) – JeJo May 04 '19 at 10:02
  • Writes comment about std:map needing two template classes. Realizes it's talking about Eigen::Map. Deletes comment and slowly backs away... –  May 04 '19 at 20:32

1 Answers1

0

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 (i.e., 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 e.g. the following Q&A for how to easily map the logic of your 2D array into a 1D array:

dfrib
  • 70,367
  • 12
  • 127
  • 192