-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
55 lines (44 loc) · 1.18 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* 4. read input matrices from file
*
*
*
*/
#include <iostream>
#include <Eigen/Dense>
#include "src/utils.h"
int main(int argc, char *argv[]){
// generating data
Eigen::MatrixXd A(3,4);
Eigen::VectorXd b(3);
Eigen::VectorXd x(3);
A << 1, 3, -2, 4,
2, 6, -4, 4,
2, 4, 3, 4;
b << 5, 7, 8;
if(!if_square(A) || A.rows() < 2){
std::cerr << "Cannot process rectangular or single element matrix" << std::endl;
return -1;
}
Eigen::FullPivLU<Eigen::MatrixXd> lu_decomp(A);
if(lu_decomp.rank() != A.rows()){
std::cerr << "Cannot process not a full rank matrix" << std::endl;
return -1;
}
// solving
if(lu_decomp.isInvertible()){
x = lu_decomp.inverse() * b;
} else {
x = A.completeOrthogonalDecomposition().pseudoInverse() * b; // todo: that should be used when matrix
}
// check solution
Eigen::VectorXd x_hat = A*x;
bool result = x_hat.isApprox(b);
// printing out results
if(result){
std::cout << "Solution is CORRECT!" << std::endl;
} else {
std::cout << "Solution is INCORRECT!" << std::endl;
}
return 0;
}