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