Abstract base for sparse least squares functions. More...
#include <vnl_sparse_lst_sqr_function.h>
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. More... | |
| 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 std::vector< std::vector< bool > > &xmask, unsigned int num_residuals_per_e, UseGradient g=use_gradient, UseWeights w=no_weights) | |
| Construct vnl_sparse_lst_sqr_function. More... | |
| vnl_sparse_lst_sqr_function (const std::vector< unsigned int > &a_sizes, const std::vector< unsigned int > &b_sizes, unsigned int num_params_c, const std::vector< unsigned int > &e_sizes, const std::vector< std::vector< bool > > &xmask, UseGradient g=use_gradient, UseWeights w=no_weights) | |
| Construct vnl_sparse_lst_sqr_function. More... | |
| virtual | ~vnl_sparse_lst_sqr_function ()=default |
| 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. More... | |
| virtual void | jac_blocks (vnl_vector< double > const &a, vnl_vector< double > const &b, vnl_vector< double > const &c, std::vector< vnl_matrix< double > > &A, std::vector< vnl_matrix< double > > &B, std::vector< vnl_matrix< double > > &C) |
| Compute the sparse Jacobian in block form. More... | |
| virtual void | fd_jac_blocks (vnl_vector< double > const &a, vnl_vector< double > const &b, vnl_vector< double > const &c, std::vector< vnl_matrix< double > > &A, std::vector< vnl_matrix< double > > &B, std::vector< vnl_matrix< double > > &C, double stepsize) |
| Compute the sparse Jacobian in block form using a finite difference approximation. More... | |
| 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. More... | |
| virtual void | apply_weights (vnl_vector< double > const &weights, vnl_vector< double > &f) |
| If using weighted least squares, apply the weights to residuals f. More... | |
| virtual void | apply_weights (vnl_vector< double > const &weights, std::vector< vnl_matrix< double > > &A, std::vector< vnl_matrix< double > > &B, std::vector< vnl_matrix< double > > &C) |
| If using weighted least squares, apply the weights to residuals A, B, C. More... | |
| 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. More... | |
| virtual void | jac_Aij (unsigned int i, unsigned 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. More... | |
| virtual void | jac_Bij (unsigned int i, unsigned 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. More... | |
| virtual void | jac_Cij (unsigned int i, unsigned 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. More... | |
| 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. More... | |
| 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. More... | |
| 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. More... | |
| 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. More... | |
| 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. More... | |
| 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. More... | |
| 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. More... | |
| unsigned int | number_of_params_a (int i) const |
| Return the number of parameters of a_j. More... | |
| unsigned int | number_of_params_b (int j) const |
| Return the number of parameters of b_i. More... | |
| unsigned int | number_of_params_c () const |
| Return the number of parameters of c. More... | |
| unsigned int | number_of_residuals (int k) const |
| Return the number of residuals in the kth residual vector. More... | |
| unsigned int | number_of_residuals (int i, int j) const |
| Return the number of residuals for x_ij. More... | |
| unsigned int | index_a (int i) const |
| return the index of aj in a. More... | |
| unsigned int | index_b (int j) const |
| return the index of bj in b. More... | |
| unsigned int | index_e (int k) const |
| return the index of ek in e. More... | |
| unsigned int | number_of_a () const |
Return the number of subsets in a. More... | |
| unsigned int | number_of_b () const |
Return the number of subsets in b. More... | |
| unsigned int | number_of_e () const |
| Return the number of residual vectors. More... | |
| bool | has_gradient () const |
| Return true if the derived class has indicated that gradf has been implemented. More... | |
| bool | has_weights () const |
| Return true if the derived class has indicated that. More... | |
| const vnl_crs_index & | residual_indices () const |
| Return a const reference to the residual indexer. More... | |
Public Attributes | |
| bool | failure |
Protected Attributes | |
| vnl_crs_index | residual_indices_ |
| std::vector< unsigned int > | indices_a_ |
| std::vector< unsigned int > | indices_b_ |
| unsigned int | num_params_c_ |
| std::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) |
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 45 of file vnl_sparse_lst_sqr_function.h.
| Enumerator | |
|---|---|
| no_gradient | |
| use_gradient | |
Definition at line 48 of file vnl_sparse_lst_sqr_function.h.
| Enumerator | |
|---|---|
| no_weights | |
| use_weights | |
Definition at line 52 of file vnl_sparse_lst_sqr_function.h.
| 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 29 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 std::vector< std::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 68 of file vnl_sparse_lst_sqr_function.cxx.
| vnl_sparse_lst_sqr_function::vnl_sparse_lst_sqr_function | ( | const std::vector< unsigned int > & | a_sizes, |
| const std::vector< unsigned int > & | b_sizes, | ||
| unsigned int | num_params_c, | ||
| const std::vector< unsigned int > & | e_sizes, | ||
| const std::vector< std::vector< bool > > & | xmask, | ||
| UseGradient | g = use_gradient, |
||
| UseWeights | w = no_weights |
||
| ) |
Construct vnl_sparse_lst_sqr_function.
This constructor is the most general
| a_sizes | is a vector describing the number of parameters for each a_i |
| b_sizes | is a vector describing the number of parameters for each b_j |
| num_params_c | is the number of C parameters |
| e_sizes | is a vector describing the number of parameters for each residual e_ij |
| xmask | is 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 113 of file vnl_sparse_lst_sqr_function.cxx.
|
virtualdefault |
|
virtual |
If using weighted least squares, apply the weight to fij.
The default implementation multiplies fij by weight.
Definition at line 531 of file vnl_sparse_lst_sqr_function.cxx.
|
virtual |
If using weighted least squares, apply the weight to Aij, Bij, Cij.
The default implementation multiplies each matrix by weight.
Definition at line 542 of file vnl_sparse_lst_sqr_function.cxx.
|
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 301 of file vnl_sparse_lst_sqr_function.cxx.
|
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 324 of file vnl_sparse_lst_sqr_function.cxx.
|
inline |
Definition at line 114 of file vnl_sparse_lst_sqr_function.h.
|
virtual |
If using weighted least squares, compute the weight.
Return the weight in weight. The default implementation sets weight = 1
Definition at line 517 of file vnl_sparse_lst_sqr_function.cxx.
|
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 267 of file vnl_sparse_lst_sqr_function.cxx.
|
private |
Definition at line 13 of file vnl_sparse_lst_sqr_function.cxx.
|
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 155 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 391 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 431 of file vnl_sparse_lst_sqr_function.cxx.
|
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 229 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 471 of file vnl_sparse_lst_sqr_function.cxx.
|
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 346 of file vnl_sparse_lst_sqr_function.cxx.
|
inline |
Return true if the derived class has indicated that gradf has been implemented.
Definition at line 312 of file vnl_sparse_lst_sqr_function.h.
|
inline |
Return true if the derived class has indicated that.
apply_weights or apply_weight_ij have been implemented
Definition at line 316 of file vnl_sparse_lst_sqr_function.h.
|
inline |
return the index of aj in a.
Definition at line 294 of file vnl_sparse_lst_sqr_function.h.
|
inline |
return the index of bj in b.
Definition at line 297 of file vnl_sparse_lst_sqr_function.h.
|
inline |
return the index of ek in e.
Definition at line 300 of file vnl_sparse_lst_sqr_function.h.
|
virtual |
Calculate the Jacobian A_ij, given the parameter vectors a_i, b_j, and c.
Definition at line 357 of file vnl_sparse_lst_sqr_function.cxx.
|
virtual |
Calculate the Jacobian B_ij, given the parameter vectors a_i, b_j, and c.
Definition at line 368 of file vnl_sparse_lst_sqr_function.cxx.
|
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 190 of file vnl_sparse_lst_sqr_function.cxx.
|
virtual |
Calculate the Jacobian C_ij, given the parameter vectors a_i, b_j, and c.
Definition at line 379 of file vnl_sparse_lst_sqr_function.cxx.
|
inline |
Return the number of subsets in a.
Definition at line 303 of file vnl_sparse_lst_sqr_function.h.
|
inline |
Return the number of subsets in b.
Definition at line 306 of file vnl_sparse_lst_sqr_function.h.
|
inline |
Return the number of residual vectors.
Definition at line 309 of file vnl_sparse_lst_sqr_function.h.
|
inline |
Return the number of parameters of a_j.
Definition at line 274 of file vnl_sparse_lst_sqr_function.h.
|
inline |
Return the number of parameters of b_i.
Definition at line 277 of file vnl_sparse_lst_sqr_function.h.
|
inline |
Return the number of parameters of c.
Definition at line 280 of file vnl_sparse_lst_sqr_function.h.
|
inline |
Return the number of residuals in the kth residual vector.
Definition at line 283 of file vnl_sparse_lst_sqr_function.h.
|
inline |
Return the number of residuals for x_ij.
Definition at line 286 of file vnl_sparse_lst_sqr_function.h.
|
inline |
Return a const reference to the residual indexer.
Definition at line 319 of file vnl_sparse_lst_sqr_function.h.
|
inline |
Definition at line 113 of file vnl_sparse_lst_sqr_function.h.
|
virtual |
Called after each LM iteration to print debugging etc.
Definition at line 554 of file vnl_sparse_lst_sqr_function.cxx.
| bool vnl_sparse_lst_sqr_function::failure |
Definition at line 56 of file vnl_sparse_lst_sqr_function.h.
|
protected |
Definition at line 323 of file vnl_sparse_lst_sqr_function.h.
|
protected |
Definition at line 324 of file vnl_sparse_lst_sqr_function.h.
|
protected |
Definition at line 326 of file vnl_sparse_lst_sqr_function.h.
|
protected |
Definition at line 325 of file vnl_sparse_lst_sqr_function.h.
|
protected |
Definition at line 322 of file vnl_sparse_lst_sqr_function.h.
|
protected |
Definition at line 328 of file vnl_sparse_lst_sqr_function.h.
|
protected |
Definition at line 329 of file vnl_sparse_lst_sqr_function.h.
1.8.15