13 #include "msdevstudio/MSconfig.h"
28 #ifdef ITERATOR_MEMBER_DEFECT
37 using namespace hippodraw::Numeric;
39 using namespace hippodraw;
42 LMFitter (
const char * name )
44 m_chi_cutoff ( 0.000001 ),
45 m_start_lambda ( 0.001 ),
46 m_lambda_shrink_factor( 9.8 ),
47 m_lambda_expand_factor( 10.2 )
55 m_chi_cutoff ( fitter.m_chi_cutoff ),
56 m_start_lambda ( fitter.m_start_lambda ),
57 m_lambda_shrink_factor ( fitter.m_lambda_shrink_factor ),
58 m_lambda_expand_factor ( fitter.m_lambda_expand_factor )
75 unsigned int num_parms =
m_beta.size();
78 for ( ; j < num_parms; j++ ) {
79 for (
unsigned int k = 0; k < j; k++ ) {
85 for ( ; j < num_parms; j++ ) {
109 unsigned int num_parms =
m_beta.size ();
111 vector< int > ipiv ( num_parms, 0 );
113 vector< int > indxr ( num_parms, -1 );
114 vector< int > indxc ( num_parms, -1 );
116 unsigned int irow = UINT_MAX;
117 unsigned int icol = UINT_MAX;
119 for (
unsigned int i = 0;
i < num_parms;
i++ ) {
122 for (
unsigned int j = 0; j < num_parms; j++ ) {
123 if ( ipiv[j] != 1 ) {
125 for (
unsigned int k = 0; k < num_parms; k++ ) {
126 if ( ipiv[k] == 0 ) {
127 if ( abs (
m_alpha[j][k] ) >= big ) {
133 else if ( ipiv[k] > 1 ) {
140 if ( irow == UINT_MAX ) {
145 if ( irow != icol ) {
146 for (
unsigned int l = 0; l < num_parms; l++ ) {
153 if (
m_alpha[icol][icol] == 0.0 ) {
156 double pivinv = 1.0 /
m_alpha[icol][icol];
159 for (
unsigned int l = 0; l < num_parms; l++ ) {
164 for (
unsigned int ll = 0; ll < num_parms; ll++ ) {
166 double dum =
m_alpha[ll][icol];
169 for (
unsigned int l = 0; l < num_parms; l++ ) {
177 for (
int l = num_parms - 1; l >= 0; l-- ) {
178 if ( indxr[l] != indxc[l] ) {
180 for (
unsigned int k = 0; k < num_parms; k++ ) {
205 vector< double > old_parms;
209 assert ( old_parms.size() ==
m_beta.size() );
211 vector< double > new_parms ( old_parms );
212 vector< double >::iterator pit = new_parms.begin ( );
213 vector< double >::iterator dit =
m_beta.begin ( );
215 while ( pit != new_parms.end () ) {
218 m_fcn -> setFreeParameters ( new_parms );
222 if ( abs ( old_chisq - new_chisq ) <
m_chi_cutoff )
break;
224 if ( new_chisq < old_chisq ) {
229 m_fcn -> setFreeParameters ( old_parms );
232 if ( ! ok )
return ok;
244 fcn -> setNeedsDerivatives (
true );
void fillFreeParameters(std::vector< double > &) const
Fills the vector with the free parameters values.
Collection of linear algebra functions.
virtual bool calcStep()
Takes a step in the fitting and returns the change to the parameters of the function.
double m_start_lambda
The starting lambda factor.
virtual bool calcBestFit()
Calculates the best fit.
double m_lambda_expand_factor
The factor by which lambda is divided if it is too small.
virtual void calcAlpha()
Calculates the alpha matrix.
int m_max_iterations
The maximum number of iterations allowed in attempting the fit.
virtual int calcCovariance(std::vector< std::vector< double > > &cov)
Calculates the covariance matrix.
virtual void setFCN(StatedFCN *fcn)
Sets the objective function object.
The base class for fitters.
Fitter * clone() const
Makes a copy of the receiving object.
LMFitter(const LMFitter &)
The copy constructor.
int invertMatrix(const std::vector< std::vector< double > > &A, std::vector< std::vector< double > > &Ainv)
Inverts a SPD matrix a to get inverse Ainv using the cholesky factorization.
double m_chi_cutoff
The Chi Squared cut-off parameter.
std::vector< double > m_beta
The beta vector.
double m_lambda_shrink_factor
The factor by which lambda is divided if it is too large.
double m_lambda
The current lambda factor.
LMFitter class interface.
virtual double objectiveValue() const
Calculates the value of the objective function at the current set of parameters.
hippodraw::StatedFCN class interface
A derived class for FCNBase class.
StatedFCN * m_fcn
The objective function.
virtual bool solveSystem()
Solves the system.
virtual void setFCN(StatedFCN *fcn)
Sets the objective function.
std::vector< std::vector< double > > m_alpha
The alpha matrix.