GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
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 
24 #include <gtsam/global_includes.h>
26 
27 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
28 #include <boost/serialization/version.hpp>
29 #include <boost/serialization/split_member.hpp>
30 #endif
31 
32 namespace gtsam {
33 
34  // Forward declarations
35  class HessianFactor;
36  class VariableSlots;
37  class GaussianFactorGraph;
38  class GaussianConditional;
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 
95  typedef JacobianFactor This;
96  typedef GaussianFactor Base;
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 
121  JacobianFactor();
122 
124  explicit JacobianFactor(const Vector& b_in);
125 
127  JacobianFactor(Key i1, const Matrix& A1,
128  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
129 
131  JacobianFactor(Key i1, const Matrix& A1,
132  Key i2, const Matrix& A2,
133  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
134 
136  JacobianFactor(Key i1, const Matrix& A1, Key i2,
137  const Matrix& A2, Key i3, const Matrix& A3,
138  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
139 
143  template<typename TERMS>
144  JacobianFactor(const TERMS& terms, const Vector& b, const SharedDiagonal& model = SharedDiagonal());
145 
150  template<typename KEYS>
152  const KEYS& keys, const VerticalBlockMatrix& augmentedMatrix, const SharedDiagonal& sigmas = SharedDiagonal());
153 
158  explicit JacobianFactor(
159  const GaussianFactorGraph& graph);
160 
165  explicit JacobianFactor(
166  const GaussianFactorGraph& graph,
167  const VariableSlots& p_variableSlots);
168 
173  explicit JacobianFactor(
174  const GaussianFactorGraph& graph,
175  const Ordering& ordering);
176 
181  explicit JacobianFactor(
182  const GaussianFactorGraph& graph,
183  const Ordering& ordering,
184  const VariableSlots& p_variableSlots);
185 
187  ~JacobianFactor() override {}
188 
191  return std::static_pointer_cast<GaussianFactor>(
192  std::make_shared<JacobianFactor>(*this));
193  }
194 
195  // Implementing Testable interface
196  void print(const std::string& s = "",
197  const KeyFormatter& formatter = DefaultKeyFormatter) const override;
198  bool equals(const GaussianFactor& lf, double tol = 1e-9) const override;
199 
200  Vector unweighted_error(const VectorValues& c) const;
201  Vector error_vector(const VectorValues& c) const;
203  using GaussianFactor::error;
205 
207  double error(const VectorValues& c) const override;
208 
217  Matrix augmentedInformation() const override;
218 
222  Matrix information() const override;
223 
225  using Base::hessianDiagonal;
226 
228  void hessianDiagonalAdd(VectorValues& d) const override;
229 
231  void hessianDiagonal(double* d) const override;
232 
234  std::map<Key,Matrix> hessianBlockDiagonal() const override;
235 
239  std::pair<Matrix, Vector> jacobian() const override;
240 
244  std::pair<Matrix, Vector> jacobianUnweighted() const;
245 
249  Matrix augmentedJacobian() const override;
250 
254  Matrix augmentedJacobianUnweighted() const;
255 
257  const VerticalBlockMatrix& matrixObject() const { return Ab_; }
258 
261 
267  GaussianFactor::shared_ptr negate() const override;
268 
270  bool isConstrained() const {
271  return model_ && model_->isConstrained();
272  }
273 
277  DenseIndex getDim(const_iterator variable) const override {
278  return Ab_(variable - begin()).cols();
279  }
280 
284  size_t rows() const { return Ab_.rows(); }
285 
289  size_t cols() const { return Ab_.cols(); }
290 
292  const SharedDiagonal& get_model() const { return model_; }
293 
295  SharedDiagonal& get_model() { return model_; }
296 
298  const constBVector getb() const { return Ab_(size()).col(0); }
299 
301  constABlock getA(const_iterator variable) const { return Ab_(variable - begin()); }
302 
304  constABlock getA() const { return Ab_.range(0, size()); }
305 
307  BVector getb() { return Ab_(size()).col(0); }
308 
310  ABlock getA(iterator variable) { return Ab_(variable - begin()); }
311 
313  ABlock getA() { return Ab_.range(0, size()); }
314 
320  void updateHessian(const KeyVector& keys, SymmetricBlockMatrix* info) const override;
321 
323  Vector operator*(const VectorValues& x) const;
324 
327  void transposeMultiplyAdd(double alpha, const Vector& e,
328  VectorValues& x) const;
329 
331  void multiplyHessianAdd(double alpha, const VectorValues& x,
332  VectorValues& y) const override;
333 
342  void multiplyHessianAdd(double alpha, const double* x, double* y,
343  const std::vector<size_t>& accumulatedDims) const;
344 
346  VectorValues gradientAtZero() const override;
347 
349  void gradientAtZero(double* d) const override;
350 
352  Vector gradient(Key key, const VectorValues& x) const override;
353 
355  JacobianFactor whiten() const;
356 
358  std::pair<std::shared_ptr<GaussianConditional>, shared_ptr>
359  eliminate(const Ordering& keys);
360 
362  void setModel(bool anyConstrained, const Vector& sigmas);
363 
375  friend GTSAM_EXPORT std::pair<std::shared_ptr<GaussianConditional>, shared_ptr>
376  EliminateQR(const GaussianFactorGraph& factors, const Ordering& keys);
377 
385  std::shared_ptr<GaussianConditional> splitConditional(size_t nrFrontals);
386 
387  protected:
388 
390  template<typename TERMS>
391  void fillTerms(const TERMS& terms, const Vector& b, const SharedDiagonal& noiseModel);
392 
393  private:
394 
399  void JacobianFactorHelper(
400  const GaussianFactorGraph& graph,
401  const FastVector<VariableSlots::const_iterator>& orderedSlots);
402 
409  template<class KEYS, class DIMENSIONS>
410  JacobianFactor(const KEYS& keys, const DIMENSIONS& dims, DenseIndex m,
411  const SharedDiagonal& model = SharedDiagonal()) :
412  Base(keys), Ab_(dims.begin(), dims.end(), m, true), model_(model) {
413  }
414 
415  // be very selective on who can access these private methods:
416  template<typename T> friend class ExpressionFactor;
417 
418 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
419 
420  friend class boost::serialization::access;
421  template<class ARCHIVE>
422  void save(ARCHIVE & ar, const unsigned int version) const {
423  // TODO(fan): This is a hack for Boost < 1.66
424  // We really need to introduce proper versioning in the archives
425  // As otherwise this will not read objects serialized by older
426  // versions of GTSAM
427  ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
428  ar << BOOST_SERIALIZATION_NVP(Ab_);
429  bool model_null = false;
430  if(model_.get() == nullptr) {
431  model_null = true;
432  ar << boost::serialization::make_nvp("model_null", model_null);
433  } else {
434  ar << boost::serialization::make_nvp("model_null", model_null);
435  ar << BOOST_SERIALIZATION_NVP(model_);
436  }
437  }
438 
439  template<class ARCHIVE>
440  void load(ARCHIVE & ar, const unsigned int version) {
441  // invoke serialization of the base class
442  ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
443  ar >> BOOST_SERIALIZATION_NVP(Ab_);
444  if (version < 1) {
445  ar >> BOOST_SERIALIZATION_NVP(model_);
446  } else {
447  bool model_null;
448  ar >> BOOST_SERIALIZATION_NVP(model_null);
449  if (!model_null) {
450  ar >> BOOST_SERIALIZATION_NVP(model_);
451  }
452  }
453  }
454 
455  BOOST_SERIALIZATION_SPLIT_MEMBER()
456 #endif
457  }; // JacobianFactor
459 template<>
460 struct traits<JacobianFactor> : public Testable<JacobianFactor> {
461 };
462 
463 } // \ namespace gtsam
464 
465 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
466 BOOST_CLASS_VERSION(gtsam::JacobianFactor, 1)
467 #endif
468 
469 #include <gtsam/linear/JacobianFactor-inl.h>
470 
471 
DenseIndex cols() const
Column size.
Definition: VerticalBlockMatrix.h:118
const VerticalBlockMatrix & matrixObject() const
Definition: JacobianFactor.h:257
Definition: VerticalBlockMatrix.h:42
DenseIndex rows() const
Row size.
Definition: VerticalBlockMatrix.h:115
Definition: Testable.h:152
A matrix with column blocks of pre-defined sizes. Used in JacobianFactor and GaussianConditional.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
Definition: Group.h:43
A factor with a quadratic error function - a Gaussian.
Definition: Ordering.h:37
ABlock getA()
Definition: JacobianFactor.h:313
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
JacobianFactor(const JacobianFactor &jf)
Definition: JacobianFactor.h:115
SharedDiagonal & get_model()
Definition: JacobianFactor.h:295
Definition: VectorValues.h:74
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:108
Included from all GTSAM files.
Definition: GaussianFactor.h:38
VariableSlots describes the structure of a combined factor in terms of where each block comes from in...
Definition: SymmetricBlockMatrix.h:53
bool isConstrained() const
Definition: JacobianFactor.h:270
GTSAM_EXPORT std::pair< std::shared_ptr< GaussianConditional >, std::shared_ptr< JacobianFactor > > EliminateQR(const GaussianFactorGraph &factors, const Ordering &keys)
size_t rows() const
Definition: JacobianFactor.h:284
Definition: Testable.h:112
Definition: VariableSlots.h:51
GaussianFactor::shared_ptr clone() const override
Definition: JacobianFactor.h:190
GTSAM_EXPORT void print(const Matrix &A, const std::string &s, std::ostream &stream)
const constBVector getb() const
Definition: JacobianFactor.h:298
KeyVector::iterator iterator
Iterator over keys.
Definition: Factor.h:79
GTSAM_EXPORT void save(const Matrix &A, const std::string &s, const std::string &filename)
DenseIndex getDim(const_iterator variable) const override
Definition: JacobianFactor.h:277
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: JacobianFactor.h:97
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
Definition: JacobianFactor.h:91
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactor.h:42
ABlock getA(iterator variable)
Definition: JacobianFactor.h:310
Block range(DenseIndex startBlock, DenseIndex endBlock)
Definition: VerticalBlockMatrix.h:130
VerticalBlockMatrix & matrixObject()
Definition: JacobianFactor.h:260
Definition: chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
constABlock getA() const
Definition: JacobianFactor.h:304
size_t cols() const
Definition: JacobianFactor.h:289
JacobianFactor This
Typedef to this class.
Definition: JacobianFactor.h:95
A Gaussian factor using the canonical parameters (information form)
Definition: HessianFactor.h:100
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:82
Definition: Expression.h:36
~JacobianFactor() override
Definition: JacobianFactor.h:187
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
GaussianFactor Base
Typedef to base class.
Definition: JacobianFactor.h:96
Definition: GaussianFactorGraph.h:73
const SharedDiagonal & get_model() const
Definition: JacobianFactor.h:292
constABlock getA(const_iterator variable) const
Definition: JacobianFactor.h:301
BVector getb()
Definition: JacobianFactor.h:307