core/vnl/vnl_cost_function.h
Go to the documentation of this file.
00001 // This is core/vnl/vnl_cost_function.h
00002 #ifndef vnl_cost_function_h_
00003 #define vnl_cost_function_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 //  \file
00009 //  \brief Vector->Real function
00010 //  \author Andrew W. Fitzgibbon, Oxford RRG
00011 //  \date   23 Oct 97
00012 //
00013 // \verbatim
00014 // Modifications
00015 //  971023 AWF Initial version.
00016 //  LSB (Manchester) 26/3/01 Tidied documentation
00017 //   Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
00018 // \endverbatim
00019 //
00020 //-----------------------------------------------------------------------------
00021 
00022 #include <vnl/vnl_unary_function.h>
00023 #include <vnl/vnl_vector.h>
00024 
00025 //:   An object that represents a function from R^n -> R.
00026 //    It is commonly used to express the
00027 //    interface of a minimizer.
00028 class vnl_cost_function : public vnl_unary_function<double, vnl_vector<double> >
00029 {
00030  public:
00031 
00032   //! Default constructor   
00033   vnl_cost_function():dim(0) {}
00034 
00035   //! Construct with a specified number of unknowns
00036   vnl_cost_function(int number_of_unknowns):dim(number_of_unknowns) {}
00037 
00038   virtual ~vnl_cost_function() {}
00039 
00040   //:  The main function.  Given the parameter vector x, compute the value of f(x).
00041   virtual double f(vnl_vector<double> const& x);
00042 
00043   //:  Calculate the gradient of f at parameter vector x.
00044   virtual void gradf(vnl_vector<double> const& x, vnl_vector<double>& gradient);
00045 
00046   //:  Compute one or both of f and g.
00047   //   Normally implemented in terms of the above two, but may be faster if specialized. f != 0 => compute f
00048   virtual void compute(vnl_vector<double> const& x, double *f, vnl_vector<double>* g);
00049 
00050   //:  Return the number of unknowns
00051   int get_number_of_unknowns() const { return dim; }
00052 
00053   //:  Compute finite-difference gradient
00054   void fdgradf(vnl_vector<double> const& x, vnl_vector<double>& gradient, double stepsize = 1e-5);
00055 
00056   //:  Called when error is printed for user.
00057   virtual double reported_error(double f_value) { return f_value; }
00058 
00059   //:  Conveniences for printing grad, fdgrad
00060   vnl_vector<double> gradf(vnl_vector<double> const& x);
00061   vnl_vector<double> fdgradf(vnl_vector<double> const& x);
00062 
00063 protected:
00064 
00065     //! Set number of unknowns. 
00066     void set_number_of_unknowns(int number_of_unknowns) { dim=number_of_unknowns; }
00067 
00068 public:
00069     int dim;
00070 };
00071 
00072 
00073 #endif // vnl_cost_function_h_