vnl_cost_function.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_cost_function.h
2 #ifndef vnl_cost_function_h_
3 #define vnl_cost_function_h_
4 //:
5 // \file
6 // \brief Vector->Real function
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 23 Oct 97
9 //
10 // \verbatim
11 // Modifications
12 // 971023 AWF Initial version.
13 // LSB (Manchester) 26/3/01 Tidied documentation
14 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
15 // \endverbatim
16 //
17 //-----------------------------------------------------------------------------
18 
19 #include <vnl/vnl_unary_function.h>
20 #include <vnl/vnl_vector.h>
21 #include "vnl/vnl_export.h"
22 
23 //: An object that represents a function from R^n -> R.
24 // It is commonly used to express the
25 // interface of a minimizer.
26 class VNL_EXPORT vnl_cost_function : public vnl_unary_function<double, vnl_vector<double> >
27 {
28  public:
29 
30  //! Default constructor
31  vnl_cost_function():dim(0) {}
32 
33  //! Construct with a specified number of unknowns
34  vnl_cost_function(int number_of_unknowns):dim(number_of_unknowns) {}
35 
36  ~vnl_cost_function() override = default;
37 
38  //: The main function. Given the parameter vector x, compute the value of f(x).
39  double f(vnl_vector<double> const& x) override;
40 
41  //: Calculate the gradient of f at parameter vector x.
42  virtual void gradf(vnl_vector<double> const& x, vnl_vector<double>& gradient);
43 
44  //: Compute one or both of f and g.
45  // Normally implemented in terms of the above two, but may be faster if specialized. f != 0 => compute f
46  virtual void compute(vnl_vector<double> const& x, double *f, vnl_vector<double>* g);
47 
48  //: Return the number of unknowns
49  int get_number_of_unknowns() const { return dim; }
50 
51  //: Compute finite-difference gradient
52  void fdgradf(vnl_vector<double> const& x, vnl_vector<double>& gradient, double stepsize = 1e-5);
53 
54  //: Called when error is printed for user.
55  virtual double reported_error(double f_value) { return f_value; }
56 
57  //: Conveniences for printing grad, fdgrad
58  vnl_vector<double> gradf(vnl_vector<double> const& x);
59  vnl_vector<double> fdgradf(vnl_vector<double> const& x);
60 
61 protected:
62 
63  //! Set number of unknowns.
64  void set_number_of_unknowns(int number_of_unknowns) { dim=number_of_unknowns; }
65 
66 public:
67  int dim;
68 };
69 
70 #endif // vnl_cost_function_h_
An object that represents a function from R^n -> R.
virtual Return f(Argument const &i)=0
Apply the function.
Abstract 1D map.
Abstract 1D map between two types (read spaces).
Definition: vnl_fwd.h:30