blob: 1837df4af3cff13d37a7ff0625794c4581aaaef7 [file] [log] [blame]
Sameer Agarwal2fc0ed62013-01-15 11:34:10 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2013 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30// mierle@gmail.com (Keir Mierle)
31//
32// Finite differencing routine used by NumericDiffCostFunction.
33
34#ifndef CERES_PUBLIC_INTERNAL_NUMERIC_DIFF_H_
35#define CERES_PUBLIC_INTERNAL_NUMERIC_DIFF_H_
36
37#include <cstring>
38#include <glog/logging.h>
39#include "Eigen/Dense"
40#include "ceres/internal/scoped_ptr.h"
41#include "ceres/cost_function.h"
42#include "ceres/internal/variadic_evaluate.h"
43#include "ceres/types.h"
44#include "ceres/cost_function.h"
45
46namespace ceres {
47namespace internal {
48
49// Helper templates that allow evaluation of a variadic functor or a
50// CostFunction object.
51template <typename CostFunctor,
52 int N0, int N1, int N2, int N3, int N4,
53 int N5, int N6, int N7, int N8, int N9 >
54bool EvaluateImpl(const CostFunctor* functor,
55 double const* const* parameters,
56 double* residuals,
57 const void* /* NOT USED */) {
58 return VariadicEvaluate<CostFunctor,
59 double,
60 N0, N1, N2, N3, N4, N5, N6, N7, N8, N9>::Call(
61 *functor,
62 parameters,
63 residuals);
64}
65
66template <typename CostFunctor,
67 int N0, int N1, int N2, int N3, int N4,
68 int N5, int N6, int N7, int N8, int N9 >
69bool EvaluateImpl(const CostFunctor* functor,
70 double const* const* parameters,
71 double* residuals,
72 const CostFunction* /* NOT USED */) {
73 return functor->Evaluate(parameters, residuals, NULL);
74}
75
76// This is split from the main class because C++ doesn't allow partial template
77// specializations for member functions. The alternative is to repeat the main
78// class for differing numbers of parameters, which is also unfortunate.
79template <typename CostFunctor,
80 NumericDiffMethod kMethod,
81 int kNumResiduals,
82 int N0, int N1, int N2, int N3, int N4,
83 int N5, int N6, int N7, int N8, int N9,
84 int kParameterBlock,
85 int kParameterBlockSize>
86struct NumericDiff {
87 // Mutates parameters but must restore them before return.
88 static bool EvaluateJacobianForParameterBlock(
89 const CostFunctor* functor,
90 double const* residuals_at_eval_point,
91 const double relative_step_size,
92 double **parameters,
93 double *jacobian) {
94 using Eigen::Map;
95 using Eigen::Matrix;
96 using Eigen::RowMajor;
97 using Eigen::ColMajor;
98
99 typedef Matrix<double, kNumResiduals, 1> ResidualVector;
100 typedef Matrix<double, kParameterBlockSize, 1> ParameterVector;
101 typedef Matrix<double, kNumResiduals, kParameterBlockSize,
102 (kParameterBlockSize == 1 &&
103 kNumResiduals > 1) ? ColMajor : RowMajor> JacobianMatrix;
104
105
106 Map<JacobianMatrix> parameter_jacobian(jacobian,
107 kNumResiduals,
108 kParameterBlockSize);
109
110 // Mutate 1 element at a time and then restore.
111 Map<ParameterVector> x_plus_delta(parameters[kParameterBlock],
112 kParameterBlockSize);
113 ParameterVector x(x_plus_delta);
114 ParameterVector step_size = x.array().abs() * relative_step_size;
115
116 // To handle cases where a parameter is exactly zero, instead use
117 // the mean step_size for the other dimensions. If all the
118 // parameters are zero, there's no good answer. Take
119 // relative_step_size as a guess and hope for the best.
120 const double fallback_step_size =
121 (step_size.sum() == 0)
122 ? relative_step_size
123 : step_size.sum() / step_size.rows();
124
125 // For each parameter in the parameter block, use finite differences to
126 // compute the derivative for that parameter.
127 for (int j = 0; j < kParameterBlockSize; ++j) {
128 const double delta =
129 (step_size(j) == 0.0) ? fallback_step_size : step_size(j);
130
131 x_plus_delta(j) = x(j) + delta;
132
133 double residuals[kNumResiduals]; // NOLINT
134
135 if (!EvaluateImpl<CostFunctor, N0, N1, N2, N3, N4, N5, N6, N7, N8, N9>(
136 functor, parameters, residuals, functor)) {
137 return false;
138 }
139
140 // Compute this column of the jacobian in 3 steps:
141 // 1. Store residuals for the forward part.
142 // 2. Subtract residuals for the backward (or 0) part.
143 // 3. Divide out the run.
144 parameter_jacobian.col(j) =
145 Map<const ResidualVector>(residuals, kNumResiduals);
146
147 double one_over_delta = 1.0 / delta;
148 if (kMethod == CENTRAL) {
149 // Compute the function on the other side of x(j).
150 x_plus_delta(j) = x(j) - delta;
151
152 if (!EvaluateImpl<CostFunctor, N0, N1, N2, N3, N4, N5, N6, N7, N8, N9>(
153 functor, parameters, residuals, functor)) {
154 return false;
155 }
156
157 parameter_jacobian.col(j) -=
158 Map<ResidualVector>(residuals, kNumResiduals, 1);
159 one_over_delta /= 2;
160 } else {
161 // Forward difference only; reuse existing residuals evaluation.
162 parameter_jacobian.col(j) -=
163 Map<const ResidualVector>(residuals_at_eval_point, kNumResiduals);
164 }
165 x_plus_delta(j) = x(j); // Restore x_plus_delta.
166
167 // Divide out the run to get slope.
168 parameter_jacobian.col(j) *= one_over_delta;
169 }
170 return true;
171 }
172};
173
174template <typename CostFunctor,
175 NumericDiffMethod kMethod,
176 int kNumResiduals,
177 int N0, int N1, int N2, int N3, int N4,
178 int N5, int N6, int N7, int N8, int N9,
179 int kParameterBlock>
180struct NumericDiff<CostFunctor, kMethod, kNumResiduals,
181 N0, N1, N2, N3, N4, N5, N6, N7, N8, N9,
182 kParameterBlock, 0> {
183 // Mutates parameters but must restore them before return.
184 static bool EvaluateJacobianForParameterBlock(
185 const CostFunctor* functor,
186 double const* residuals_at_eval_point,
187 const double relative_step_size,
188 double **parameters,
189 double *jacobian) {
190 LOG(FATAL) << "Control should never reach here.";
191 return true;
192 }
193};
194
195} // namespace internal
196} // namespace ceres
197
198#endif // CERES_PUBLIC_INTERNAL_NUMERIC_DIFF_H_