【发布时间】:2017-07-26 13:36:05
【问题描述】:
我正在尝试使用 Eigen 对 A 作为二维平方向量的能力来求解线性方程 Ax=b。我将 A 和 b 分别作为基于 C++ 的 2D 向量和 1D 向量。但是,我找不到将它们的值传递给特征格式矩阵和向量的方法。你能告诉我如何以 Eigen 格式复制变量吗? 此外,一开始应该包括什么才能使用 Map 类作为可能的溶剂?! 代码如下:
#include <iostream>
#include <vector>
#include "Eigen/Dense"
using namespace std;
using namespace Eigen;
int main()
{
// Let's consider that the A and b are following CPP based vectors:
vector<vector<double>> mainA= { { 10.,11.,12. },{ 13.,14.,15. },{ 16.,17.,18. } };
vector<double> mainB = { 2.,5.,8. };
// ??? Here I need to do something to pass the values to the following Eigen
//format matrix and vector
MatrixXf A;
VectorXf b;
cout << "Here is the matrix A:\n" << A << endl;
cout << "Here is the vector b:\n" << b << endl;
Vector3f x = A.colPivHouseholderQr().solve(b);
cout << "The solution is:\n" << x << endl;
}
【问题讨论】:
-
你为什么不only使用Eigen类型?为什么需要 C++ 向量?
-
@Some程序员老兄原因是我目前有现有的向量,我需要将这些值用于我的操作!
-
我在这里找到了一些东西:eigen.tuxfamily.org/dox/group__TutorialMapClass.html> 但不知道如何正确使用它以及我应该在开头包含什么!
-
@Avi Ginsburg 谢谢你的回答,我想知道主向量是否应该是“std::vector<:vector>>>”?!因为我尝试了 "std::vector<:vector>>" 并在 Map 中出现错误!
-
如果您想将
doubleEigen-vector 类型分配给floatEigen 矩阵/向量,您还需要将其转换为:Eigen::VectorXd::Map(mainA[i].data(), 3).cast<float>()