Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

vnl_levenberg_marquardt.h

Go to the documentation of this file.
00001 #ifndef vnl_levenberg_marquardt_h_
00002 #define vnl_levenberg_marquardt_h_
00003 
00004 //:
00005 //  \file
00006 //  \brief Levenberg Marquardt nonlinear least squares
00007 //  \author Andrew W. Fitzgibbon, Oxford RRG, 31 Aug 96
00008 //
00009 //  Modifications
00010 //  RWMC 001097 Added verbose flag to get rid of all that blathering.
00011 //  AWF  151197 Added trace flag to increase blather.
00012 //
00013 
00014 #include <vcl_iosfwd.h>
00015 #include <vnl/vnl_vector.h>
00016 #include <vnl/vnl_matrix.h>
00017 #include <vnl/vnl_nonlinear_minimizer.h>
00018 
00019 class vnl_least_squares_function;
00020 
00021 //: Levenberg Marquardt nonlinear least squares
00022 //  vnl_levenberg_marquardt is an interface to the MINPACK routine lmdif,
00023 //  and implements Levenberg Marquardt nonlinear fitting.  The function
00024 //  to be minimized is passed as a vnl_least_squares_function object, which
00025 //  may or may not wish to provide derivatives.  If derivatives are not
00026 //  supplied, they are calculated by forward differencing, which costs
00027 //  one function evaluation per dimension, but is perfectly accurate.
00028 //  (See Hartley in ``Applications of Invariance in Computer Vision''
00029 //  for example).
00030 
00031 class vnl_levenberg_marquardt : public vnl_nonlinear_minimizer {
00032 public:
00033 
00034 //: Initialize with the function object that is to be minimized.
00035   vnl_levenberg_marquardt(vnl_least_squares_function& f) { init(&f); }
00036 
00037 //: Initialize as above, and then run minimization.
00038   vnl_levenberg_marquardt(vnl_least_squares_function& f, 
00039     vnl_vector<double>& x) {
00040     init(&f);
00041     minimize(x);
00042   }
00043 
00044   ~vnl_levenberg_marquardt();
00045 
00046 //: Minimize the function supplied in the constructor until convergence
00047 // or failure.  On return, x is such that f(x) is the lowest value achieved.
00048 // Returns true for convergence, false for failure.
00049   bool minimize_without_gradient(vnl_vector<double>& x);
00050   bool minimize_using_gradient  (vnl_vector<double>& x);
00051 
00052   // use this one if you can remember whether it uses the gradient or not.
00053   bool minimize(vnl_vector<double>& x) { return minimize_without_gradient(x); }
00054 
00055   // Coping with failure-------------------------------------------------------
00056 
00057 //: Provide an ASCII diagnosis of the last minimization on ostream.
00058   void diagnose_outcome(/*vcl_cerr*/) const;
00059   void diagnose_outcome(vcl_ostream&) const;
00060 
00061 //: Return J'*J computed at last minimum.
00062   vnl_matrix<double> const& get_JtJ();
00063 
00064 protected:
00065 
00066   vnl_least_squares_function* f_;
00067   vnl_matrix<double>* fdjac_; // Computed during lmdif/lmder
00068   vnl_vector<int>*    ipvt_;  // Also computed, both needed to get J'*J at end.
00069 
00070   vnl_matrix<double>* covariance_;
00071   bool set_covariance_; // Set if covariance_ holds J'*J
00072 
00073   void init(vnl_least_squares_function* f);
00074 
00075   // Communication with callback
00076   friend class vnl_levenberg_marquardt_Activate;
00077   static int lmdif_lsqfun(int* m, int* n, const double* x, 
00078         double* fx, int* iflag);
00079   static int lmder_lsqfun(int* m, int* n, const double* x, 
00080         double* fx, double* fJ, int&, int* iflag);
00081 };
00082 
00083 #endif // vnl_levenberg_marquardt_h_

Generated at Fri May 21 01:15:48 2004 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000