gtsam
Loading...
Searching...
No Matches
factorTesting.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
19
20#pragma once
21
23#include <string>
24#include <vector>
25
26namespace gtsam {
27
39 const Values& values,
40 double delta = 1e-5) {
41 // We will fill a vector of key/Jacobians pairs (a map would sort)
42 std::vector<std::pair<Key, Matrix> > jacobians;
43
44 // Get size
45 const Vector e = factor.whitenedError(values);
46 const size_t rows = e.size();
47
48 // Loop over all variables
49 const double one_over_2delta = 1.0 / (2.0 * delta);
50 for (Key key : factor) {
51 // Compute central differences using the values struct.
52 VectorValues dX = values.zeroVectors();
53 const size_t cols = dX.dim(key);
54 Matrix J = Matrix::Zero(rows, cols);
55 for (size_t col = 0; col < cols; ++col) {
56 Vector dx = Vector::Zero(cols);
57 dx(col) = delta;
58 dX[key] = dx;
59 Values eval_values = values.retract(dX);
60 const Vector left = factor.whitenedError(eval_values);
61 dx(col) = -delta;
62 dX[key] = dx;
63 eval_values = values.retract(dX);
64 const Vector right = factor.whitenedError(eval_values);
65 J.col(col) = (left - right) * one_over_2delta;
66 }
67 jacobians.emplace_back(key, J);
68 }
69
70 // Next step...return JacobianFactor
71 return JacobianFactor(jacobians, -e);
72}
73
74namespace internal {
75// CPPUnitLite-style test for linearization of a factor
76inline bool testFactorJacobians(const std::string& name_,
77 const NoiseModelFactor& factor,
78 const gtsam::Values& values, double delta,
79 double tolerance) {
80 // Create expected value by numerical differentiation
81 JacobianFactor expected = linearizeNumerically(factor, values, delta);
82
83 // Create actual value by linearize
84 auto actual =
85 std::dynamic_pointer_cast<JacobianFactor>(factor.linearize(values));
86 if (!actual) return false;
87
88 // Check cast result and then equality
89 bool equal = assert_equal(expected, *actual, tolerance);
90
91 // if not equal, test individual jacobians:
92 if (!equal) {
93 for (size_t i = 0; i < actual->size(); i++) {
94 bool i_good =
95 assert_equal((Matrix)(expected.getA(expected.begin() + i)),
96 (Matrix)(actual->getA(actual->begin() + i)), tolerance);
97 if (!i_good) {
98 std::cout << "Mismatch in Jacobian " << i + 1
99 << " (base 1), as shown above" << std::endl;
100 }
101 }
102 }
103
104 return equal;
105}
106} // namespace internal
107
113#define EXPECT_CORRECT_FACTOR_JACOBIANS(factor, values, numerical_derivative_step, tolerance) \
114 { EXPECT(gtsam::internal::testFactorJacobians(name_, factor, values, numerical_derivative_step, tolerance)); }
115
116} // namespace gtsam
Non-linear factor base classes.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition Matrix.cpp:39
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:38
bool equal(const T &obj1, const T &obj2, double tol)
Call equal on the object.
Definition Testable.h:85
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
A Gaussian factor in the squared-error form.
Definition JacobianFactor.h:92
VectorValues represents a collection of vector-valued variables associated each with a unique integer...
Definition VectorValues.h:73
size_t dim(Key j) const
Return the dimension of variable j.
Definition VectorValues.h:132
A nonlinear sum-of-squares factor with a zero-mean noise model implementing the density Templated on...
Definition NonlinearFactor.h:208
Vector whitenedError(const Values &c) const
Vector of errors, whitened This is the raw error, i.e., i.e.
Definition NonlinearFactor.cpp:117
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
Values retract(const VectorValues &delta) const
Add a delta config to current config and returns a new config.
Definition Values.cpp:100
VectorValues zeroVectors() const
Return a VectorValues of zero vectors for each variable in this Values.
Definition Values.cpp:284
In Gaussian factors, the error function returns either the negative log-likelihood,...
noise model to the factor, and calculates the error by asking the user to implement the method