forked from harusametime/LSQRwithEigen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
52 lines (37 loc) · 1.18 KB
/
Copy pathexample.cpp
File metadata and controls
52 lines (37 loc) · 1.18 KB
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
#include "stdafx.h"
#include "./Eigen/Eigen/Dense"
#include "./Eigen/Eigen/Sparse"
#include <iostream>
#include "LSQR.h"
#define _CRT_SECURE_NO_WARNINGS
int _tmain(int argc, _TCHAR* argv[])
{
//Matrix size m x n
int m = 200;
int n = 100;
// Determine A randomly
Eigen::MatrixXd A = Eigen::MatrixXd::Random(m, n);
Eigen::SparseMatrix<double> Asp = A.sparseView();
// Determine true minimizer x randomly
Eigen::VectorXd x_t = Eigen::VectorXd::Random(n);
// Determine b (= A x_t)
Eigen::VectorXd b = Asp * x_t;
// Minimizer x
Eigen::VectorXd x(n);
x.setZero();
//Run LSQR
LSQR lsqr = LSQR(Asp, b, x, 1.0e-8);
x = lsqr.SolutionX();
std::cout << "--------------------------------------------" << std::endl;
std::cout << "Solution obtained by LSQR" << std::endl;
std::cout << "--------------------------------------------" << std::endl;
for (int i = 0; i < x.rows(); i++){
std::cout << x(i) << std::endl;
}
std::cout << "--------------------------------------------" << std::endl;
std::cout << "Ground Truth" << std::endl;
std::cout <<"--------------------------------------------" << std::endl;
for (int i = 0; i < x.rows(); i++){
std::cout << x_t(i) << std::endl;
}
}