Skip to content

Commit

Permalink
Remove spurious conversion from doubles to Jets.
Browse files Browse the repository at this point in the history
Binary operations between Jets and doubles are well defined
and should not require an explicit conversion to Jets to work.
This was an oversight earlier and lead to overzealous conversions
all over our in our example code.

Change-Id: I1799770818e136edfc0a5802d86037ce9aec4923
  • Loading branch information
sandwichmaker committed Feb 10, 2017
1 parent 5419684 commit d05515b
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 132 deletions.
16 changes: 2 additions & 14 deletions docs/source/nnls_modeling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 223,7 @@ the corresponding accessors. This information will be verified by the

template <typename T>
bool operator()(const T* const x , const T* const y, T* e) const {
e[0] = T(k_) - x[0] * y[0] - x[1] * y[1];
e[0] = k_ - x[0] * y[0] - x[1] * y[1];
return true;
}

Expand Down Expand Up @@ -281,12 281,6 @@ the corresponding accessors. This information will be verified by the
independent variables, and there is no limit on the dimensionality
of each of them.

**WARNING 1** Since the functor will get instantiated with
different types for ``T``, you must convert from other numeric
types to ``T`` before mixing computations with other variables
of type ``T``. In the example above, this is seen where instead of
using ``k_`` directly, ``k_`` is wrapped with ``T(k_)``.

**WARNING 2** A common beginner's error when first using
:class:`AutoDiffCostFunction` is to get the sizing wrong. In particular,
there is a tendency to set the template parameters to (dimension of
Expand Down Expand Up @@ -1420,7 1414,7 @@ Instances
delta[0] * delta[0] delta[1] * delta[1] delta[2] * delta[2];

T q_delta[4];
if (squared_norm_delta > T(0.0)) {
if (squared_norm_delta > 0.0) {
T norm_delta = sqrt(squared_norm_delta);
const T sin_delta_by_delta = sin(norm_delta) / norm_delta;
q_delta[0] = cos(norm_delta);
Expand Down Expand Up @@ -1454,12 1448,6 @@ Instances
Global Size --------------- |
Local Size -------------------

**WARNING:** Since the functor will get instantiated with different
types for ``T``, you must to convert from other numeric types to
``T`` before mixing computations with other variables of type
``T``. In the example above, this is seen where instead of using
``k_`` directly, ``k_`` is wrapped with ``T(k_)``.


:class:`Problem`
================
Expand Down
2 changes: 1 addition & 1 deletion examples/curve_fitting.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 125,7 @@ struct ExponentialResidual {
template <typename T> bool operator()(const T* const m,
const T* const c,
T* residual) const {
residual[0] = T(y_) - exp(m[0] * T(x_) c[0]);
residual[0] = y_ - exp(m[0] * x_ c[0]);
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions examples/ellipse_approximation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 342,8 @@ struct EuclideanDistanceFunctor {

template <typename T>
bool operator()(const T* x0, const T* x1, T* residuals) const {
residuals[0] = T(sqrt_weight_) * (x0[0] - x1[0]);
residuals[1] = T(sqrt_weight_) * (x0[1] - x1[1]);
residuals[0] = sqrt_weight_ * (x0[0] - x1[0]);
residuals[1] = sqrt_weight_ * (x0[1] - x1[1]);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/helloworld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 48,7 @@ using ceres::Solve;
// derivatives.
struct CostFunctor {
template <typename T> bool operator()(const T* const x, T* residual) const {
residual[0] = T(10.0) - x[0];
residual[0] = 10.0 - x[0];
return true;
}
};
Expand Down
10 changes: 5 additions & 5 deletions examples/libmv_bundle_adjuster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 471,9 @@ inline void ApplyRadialDistortionCameraIntrinsics(const T &focal_length_x,
T r2 = x*x y*y;
T r4 = r2 * r2;
T r6 = r4 * r2;
T r_coeff = (T(1) k1*r2 k2*r4 k3*r6);
T xd = x * r_coeff T(2)*p1*x*y p2*(r2 T(2)*x*x);
T yd = y * r_coeff T(2)*p2*x*y p1*(r2 T(2)*y*y);
T r_coeff = 1.0 k1 * r2 k2 * r4 k3 * r6;
T xd = x * r_coeff 2.0 * p1 * x * y p2 * (r2 2.0 * x * x);
T yd = y * r_coeff 2.0 * p2 * x * y p1 * (r2 2.0 * y * y);

// Apply focal length and principal point to get the final image coordinates.
*image_x = focal_length_x * xd principal_point_x;
Expand Down Expand Up @@ -533,8 533,8 @@ struct OpenCVReprojectionError {
&predicted_y);

// The error is the difference between the predicted and observed position.
residuals[0] = predicted_x - T(observed_x);
residuals[1] = predicted_y - T(observed_y);
residuals[0] = predicted_x - observed_x;
residuals[1] = predicted_y - observed_y;

return true;
}
Expand Down
124 changes: 59 additions & 65 deletions examples/more_garbow_hillstrom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 127,8 @@ static void SetNumericDiffOptions(ceres::NumericDiffOptions* options) {
BEGIN_MGH_PROBLEM(TestProblem1, 2, 2)
const T x1 = x[0];
const T x2 = x[1];
residual[0] = T(10.0) * (x2 - x1 * x1);
residual[1] = T(1.0) - x1;
residual[0] = 10.0 * (x2 - x1 * x1);
residual[1] = 1.0 - x1;
END_MGH_PROBLEM;

const double TestProblem1::initial_x[] = {-1.2, 1.0};
Expand All @@ -142,8 142,8 @@ const double TestProblem1::unconstrained_optimal_cost = 0.0;
BEGIN_MGH_PROBLEM(TestProblem2, 2, 2)
const T x1 = x[0];
const T x2 = x[1];
residual[0] = T(-13.0) x1 ((T(5.0) - x2) * x2 - T(2.0)) * x2;
residual[1] = T(-29.0) x1 ((x2 T(1.0)) * x2 - T(14.0)) * x2;
residual[0] = -13.0 x1 ((5.0 - x2) * x2 - 2.0) * x2;
residual[1] = -29.0 x1 ((x2 1.0) * x2 - 14.0) * x2;
END_MGH_PROBLEM;

const double TestProblem2::initial_x[] = {0.5, -2.0};
Expand All @@ -157,8 157,8 @@ const double TestProblem2::unconstrained_optimal_cost = 0.0;
BEGIN_MGH_PROBLEM(TestProblem3, 2, 2)
const T x1 = x[0];
const T x2 = x[1];
residual[0] = T(10000.0) * x1 * x2 - T(1.0);
residual[1] = exp(-x1) exp(-x2) - T(1.0001);
residual[0] = 10000.0 * x1 * x2 - 1.0;
residual[1] = exp(-x1) exp(-x2) - 1.0001;
END_MGH_PROBLEM;

const double TestProblem3::initial_x[] = {0.0, 1.0};
Expand All @@ -171,9 171,9 @@ const double TestProblem3::unconstrained_optimal_cost = 0.0;
BEGIN_MGH_PROBLEM(TestProblem4, 2, 3)
const T x1 = x[0];
const T x2 = x[1];
residual[0] = x1 - T(1000000.0);
residual[1] = x2 - T(0.000002);
residual[2] = x1 * x2 - T(2.0);
residual[0] = x1 - 1000000.0;
residual[1] = x2 - 0.000002;
residual[2] = x1 * x2 - 2.0;
END_MGH_PROBLEM;

const double TestProblem4::initial_x[] = {1.0, 1.0};
Expand All @@ -186,9 186,9 @@ const double TestProblem4::unconstrained_optimal_cost = 0.0;
BEGIN_MGH_PROBLEM(TestProblem5, 2, 3)
const T x1 = x[0];
const T x2 = x[1];
residual[0] = T(1.5) - x1 * (T(1.0) - x2);
residual[1] = T(2.25) - x1 * (T(1.0) - x2 * x2);
residual[2] = T(2.625) - x1 * (T(1.0) - x2 * x2 * x2);
residual[0] = 1.5 - x1 * (1.0 - x2);
residual[1] = 2.25 - x1 * (1.0 - x2 * x2);
residual[2] = 2.625 - x1 * (1.0 - x2 * x2 * x2);
END_MGH_PROBLEM;

const double TestProblem5::initial_x[] = {1.0, 1.0};
Expand All @@ -202,9 202,9 @@ BEGIN_MGH_PROBLEM(TestProblem6, 2, 10)
const T x1 = x[0];
const T x2 = x[1];
for (int i = 1; i <= 10; i) {
residual[i - 1] = T(2.0) T(2.0 * i) -
(exp(T(static_cast<double>(i)) * x1)
exp(T(static_cast<double>(i) * x2)));
residual[i - 1] = 2.0 2.0 * i -
(exp(static_cast<double>(i) * x1)
exp(static_cast<double>(i) * x2));
}
END_MGH_PROBLEM;

Expand All @@ -220,9 220,9 @@ BEGIN_MGH_PROBLEM(TestProblem7, 3, 3)
const T x1 = x[0];
const T x2 = x[1];
const T x3 = x[2];
const T theta = T(0.5 / M_PI) * atan(x2 / x1) (x1 > 0.0 ? T(0.0) : T(0.5));
residual[0] = T(10.0) * (x3 - T(10.0) * theta);
residual[1] = T(10.0) * (sqrt(x1 * x1 x2 * x2) - T(1.0));
const T theta = (0.5 / M_PI) * atan(x2 / x1) (x1 > 0.0 ? 0.0 : 0.5);
residual[0] = 10.0 * (x3 - 10.0 * theta);
residual[1] = 10.0 * (sqrt(x1 * x1 x2 * x2) - 1.0);
residual[2] = x3;
END_MGH_PROBLEM;

Expand All @@ -243,10 243,10 @@ BEGIN_MGH_PROBLEM(TestProblem8, 3, 15)
0.73, 0.96, 1.34, 2.10, 4.39};

for (int i = 1; i <=15; i) {
const T u = T(static_cast<double>(i));
const T v = T(static_cast<double>(16 - i));
const T w = T(static_cast<double>(std::min(i, 16 - i)));
residual[i - 1] = T(y[i - 1]) - (x1 u / (v * x2 w * x3));
const double u = static_cast<double>(i);
const double v = static_cast<double>(16 - i);
const double w = static_cast<double>(std::min(i, 16 - i));
residual[i - 1] = y[i - 1] - (x1 u / (v * x2 w * x3));
}
END_MGH_PROBLEM;

Expand All @@ -269,9 269,8 @@ BEGIN_MGH_PROBLEM(TestProblem9, 3, 15)
0.3989,
0.3521, 0.2420, 0.1295, 0.0540, 0.0175, 0.0044, 0.0009};
for (int i = 0; i < 15; i) {
const T t_i = T((8.0 - i - 1.0) / 2.0);
const T y_i = T(y[i]);
residual[i] = x1 * exp(-x2 * (t_i - x3) * (t_i - x3) / T(2.0)) - y_i;
const double t_i = (8.0 - i - 1.0) / 2.0;
residual[i] = x1 * exp(-x2 * (t_i - x3) * (t_i - x3) / 2.0) - y[i];
}
END_MGH_PROBLEM;

Expand All @@ -291,9 290,8 @@ BEGIN_MGH_PROBLEM(TestProblem10, 3, 16)
8261, 7030, 6005, 5147, 4427, 3820, 3307, 2872};

for (int i = 0; i < 16; i) {
const T ti = T(45 5.0 * (i 1));
const T yi = T(y[i]);
residual[i] = x1 * exp(x2 / (ti x3)) - yi;
const double ti = 45.0 5.0 * (i 1);
residual[i] = x1 * exp(x2 / (ti x3)) - y[i];
}
END_MGH_PROBLEM

Expand All @@ -314,7 312,7 @@ BEGIN_MGH_PROBLEM(TestProblem11, 3, 100)
for (int i = 1; i <= 100; i) {
const double ti = static_cast<double>(i) / 100.0;
const double yi = 25.0 pow(-50.0 * log(ti), 2.0 / 3.0);
residual[i - 1] = exp(-pow(abs(T(yi * 100.0 * i) * x2), x3) / x1) - T(ti);
residual[i - 1] = exp(-pow(abs((yi * 100.0 * i) * x2), x3) / x1) - ti;
}
END_MGH_PROBLEM

Expand All @@ -330,13 328,13 @@ BEGIN_MGH_PROBLEM(TestProblem12, 3, 3)
const T x2 = x[1];
const T x3 = x[2];

const T t1 = T(0.1);
const T t2 = T(0.2);
const T t3 = T(0.3);
const double t1 = 0.1;
const double t2 = 0.2;
const double t3 = 0.3;

residual[0] = exp(-t1 * x1) - exp(-t1 * x2) - x3 * (exp(-t1) - exp(-T(10.0) * t1));
residual[1] = exp(-t2 * x1) - exp(-t2 * x2) - x3 * (exp(-t2) - exp(-T(10.0) * t2));
residual[2] = exp(-t3 * x1) - exp(-t3 * x2) - x3 * (exp(-t3) - exp(-T(10.0) * t3));
residual[0] = exp(-t1 * x1) - exp(-t1 * x2) - x3 * (exp(-t1) - exp(-10.0 * t1));
residual[1] = exp(-t2 * x1) - exp(-t2 * x2) - x3 * (exp(-t2) - exp(-10.0 * t2));
residual[2] = exp(-t3 * x1) - exp(-t3 * x2) - x3 * (exp(-t3) - exp(-10.0 * t3));
END_MGH_PROBLEM

const double TestProblem12::initial_x[] = {0.0, 10.0, 20.0};
Expand All @@ -352,9 350,9 @@ BEGIN_MGH_PROBLEM(TestProblem13, 4, 4)
const T x3 = x[2];
const T x4 = x[3];

residual[0] = x1 T(10.0) * x2;
residual[1] = T(sqrt(5.0)) * (x3 - x4);
residual[2] = (x2 - T(2.0) * x3) * (x2 - T(2.0) * x3);
residual[0] = x1 10.0 * x2;
residual[1] = sqrt(5.0) * (x3 - x4);
residual[2] = (x2 - 2.0 * x3) * (x2 - 2.0 * x3);
residual[3] = sqrt(10.0) * (x1 - x4) * (x1 - x4);
END_MGH_PROBLEM

Expand All @@ -374,22 372,22 @@ BEGIN_MGH_PROBLEM(TestProblem14, 4, 6)
const T x3 = x[2];
const T x4 = x[3];

residual[0] = T(10.0) * (x2 - x1 * x1);
residual[1] = T(1.0) - x1;
residual[2] = T(sqrt(90.0)) * (x4 - x3 * x3);
residual[3] = T(1.0) - x3;
residual[4] = T(sqrt(10.0)) * (x2 x4 - T(2.0));
residual[5] = T(1.0/sqrt(10.0)) * (x2 - x4);
residual[0] = 10.0 * (x2 - x1 * x1);
residual[1] = 1.0 - x1;
residual[2] = sqrt(90.0) * (x4 - x3 * x3);
residual[3] = 1.0 - x3;
residual[4] = sqrt(10.0) * (x2 x4 - 2.0);
residual[5] = 1.0 / sqrt(10.0) * (x2 - x4);
END_MGH_PROBLEM;

const double TestProblem14::initial_x[] = {-3.0, -1.0, -3.0, -1.0};
const double TestProblem14::lower_bounds[] = {-100.0, -100.0, -100.0, -100.0};
const double TestProblem14::upper_bounds[] = {0.0, 10.0, 100.0, 100.0};
const double TestProblem14::constrained_optimal_cost = 0.15567008e1;
const double TestProblem14::unconstrained_optimal_cost = 0.0;
const double TestProblem14::initial_x[] = {-3.0, -1.0, -3.0, -1.0};
const double TestProblem14::lower_bounds[] = {-100.0, -100.0, -100.0, -100.0};
const double TestProblem14::upper_bounds[] = {0.0, 10.0, 100.0, 100.0};
const double TestProblem14::constrained_optimal_cost = 0.15567008e1;
const double TestProblem14::unconstrained_optimal_cost = 0.0;

// Kowalik and Osborne function.
BEGIN_MGH_PROBLEM(TestProblem15, 4, 11)
// Kowalik and Osborne function.
BEGIN_MGH_PROBLEM(TestProblem15, 4, 11)
const T x1 = x[0];
const T x2 = x[1];
const T x3 = x[2];
Expand All @@ -401,9 399,7 @@ BEGIN_MGH_PROBLEM(TestProblem15, 4, 11)
0.0833, 0.0714, 0.0625};

for (int i = 0; i < 11; i) {
const T yi = T(y[i]);
const T ui = T(u[i]);
residual[i] = yi - x1 * (ui * ui ui * x2) / (ui * ui ui * x3 x4);
residual[i] = y[i] - x1 * (u[i] * u[i] u[i] * x2) / (u[i] * u[i] u[i] * x3 x4);
}
END_MGH_PROBLEM;

Expand All @@ -424,7 420,7 @@ BEGIN_MGH_PROBLEM(TestProblem16, 4, 20)
const T x4 = x[3];

for (int i = 0; i < 20; i) {
const T ti = T(static_cast<double>(i 1) / 5.0);
const double ti = static_cast<double>(i 1) / 5.0;
residual[i] = (x1 ti * x2 - exp(ti)) * (x1 ti * x2 - exp(ti))
(x3 x4 * sin(ti) - cos(ti)) * (x3 x4 * sin(ti) - cos(ti));
}
Expand All @@ -450,9 446,8 @@ BEGIN_MGH_PROBLEM(TestProblem17, 5, 33)
0.431, 0.424, 0.420, 0.414, 0.411, 0.406};

for (int i = 0; i < 33; i) {
const T yi = T(y[i]);
const T ti = T(10.0 * i);
residual[i] = yi - (x1 x2 * exp(-ti * x4) x3 * exp(-ti * x5));
const double ti = 10.0 * i;
residual[i] = y[i] - (x1 x2 * exp(-ti * x4) x3 * exp(-ti * x5));
}
END_MGH_PROBLEM;

Expand All @@ -477,8 472,7 @@ BEGIN_MGH_PROBLEM(TestProblem18, 6, 13)
for (int i = 0; i < 13; i) {
const double ti = 0.1 * (i 1.0);
const double yi = exp(-ti) - 5.0 * exp(-10.0 * ti) 3.0 * exp(-4.0 * ti);
const T si = T(ti);
residual[i] =x3 * exp(-si * x1) - x4 * exp(-si * x2) x6 * exp(-si * x5) - T(yi);
residual[i] =x3 * exp(-ti * x1) - x4 * exp(-ti * x2) x6 * exp(-ti * x5) - yi;
}
END_MGH_PROBLEM

Expand Down Expand Up @@ -517,11 511,11 @@ BEGIN_MGH_PROBLEM(TestProblem19, 11, 65)
0.428, 0.292, 0.162, 0.098, 0.054};

for (int i = 0; i < 65; i) {
const T ti = T(static_cast<double>(i) / 10.0);
residual[i] = T(y[i]) - (x1 * exp(-(ti * x5))
x2 * exp(-(ti - x9) * (ti - x9) * x6)
x3 * exp(-(ti - x10) * (ti - x10) * x7)
x4 * exp(-(ti - x11) * (ti - x11) * x8));
const double ti = static_cast<double>(i) / 10.0;
residual[i] = y[i] - (x1 * exp(-(ti * x5))
x2 * exp(-(ti - x9) * (ti - x9) * x6)
x3 * exp(-(ti - x10) * (ti - x10) * x7)
x4 * exp(-(ti - x11) * (ti - x11) * x8));
}
END_MGH_PROBLEM;

Expand Down
Loading

0 comments on commit d05515b

Please sign in to comment.