-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharma2.cpp
71 lines (49 loc) · 1.36 KB
/
arma2.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#if defined(HAVE_ARMA)
// Tutorial for linear least square fitting
// Author: Pierre Moulon
// Date: 8 December 2009
// Objective:
// Fit a 2D line with to a set of points
// Specifically, find a and b in the model y = ax + b
//
// Direct application of the example in:
// http://en.wikipedia.org/wiki/Linear_least_squares#Motivational_example
#include <armadillo>
using namespace arma;
using namespace std;
int main(int argc, char** argv)
{
// points to which we will fit the line
mat data = "1 6; 2 5; 3 7; 4 10";
cout << "Points used for the estimation:" << endl;
cout << data << endl;
// Build matrices to solve Ax = b problem:
vec b(data.n_rows);
mat C(data.n_rows, 2);
for(u32 i=0; i<data.n_rows; ++i) {
b(i) = data(i,1);
C(i,0) = 1;
C(i,1) = data(i,0);
}
cout << "b:" << endl;
cout << b << endl;
cout << "Constraint matrix:" << endl;
cout << C << endl;
// Compute least-squares solution:
vec solution = solve(C,b);
// solution should be "3.5; 1.4"
cout << "solution:" << endl;
cout << solution << endl;
cout << "Reprojection error:" << endl;
for(u32 i=0; i<data.n_rows; ++i) {
cout << " residual: " << ( data(i,1) - (solution(0) + solution(1) * data(i,0)) ) << endl;
}
return 0;
}
#else
int main()
{
std::cout << "Armadillo not available." << std::endl << std::endl;
}
#endif