gtsam
Loading...
Searching...
No Matches
JacobianFactor.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#pragma once
20
26
27#if GTSAM_ENABLE_BOOST_SERIALIZATION
28#include <boost/serialization/version.hpp>
29#include <boost/serialization/split_member.hpp>
30#endif
31
32namespace gtsam {
33
34 // Forward declarations
35 class HessianFactor;
36 class VariableSlots;
39 class HessianFactor;
40 class VectorValues;
41 class Ordering;
42 class JacobianFactor;
43
49 GTSAM_EXPORT std::pair<std::shared_ptr<GaussianConditional>, std::shared_ptr<JacobianFactor> >
50 EliminateQR(const GaussianFactorGraph& factors, const Ordering& keys);
51
91 class GTSAM_EXPORT JacobianFactor : public GaussianFactor
92 {
93 public:
94
97 typedef std::shared_ptr<This> shared_ptr;
98
99 typedef VerticalBlockMatrix::Block ABlock;
100 typedef VerticalBlockMatrix::constBlock constABlock;
101 typedef ABlock::ColXpr BVector;
102 typedef constABlock::ConstColXpr constBVector;
103
104 protected:
105
106 VerticalBlockMatrix Ab_; // the block view of the full matrix
107 noiseModel::Diagonal::shared_ptr model_; // Gaussian noise model with diagonal covariance matrix
108
109 public:
110
112 explicit JacobianFactor(const GaussianFactor& gf);
113
115 JacobianFactor(const JacobianFactor& jf) : Base(jf), Ab_(jf.Ab_), model_(jf.model_) {}
116
118 explicit JacobianFactor(const HessianFactor& hf);
119
120 JacobianFactor& operator=(const JacobianFactor& jf) = default;
121
124
126 explicit JacobianFactor(const Vector& b_in);
127
129 JacobianFactor(Key i1, const Matrix& A1,
130 const Vector& b, const SharedDiagonal& model = SharedDiagonal());
131
133 template <int M, int N1,
134 typename = std::enable_if_t<(M != Eigen::Dynamic &&
135 N1 != Eigen::Dynamic)>>
136 JacobianFactor(Key i1, const Eigen::Matrix<double, M, N1>& A1,
137 const Eigen::Matrix<double, M, 1>& b,
138 const SharedDiagonal& model = SharedDiagonal());
139
141 JacobianFactor(Key i1, const Matrix& A1,
142 Key i2, const Matrix& A2,
143 const Vector& b, const SharedDiagonal& model = SharedDiagonal());
144
146 template <int M, int N1, int N2,
147 typename = std::enable_if_t<(M != Eigen::Dynamic &&
148 N1 != Eigen::Dynamic &&
149 N2 != Eigen::Dynamic)>>
150 JacobianFactor(Key i1, const Eigen::Matrix<double, M, N1>& A1,
151 Key i2, const Eigen::Matrix<double, M, N2>& A2,
152 const Eigen::Matrix<double, M, 1>& b,
153 const SharedDiagonal& model = SharedDiagonal());
154
156 JacobianFactor(Key i1, const Matrix& A1, Key i2,
157 const Matrix& A2, Key i3, const Matrix& A3,
158 const Vector& b, const SharedDiagonal& model = SharedDiagonal());
159
161 template <int M, int N1, int N2, int N3,
162 typename = std::enable_if_t<(M != Eigen::Dynamic &&
163 N1 != Eigen::Dynamic &&
164 N2 != Eigen::Dynamic &&
165 N3 != Eigen::Dynamic)>>
166 JacobianFactor(Key i1, const Eigen::Matrix<double, M, N1>& A1,
167 Key i2, const Eigen::Matrix<double, M, N2>& A2,
168 Key i3, const Eigen::Matrix<double, M, N3>& A3,
169 const Eigen::Matrix<double, M, 1>& b,
170 const SharedDiagonal& model = SharedDiagonal());
171
175 template<typename TERMS>
176 JacobianFactor(const TERMS& terms, const Vector& b, const SharedDiagonal& model = SharedDiagonal());
177
181 template <typename KEYS>
182 JacobianFactor(const KEYS& keys, const VerticalBlockMatrix& augmentedMatrix,
183 const SharedDiagonal& sigmas = SharedDiagonal());
184
186 template <typename KEYS>
187 JacobianFactor(const KEYS& keys, VerticalBlockMatrix&& augmentedMatrix,
188 const SharedDiagonal& model);
189
194 explicit JacobianFactor(
195 const GaussianFactorGraph& graph);
196
201 explicit JacobianFactor(
202 const GaussianFactorGraph& graph,
203 const VariableSlots& p_variableSlots);
204
209 explicit JacobianFactor(
210 const GaussianFactorGraph& graph,
211 const Ordering& ordering);
212
217 explicit JacobianFactor(
218 const GaussianFactorGraph& graph,
219 const Ordering& ordering,
220 const VariableSlots& p_variableSlots);
221
223 ~JacobianFactor() override {}
224
227 return std::static_pointer_cast<GaussianFactor>(
228 std::make_shared<JacobianFactor>(*this));
229 }
230
232 bool isJacobian() const override { return true; }
233
234 // Implementing Testable interface
235 void print(const std::string& s = "",
236 const KeyFormatter& formatter = DefaultKeyFormatter) const override;
237 bool equals(const GaussianFactor& lf, double tol = 1e-9) const override;
238
239 Vector unweighted_error(const VectorValues& c) const;
240 Vector error_vector(const VectorValues& c) const;
241
243 using GaussianFactor::error;
244
246 double error(const VectorValues& c) const override;
247
252 double deltaError(const VectorValues& c, double* oldError = nullptr,
253 double* newError = nullptr) const override;
254
263 Matrix augmentedInformation() const override;
264
268 Matrix information() const override;
269
271 using Base::hessianDiagonal;
272
274 void hessianDiagonalAdd(VectorValues& d) const override;
275
277 void hessianDiagonal(double* d) const override;
278
280 std::map<Key,Matrix> hessianBlockDiagonal() const override;
281
285 std::pair<Matrix, Vector> jacobian() const override;
286
290 std::pair<Matrix, Vector> jacobianUnweighted() const;
291
295 Matrix augmentedJacobian() const override;
296
300 Matrix augmentedJacobianUnweighted() const;
301
303 const VerticalBlockMatrix& matrixObject() const { return Ab_; }
304
307
313 GaussianFactor::shared_ptr negate() const override;
314
316 bool isConstrained() const {
317 return model_ && model_->isConstrained();
318 }
319
323 DenseIndex getDim(const_iterator variable) const override {
324 return Ab_(variable - begin()).cols();
325 }
326
330 size_t rows() const { return Ab_.rows(); }
331
335 size_t cols() const { return Ab_.cols(); }
336
338 const SharedDiagonal& get_model() const { return model_; }
339
341 SharedDiagonal& get_model() { return model_; }
342
344 const constBVector getb() const { return Ab_(size()).col(0); }
345
347 constABlock getA(const_iterator variable) const { return Ab_(variable - begin()); }
348
350 constABlock getA() const { return Ab_.range(0, size()); }
351
353 BVector getb() { return Ab_(size()).col(0); }
354
356 ABlock getA(iterator variable) { return Ab_(variable - begin()); }
357
359 ABlock getA() { return Ab_.range(0, size()); }
360
365 ABlock getA(const Key& key) { return Ab_(find(key) - begin()); }
366
372 void updateHessian(const KeyVector& keys, SymmetricBlockMatrix* info) const override;
373
382 void updateHessian(const KeyVector& keys, SymmetricBlockMatrix* info,
383 DenseIndex beginCol, DenseIndex endCol) const override;
384
386 Vector operator*(const VectorValues& x) const;
387
390 void transposeMultiplyAdd(double alpha, const Vector& e,
391 VectorValues& x) const;
392
394 void multiplyHessianAdd(double alpha, const VectorValues& x,
395 VectorValues& y) const override;
396
405 void multiplyHessianAdd(double alpha, const double* x, double* y,
406 const std::vector<size_t>& accumulatedDims) const;
407
409 VectorValues gradientAtZero() const override;
410
412 void gradientAtZero(double* d) const override;
413
415 Vector gradient(Key key, const VectorValues& x) const override;
416
418 JacobianFactor whiten() const;
419
421 std::pair<std::shared_ptr<GaussianConditional>, shared_ptr>
422 eliminate(const Ordering& keys);
423
425 void setModel(bool anyConstrained, const Vector& sigmas);
426
438 friend GTSAM_EXPORT std::pair<std::shared_ptr<GaussianConditional>, shared_ptr>
439 EliminateQR(const GaussianFactorGraph& factors, const Ordering& keys);
440
448 std::shared_ptr<GaussianConditional> splitConditional(size_t nrFrontals);
449
450 protected:
451
453 template<typename TERMS>
454 void fillTerms(const TERMS& terms, const Vector& b, const SharedDiagonal& noiseModel);
455
457 void checkAb(const SharedDiagonal& model,
458 const VerticalBlockMatrix& augmentedMatrix) const;
459
460 private:
461
466 void JacobianFactorHelper(
467 const GaussianFactorGraph& graph,
469
476 template<class KEYS, class DIMENSIONS>
477 JacobianFactor(const KEYS& keys, const DIMENSIONS& dims, DenseIndex m,
478 const SharedDiagonal& model = SharedDiagonal()) :
479 Base(keys), Ab_(dims.begin(), dims.end(), m, true), model_(model) {
480 }
481
482 // be very selective on who can access these private methods:
483 template<typename T> friend class ExpressionFactor;
484
485#if GTSAM_ENABLE_BOOST_SERIALIZATION
487 friend class boost::serialization::access;
488 template<class ARCHIVE>
489 void save(ARCHIVE & ar, const unsigned int version) const {
490 // TODO(fan): This is a hack for Boost < 1.66
491 // We really need to introduce proper versioning in the archives
492 // As otherwise this will not read objects serialized by older
493 // versions of GTSAM
494 ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
495 ar << BOOST_SERIALIZATION_NVP(Ab_);
496 bool model_null = false;
497 if(model_.get() == nullptr) {
498 model_null = true;
499 ar << boost::serialization::make_nvp("model_null", model_null);
500 } else {
501 ar << boost::serialization::make_nvp("model_null", model_null);
502 ar << BOOST_SERIALIZATION_NVP(model_);
503 }
504 }
505
506 template<class ARCHIVE>
507 void load(ARCHIVE & ar, const unsigned int version) {
508 // invoke serialization of the base class
509 ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
510 ar >> BOOST_SERIALIZATION_NVP(Ab_);
511 if (version < 1) {
512 ar >> BOOST_SERIALIZATION_NVP(model_);
513 } else {
514 bool model_null;
515 ar >> BOOST_SERIALIZATION_NVP(model_null);
516 if (!model_null) {
517 ar >> BOOST_SERIALIZATION_NVP(model_);
518 }
519 }
520 }
521
522 BOOST_SERIALIZATION_SPLIT_MEMBER()
523#endif
524 }; // JacobianFactor
525
526template<>
527struct traits<JacobianFactor> : public Testable<JacobianFactor> {
528};
529
530} // \ namespace gtsam
531
532#if GTSAM_ENABLE_BOOST_SERIALIZATION
533BOOST_CLASS_VERSION(gtsam::JacobianFactor, 1)
534#endif
535
536#include <gtsam/linear/JacobianFactor-inl.h>
A matrix with column blocks of pre-defined sizes.
VariableSlots describes the structure of a combined factor in terms of where each block comes from in...
A factor with a quadratic error function - a Gaussian.
Included from all GTSAM files.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition types.h:49
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
std::pair< GaussianConditional::shared_ptr, JacobianFactor::shared_ptr > EliminateQR(const GaussianFactorGraph &factors, const Ordering &keys)
Multiply all factors and eliminate the given keys from the resulting factor using a QR variant that h...
Definition JacobianFactor.cpp:976
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition Point2.h:52
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
All noise models live in the noiseModel namespace.
Definition LossFunctions.cpp:33
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
This class stores a dense matrix and allows it to be accessed as a collection of blocks.
Definition SymmetricBlockMatrix.h:80
Template to create a binary predicate.
Definition Testable.h:112
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
This class stores a dense matrix and allows it to be accessed as a collection of vertical blocks.
Definition VerticalBlockMatrix.h:47
const_iterator find(Key key) const
find
Definition Factor.h:140
const_iterator begin() const
Iterator at beginning of involved variable keys.
Definition Factor.h:146
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition Factor.h:83
KeyVector::iterator iterator
Iterator over keys.
Definition Factor.h:80
size_t size() const
Definition Factor.h:160
Definition Ordering.h:33
A combined factor is assembled as one block of rows for each component factor.
Definition VariableSlots.h:51
A GaussianConditional functions as the node in a Bayes network.
Definition GaussianConditional.h:43
An abstract virtual base class for JacobianFactor and HessianFactor.
Definition GaussianFactor.h:39
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition GaussianFactor.h:42
GaussianFactor()
Default constructor creates empty factor.
Definition GaussianFactor.h:49
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition GaussianFactorGraph.h:77
A Gaussian factor using the canonical parameters (information form).
Definition HessianFactor.h:101
A Gaussian factor in the squared-error form.
Definition JacobianFactor.h:92
const constBVector getb() const
Get a view of the r.h.s.
Definition JacobianFactor.h:344
BVector getb()
Get a view of the r.h.s.
Definition JacobianFactor.h:353
const SharedDiagonal & get_model() const
get a copy of model
Definition JacobianFactor.h:338
JacobianFactor(const GaussianFactor &gf)
Convert from other GaussianFactor.
Definition JacobianFactor.cpp:51
JacobianFactor(const JacobianFactor &jf)
Copy constructor.
Definition JacobianFactor.h:115
JacobianFactor This
Typedef to this class.
Definition JacobianFactor.h:95
bool isConstrained() const
is noise model constrained ?
Definition JacobianFactor.h:316
constABlock getA() const
Get a view of the A matrix, not weighted by noise.
Definition JacobianFactor.h:350
GaussianFactor::shared_ptr clone() const override
Clone this JacobianFactor.
Definition JacobianFactor.h:226
bool isJacobian() const override
Identify JacobianFactor-based types.
Definition JacobianFactor.h:232
ABlock getA(iterator variable)
Get a view of the A matrix for the variable pointed to by the given key iterator (non-const version).
Definition JacobianFactor.h:356
SharedDiagonal & get_model()
get a copy of model (non-const version)
Definition JacobianFactor.h:341
DenseIndex getDim(const_iterator variable) const override
Return the dimension of the variable pointed to by the given key iterator todo: Remove this in favor ...
Definition JacobianFactor.h:323
VerticalBlockMatrix & matrixObject()
Mutable access to the full augmented Jacobian matrix of this factor as a VerticalBlockMatrix object.
Definition JacobianFactor.h:306
ABlock getA()
Get a view of the A matrix.
Definition JacobianFactor.h:359
GaussianFactor Base
Typedef to base class.
Definition JacobianFactor.h:96
~JacobianFactor() override
Virtual destructor.
Definition JacobianFactor.h:223
const VerticalBlockMatrix & matrixObject() const
Return the full augmented Jacobian matrix of this factor as a VerticalBlockMatrix object.
Definition JacobianFactor.h:303
size_t rows() const
return the number of rows in the corresponding linear system
Definition JacobianFactor.h:330
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition JacobianFactor.h:97
size_t cols() const
return the number of columns in the corresponding linear system
Definition JacobianFactor.h:335
ABlock getA(const Key &key)
Get a view of the A matrix for the variable pointed to by the given key.
Definition JacobianFactor.h:365
constABlock getA(const_iterator variable) const
Get a view of the A matrix for the variable pointed to by the given key iterator.
Definition JacobianFactor.h:347
VectorValues represents a collection of vector-valued variables associated each with a unique integer...
Definition VectorValues.h:73
The Factor::error simply extracts the.