72 static_cast<vnl_levenberg_marquardt*>(userdata);
81 std::cerr <<
"lmdif: iter " <<
self->num_iterations_ <<
" err [" 82 << x[0] <<
", " << x[1] <<
", " << x[2] <<
", " << x[3] <<
", " 83 << x[4] <<
", ... ] = " << ref_fx.
magnitude() <<
'\n';
85 f->
trace(self->num_iterations_, ref_x, ref_fx);
86 ++(
self->num_iterations_);
91 if (self->start_error_ == 0)
92 self->start_error_ = ref_fx.
rms();
122 std::cerr << __FILE__
" : WARNING. calling minimize_without_gradient(), but f_ has gradient.\n";
130 std::cerr <<
"vnl_levenberg_marquardt: Number of unknowns("<<n<<
") greater than number of data ("<<
m<<
")\n";
135 if (
int(x.
size()) != n) {
136 std::cerr <<
"vnl_levenberg_marquardt: Input vector length ("<<x.
size()<<
") not equal to num unknowns ("<<n<<
")\n";
143 long user_provided_scale_factors = 1;
167 &user_provided_scale_factors, &factor, &nprint,
171 &wa1[0], &wa2[0], &wa3[0], &wa4[0],
207 static_cast<vnl_levenberg_marquardt*>(userdata);
217 std::cerr <<
"lmder: iter " <<
self->num_iterations_ <<
" err [" 218 << x[0] <<
", " << x[1] <<
", " << x[2] <<
", " << x[3] <<
", " 219 << x[4] <<
", ... ] = " << ref_fx.
magnitude() <<
'\n';
220 f->
trace(self->num_iterations_, ref_x, ref_fx);
222 else if (*iflag == 1) {
224 if (self->start_error_ == 0)
225 self->start_error_ = ref_fx.
rms();
226 ++(
self->num_iterations_);
228 else if (*iflag == 2) {
229 f->
gradf(ref_x, ref_fJ);
233 if ( self->check_derivatives_ > 0 )
235 self->check_derivatives_--;
243 f->
f( ref_x, feval );
254 for (
unsigned i=0; i<ref_fJ.
cols(); ++i )
255 for (
unsigned j=0; j<ref_fJ.
rows(); ++j ) {
256 diff = ref_fJ(j,i) - finite_jac(j,i);
258 if ( diff > self->epsfcn ) {
259 std::cout <<
"Jac(" << i <<
", " << j <<
") diff: " << ref_fJ(j,i)
260 <<
" " << finite_jac(j,i)
261 <<
" " << ref_fJ(j,i)-finite_jac(j,i) <<
'\n';
279 std::cerr << __FILE__
": called method minimize_using_gradient(), but f_ has no gradient.\n";
287 std::cerr << __FILE__
": Number of unknowns("<<n<<
") greater than number of data ("<<
m<<
")\n";
302 long mode=1, nfev, njev;
367 #define whoami "vnl_levenberg_marquardt" 374 s << (
whoami ": OIOIOI -- failure in leastsquares function\n");
377 s << (
whoami ": OIOIOI -- lmdif dodgy input\n");
380 s << (
whoami ": converged to ftol\n");
383 s << (
whoami ": converged to xtol\n");
386 s << (
whoami ": converged nicely\n");
389 s << (
whoami ": converged via gtol\n");
392 s << (
whoami ": too many iterations\n");
395 s << (
whoami ": ftol is too small. no further reduction in the sum of squares is possible.\n");
398 s << (
whoami ": xtol is too small. no further improvement in the approximate solution x is possible.\n");
401 s << (
whoami ": gtol is too small. Fx is orthogonal to the columns of the jacobian to machine precision.\n");
404 s << (
whoami ": OIOIOI: unkown info code from lmder.\n");
435 std::cerr << __FILE__
": get_covariance() not confirmed tested yet\n";
444 for (
unsigned int i=0; i<n; ++i)
445 for (
unsigned int j=0; j<i; ++j)
456 for (
unsigned int j = 0; j < n; ++j) {
459 if (
ipvt_[i] == (
int)j+1) {
468 for (
unsigned int j = 0; j < n; ++j) {
unsigned int cols() const
Return the number of columns.
vnl_matrix< double > const & get_JtJ()
Return J'*J computed at last minimum.
abs_t rms() const
Root Mean Squares of values.
void init(vnl_least_squares_function *f)
vnl_matrix< T > transpose() const
Return transpose.
bool set_size(size_t n)
Resize to n elements.
Abstract base for minimising functions.
vnl_vector using user-supplied storage.
vnl_matrix< T > extract(unsigned r, unsigned c, unsigned top=0, unsigned left=0) const
Extract a sub-matrix of size r x c, starting at (top,left).
double get_end_error() const
Return the best error that was achieved by the last minimization, corresponding to the returned x.
size_t size() const
Return the length, number of elements, dimension of this vector.
T const * data_block() const
Access the contiguous block storing the elements in the vector. O(1).
~vnl_levenberg_marquardt() override
unsigned int get_number_of_unknowns() const
Return the number of unknowns.
Abstract base for minimising functions.
abs_t magnitude() const
Return magnitude (length) of vector.
virtual void gradf(vnl_vector< double > const &x, vnl_matrix< double > &jacobian)
Calculate the Jacobian, given the parameter vector x.
double epsfcn
Step length for FD Jacobian.
static void AtA(vnl_matrix< double > &out, const vnl_matrix< double > &A)
Compute $A^\top A$.
vnl_matrix< double > fdjac_
Levenberg Marquardt nonlinear least squares.
double ftol
Termination tolerance on F (sum of squared residuals)
vnl_matrix & inplace_transpose()
Transposes this matrix efficiently, and returns it.
Collection of C-style matrix functions.
Levenberg Marquardt nonlinear least squares.
vnl_matrix & set_column(unsigned i, T const *v)
Set the elements of the i'th column to v[i] (No bounds checking).
virtual void trace(int iteration, vnl_vector< double > const &x, vnl_vector< double > const &fx)
Called after each LM iteration to print debugging etc.
double gtol
Termination tolerance on Grad(F)' * F = 0.
bool minimize_without_gradient(vnl_vector< double > &x)
Minimize the function supplied in the constructor until convergence or failure.
Declare in a central place the list of symbols from netlib.
vnl_matrix & fill(T const &)
Sets all elements of matrix to specified value, and returns "*this".
static void lmdif_lsqfun(long *m, long *n, double *x, double *fx, long *iflag, void *userdata)
vnl_vector & fill(T const &v)
Set all values to v.
unsigned int get_number_of_residuals() const
Return the number of residuals.
T const * data_block() const
Access the contiguous block storing the elements in the matrix row-wise. O(1).
vnl_vector< T > get_column(unsigned c) const
Get a vector equal to the given column.
vnl_vector< double > vnl_levenberg_marquardt_minimize(vnl_least_squares_function &f, vnl_vector< double > const &initial_estimate)
Find minimum of "f", starting at "initial_estimate", and return.
vnl_vector< T > get_row(unsigned r) const
Get a vector equal to the given row.
vnl_matrix< double > inv_covar_
ReturnCodes
Some generic return codes that apply to all minimizers.
double xtol
Termination tolerance on X (solution vector)
vnl_matrix & set_row(unsigned i, T const *v)
Set the elements of the i'th row to v[i] (No bounds checking).
unsigned int rows() const
Return the number of rows.
double get_start_error() const
Return the error of the function when it was evaluated at the start point of the last minimization.
long maxfev
Termination maximum number of iterations.
void diagnose_outcome() const
Provide an ASCII diagnosis of the last minimization on std::ostream.
vnl_least_squares_function * f_
static void lmder_lsqfun(long *m, long *n, double *x, double *fx, double *fJ, long *, long *iflag, void *userdata)
bool minimize_using_gradient(vnl_vector< double > &x)
Minimize the function supplied in the constructor until convergence or failure.
bool has_gradient() const
Return true if the derived class has indicated that gradf has been implemented.
bool set_size(unsigned r, unsigned c)
Resize to r rows by c columns. Old data lost.
vnl_matrix reference to user-supplied storage.
virtual void f(vnl_vector< double > const &x, vnl_vector< double > &fx)=0
The main function.
bool minimize(vnl_vector< double > &x)
Calls minimize_using_gradient() or minimize_without_gradient(),.
vnl_matrix reference to user-supplied storage.
ReturnCodes failure_code_