vnl_least_squares_cost_function.cxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_least_squares_cost_function.cxx
2 //
3 // vnl_least_squares_cost_function
4 // Author: Andrew W. Fitzgibbon, Oxford RRG
5 // Created: 20 Aug 99
6 //
7 //-----------------------------------------------------------------------------
8 
10 
12  vnl_cost_function(func->get_number_of_unknowns()),
13  storage_(func->get_number_of_residuals()),
14  jacobian_(func->get_number_of_residuals(), func->get_number_of_unknowns()),
15  f_(func)
16 {
17 }
18 
20 {
21  f_->f(x, storage_);
22  return storage_.squared_magnitude();
23 }
24 
26 {
27  // residuals = a, b, c, ...
28  // params = x, y, z, ...
29  // f = a^2 + b^2 + c^2 + ...
30  // df/dx = 2a*da/dx + 2b*db/dx + ...
31 
32  if (f_->has_gradient()) {
33  f_->f(x,storage_);
34  f_->gradf(x, jacobian_);
35  for (unsigned int c=0; c<jacobian_.columns(); ++c) {
36  gradient[c] = 0.0;
37  for (unsigned int r=0; r<jacobian_.rows(); ++r)
38  gradient[c] += storage_[r] * jacobian_(r,c);
39  gradient[c] *= 2;
40  }
41  }
42 }
An object that represents a function from R^n -> R.
vnl_least_squares_function -> vnl_cost_function adaptor
Abstract base for minimising functions.
virtual void gradf(vnl_vector< double > const &x, vnl_matrix< double > &jacobian)
Calculate the Jacobian, given the parameter vector x.
vnl_least_squares_cost_function(vnl_least_squares_function *f)
void gradf(const vnl_vector< double > &x, vnl_vector< double > &gradient) override
Calculate the gradient of f at parameter vector x.
abs_t squared_magnitude() const
Return sum of squares of elements.
Definition: vnl_vector.h:279
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
bool has_gradient() const
Return true if the derived class has indicated that gradf has been implemented.
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
double f(const vnl_vector< double > &x) override
The main function. Given the parameter vector x, compute the value of f(x).