参考官网:http://eigen.tuxfamily.org/dox/group__TutorialMatrixArithmetic.html
在百度文库里也有一些说明,但是要下载却要收费,真是 只是就是money啊。
百度文库:https://wenku.baidu.com/view/d1c5535de418964bcf84b9d528ea81c758f52e66.html
可参考说明:Eigen库使用指南
后续补充相应使用说明,下面是一段我的测试程序。
#include <Eigen/Dense>
#include <Eigen/Core>
double polyeval(Eigen::VectorXd coeffs, double x) {
double result = 0.0;
for (int i = 0; i < coeffs.size(); i++) { result += coeffs[i] * pow(x, i); }
return result;
}
Eigen::VectorXd polyfit(Eigen::VectorXd xvals, Eigen::VectorXd yvals, int order) {
assert(xvals.size() == yvals.size());
assert(order >= 1 && order <= xvals.size() - 1);
Eigen::MatrixXd A(xvals.size(), order + 1);
for (int i = 0; i < xvals.size(); i++) {
A(i, 0) = 1.0;
}
for (int j = 0; j < xvals.size(); j++) {
for (int i = 0; i < order; i++) {
A(j, i + 1) = A(j, i) * xvals(j);
}
}
auto Q = A.householderQr();
auto result = Q.solve(yvals);
return result;
}
#include <iostream>
#include <vector>
int main(int argc, char** argv)
{
Eigen::VectorXd xvals(6);
Eigen::VectorXd yvals(6);
/*xvals << 9.261977, -2.06803, -19.6663, -36.868, -51.6263, -66.3482;
yvals << 5.17, -2.25, -15.306, -29.46, -42.85, -57.6116;*/
Eigen::VectorXf xvalfs(6);
Eigen::VectorXf yvalfs(6);
xvalfs << 9.261977, -2.06803, -19.6663, -36.868, -51.6263, -66.3482;
yvalfs << 18.52, -4.136, -39.333, -73.736, -103.2526, -132.6962;
xvals = xvalfs.cast<double>();
yvals = yvalfs.cast<double>();
std::vector<float> a = { 1, 2, 3, 4 };
Eigen::VectorXf b = Eigen::Map<Eigen::VectorXf, Eigen::Unaligned>(a.data(), a.size());
std::cout << "b:\n"<< b << std::endl;
Eigen::VectorXd d = b.cast<double>();
std::cout << "d:\n" << d << std::endl;
std::vector<std::vector<float>> e(2, std::vector<float>(2));
for (int i = 0; i < a.size(); i++)
{
e[i / 2][i % 2] = a[i];
}
Eigen::MatrixXf f(2,2);
for (int k = 0; k < e.size(); k++)
{
f.row(k) = Eigen::VectorXf::Map(&e[k][0], e[k].size());
}
std::cout << "f:\n" << f << std::endl;
std::cout << "f.row(1):\n" << f.row(1) << std::endl;
std::cout << "f.col(1):\n" << f.col(1) << std::endl;
Eigen::VectorXf g;
g = f.col(1);
std::cout << "g:\n" << g << std::endl;
std::cout << "g.sum():\n" << g.sum() << std::endl;
auto coeffs = polyfit(xvals, yvals, 1);
std::cout << coeffs << std::endl;
std::cout << "Y(16)=" << polyeval(coeffs, 16) << std::endl;
return 0;
}