blob: cb9c59b607a265216da2fc0ff54c040686a697cf [file] [log] [blame]
Austin Schuh189376f2018-12-20 22:11:15 +11001#include <iostream>
2struct init {
3 init() { std::cout << "[" << "init" << "]" << std::endl; }
4};
5init init_obj;
6// [init]
7#include <iostream>
8#include <Eigen/Dense>
9
10using namespace std;
11using namespace Eigen;
12
13int main()
14{
15 MatrixXd A(2,2);
16 A << 2, -1, 1, 3;
17 cout << "Here is the input matrix A before decomposition:\n" << A << endl;
18cout << "[init]" << endl;
19
20cout << "[declaration]" << endl;
21 PartialPivLU<Ref<MatrixXd> > lu(A);
22 cout << "Here is the input matrix A after decomposition:\n" << A << endl;
23cout << "[declaration]" << endl;
24
25cout << "[matrixLU]" << endl;
26 cout << "Here is the matrix storing the L and U factors:\n" << lu.matrixLU() << endl;
27cout << "[matrixLU]" << endl;
28
29cout << "[solve]" << endl;
30 MatrixXd A0(2,2); A0 << 2, -1, 1, 3;
31 VectorXd b(2); b << 1, 2;
32 VectorXd x = lu.solve(b);
33 cout << "Residual: " << (A0 * x - b).norm() << endl;
34cout << "[solve]" << endl;
35
36cout << "[modifyA]" << endl;
37 A << 3, 4, -2, 1;
38 x = lu.solve(b);
39 cout << "Residual: " << (A0 * x - b).norm() << endl;
40cout << "[modifyA]" << endl;
41
42cout << "[recompute]" << endl;
43 A0 = A; // save A
44 lu.compute(A);
45 x = lu.solve(b);
46 cout << "Residual: " << (A0 * x - b).norm() << endl;
47cout << "[recompute]" << endl;
48
49cout << "[recompute_bis0]" << endl;
50 MatrixXd A1(2,2);
51 A1 << 5,-2,3,4;
52 lu.compute(A1);
53 cout << "Here is the input matrix A1 after decomposition:\n" << A1 << endl;
54cout << "[recompute_bis0]" << endl;
55
56cout << "[recompute_bis1]" << endl;
57 x = lu.solve(b);
58 cout << "Residual: " << (A1 * x - b).norm() << endl;
59cout << "[recompute_bis1]" << endl;
60
61}