gtsam  4.0.0
gtsam
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>
25 
26 #include <boost/make_shared.hpp>
27 
28 namespace gtsam {
29 
30  // Forward declarations
31  class HessianFactor;
32  class VariableSlots;
33  class GaussianFactorGraph;
34  class GaussianConditional;
35  class HessianFactor;
36  class VectorValues;
37  class Ordering;
38  class JacobianFactor;
39 
45  GTSAM_EXPORT std::pair<boost::shared_ptr<GaussianConditional>, boost::shared_ptr<JacobianFactor> >
46  EliminateQR(const GaussianFactorGraph& factors, const Ordering& keys);
47 
87  class GTSAM_EXPORT JacobianFactor : public GaussianFactor
88  {
89  public:
90 
91  typedef JacobianFactor This;
92  typedef GaussianFactor Base;
93  typedef boost::shared_ptr<This> shared_ptr;
94 
95  typedef VerticalBlockMatrix::Block ABlock;
96  typedef VerticalBlockMatrix::constBlock constABlock;
97  typedef ABlock::ColXpr BVector;
98  typedef constABlock::ConstColXpr constBVector;
99 
100  protected:
101 
102  VerticalBlockMatrix Ab_; // the block view of the full matrix
103  noiseModel::Diagonal::shared_ptr model_; // Gaussian noise model with diagonal covariance matrix
104 
105  public:
106 
108  explicit JacobianFactor(const GaussianFactor& gf);
109 
111  JacobianFactor(const JacobianFactor& jf) : Base(jf), Ab_(jf.Ab_), model_(jf.model_) {}
112 
114  explicit JacobianFactor(const HessianFactor& hf);
115 
117  JacobianFactor();
118 
120  explicit JacobianFactor(const Vector& b_in);
121 
123  JacobianFactor(Key i1, const Matrix& A1,
124  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
125 
127  JacobianFactor(Key i1, const Matrix& A1,
128  Key i2, const Matrix& A2,
129  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
130 
132  JacobianFactor(Key i1, const Matrix& A1, Key i2,
133  const Matrix& A2, Key i3, const Matrix& A3,
134  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
135 
139  template<typename TERMS>
140  JacobianFactor(const TERMS& terms, const Vector& b, const SharedDiagonal& model = SharedDiagonal());
141 
146  template<typename KEYS>
148  const KEYS& keys, const VerticalBlockMatrix& augmentedMatrix, const SharedDiagonal& sigmas = SharedDiagonal());
149 
154  explicit JacobianFactor(
155  const GaussianFactorGraph& graph,
156  boost::optional<const Ordering&> ordering = boost::none,
157  boost::optional<const VariableSlots&> p_variableSlots = boost::none);
158 
160  virtual ~JacobianFactor() {}
161 
164  return boost::static_pointer_cast<GaussianFactor>(
165  boost::make_shared<JacobianFactor>(*this));
166  }
167 
168  // Implementing Testable interface
169  virtual void print(const std::string& s = "",
170  const KeyFormatter& formatter = DefaultKeyFormatter) const;
171  virtual bool equals(const GaussianFactor& lf, double tol = 1e-9) const;
172 
173  Vector unweighted_error(const VectorValues& c) const;
174  Vector error_vector(const VectorValues& c) const;
175  virtual double error(const VectorValues& c) const;
185  virtual Matrix augmentedInformation() const;
186 
190  virtual Matrix information() const;
191 
193  virtual VectorValues hessianDiagonal() const;
194 
196  virtual void hessianDiagonal(double* d) const;
197 
199  virtual std::map<Key,Matrix> hessianBlockDiagonal() const;
200 
204  virtual std::pair<Matrix, Vector> jacobian() const;
205 
209  std::pair<Matrix, Vector> jacobianUnweighted() const;
210 
214  virtual Matrix augmentedJacobian() const;
215 
219  Matrix augmentedJacobianUnweighted() const;
220 
222  const VerticalBlockMatrix& matrixObject() const { return Ab_; }
223 
226 
232  virtual GaussianFactor::shared_ptr negate() const;
233 
235  virtual bool empty() const { return size() == 0 /*|| rows() == 0*/; }
236 
238  bool isConstrained() const {
239  return model_ && model_->isConstrained();
240  }
241 
245  virtual DenseIndex getDim(const_iterator variable) const { return Ab_(variable - begin()).cols(); }
246 
250  size_t rows() const { return Ab_.rows(); }
251 
255  size_t cols() const { return Ab_.cols(); }
256 
258  const SharedDiagonal& get_model() const { return model_; }
259 
261  SharedDiagonal& get_model() { return model_; }
262 
264  const constBVector getb() const { return Ab_(size()).col(0); }
265 
267  constABlock getA(const_iterator variable) const { return Ab_(variable - begin()); }
268 
270  constABlock getA() const { return Ab_.range(0, size()); }
271 
273  BVector getb() { return Ab_(size()).col(0); }
274 
276  ABlock getA(iterator variable) { return Ab_(variable - begin()); }
277 
279  ABlock getA() { return Ab_.range(0, size()); }
280 
286  void updateHessian(const KeyVector& keys, SymmetricBlockMatrix* info) const;
287 
289  Vector operator*(const VectorValues& x) const;
290 
293  void transposeMultiplyAdd(double alpha, const Vector& e, VectorValues& x) const;
294 
296  void multiplyHessianAdd(double alpha, const VectorValues& x, VectorValues& y) const;
297 
306  void multiplyHessianAdd(double alpha, const double* x, double* y,
307  const std::vector<size_t>& accumulatedDims) const;
308 
310  VectorValues gradientAtZero() const;
311 
313  virtual void gradientAtZero(double* d) const;
314 
316  Vector gradient(Key key, const VectorValues& x) const;
317 
319  JacobianFactor whiten() const;
320 
322  std::pair<boost::shared_ptr<GaussianConditional>, shared_ptr>
323  eliminate(const Ordering& keys);
324 
326  void setModel(bool anyConstrained, const Vector& sigmas);
327 
339  friend GTSAM_EXPORT std::pair<boost::shared_ptr<GaussianConditional>, shared_ptr>
340  EliminateQR(const GaussianFactorGraph& factors, const Ordering& keys);
341 
349  boost::shared_ptr<GaussianConditional> splitConditional(size_t nrFrontals);
350 
351  protected:
352 
354  template<typename TERMS>
355  void fillTerms(const TERMS& terms, const Vector& b, const SharedDiagonal& noiseModel);
356 
357  private:
358 
365  template<class KEYS, class DIMENSIONS>
366  JacobianFactor(const KEYS& keys, const DIMENSIONS& dims, DenseIndex m,
367  const SharedDiagonal& model = SharedDiagonal()) :
368  Base(keys), Ab_(dims.begin(), dims.end(), m, true), model_(model) {
369  }
370 
371  // be very selective on who can access these private methods:
372  template<typename T> friend class ExpressionFactor;
373 
375  friend class boost::serialization::access;
376  template<class ARCHIVE>
377  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
378  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
379  ar & BOOST_SERIALIZATION_NVP(Ab_);
380  ar & BOOST_SERIALIZATION_NVP(model_);
381  }
382  }; // JacobianFactor
383 
385 template<>
386 struct traits<JacobianFactor> : public Testable<JacobianFactor> {
387 };
388 
389 } // \ namespace gtsam
390 
391 #include <gtsam/linear/JacobianFactor-inl.h>
392 
393 
virtual bool empty() const
Check if the factor is empty.
Definition: JacobianFactor.h:235
DenseIndex cols() const
Column size.
Definition: VerticalBlockMatrix.h:117
This is the base class for all factor types.
Definition: Factor.h:54
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:63
bool isConstrained() const
is noise model constrained ?
Definition: JacobianFactor.h:238
const constBVector getb() const
Get a view of the r.h.s.
Definition: JacobianFactor.h:264
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:141
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: JacobianFactor.h:93
Block range(DenseIndex startBlock, DenseIndex endBlock)
access ranges of blocks at a time
Definition: VerticalBlockMatrix.h:129
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
Template to create a binary predicate.
Definition: Testable.h:110
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition: GaussianFactorGraph.h:65
Included from all GTSAM files.
An abstract virtual base class for JacobianFactor and HessianFactor.
Definition: GaussianFactor.h:38
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:721
BVector getb()
Get a view of the r.h.s.
Definition: JacobianFactor.h:273
size_t rows() const
return the number of rows in the corresponding linear system
Definition: JacobianFactor.h:250
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:267
Definition: VerticalBlockMatrix.h:41
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33
constABlock getA() const
Get a view of the A matrix, not weighted by noise.
Definition: JacobianFactor.h:270
VerticalBlockMatrix & matrixObject()
Mutable access to the full augmented Jacobian matrix of this factor as a VerticalBlockMatrix object.
Definition: JacobianFactor.h:225
virtual ~JacobianFactor()
Virtual destructor.
Definition: JacobianFactor.h:160
size_t cols() const
return the number of columns in the corresponding linear system
Definition: JacobianFactor.h:255
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:73
const VerticalBlockMatrix & matrixObject() const
Return the full augmented Jacobian matrix of this factor as a VerticalBlockMatrix object.
Definition: JacobianFactor.h:222
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:56
SharedDiagonal & get_model()
get a copy of model (non-const version)
Definition: JacobianFactor.h:261
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
JacobianFactor(const JacobianFactor &jf)
Copy constructor.
Definition: JacobianFactor.h:111
virtual DenseIndex getDim(const_iterator variable) const
Return the dimension of the variable pointed to by the given key iterator todo: Remove this in favor ...
Definition: JacobianFactor.h:245
JacobianFactor This
Typedef to this class.
Definition: JacobianFactor.h:91
A Gaussian factor in the squared-error form.
Definition: JacobianFactor.h:87
A matrix with column blocks of pre-defined sizes.
virtual GaussianFactor::shared_ptr clone() const
Clone this JacobianFactor.
Definition: JacobianFactor.h:163
A factor with a quadratic error function - a Gaussian.
Definition: SymmetricBlockMatrix.h:51
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
DenseIndex rows() const
Row size.
Definition: VerticalBlockMatrix.h:114
GaussianFactor Base
Typedef to base class.
Definition: JacobianFactor.h:92
ABlock getA()
Get a view of the A matrix.
Definition: JacobianFactor.h:279
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:276
KeyVector::iterator iterator
Iterator over keys.
Definition: Factor.h:64
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:67
Definition: Ordering.h:34
const SharedDiagonal & get_model() const
get a copy of model
Definition: JacobianFactor.h:258
Point2 operator *(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:170
A Gaussian factor using the canonical parameters (information form)
Definition: HessianFactor.h:101
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactor.h:42