简体   繁体   English

如何将静态2d数组转换为Eigen

[英]How to convert static 2d array into Eigen

Excuse me, I am a beginner in C++. 对不起,我是C ++的初学者。 So I do not use pointers. 所以我不使用指针。 I need to convert a static 2d array of C++ into Eigen library format. 我需要将C ++的静态二维数组转换为特征库格式。 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. 但下面的代码(将C ++的静态1d数组转换为Eigen库格式)是正确的。 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). Eigen::Map<>的构造函数重载的语义仅允许将“原始”数组映射到其Eigen等价物,但不允许映射原始数组的原始数组(即2D原始数组)。

To convert your 2D array into your Eigen equivalent of choice, you need to represent the former as a 1D raw array, 要将2D数组转换为您选择的Eigen等价物,您需要将前者表示为1D原始数组,

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: 有关如何轻松将2D阵列的逻辑映射到1D阵列,请参阅以下问答:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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