用Eigen库解Ax=b线性方程,使用最小二乘法
1 #include <iostream>
2
3 #include <Eigen/Dense>
4
5 using namespace std;
6
7 using namespace Eigen;
8
9 int main()
10
11 {
12
13 //对方程Ax=b
14
15
16 MatrixXf A = MatrixXf::Random(3, 2);
17
18 std::cout << "Here is the matrix A:\n" << A << std::endl;
19
20 VectorXf b = VectorXf::Random(3);
21
22 std::cout << "Here is the right hand side b:\n" << b << std::endl;
23
24 cout << endl;
25
26 cout << endl;
27
28 cout << "**********jacobiSvd方法********************" << endl;
29
30 MatrixXf x_jacobiSvd, x_colPivHouseholderQr;
31
32 //jacobiSvd 方式:Slow (but fast for small matrices)
33
34 x_jacobiSvd = A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b);
35
36 std::cout << "The least-squares solution is:\n"
37
38 << A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b) << std::endl;
39
40
41 cout << endl;
42
43 cout << endl;
44
45 cout << "**********colPivHouseholderQr方法********************" << endl;
46
47 x_colPivHouseholderQr = A.colPivHouseholderQr().solve(b);
48
49 //colPivHouseholderQr方法:fast
50
51 std::cout << "The least-squares solution is:\n"
52
53 << x_colPivHouseholderQr << std::endl;
54
55
56
57
58 system("pause");
59
60 return 0;
61
62
63 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2021-07-06 C++ delete指针以后应赋值为NULL
2020-07-06 Qt Qchart 中清空绘图
2020-07-06 Qt QChartView 如何放入widget