https://eigen.tuxfamily.org/index.php?title=Main_Page
下载Eigen库
包含目录操作
下载完解压,打开到如图所示的路径,复制它,丢编译器里
首先,列出要解的方程,一定要写成这种格式的:
2x - y =1
-x + 3y = 3
等式左边的常数装进Matrix2f A
等式右边的常数装进Vector2f b
结果是Vector2f x
#include <iostream>
#include<Eigen>
using namespace std;
using namespace Eigen;
int main()
{
Matrix2f A;
Vector2f b;
A << 2, -1, -1, 3;
b << 1, 3;
cout <<"Here is the matrix A:\n" << A << endl;
cout << "Here is the right hand side b:\n" << b << endl;
Vector2f x = A.colPivHouseholderQr().solve(b);
// Vector2f x =A.ldlt().solve(b);也可以
cout << "The solution is:\n" << x << endl;
return 0;
}
解三元一次方程的接口是一样的,但是要把容器类型改一下,Matrix2f 改 Matrix3f,Vector2f 改 Vector3f