38 const Values& values,
double delta = 1e-5) {
41 std::vector<std::pair<Key, Matrix> > jacobians;
45 const size_t rows = e.size();
48 const double one_over_2delta = 1.0 / (2.0 * delta);
50 for(
Key key: factor) {
52 const size_t cols = dX.
dim(key);
53 Matrix J = Matrix::Zero(rows, cols);
54 for (
size_t col = 0; col < cols; ++col) {
55 Eigen::VectorXd dx = Eigen::VectorXd::Zero(cols);
59 const Eigen::VectorXd left = factor.whitenedError(eval_values);
62 eval_values = values.
retract(dX);
63 const Eigen::VectorXd right = factor.whitenedError(eval_values);
64 J.col(col) = (left - right) * one_over_2delta;
66 jacobians.push_back(std::make_pair(key,J));
75 bool testFactorJacobians(
const std::string& name_,
76 const NoiseModelFactor& factor,
const gtsam::Values& values,
double delta,
83 boost::shared_ptr<JacobianFactor> actual =
84 boost::dynamic_pointer_cast<JacobianFactor>(factor.linearize(values));
85 if (!actual)
return false;
92 for (
size_t i = 0; i < actual->size(); i++) {
93 bool i_good =
assert_equal((Matrix) (expected.getA(expected.begin() + i)),
94 (Matrix) (actual->getA(actual->begin() + i)), tolerance);
96 std::cout <<
"Mismatch in Jacobian " << i+1 <<
" (base 1), as shown above" << std::endl;
110 #define EXPECT_CORRECT_FACTOR_JACOBIANS(factor, values, numerical_derivative_step, tolerance) \ 111 { EXPECT(gtsam::internal::testFactorJacobians(name_, factor, values, numerical_derivative_step, tolerance)); } A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:70
size_t dim(Key j) const
Return the dimension of variable j.
Definition: VectorValues.h:127
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition: Matrix.cpp:42
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
JacobianFactor linearizeNumerically(const NoiseModelFactor &factor, const Values &values, double delta=1e-5)
Linearize a nonlinear factor using numerical differentiation The benefit of this method is that it do...
Definition: factorTesting.h:37
Vector whitenedError(const Values &c) const
Vector of errors, whitened This is the raw error, i.e., i.e.
Definition: NonlinearFactor.cpp:90
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:73
A nonlinear sum-of-squares factor with a zero-mean noise model implementing the density Templated on...
Definition: NonlinearFactor.h:161
Non-linear factor base classes.
Values retract(const VectorValues &delta) const
Add a delta config to current config and returns a new config.
Definition: Values.cpp:102
A Gaussian factor in the squared-error form.
Definition: JacobianFactor.h:87
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
bool equal(const T &obj1, const T &obj2, double tol)
Call equal on the object.
Definition: Testable.h:83
Some functions to compute numerical derivatives.
VectorValues zeroVectors() const
Return a VectorValues of zero vectors for each variable in this Values.
Definition: Values.cpp:209