vnl_least_squares_function.cxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_least_squares_function.cxx
2 //:
3 // \file
4 // \author Andrew W. Fitzgibbon, Oxford RRG
5 // \date 31 Aug 96
6 
7 #include <iostream>
8 #include <cassert>
10 
11 void vnl_least_squares_function::dim_warning(unsigned int number_of_unknowns,
12  unsigned int number_of_residuals)
13 {
14  if (number_of_unknowns > number_of_residuals)
15  std::cerr << "vnl_least_squares_function: WARNING: "
16  << "unknowns(" << number_of_unknowns << ") > "
17  << "residuals("<< number_of_residuals << ")\n";
18 }
19 
21  vnl_matrix<double>& /*jacobian*/)
22 {
23  std::cerr << "Warning: gradf() called but not implemented in derived class\n";
24 }
25 
26 //: Compute finite differences gradient using central differences.
28  vnl_matrix<double>& jacobian,
29  double stepsize)
30 {
31  unsigned int dim = x.size();
32  unsigned int n = jacobian.rows();
33  assert(dim == get_number_of_unknowns());
34  assert(n == get_number_of_residuals());
35  assert(dim == jacobian.columns());
36 
37  vnl_vector<double> tx = x;
38  vnl_vector<double> fplus(n);
39  vnl_vector<double> fminus(n);
40  for (unsigned int i = 0; i < dim; ++i)
41  {
42  // calculate f just to the right of x[i]
43  double tplus = tx[i] = x[i] + stepsize;
44  this->f(tx, fplus);
45 
46  // calculate f just to the left of x[i]
47  double tminus = tx[i] = x[i] - stepsize;
48  this->f(tx, fminus);
49 
50  double h = 1.0 / (tplus - tminus);
51  for (unsigned int j = 0; j < n; ++j)
52  jacobian(j,i) = (fplus[j] - fminus[j]) * h;
53 
54  // restore tx
55  tx[i] = x[i];
56  }
57 }
58 
59 
60 //: Compute finite differences gradient using forward differences.
62  vnl_matrix<double>& jacobian,
63  double stepsize)
64 {
65  unsigned int dim = x.size();
66  unsigned int n = jacobian.rows();
67  assert(dim == get_number_of_unknowns());
68  assert(n == get_number_of_residuals());
69  assert(dim == jacobian.columns());
70 
71  vnl_vector<double> tx = x;
72  vnl_vector<double> fplus(n);
73  vnl_vector<double> fcentre(n);
74  this->f(x, fcentre);
75  for (unsigned int i = 0; i < dim; ++i)
76  {
77  // calculate f just to the right of x[i]
78  double tplus = tx[i] = x[i] + stepsize;
79  this->f(tx, fplus);
80 
81  double h = 1.0 / (tplus - x[i]);
82  for (unsigned int j = 0; j < n; ++j)
83  jacobian(j,i) = (fplus[j] - fcentre[j]) * h;
84 
85  // restore tx
86  tx[i] = x[i];
87  }
88 }
89 
90 void vnl_least_squares_function::trace(int /* iteration */,
91  vnl_vector<double> const& /*x*/,
92  vnl_vector<double> const& /*fx*/)
93 {
94  // This default implementation is empty; overloaded in derived class.
95 }
96 
98 {
100  f(x, fx);
101  return fx.rms();
102 }
abs_t rms() const
Root Mean Squares of values.
Definition: vnl_vector.h:300
Abstract base for minimising functions.
size_t size() const
Return the length, number of elements, dimension of this vector.
Definition: vnl_vector.h:126
unsigned int get_number_of_unknowns() const
Return the number of unknowns.
double rms(vnl_vector< double > const &x)
Compute the rms error at x by calling f and returning the norm of the residual vector.
virtual void gradf(vnl_vector< double > const &x, vnl_matrix< double > &jacobian)
Calculate the Jacobian, given the parameter vector x.
void fdgradf(vnl_vector< double > const &x, vnl_matrix< double > &jacobian, double stepsize)
Use this to compute a finite-difference gradient other than lmdif.
void ffdgradf(vnl_vector< double > const &x, vnl_matrix< double > &jacobian, double stepsize)
Use this to compute a finite-forward-difference gradient other than lmdif.
virtual void trace(int iteration, vnl_vector< double > const &x, vnl_vector< double > const &fx)
Called after each LM iteration to print debugging etc.
unsigned int get_number_of_residuals() const
Return the number of residuals.
void dim_warning(unsigned int n_unknowns, unsigned int n_residuals)
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
virtual void f(vnl_vector< double > const &x, vnl_vector< double > &fx)=0
The main function.
unsigned int columns() const
Return the number of columns.
Definition: vnl_matrix.h:187