gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
40  GTSAM_EXPORT std::pair<boost::shared_ptr<GaussianConditional>, boost::shared_ptr<JacobianFactor> >
41  EliminateQR(const GaussianFactorGraph& factors, const Ordering& keys);
42 
82  class GTSAM_EXPORT JacobianFactor : public GaussianFactor
83  {
84  public:
85  typedef JacobianFactor This;
86  typedef GaussianFactor Base;
87  typedef boost::shared_ptr<This> shared_ptr;
88 
89  protected:
90  VerticalBlockMatrix Ab_; // the block view of the full matrix
91  noiseModel::Diagonal::shared_ptr model_; // Gaussian noise model with diagonal covariance matrix
92 
93  public:
94  typedef VerticalBlockMatrix::Block ABlock;
95  typedef VerticalBlockMatrix::constBlock constABlock;
96  typedef ABlock::ColXpr BVector;
97  typedef constABlock::ConstColXpr constBVector;
98 
99 
101  explicit JacobianFactor(const GaussianFactor& gf);
102 
104  JacobianFactor(const JacobianFactor& jf) : Base(jf), Ab_(jf.Ab_), model_(jf.model_) {}
105 
107  explicit JacobianFactor(const HessianFactor& hf);
108 
110  JacobianFactor();
111 
113  explicit JacobianFactor(const Vector& b_in);
114 
116  JacobianFactor(Key i1, const Matrix& A1,
117  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
118 
120  JacobianFactor(Key i1, const Matrix& A1,
121  Key i2, const Matrix& A2,
122  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
123 
125  JacobianFactor(Key i1, const Matrix& A1, Key i2,
126  const Matrix& A2, Key i3, const Matrix& A3,
127  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
128 
132  template<typename TERMS>
133  JacobianFactor(const TERMS& terms, const Vector& b, const SharedDiagonal& model = SharedDiagonal());
134 
139  template<typename KEYS>
141  const KEYS& keys, const VerticalBlockMatrix& augmentedMatrix, const SharedDiagonal& sigmas = SharedDiagonal());
142 
147  explicit JacobianFactor(
148  const GaussianFactorGraph& graph,
149  boost::optional<const Ordering&> ordering = boost::none,
150  boost::optional<const VariableSlots&> variableSlots = boost::none);
151 
153  virtual ~JacobianFactor() {}
154 
157  return boost::static_pointer_cast<GaussianFactor>(
158  boost::make_shared<JacobianFactor>(*this));
159  }
160 
161  // Implementing Testable interface
162  virtual void print(const std::string& s = "",
163  const KeyFormatter& formatter = DefaultKeyFormatter) const;
164  virtual bool equals(const GaussianFactor& lf, double tol = 1e-9) const;
165 
166  Vector unweighted_error(const VectorValues& c) const;
167  Vector error_vector(const VectorValues& c) const;
168  virtual double error(const VectorValues& c) const;
178  virtual Matrix augmentedInformation() const;
179 
183  virtual Matrix information() const;
184 
186  virtual VectorValues hessianDiagonal() const;
187 
188  /* ************************************************************************* */
189  virtual void hessianDiagonal(double* d) const;
190 
192  virtual std::map<Key,Matrix> hessianBlockDiagonal() const;
193 
197  virtual std::pair<Matrix, Vector> jacobian() const;
198 
202  std::pair<Matrix, Vector> jacobianUnweighted() const;
203 
207  virtual Matrix augmentedJacobian() const;
208 
212  Matrix augmentedJacobianUnweighted() const;
213 
215  const VerticalBlockMatrix& matrixObject() const { return Ab_; }
216 
219 
225  virtual GaussianFactor::shared_ptr negate() const;
226 
228  virtual bool empty() const { return size() == 0 /*|| rows() == 0*/; }
229 
231  bool isConstrained() const { return model_->isConstrained(); }
232 
236  virtual DenseIndex getDim(const_iterator variable) const { return Ab_(variable - begin()).cols(); }
237 
241  size_t rows() const { return Ab_.rows(); }
242 
246  size_t cols() const { return Ab_.cols(); }
247 
249  const SharedDiagonal& get_model() const { return model_; }
250 
252  SharedDiagonal& get_model() { return model_; }
253 
255  const constBVector getb() const { return Ab_(size()).col(0); }
256 
258  constABlock getA(const_iterator variable) const { return Ab_(variable - begin()); }
259 
261  constABlock getA() const { return Ab_.range(0, size()); }
262 
264  BVector getb() { return Ab_(size()).col(0); }
265 
267  ABlock getA(iterator variable) { return Ab_(variable - begin()); }
268 
270  ABlock getA() { return Ab_.range(0, size()); }
271 
273  Vector operator*(const VectorValues& x) const;
274 
277  void transposeMultiplyAdd(double alpha, const Vector& e, VectorValues& x) const;
278 
280  void multiplyHessianAdd(double alpha, const VectorValues& x, VectorValues& y) const;
281 
282  void multiplyHessianAdd(double alpha, const double* x, double* y, std::vector<size_t> keys) const;
283 
284  void multiplyHessianAdd(double alpha, const double* x, double* y) const {};
285 
287  VectorValues gradientAtZero() const;
288 
289  /* ************************************************************************* */
290  virtual void gradientAtZero(double* d) const;
291 
293  JacobianFactor whiten() const;
294 
296  std::pair<boost::shared_ptr<GaussianConditional>, boost::shared_ptr<JacobianFactor> >
297  eliminate(const Ordering& keys);
298 
300  void setModel(bool anyConstrained, const Vector& sigmas);
301 
313  friend GTSAM_EXPORT std::pair<boost::shared_ptr<GaussianConditional>, boost::shared_ptr<JacobianFactor> >
314  EliminateQR(const GaussianFactorGraph& factors, const Ordering& keys);
315 
321  boost::shared_ptr<GaussianConditional> splitConditional(size_t nrFrontals);
322 
323  protected:
324 
326  template<typename TERMS>
327  void fillTerms(const TERMS& terms, const Vector& b, const SharedDiagonal& noiseModel);
328 
329  private:
330 
332  friend class boost::serialization::access;
333  template<class ARCHIVE>
334  void serialize(ARCHIVE & ar, const unsigned int version) {
335  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
336  ar & BOOST_SERIALIZATION_NVP(Ab_);
337  ar & BOOST_SERIALIZATION_NVP(model_);
338  }
339  }; // JacobianFactor
340 
341 } // gtsam
342 
343 #include <gtsam/linear/JacobianFactor-inl.h>
344 
345 
void transposeMultiplyAdd(double alpha, const Matrix &A, const Vector &e, Vector &x)
BLAS Level-2 style x <- x + alpha*A'*e.
Definition: Matrix.cpp:168
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:236
size_t cols() const
return the number of columns in the corresponding linear system
Definition: JacobianFactor.h:246
SharedDiagonal & get_model()
get a copy of model (non-const version)
Definition: JacobianFactor.h:252
ABlock getA()
Get a view of the A matrix.
Definition: JacobianFactor.h:270
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:258
A Gaussian factor using the canonical parameters (information form)
Definition: HessianFactor.h:131
const VerticalBlockMatrix & matrixObject() const
Return the full augmented Jacobian matrix of this factor as a VerticalBlockMatrix object...
Definition: JacobianFactor.h:215
const SharedDiagonal & get_model() const
get a copy of model
Definition: JacobianFactor.h:249
JacobianFactor(const JacobianFactor &jf)
Copy constructor.
Definition: JacobianFactor.h:104
Definition: VerticalBlockMatrix.h:41
A Gaussian factor in the squared-error form.
Definition: JacobianFactor.h:82
This is the base class for all factor types.
Definition: Factor.h:51
Included from all GTSAM files.
GaussianFactor Base
Typedef to base class.
Definition: JacobianFactor.h:86
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:249
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:267
BVector getb()
Get a view of the r.h.s.
Definition: JacobianFactor.h:264
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
constABlock getA() const
Get a view of the A matrix, not weighted by noise.
Definition: JacobianFactor.h:261
virtual ~JacobianFactor()
Virtual destructor.
Definition: JacobianFactor.h:153
A factor with a quadratic error function - a Gaussian.
A matrix with column blocks of pre-defined sizes.
void multiplyHessianAdd(double alpha, const double *x, double *y) const
y += alpha * A'*A*x
Definition: JacobianFactor.h:284
Template to create a binary predicate.
Definition: Testable.h:102
bool isConstrained() const
is noise model constrained ?
Definition: JacobianFactor.h:231
size_t Key
Integer nonlinear key type.
Definition: types.h:59
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:74
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactor.h:39
virtual GaussianFactor::shared_ptr clone() const
Clone this JacobianFactor.
Definition: JacobianFactor.h:156
FastVector< Key >::iterator iterator
Iterator over keys.
Definition: Factor.h:61
virtual bool empty() const
Check if the factor is empty.
Definition: JacobianFactor.h:228
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:89
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition: GaussianFactorGraph.h:65
JacobianFactor This
Typedef to this class.
Definition: JacobianFactor.h:85
Definition: Ordering.h:30
size_t rows() const
return the number of rows in the corresponding linear system
Definition: JacobianFactor.h:241
const constBVector getb() const
Get a view of the r.h.s.
Definition: JacobianFactor.h:255
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: JacobianFactor.h:87
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
FastVector< Key >::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:64
VerticalBlockMatrix & matrixObject()
Mutable access to the full augmented Jacobian matrix of this factor as a VerticalBlockMatrix object...
Definition: JacobianFactor.h:218
An abstract virtual base class for JacobianFactor and HessianFactor.
Definition: GaussianFactor.h:35