Skip to content

Commit 1a740cc

Browse files
committed
Add the option to use numeric differentiation to nist and more_garbow_hillstrom
Change-Id: If0a5caef90b524dcf5e2567c5b681987f5459401
1 parent ea667ed commit 1a740cc

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

examples/more_garbow_hillstrom.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
#include "glog/logging.h"
6161

6262
DEFINE_string(problem, "all", "Which problem to solve");
63+
DEFINE_bool(use_numeric_diff, false,
64+
"Use numeric differentiation instead of automatic "
65+
"differentiation.");
66+
6367

6468
namespace ceres {
6569
namespace examples {
@@ -75,9 +79,16 @@ const double kDoubleMax = std::numeric_limits<double>::max();
7579
static const double constrained_optimal_cost; \
7680
static const double unconstrained_optimal_cost; \
7781
static CostFunction* Create() { \
78-
return new AutoDiffCostFunction<name, \
79-
num_residuals, \
80-
num_parameters>(new name); \
82+
if (FLAGS_use_numeric_diff) { \
83+
return new NumericDiffCostFunction<name, \
84+
CENTRAL, \
85+
num_residuals, \
86+
num_parameters>(new name); \
87+
} else { \
88+
return new AutoDiffCostFunction<name, \
89+
num_residuals, \
90+
num_parameters>(new name); \
91+
} \
8192
} \
8293
template <typename T> \
8394
bool operator()(const T* const x, T* residual) const {

examples/nist.cc

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ DEFINE_int32(num_iterations, 10000, "Number of iterations");
115115
DEFINE_bool(nonmonotonic_steps, false, "Trust region algorithm can use"
116116
" nonmonotic steps");
117117
DEFINE_double(initial_trust_region_radius, 1e4, "Initial trust region radius");
118+
DEFINE_bool(use_numeric_diff, false,
119+
"Use numeric differentiation instead of automatic "
120+
"differentiation.");
118121

119122
namespace ceres {
120123
namespace examples {
@@ -431,12 +434,25 @@ int RegressionDriver(const string& filename,
431434

432435
ceres::Problem problem;
433436
for (int i = 0; i < nist_problem.num_observations(); ++i) {
434-
problem.AddResidualBlock(
435-
new ceres::AutoDiffCostFunction<Model, num_residuals, num_parameters>(
436-
new Model(predictor.data() + nist_problem.predictor_size() * i,
437-
response.data() + nist_problem.response_size() * i)),
438-
NULL,
439-
initial_parameters.data());
437+
Model* model = new Model(predictor.data() + nist_problem.predictor_size() * i,
438+
response.data() + nist_problem.response_size() * i);
439+
ceres::CostFunction* cost_function = NULL;
440+
if (FLAGS_use_numeric_diff) {
441+
cost_function =
442+
new ceres::NumericDiffCostFunction<Model,
443+
ceres::CENTRAL,
444+
num_residuals,
445+
num_parameters>(model);
446+
} else {
447+
cost_function =
448+
new ceres::AutoDiffCostFunction<Model,
449+
num_residuals,
450+
num_parameters>(model);
451+
}
452+
453+
problem.AddResidualBlock(cost_function,
454+
NULL,
455+
initial_parameters.data());
440456
}
441457

442458
ceres::Solver::Summary summary;

0 commit comments

Comments
 (0)