Public Types | Public Member Functions | Public Attributes | Protected Attributes | Private Member Functions
vnl_sparse_lst_sqr_function Class Reference

Abstract base for sparse least squares functions. More...

#include <vnl_sparse_lst_sqr_function.h>

List of all members.

Public Types

enum  UseGradient { no_gradient, use_gradient }
enum  UseWeights { no_weights, use_weights }

Public Member Functions

 vnl_sparse_lst_sqr_function (unsigned int num_a, unsigned int num_params_per_a, unsigned int num_b, unsigned int num_params_per_b, unsigned int num_params_c, unsigned int num_residuals_per_e, UseGradient g=use_gradient, UseWeights w=no_weights)
 Construct vnl_sparse_lst_sqr_function.
 vnl_sparse_lst_sqr_function (unsigned int num_a, unsigned int num_params_per_a, unsigned int num_b, unsigned int num_params_per_b, unsigned int num_params_c, const vcl_vector< vcl_vector< bool > > &xmask, unsigned int num_residuals_per_e, UseGradient g=use_gradient, UseWeights w=no_weights)
 Construct vnl_sparse_lst_sqr_function.
 vnl_sparse_lst_sqr_function (const vcl_vector< unsigned int > &a_sizes, const vcl_vector< unsigned int > &b_sizes, unsigned int num_params_c, const vcl_vector< unsigned int > &e_sizes, const vcl_vector< vcl_vector< bool > > &xmask, UseGradient g=use_gradient, UseWeights w=no_weights)
 Construct vnl_sparse_lst_sqr_function.
virtual ~vnl_sparse_lst_sqr_function ()
void throw_failure ()
void clear_failure ()
virtual void f (vnl_vector< double > const &a, vnl_vector< double > const &b, vnl_vector< double > const &c, vnl_vector< double > &f)
 Compute all residuals.
virtual void jac_blocks (vnl_vector< double > const &a, vnl_vector< double > const &b, vnl_vector< double > const &c, vcl_vector< vnl_matrix< double > > &A, vcl_vector< vnl_matrix< double > > &B, vcl_vector< vnl_matrix< double > > &C)
 Compute the sparse Jacobian in block form.
virtual void fd_jac_blocks (vnl_vector< double > const &a, vnl_vector< double > const &b, vnl_vector< double > const &c, vcl_vector< vnl_matrix< double > > &A, vcl_vector< vnl_matrix< double > > &B, vcl_vector< vnl_matrix< double > > &C, double stepsize)
 Compute the sparse Jacobian in block form using a finite difference approximation.
virtual void compute_weights (vnl_vector< double > const &a, vnl_vector< double > const &b, vnl_vector< double > const &c, vnl_vector< double > const &f, vnl_vector< double > &weights)
 If using weighted least squares, compute the weights for each i and j.
virtual void apply_weights (vnl_vector< double > const &weights, vnl_vector< double > &f)
 If using weighted least squares, apply the weights to residuals f.
virtual void apply_weights (vnl_vector< double > const &weights, vcl_vector< vnl_matrix< double > > &A, vcl_vector< vnl_matrix< double > > &B, vcl_vector< vnl_matrix< double > > &C)
 If using weighted least squares, apply the weights to residuals A, B, C.
virtual void fij (int i, int j, vnl_vector< double > const &ai, vnl_vector< double > const &bj, vnl_vector< double > const &c, vnl_vector< double > &fij)
 Compute the residuals from the ith component of a, the jth component of b.
virtual void jac_Aij (int i, int j, vnl_vector< double > const &ai, vnl_vector< double > const &bj, vnl_vector< double > const &c, vnl_matrix< double > &Aij)
 Calculate the Jacobian A_ij, given the parameter vectors a_i, b_j, and c.
virtual void jac_Bij (int i, int j, vnl_vector< double > const &ai, vnl_vector< double > const &bj, vnl_vector< double > const &c, vnl_matrix< double > &Bij)
 Calculate the Jacobian B_ij, given the parameter vectors a_i, b_j, and c.
virtual void jac_Cij (int i, int j, vnl_vector< double > const &ai, vnl_vector< double > const &bj, vnl_vector< double > const &c, vnl_matrix< double > &Cij)
 Calculate the Jacobian C_ij, given the parameter vectors a_i, b_j, and c.
void fd_jac_Aij (int i, int j, vnl_vector< double > const &ai, vnl_vector< double > const &bj, vnl_vector< double > const &c, vnl_matrix< double > &Aij, double stepsize)
 Use this to compute a finite-difference Jacobian A_ij.
void fd_jac_Bij (int i, int j, vnl_vector< double > const &ai, vnl_vector< double > const &bj, vnl_vector< double > const &c, vnl_matrix< double > &Bij, double stepsize)
 Use this to compute a finite-difference Jacobian B_ij.
void fd_jac_Cij (int i, int j, vnl_vector< double > const &ai, vnl_vector< double > const &bj, vnl_vector< double > const &c, vnl_matrix< double > &Cij, double stepsize)
 Use this to compute a finite-difference Jacobian C_ij.
virtual void compute_weight_ij (int i, int j, vnl_vector< double > const &ai, vnl_vector< double > const &bj, vnl_vector< double > const &c, vnl_vector< double > const &fij, double &weight)
 If using weighted least squares, compute the weight.
virtual void apply_weight_ij (int i, int j, double const &weight, vnl_vector< double > &fij)
 If using weighted least squares, apply the weight to fij.
virtual void apply_weight_ij (int i, int j, double const &weight, vnl_matrix< double > &Aij, vnl_matrix< double > &Bij, vnl_matrix< double > &Cij)
 If using weighted least squares, apply the weight to Aij, Bij, Cij.
virtual void trace (int iteration, vnl_vector< double > const &a, vnl_vector< double > const &b, vnl_vector< double > const &c, vnl_vector< double > const &e)
 Called after each LM iteration to print debugging etc.
unsigned int number_of_params_a (int i) const
 Return the number of parameters of a_j.
unsigned int number_of_params_b (int j) const
 Return the number of parameters of b_i.
unsigned int number_of_params_c () const
 Return the number of parameters of c.
unsigned int number_of_residuals (int k) const
 Return the number of residuals in the kth residual vector.
unsigned int number_of_residuals (int i, int j) const
 Return the number of residuals for x_ij.
unsigned int index_a (int i) const
 return the index of aj in a.
unsigned int index_b (int j) const
 return the index of bj in b.
unsigned int index_e (int k) const
 return the index of ek in e.
unsigned int number_of_a () const
 Return the number of subsets in a.
unsigned int number_of_b () const
 Return the number of subsets in b.
unsigned int number_of_e () const
 Return the number of residual vectors.
bool has_gradient () const
 Return true if the derived class has indicated that gradf has been implemented.
bool has_weights () const
 Return true if the derived class has indicated that.
const vnl_crs_indexresidual_indices () const
 Return a const reference to the residual indexer.

Public Attributes

bool failure

Protected Attributes

vnl_crs_index residual_indices_
vcl_vector< unsigned int > indices_a_
vcl_vector< unsigned int > indices_b_
unsigned int num_params_c_
vcl_vector< unsigned int > indices_e_
bool use_gradient_
bool use_weights_

Private Member Functions

void dim_warning (unsigned int n_unknowns, unsigned int n_residuals)

Detailed Description

Abstract base for sparse least squares functions.

vnl_sparse_lst_sqr_function is an abstract base for functions to be minimized by an optimizer. To define your own function to be minimized, subclass from vnl_sparse_lst_sqr_function, and implement the pure virtual f (and optionally grad_f).

This differs from a vnl_least_squares_function in that many entries in the Jacobian are known to be zero, and we don't want to compute them. The particular sparse structure is that described in Hartley and Zisserman section A4.3. It is assumed that the parameter vector can be partitioned into sets A and B. These are further partitioned into subsets {a_1, a_2, ... a_m} and {b_1, b_2, ... b_n}. Likewise, the residual vector X is partitioned into {x_11, x_12, ... x_mn} (not all x_ij are required). We further assume that dx_ij/da_k = 0 for all i != k and dx_ij/db_k = 0 for all j != k.

This implementation further generalizes the concept by allowing for a third set of parameters C that are non-sparse. That is, dx_ij/dC != 0 for all i and j (in general).

An example use case is bundle adjustment where each a_i is the parameters for one of m cameras, each b_j is the parameters of a 3D point, and x_ij is the projection error of the jth point by the ith camera. If type C parameters are used, they might represent the unknown intrinic camera parameters that are assumed to be fixed over all images.

Definition at line 47 of file vnl_sparse_lst_sqr_function.h.


Member Enumeration Documentation

Enumerator:
no_gradient 
use_gradient 

Definition at line 50 of file vnl_sparse_lst_sqr_function.h.

Enumerator:
no_weights 
use_weights 

Definition at line 54 of file vnl_sparse_lst_sqr_function.h.


Constructor & Destructor Documentation

vnl_sparse_lst_sqr_function::vnl_sparse_lst_sqr_function ( unsigned int  num_a,
unsigned int  num_params_per_a,
unsigned int  num_b,
unsigned int  num_params_per_b,
unsigned int  num_params_c,
unsigned int  num_residuals_per_e,
UseGradient  g = use_gradient,
UseWeights  w = no_weights 
)

Construct vnl_sparse_lst_sqr_function.

Assumes A consists of num_a parameters each of size num_params_per_a Assumes B consists of num_b parameters each of size num_params_per_b Assumes C consists of num_params_c parameters Assumes there is a residual x_ij for all i and j, each of size num_residuals_per_e The optional argument should be no_gradient if the gradf function has not been implemented. Default is use_gradient.

Definition at line 32 of file vnl_sparse_lst_sqr_function.cxx.

vnl_sparse_lst_sqr_function::vnl_sparse_lst_sqr_function ( unsigned int  num_a,
unsigned int  num_params_per_a,
unsigned int  num_b,
unsigned int  num_params_per_b,
unsigned int  num_params_c,
const vcl_vector< vcl_vector< bool > > &  xmask,
unsigned int  num_residuals_per_e,
UseGradient  g = use_gradient,
UseWeights  w = no_weights 
)

Construct vnl_sparse_lst_sqr_function.

Assumes A consists of num_a parameters each of size num_params_per_a Assumes B consists of num_b parameters each of size num_params_per_b Assumes C consists of num_params_c parameters xmask is a mask for residual availability. residual e_ij exists only if mask[i][j]==true Assumes each available residual has size num_residuals_per_e The optional argument should be no_gradient if the gradf function has not been implemented. Default is use_gradient.

Definition at line 71 of file vnl_sparse_lst_sqr_function.cxx.

vnl_sparse_lst_sqr_function::vnl_sparse_lst_sqr_function ( const vcl_vector< unsigned int > &  a_sizes,
const vcl_vector< unsigned int > &  b_sizes,
unsigned int  num_params_c,
const vcl_vector< unsigned int > &  e_sizes,
const vcl_vector< vcl_vector< bool > > &  xmask,
UseGradient  g = use_gradient,
UseWeights  w = no_weights 
)

Construct vnl_sparse_lst_sqr_function.

This constructor is the most general

Parameters:
a_sizesis a vector describing the number of parameters for each a_i
b_sizesis a vector describing the number of parameters for each b_j
num_params_cis the number of C parameters
e_sizesis a vector describing the number of parameters for each residual e_ij
xmaskis a mask for residual availability. residual e_ij exists only if mask[i][j]==true xmask must be a_sizes.size() by b_sizes.size() and contain e_sizes.size() true entries The optional argument should be no_gradient if the gradf function has not been implemented. Default is use_gradient.

Definition at line 116 of file vnl_sparse_lst_sqr_function.cxx.

virtual vnl_sparse_lst_sqr_function::~vnl_sparse_lst_sqr_function ( ) [inline, virtual]

Definition at line 112 of file vnl_sparse_lst_sqr_function.h.


Member Function Documentation

void vnl_sparse_lst_sqr_function::apply_weight_ij ( int  i,
int  j,
double const &  weight,
vnl_vector< double > &  fij 
) [virtual]

If using weighted least squares, apply the weight to fij.

The default implementation multiplies fij by weight.

Definition at line 536 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::apply_weight_ij ( int  i,
int  j,
double const &  weight,
vnl_matrix< double > &  Aij,
vnl_matrix< double > &  Bij,
vnl_matrix< double > &  Cij 
) [virtual]

If using weighted least squares, apply the weight to Aij, Bij, Cij.

The default implementation multiplies each matrix by weight.

Definition at line 547 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::apply_weights ( vnl_vector< double > const &  weights,
vnl_vector< double > &  e 
) [virtual]

If using weighted least squares, apply the weights to residuals f.

The default implementation applies weights by calling apply_weight_ij for each valid pair of i and j. You do not need to overload this method unless you want to provide a more specialized implementation for your problem.

Definition at line 308 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::apply_weights ( vnl_vector< double > const &  weights,
vcl_vector< vnl_matrix< double > > &  A,
vcl_vector< vnl_matrix< double > > &  B,
vcl_vector< vnl_matrix< double > > &  C 
) [virtual]

If using weighted least squares, apply the weights to residuals A, B, C.

The default implementation applies weights by calling apply_weight_ij for each valid pair of i and j. You do not need to overload this method unless you want to provide a more specialized implementation for your problem.

Definition at line 332 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::clear_failure ( ) [inline]

Definition at line 116 of file vnl_sparse_lst_sqr_function.h.

void vnl_sparse_lst_sqr_function::compute_weight_ij ( int  i,
int  j,
vnl_vector< double > const &  ai,
vnl_vector< double > const &  bj,
vnl_vector< double > const &  c,
vnl_vector< double > const &  fij,
double &  weight 
) [virtual]

If using weighted least squares, compute the weight.

Return the weight in weight. The default implementation sets weight = 1

Definition at line 522 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::compute_weights ( vnl_vector< double > const &  a,
vnl_vector< double > const &  b,
vnl_vector< double > const &  c,
vnl_vector< double > const &  e,
vnl_vector< double > &  weights 
) [virtual]

If using weighted least squares, compute the weights for each i and j.

Return the weights in weights. The default implementation computes weights by calling compute_weight_ij for each valid pair of i and j. You do not need to overload this method unless you want to provide a more specialized implementation for your problem.

Definition at line 273 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::dim_warning ( unsigned int  n_unknowns,
unsigned int  n_residuals 
) [private]

Definition at line 16 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::f ( vnl_vector< double > const &  a,
vnl_vector< double > const &  b,
vnl_vector< double > const &  c,
vnl_vector< double > &  e 
) [virtual]

Compute all residuals.

Given the parameter vectors a, b, and c, compute the vector of residuals f. f has been sized appropriately before the call. The default implementation computes f by calling fij for each valid pair of i and j. You do not need to overload this method unless you want to provide a more efficient implementation for your problem.

Definition at line 158 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::fd_jac_Aij ( int  i,
int  j,
vnl_vector< double > const &  ai,
vnl_vector< double > const &  bj,
vnl_vector< double > const &  c,
vnl_matrix< double > &  Aij,
double  stepsize 
)

Use this to compute a finite-difference Jacobian A_ij.

Definition at line 400 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::fd_jac_Bij ( int  i,
int  j,
vnl_vector< double > const &  ai,
vnl_vector< double > const &  bj,
vnl_vector< double > const &  c,
vnl_matrix< double > &  Bij,
double  stepsize 
)

Use this to compute a finite-difference Jacobian B_ij.

Definition at line 440 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::fd_jac_blocks ( vnl_vector< double > const &  a,
vnl_vector< double > const &  b,
vnl_vector< double > const &  c,
vcl_vector< vnl_matrix< double > > &  A,
vcl_vector< vnl_matrix< double > > &  B,
vcl_vector< vnl_matrix< double > > &  C,
double  stepsize 
) [virtual]

Compute the sparse Jacobian in block form using a finite difference approximation.

Given the parameter vectors a, b and c, compute the set of block Jacobians Aij, Bij, and Cij. The finite difference approximation is done independently at each block. All Aij, Bij, and Cij have been sized appropriately before the call. The default implementation computes A, B, and C by calling jac_Aij, jac_Bij, and jac_Cij for each valid pair of i and j. You do not need to overload this method unless you want to provide a more efficient implementation for your problem.

Definition at line 234 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::fd_jac_Cij ( int  i,
int  j,
vnl_vector< double > const &  ai,
vnl_vector< double > const &  bj,
vnl_vector< double > const &  c,
vnl_matrix< double > &  Cij,
double  stepsize 
)

Use this to compute a finite-difference Jacobian C_ij.

Definition at line 480 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::fij ( int  i,
int  j,
vnl_vector< double > const &  ai,
vnl_vector< double > const &  bj,
vnl_vector< double > const &  c,
vnl_vector< double > &  f_i_j 
) [virtual]

Compute the residuals from the ith component of a, the jth component of b.

Given the parameter vectors ai, bj, and c, compute the vector of residuals fij. fij has been sized appropriately before the call.

Definition at line 355 of file vnl_sparse_lst_sqr_function.cxx.

bool vnl_sparse_lst_sqr_function::has_gradient ( ) const [inline]

Return true if the derived class has indicated that gradf has been implemented.

Definition at line 314 of file vnl_sparse_lst_sqr_function.h.

bool vnl_sparse_lst_sqr_function::has_weights ( ) const [inline]

Return true if the derived class has indicated that.

apply_weights or apply_weight_ij have been implemented

Definition at line 318 of file vnl_sparse_lst_sqr_function.h.

unsigned int vnl_sparse_lst_sqr_function::index_a ( int  i) const [inline]

return the index of aj in a.

Definition at line 296 of file vnl_sparse_lst_sqr_function.h.

unsigned int vnl_sparse_lst_sqr_function::index_b ( int  j) const [inline]

return the index of bj in b.

Definition at line 299 of file vnl_sparse_lst_sqr_function.h.

unsigned int vnl_sparse_lst_sqr_function::index_e ( int  k) const [inline]

return the index of ek in e.

Definition at line 302 of file vnl_sparse_lst_sqr_function.h.

void vnl_sparse_lst_sqr_function::jac_Aij ( int  i,
int  j,
vnl_vector< double > const &  ai,
vnl_vector< double > const &  bj,
vnl_vector< double > const &  c,
vnl_matrix< double > &  Aij 
) [virtual]

Calculate the Jacobian A_ij, given the parameter vectors a_i, b_j, and c.

Definition at line 366 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::jac_Bij ( int  i,
int  j,
vnl_vector< double > const &  ai,
vnl_vector< double > const &  bj,
vnl_vector< double > const &  c,
vnl_matrix< double > &  Bij 
) [virtual]

Calculate the Jacobian B_ij, given the parameter vectors a_i, b_j, and c.

Definition at line 377 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::jac_blocks ( vnl_vector< double > const &  a,
vnl_vector< double > const &  b,
vnl_vector< double > const &  c,
vcl_vector< vnl_matrix< double > > &  A,
vcl_vector< vnl_matrix< double > > &  B,
vcl_vector< vnl_matrix< double > > &  C 
) [virtual]

Compute the sparse Jacobian in block form.

Given the parameter vectors a, b, and c, compute the set of block Jacobians Aij, Bij, and Cij. All Aij, Bij, and Cij have been sized appropriately before the call. The default implementation computes A, B, and C by calling jac_Aij, jac_Bij, and jac_Cij for each valid pair of i and j. You do not need to overload this method unless you want to provide a more efficient implementation for your problem.

Definition at line 194 of file vnl_sparse_lst_sqr_function.cxx.

void vnl_sparse_lst_sqr_function::jac_Cij ( int  i,
int  j,
vnl_vector< double > const &  ai,
vnl_vector< double > const &  bj,
vnl_vector< double > const &  c,
vnl_matrix< double > &  Cij 
) [virtual]

Calculate the Jacobian C_ij, given the parameter vectors a_i, b_j, and c.

Definition at line 388 of file vnl_sparse_lst_sqr_function.cxx.

unsigned int vnl_sparse_lst_sqr_function::number_of_a ( ) const [inline]

Return the number of subsets in a.

Definition at line 305 of file vnl_sparse_lst_sqr_function.h.

unsigned int vnl_sparse_lst_sqr_function::number_of_b ( ) const [inline]

Return the number of subsets in b.

Definition at line 308 of file vnl_sparse_lst_sqr_function.h.

unsigned int vnl_sparse_lst_sqr_function::number_of_e ( ) const [inline]

Return the number of residual vectors.

Definition at line 311 of file vnl_sparse_lst_sqr_function.h.

unsigned int vnl_sparse_lst_sqr_function::number_of_params_a ( int  i) const [inline]

Return the number of parameters of a_j.

Definition at line 276 of file vnl_sparse_lst_sqr_function.h.

unsigned int vnl_sparse_lst_sqr_function::number_of_params_b ( int  j) const [inline]

Return the number of parameters of b_i.

Definition at line 279 of file vnl_sparse_lst_sqr_function.h.

unsigned int vnl_sparse_lst_sqr_function::number_of_params_c ( ) const [inline]

Return the number of parameters of c.

Definition at line 282 of file vnl_sparse_lst_sqr_function.h.

unsigned int vnl_sparse_lst_sqr_function::number_of_residuals ( int  k) const [inline]

Return the number of residuals in the kth residual vector.

Definition at line 285 of file vnl_sparse_lst_sqr_function.h.

unsigned int vnl_sparse_lst_sqr_function::number_of_residuals ( int  i,
int  j 
) const [inline]

Return the number of residuals for x_ij.

Definition at line 288 of file vnl_sparse_lst_sqr_function.h.

const vnl_crs_index& vnl_sparse_lst_sqr_function::residual_indices ( ) const [inline]

Return a const reference to the residual indexer.

Definition at line 321 of file vnl_sparse_lst_sqr_function.h.

void vnl_sparse_lst_sqr_function::throw_failure ( ) [inline]

Definition at line 115 of file vnl_sparse_lst_sqr_function.h.

void vnl_sparse_lst_sqr_function::trace ( int  iteration,
vnl_vector< double > const &  a,
vnl_vector< double > const &  b,
vnl_vector< double > const &  c,
vnl_vector< double > const &  e 
) [virtual]

Called after each LM iteration to print debugging etc.

Definition at line 559 of file vnl_sparse_lst_sqr_function.cxx.


Member Data Documentation

Definition at line 58 of file vnl_sparse_lst_sqr_function.h.

vcl_vector<unsigned int> vnl_sparse_lst_sqr_function::indices_a_ [protected]

Definition at line 325 of file vnl_sparse_lst_sqr_function.h.

vcl_vector<unsigned int> vnl_sparse_lst_sqr_function::indices_b_ [protected]

Definition at line 326 of file vnl_sparse_lst_sqr_function.h.

vcl_vector<unsigned int> vnl_sparse_lst_sqr_function::indices_e_ [protected]

Definition at line 328 of file vnl_sparse_lst_sqr_function.h.

Definition at line 327 of file vnl_sparse_lst_sqr_function.h.

Definition at line 324 of file vnl_sparse_lst_sqr_function.h.

Definition at line 330 of file vnl_sparse_lst_sqr_function.h.

Definition at line 331 of file vnl_sparse_lst_sqr_function.h.


The documentation for this class was generated from the following files: