mirror of
https://github.com/ceres-solver/ceres-solver.git
synced 2026-08-30 00:50:37 +08:00
Fix 3+ nested Jet constructor
In Jet types, there is an attempt to set the derivative to 0. However, if the derivative is not directly a scalar but a Jet<Jet>, this cannot be constructed directly from a 0 literal. This is solved by using the default constructor for the scalar type instead of 0. The typical use case for this is adding constraints to the second derivative of a curve in an Autodiff cost function. Change-Id: Id480096632a731f312be12e294b3d6e244211529
This commit is contained in:
+3
-3
@@ -179,18 +179,18 @@ struct Jet {
|
||||
// (where T is a Jet<T, N>). This usually only happens in opt mode. Note that
|
||||
// the C++ standard mandates that e.g. default constructed doubles are
|
||||
// initialized to 0.0; see sections 8.5 of the C++03 standard.
|
||||
Jet() : a() { v.setZero(); }
|
||||
Jet() : a() { v.setConstant(Scalar()); }
|
||||
|
||||
// Constructor from scalar: a + 0.
|
||||
explicit Jet(const T& value) {
|
||||
a = value;
|
||||
v.setZero();
|
||||
v.setConstant(Scalar());
|
||||
}
|
||||
|
||||
// Constructor from scalar plus variable: a + t_i.
|
||||
Jet(const T& value, int k) {
|
||||
a = value;
|
||||
v.setZero();
|
||||
v.setConstant(Scalar());
|
||||
v[k] = T(1.0);
|
||||
}
|
||||
|
||||
|
||||
@@ -899,5 +899,28 @@ TEST(JetTraitsTest, ArrayScalarBinaryOps) {
|
||||
ExpectJetsClose(r4(1), r4(1));
|
||||
}
|
||||
|
||||
TEST(Jet, nested3x) {
|
||||
typedef Jet<J,2> JJ;
|
||||
typedef Jet<JJ,2> JJJ;
|
||||
|
||||
JJJ x;
|
||||
x.a = JJ(J(1, 0), 0);
|
||||
x.v[0] = JJ(J(1));
|
||||
|
||||
JJJ y = x * x * x;
|
||||
|
||||
ExpectClose(y.a.a.a, 1, kTolerance);
|
||||
ExpectClose(y.v[0].a.a, 3., kTolerance);
|
||||
ExpectClose(y.v[0].v[0].a, 6., kTolerance);
|
||||
ExpectClose(y.v[0].v[0].v[0], 6., kTolerance);
|
||||
|
||||
JJJ e = exp(x);
|
||||
|
||||
ExpectClose(e.a.a.a, kE, kTolerance);
|
||||
ExpectClose(e.v[0].a.a, kE, kTolerance);
|
||||
ExpectClose(e.v[0].v[0].a, kE, kTolerance);
|
||||
ExpectClose(e.v[0].v[0].v[0], kE, kTolerance);
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace ceres
|
||||
|
||||
Reference in New Issue
Block a user