Mercurial > hg > segmenter-vamp-plugin
annotate armadillo-3.900.4/examples/example_lsq.cpp @ 84:55a047986812 tip
Update library URI so as not to be document-local
author | Chris Cannam |
---|---|
date | Wed, 22 Apr 2020 14:21:57 +0100 |
parents | 1ec0e2823891 |
children |
rev | line source |
---|---|
Chris@49 | 1 // Tutorial for linear least square fitting |
Chris@49 | 2 // Author: Pierre Moulon |
Chris@49 | 3 // Date: 8 December 2009 |
Chris@49 | 4 // Objective: |
Chris@49 | 5 // Fit a 2D line with to a set of points |
Chris@49 | 6 // Specifically, find a and b in the model y = ax + b |
Chris@49 | 7 // |
Chris@49 | 8 // Direct application of the example in: |
Chris@49 | 9 // http://en.wikipedia.org/wiki/Linear_least_squares#Motivational_example |
Chris@49 | 10 |
Chris@49 | 11 |
Chris@49 | 12 #include <iostream> |
Chris@49 | 13 |
Chris@49 | 14 #include "armadillo" |
Chris@49 | 15 |
Chris@49 | 16 using namespace arma; |
Chris@49 | 17 using namespace std; |
Chris@49 | 18 |
Chris@49 | 19 |
Chris@49 | 20 |
Chris@49 | 21 int main(int argc, char** argv) |
Chris@49 | 22 { |
Chris@49 | 23 // points to which we will fit the line |
Chris@49 | 24 mat data = "1 6; 2 5; 3 7; 4 10"; |
Chris@49 | 25 |
Chris@49 | 26 cout << "Points used for the estimation:" << endl; |
Chris@49 | 27 cout << data << endl; |
Chris@49 | 28 |
Chris@49 | 29 // Build matrices to solve Ax = b problem: |
Chris@49 | 30 vec b(data.n_rows); |
Chris@49 | 31 mat C(data.n_rows, 2); |
Chris@49 | 32 |
Chris@49 | 33 for(u32 i=0; i<data.n_rows; ++i) |
Chris@49 | 34 { |
Chris@49 | 35 b(i) = data(i,1); |
Chris@49 | 36 |
Chris@49 | 37 C(i,0) = 1; |
Chris@49 | 38 C(i,1) = data(i,0); |
Chris@49 | 39 } |
Chris@49 | 40 |
Chris@49 | 41 cout << "b:" << endl; |
Chris@49 | 42 cout << b << endl; |
Chris@49 | 43 |
Chris@49 | 44 cout << "Constraint matrix:" << endl; |
Chris@49 | 45 cout << C << endl; |
Chris@49 | 46 |
Chris@49 | 47 // Compute least-squares solution: |
Chris@49 | 48 vec solution = solve(C,b); |
Chris@49 | 49 |
Chris@49 | 50 // solution should be "3.5; 1.4" |
Chris@49 | 51 cout << "solution:" << endl; |
Chris@49 | 52 cout << solution << endl; |
Chris@49 | 53 |
Chris@49 | 54 |
Chris@49 | 55 cout << "Reprojection error:" << endl; |
Chris@49 | 56 |
Chris@49 | 57 for(u32 i=0; i<data.n_rows; ++i) |
Chris@49 | 58 { |
Chris@49 | 59 cout << " residual: " << ( data(i,1) - (solution(0) + solution(1) * data(i,0)) ) << endl; |
Chris@49 | 60 } |
Chris@49 | 61 |
Chris@49 | 62 return 0; |
Chris@49 | 63 } |
Chris@49 | 64 |