29
29
// Author: sameeragarwal@google.com (Sameer Agarwal)
31
// Helpers for making CostFunctions as needed by the least squares framework,
32
// with Jacobians computed via automatic differentiation. For more information
33
// on automatic differentation, see the wikipedia article at
34
// http://en.wikipedia.org/wiki/Automatic_differentiation
31
// Create CostFunctions as needed by the least squares framework, with
32
// Jacobians computed via automatic differentiation. For more
33
// information on automatic differentation, see the wikipedia article
34
// at http://en.wikipedia.org/wiki/Automatic_differentiation
36
36
// To get an auto differentiated cost function, you must define a class with a
37
37
// templated operator() (a functor) that computes the cost function in terms of
57
57
// To write an auto-differentiable cost function for the above model, first
58
58
// define the object
60
// class MyScalarCostFunction {
61
// MyScalarCostFunction(double k): k_(k) {}
60
// class MyScalarCostFunctor {
61
// MyScalarCostFunctor(double k): k_(k) {}
63
63
// template <typename T>
64
64
// bool operator()(const T* const x , const T* const y, T* e) const {
80
80
// it can be constructed as follows.
82
82
// CostFunction* cost_function
83
// = new AutoDiffCostFunction<MyScalarCostFunction, 1, 2, 2>(
84
// new MyScalarCostFunction(1.0)); ^ ^ ^
86
// Dimension of residual ------+ | |
87
// Dimension of x ----------------+ |
88
// Dimension of y -------------------+
83
// = new AutoDiffCostFunction<MyScalarCostFunctor, 1, 2, 2>(
84
// new MyScalarCostFunctor(1.0)); ^ ^ ^
86
// Dimension of residual -----+ | |
87
// Dimension of x ---------------+ |
88
// Dimension of y ------------------+
90
90
// In this example, there is usually an instance for each measumerent of k.
92
92
// In the instantiation above, the template parameters following
93
// "MyScalarCostFunction", "1, 2, 2", describe the functor as computing a
93
// "MyScalarCostFunctor", "1, 2, 2", describe the functor as computing a
94
94
// 1-dimensional output from two arguments, both 2-dimensional.
96
96
// The autodiff cost function also supports cost functions with a
97
97
// runtime-determined number of residuals. For example:
99
99
// CostFunction* cost_function
100
// = new AutoDiffCostFunction<MyScalarCostFunction, DYNAMIC, 2, 2>(
101
// new CostFunctionWithDynamicNumResiduals(1.0), ^ ^ ^
102
// runtime_number_of_residuals); <----+ | | |
105
// Actual number of residuals ------+ | | |
106
// Indicate dynamic number of residuals ---------+ | |
107
// Dimension of x -------------------------------------+ |
108
// Dimension of y ----------------------------------------+
100
// = new AutoDiffCostFunction<MyScalarCostFunctor, DYNAMIC, 2, 2>(
101
// new CostFunctorWithDynamicNumResiduals(1.0), ^ ^ ^
102
// runtime_number_of_residuals); <----+ | | |
105
// Actual number of residuals ------+ | | |
106
// Indicate dynamic number of residuals --------+ | |
107
// Dimension of x ------------------------------------+ |
108
// Dimension of y ---------------------------------------+
110
110
// The framework can currently accommodate cost functions of up to 6 independent
111
111
// variables, and there is no limit on the dimensionality of each of them.
119
119
// functions is to get the sizing wrong. In particular, there is a tendency to
120
120
// set the template parameters to (dimension of residual, number of parameters)
121
121
// instead of passing a dimension parameter for *every parameter*. In the
122
// example above, that would be <MyScalarCostFunction, 1, 2>, which is missing
122
// example above, that would be <MyScalarCostFunctor, 1, 2>, which is missing
123
123
// the last '2' argument. Please be careful when setting the size parameters.
125
125
#ifndef CERES_PUBLIC_AUTODIFF_COST_FUNCTION_H_
154
154
int N2 = 0, // Number of parameters in block 2.
155
155
int N3 = 0, // Number of parameters in block 3.
156
156
int N4 = 0, // Number of parameters in block 4.
157
int N5 = 0> // Number of parameters in block 5.
158
class AutoDiffCostFunction :
159
public SizedCostFunction<M, N0, N1, N2, N3, N4, N5> {
157
int N5 = 0, // Number of parameters in block 5.
158
int N6 = 0, // Number of parameters in block 6.
159
int N7 = 0, // Number of parameters in block 7.
160
int N8 = 0, // Number of parameters in block 8.
161
int N9 = 0> // Number of parameters in block 9.
162
class AutoDiffCostFunction : public SizedCostFunction<M,
164
N5, N6, N7, N8, N9> {
161
166
// Takes ownership of functor. Uses the template-provided value for the
162
167
// number of residuals ("M").
163
168
explicit AutoDiffCostFunction(CostFunctor* functor)
164
169
: functor_(functor) {
165
170
CHECK_NE(M, DYNAMIC) << "Can't run the fixed-size constructor if the "
166
<< "number of residuals is set to ceres::DYNAMIC.";
171
<< "number of residuals is set to ceres::DYNAMIC.";
169
174
// Takes ownership of functor. Ignores the template-provided number of
174
179
AutoDiffCostFunction(CostFunctor* functor, int num_residuals)
175
180
: functor_(functor) {
176
181
CHECK_EQ(M, DYNAMIC) << "Can't run the dynamic-size constructor if the "
177
<< "number of residuals is not ceres::DYNAMIC.";
178
SizedCostFunction<M, N0, N1, N2, N3, N4, N5>::set_num_residuals(num_residuals);
182
<< "number of residuals is not ceres::DYNAMIC.";
183
SizedCostFunction<M, N0, N1, N2, N3, N4, N5, N6, N7, N8, N9>
184
::set_num_residuals(num_residuals);
181
187
virtual ~AutoDiffCostFunction() {}
190
196
double** jacobians) const {
191
197
if (!jacobians) {
192
198
return internal::VariadicEvaluate<
193
CostFunctor, double, N0, N1, N2, N3, N4, N5>
199
CostFunctor, double, N0, N1, N2, N3, N4, N5, N6, N7, N8, N9>
194
200
::Call(*functor_, parameters, residuals);
196
202
return internal::AutoDiff<CostFunctor, double,
197
N0, N1, N2, N3, N4, N5>::Differentiate(
203
N0, N1, N2, N3, N4, N5, N6, N7, N8, N9>::Differentiate(
200
SizedCostFunction<M, N0, N1, N2, N3, N4, N5>::num_residuals(),
206
SizedCostFunction<M, N0, N1, N2, N3, N4, N5, N6, N7, N8, N9>