vnl_least_squares_function.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_least_squares_function.h
2 #ifndef vnl_least_squares_function_h_
3 #define vnl_least_squares_function_h_
4 //:
5 // \file
6 // \brief Abstract base for minimising functions
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 31 Aug 96
9 //
10 // \verbatim
11 // Modifications
12 // 280697 AWF Changed return type of f from double to void, as it wasn't used, and
13 // people were going to extra trouble to compute it.
14 // 20 Apr 1999 FSM Added failure flag so that f() and grad() may signal failure to the caller.
15 // 23/3/01 LSB (Manchester) Tidied documentation
16 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
17 // \endverbatim
18 //
19 // not used? #include <vcl_compiler.h>
20 #include <string>
21 #include <vnl/vnl_vector.h>
22 #include <vnl/vnl_matrix.h>
23 #include "vnl/vnl_export.h"
24 
25 //: Abstract base for minimising functions.
26 // vnl_least_squares_function is an abstract base for functions to be minimized
27 // by an optimizer. To define your own function to be minimized, subclass
28 // from vnl_least_squares_function, and implement the pure virtual f (and
29 // optionally grad_f).
30 //
31 // Whether or not f ought to be const is a problem. Clients might well
32 // want to cache some information during the call, and if they're compute
33 // objects, will almost certainly be writing to members during the
34 // computation. For the moment it's non-const, but we'll see...
36 {
37  public:
38  enum UseGradient {
40  use_gradient
41  };
42  bool failure;
43 
44  //: Construct vnl_least_squares_function.
45  // Passing number of parameters (unknowns, domain dimension) and number of
46  // residuals (range dimension).
47  // The optional argument should be no_gradient if the gradf function has not
48  // been implemented. Default is use_gradient.
49  vnl_least_squares_function(unsigned int number_of_unknowns,
50  unsigned int number_of_residuals,
51  UseGradient g = use_gradient)
52  : failure(false), p_(number_of_unknowns), n_(number_of_residuals),
53  use_gradient_(g == use_gradient)
54  { dim_warning(p_,n_); }
55 
56  virtual ~vnl_least_squares_function() = default;
57 
58  // the virtuals may call this to signal a failure.
59  void throw_failure() { failure = true; }
60  void clear_failure() { failure = false; }
61 
62  //: The main function.
63  // Given the parameter vector x, compute the vector of residuals fx.
64  // Fx has been sized appropriately before the call.
65  virtual void f(vnl_vector<double> const& x, vnl_vector<double>& fx) = 0;
66 
67  //: Calculate the Jacobian, given the parameter vector x.
68  virtual void gradf(vnl_vector<double> const& x, vnl_matrix<double>& jacobian);
69 
70  //: Use this to compute a finite-difference gradient other than lmdif
71  void fdgradf(vnl_vector<double> const& x, vnl_matrix<double>& jacobian,
72  double stepsize);
73 
74  //: Use this to compute a finite-forward-difference gradient other than lmdif
75  // This takes about half as many estimates as fdgradf
76  void ffdgradf(vnl_vector<double> const& x, vnl_matrix<double>& jacobian,
77  double stepsize);
78 
79  //: Called after each LM iteration to print debugging etc.
80  virtual void trace(int iteration,
81  vnl_vector<double> const& x,
82  vnl_vector<double> const& fx);
83 
84  //: Compute the rms error at x by calling f and returning the norm of the residual vector.
85  double rms(vnl_vector<double> const& x);
86 
87  //: Return the number of unknowns
88  unsigned int get_number_of_unknowns() const { return p_; }
89 
90  //: Return the number of residuals.
91  unsigned int get_number_of_residuals() const { return n_; }
92 
93  //: Return true if the derived class has indicated that gradf has been implemented
94  bool has_gradient() const { return use_gradient_; }
95 
96  protected:
97  unsigned int p_;
98  unsigned int n_;
100 
101  void init(unsigned int number_of_unknowns, unsigned int number_of_residuals)
102  { p_ = number_of_unknowns; n_ = number_of_residuals; dim_warning(p_,n_); }
103  private:
104  void dim_warning(unsigned int n_unknowns, unsigned int n_residuals);
105 };
106 
107 #endif // vnl_least_squares_function_h_
An ordinary mathematical matrix.
void init(unsigned int number_of_unknowns, unsigned int number_of_residuals)
unsigned int get_number_of_unknowns() const
Return the number of unknowns.
Abstract base for minimising functions.
vnl_least_squares_function(unsigned int number_of_unknowns, unsigned int number_of_residuals, UseGradient g=use_gradient)
Construct vnl_least_squares_function.
unsigned int get_number_of_residuals() const
Return the number of residuals.
bool has_gradient() const
Return true if the derived class has indicated that gradf has been implemented.