Add the option to use numeric differentiation to nist and more_garbow_hillstrom
Change-Id: If0a5caef90b524dcf5e2567c5b681987f5459401
diff --git a/examples/more_garbow_hillstrom.cc b/examples/more_garbow_hillstrom.cc
index 9f45040..b0ea78a 100644
--- a/examples/more_garbow_hillstrom.cc
+++ b/examples/more_garbow_hillstrom.cc
@@ -60,6 +60,10 @@
#include "glog/logging.h"
DEFINE_string(problem, "all", "Which problem to solve");
+DEFINE_bool(use_numeric_diff, false,
+ "Use numeric differentiation instead of automatic "
+ "differentiation.");
+
namespace ceres {
namespace examples {
@@ -75,9 +79,16 @@
static const double constrained_optimal_cost; \
static const double unconstrained_optimal_cost; \
static CostFunction* Create() { \
- return new AutoDiffCostFunction<name, \
- num_residuals, \
- num_parameters>(new name); \
+ if (FLAGS_use_numeric_diff) { \
+ return new NumericDiffCostFunction<name, \
+ CENTRAL, \
+ num_residuals, \
+ num_parameters>(new name); \
+ } else { \
+ return new AutoDiffCostFunction<name, \
+ num_residuals, \
+ num_parameters>(new name); \
+ } \
} \
template <typename T> \
bool operator()(const T* const x, T* residual) const {
diff --git a/examples/nist.cc b/examples/nist.cc
index 3d2e16a..81c3f2a 100644
--- a/examples/nist.cc
+++ b/examples/nist.cc
@@ -115,6 +115,9 @@
DEFINE_bool(nonmonotonic_steps, false, "Trust region algorithm can use"
" nonmonotic steps");
DEFINE_double(initial_trust_region_radius, 1e4, "Initial trust region radius");
+DEFINE_bool(use_numeric_diff, false,
+ "Use numeric differentiation instead of automatic "
+ "differentiation.");
namespace ceres {
namespace examples {
@@ -431,12 +434,25 @@
ceres::Problem problem;
for (int i = 0; i < nist_problem.num_observations(); ++i) {
- problem.AddResidualBlock(
- new ceres::AutoDiffCostFunction<Model, num_residuals, num_parameters>(
- new Model(predictor.data() + nist_problem.predictor_size() * i,
- response.data() + nist_problem.response_size() * i)),
- NULL,
- initial_parameters.data());
+ Model* model = new Model(predictor.data() + nist_problem.predictor_size() * i,
+ response.data() + nist_problem.response_size() * i);
+ ceres::CostFunction* cost_function = NULL;
+ if (FLAGS_use_numeric_diff) {
+ cost_function =
+ new ceres::NumericDiffCostFunction<Model,
+ ceres::CENTRAL,
+ num_residuals,
+ num_parameters>(model);
+ } else {
+ cost_function =
+ new ceres::AutoDiffCostFunction<Model,
+ num_residuals,
+ num_parameters>(model);
+ }
+
+ problem.AddResidualBlock(cost_function,
+ NULL,
+ initial_parameters.data());
}
ceres::Solver::Summary summary;