32 #ifndef __VMML__LINEAR_LEAST_SQUARES__HPP__
33 #define __VMML__LINEAR_LEAST_SQUARES__HPP__
35 #include <vmmlib/vector3.h>
36 #include "matrix_mxn.hpp"
49 template<
size_t number_of_data_po
ints,
size_t number_of_unknowns,
typename float_t >
50 vmml::matrix_mxn< number_of_unknowns, 1, float_t >
51 solve( std::vector< Vector3< float_t > >& data_points, float_t tolerance = 1e-9 )
54 assert( data_points.size() >= number_of_data_points );
56 matrix_mxn< number_of_data_points, number_of_unknowns, float_t > X;
57 matrix_mxn< number_of_data_points, 1, float_t > y;
59 for(
size_t nb_index = 0; nb_index < number_of_data_points; ++nb_index )
61 float_t x = data_points[ nb_index ].x;
63 for(
size_t index = 0; index < number_of_unknowns; ++index )
65 X[ nb_index ][ index ] = x;
69 y[ nb_index ][ 0 ] = data_points[ nb_index ].y;
72 std::cout << X << std::endl;
74 vmml::matrix_mxn< number_of_unknowns, number_of_data_points > Xt;
77 std::cout << Xt << std::endl;
79 vmml::matrix_mxm< number_of_unknowns > XtX;
82 vmml::matrix_mxm< number_of_unknowns > XtX_inverse;
83 XtX.computeInverse( XtX_inverse, tolerance );
85 vmml::matrix_mxn< number_of_unknowns, number_of_data_points > XtX_inv_mul_Xt;
87 XtX_inv_mul_Xt.mul( XtX_inverse, Xt );
89 vmml::matrix_mxn< number_of_unknowns, 1 > beta_hat;
90 beta_hat.mul( XtX_inv_mul_Xt, y );
92 std::cout << beta_hat << std::endl;
99 template<
size_t number_of_data_po
ints,
size_t number_of_unknowns,
typename float_t >
100 vmml::matrix_mxn< number_of_unknowns, 1, float_t >
101 solve_using_qr_decomposition( std::vector< Vector3< float_t > >& data_points, float_t tolerance = 1e-9 )
104 assert( data_points.size() >= number_of_data_points );
106 matrix_mxn< number_of_data_points, number_of_unknowns, float_t > X;
107 matrix_mxn< number_of_data_points, 1, float_t > y;
124 vmml::matrix_mxn< number_of_unknowns, 1 > beta_hat;
129 template<
size_t number_of_data_po
ints,
size_t number_of_unknowns,
typename float_t >
130 vmml::matrix_mxn< number_of_unknowns, 1, float_t >
131 solve_using_svd( std::vector< Vector3< float_t > >& data_points, float_t tolerance = 1e-9 )