testGaussMarkov1stOrderFactor.cpp
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 
20 #include <gtsam/base/Vector.h>
22 #include <gtsam/inference/Key.h>
23 #include <gtsam/nonlinear/Values.h>
25 
26 using namespace std::placeholders;
27 using namespace std;
28 using namespace gtsam;
29 
32 
33 /* ************************************************************************* */
35  const GaussMarkovFactor factor) {
36  return factor.evaluateError(v1, v2);
37 }
38 
39 /* ************************************************************************* */
41 {
42  // Create two identical factors and make sure they're equal
43  Key x1(1);
44  Key x2(2);
45  double delta_t = 0.10;
46  Vector tau = Vector3(100.0, 150.0, 10.0);
47  SharedGaussian model = noiseModel::Isotropic::Sigma(3, 1.0);
48 
49  GaussMarkovFactor factor1(x1, x2, delta_t, tau, model);
50  GaussMarkovFactor factor2(x1, x2, delta_t, tau, model);
51 
53 }
54 
55 /* ************************************************************************* */
57 {
58  Values linPoint;
59  Key x1(1);
60  Key x2(2);
61  double delta_t = 0.10;
62  Vector3 tau(100.0, 150.0, 10.0);
63  SharedGaussian model = noiseModel::Isotropic::Sigma(3, 1.0);
64 
65  Vector3 v1(10.0, 12.0, 13.0);
66  Vector3 v2(10.0, 15.0, 14.0);
67 
68  // Create two nodes
69  linPoint.insert(x1, v1);
70  linPoint.insert(x2, v2);
71 
72  GaussMarkovFactor factor(x1, x2, delta_t, tau, model);
73  Vector3 error1 = factor.evaluateError(v1, v2);
74 
75  // Manually calculate the error
76  Vector3 alpha(tau.size());
77  Vector3 alpha_v1(tau.size());
78  for(int i=0; i<tau.size(); i++){
79  alpha(i) = exp(- 1/tau(i)*delta_t );
80  alpha_v1(i) = alpha(i) * v1(i);
81  }
82  Vector3 error2 = v2 - alpha_v1;
83 
84  CHECK(assert_equal(error1, error2, 1e-8));
85 }
86 
87 /* ************************************************************************* */
88 TEST (GaussMarkovFactor, jacobian ) {
89 
90  Values linPoint;
91  Key x1(1);
92  Key x2(2);
93  double delta_t = 0.10;
94  Vector3 tau(100.0, 150.0, 10.0);
95  SharedGaussian model = noiseModel::Isotropic::Sigma(3, 1.0);
96 
97  GaussMarkovFactor factor(x1, x2, delta_t, tau, model);
98 
99  // Update the linearization point
100  Vector3 v1_upd(0.5, -0.7, 0.3);
101  Vector3 v2_upd(-0.7, 0.4, 0.9);
102 
103  // Calculate the Jacobian matrix using the factor
104  Matrix computed_H1, computed_H2;
105  factor.evaluateError(v1_upd, v2_upd, computed_H1, computed_H2);
106 
107  // Calculate the Jacobian matrices H1 and H2 using the numerical derivative function
108  Matrix numerical_H1, numerical_H2;
109  numerical_H1 = numericalDerivative21<Vector3, Vector3, Vector3>(
110  std::bind(&predictionError, std::placeholders::_1, std::placeholders::_2,
111  factor),
112  v1_upd, v2_upd);
113  numerical_H2 = numericalDerivative22<Vector3, Vector3, Vector3>(
114  std::bind(&predictionError, std::placeholders::_1, std::placeholders::_2,
115  factor),
116  v1_upd, v2_upd);
117 
118  // Verify they are equal for this choice of state
119  CHECK(assert_equal(numerical_H1, computed_H1, 1e-9));
120  CHECK(assert_equal(numerical_H2, computed_H2, 1e-9));
121 }
122 
123 /* ************************************************************************* */
124 int main()
125 {
126  TestResult tr; return TestRegistry::runAllTests(tr);
127 }
128 /* ************************************************************************* */
129 
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
Vector.h
typedef and functions to augment Eigen's VectorXd
simple_graph::factor2
auto factor2
Definition: testJacobianFactor.cpp:203
alpha
RealScalar alpha
Definition: level1_cplx_impl.h:147
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
GaussMarkovFactor
GaussMarkov1stOrderFactor< Vector3 > GaussMarkovFactor
Factors.
Definition: testGaussMarkov1stOrderFactor.cpp:31
TestHarness.h
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
gtsam::Vector3
Eigen::Vector3d Vector3
Definition: Vector.h:43
exp
const EIGEN_DEVICE_FUNC ExpReturnType exp() const
Definition: ArrayCwiseUnaryOps.h:97
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
GaussMarkov1stOrderFactor.h
Key.h
numericalDerivative.h
Some functions to compute numerical derivatives.
x1
Pose3 x1
Definition: testPose3.cpp:663
gtsam::SharedGaussian
noiseModel::Gaussian::shared_ptr SharedGaussian
Definition: NoiseModel.h:763
main
int main()
Definition: testGaussMarkov1stOrderFactor.cpp:124
model
noiseModel::Diagonal::shared_ptr model
Definition: doc/Code/Pose2SLAMExample.cpp:7
gtsam::equals
Definition: Testable.h:112
TestResult
Definition: TestResult.h:26
predictionError
Vector predictionError(const Vector &v1, const Vector &v2, const GaussMarkovFactor factor)
Definition: testGaussMarkov1stOrderFactor.cpp:34
TEST
TEST(GaussMarkovFactor, equals)
Definition: testGaussMarkov1stOrderFactor.cpp:40
gtsam::GaussMarkov1stOrderFactor::evaluateError
Vector evaluateError(const VALUE &p1, const VALUE &p2, OptionalMatrixType H1, OptionalMatrixType H2) const override
Definition: GaussMarkov1stOrderFactor.h:93
gtsam
traits
Definition: SFMdata.h:40
error
static double error
Definition: testRot3.cpp:37
gtsam::Values
Definition: Values.h:65
CHECK
#define CHECK(condition)
Definition: Test.h:108
std
Definition: BFloat16.h:88
v2
Vector v2
Definition: testSerializationBase.cpp:39
gtsam::Values::insert
void insert(Key j, const Value &val)
Definition: Values.cpp:155
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
gtsam::GaussMarkov1stOrderFactor
Definition: GaussMarkov1stOrderFactor.h:45
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
x2
Pose3 x2(Rot3::Ypr(0.0, 0.0, 0.0), l2)
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
simple_graph::factor1
auto factor1
Definition: testJacobianFactor.cpp:196
Values.h
A non-templated config holding any types of Manifold-group elements.
v1
Vector v1
Definition: testSerializationBase.cpp:38


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:30