Man*_*ete 6 c c++ eigenvalue eigenvector eigen
我想将double数组映射到现有的MatrixXd结构.到目前为止,我已经设法将Eigen矩阵映射到一个简单的数组,但我找不到回来的方法.
void foo(MatrixXd matrix, int n){
double arrayd = new double[n*n];
// map the input matrix to an array
Map<MatrixXd>(arrayd, n, n) = matrix;
//do something with the array
.......
// map array back to the existing matrix
}
Run Code Online (Sandbox Code Playgroud)
Jit*_*sen 13
我不确定你想要什么,但我会试着解释一下.
你在代码中混合了double和float(MatrixXf是一个矩阵,每个条目都是一个浮点数).我现在假设这是无意的,你想在任何地方使用双倍; 如果这真的是你的意图,请参见下文.
该指令Map<MatrixXd>(arrayd, n, n) = matrix将条目复制matrix到arrayd.它相当于循环
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
arrayd[i + j*n] = matrix(i, j);
Run Code Online (Sandbox Code Playgroud)
要复制arraydinto 的条目matrix,您将使用反向赋值:matrix = Map<MatrixXd>(arrayd, n, n).
但是,通常以下技术更有用:
void foo(MatrixXd matrix, int n) {
double* arrayd = matrix.data();
// do something with the array
}
Run Code Online (Sandbox Code Playgroud)
现在,arrayd指向矩阵中的条目,您可以将其作为任何C++数组进行处理.数据在matrix和之间共享arrayd,因此您不必在最后复制任何内容.顺便说一下,你不需要传递n给函数foo(),因为它存储在矩阵中; 使用matrix.rows()和matrix.cols()来查询其值.
如果您确实要将MatrixXf复制到双精度数组,则需要显式包含强制转换.Eigen中的语法是:Map<MatrixXd>(arrayd, n, n) = matrix.cast<double>().
您不需要执行任何反向操作.
使用Eigen :: Map时,您将原始数组映射到Eigen类.这意味着您现在可以使用Eighen函数读取或写入它.
如果您修改映射的数组,则更改已经存在.您只需访问原始数组即可.
float buffer[16]; //a raw array of float
//let's map the array using an Eigen matrix
Eigen::Map<Eigen::Matrix4f> eigenMatrix(buffer);
//do something on the matrix
eigenMatrix = Eigen::Matrix4f::Identity();
//now buffer will contain the following values
//buffer = [1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13319 次 |
| 最近记录: |