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 #include <boost/make_shared.hpp>
28 #include <boost/serialization/version.hpp>
29 #include <boost/serialization/split_member.hpp>
30 
31 namespace gtsam {
32 
33  // Forward declarations
34  class HessianFactor;
35  class VariableSlots;
36  class GaussianFactorGraph;
37  class GaussianConditional;
38  class HessianFactor;
39  class VectorValues;
40  class Ordering;
41  class JacobianFactor;
42 
48  GTSAM_EXPORT std::pair<boost::shared_ptr<GaussianConditional>, boost::shared_ptr<JacobianFactor> >
49  EliminateQR(const GaussianFactorGraph& factors, const Ordering& keys);
50 
90  class GTSAM_EXPORT JacobianFactor : public GaussianFactor
91  {
92  public:
93 
94  typedef JacobianFactor This;
95  typedef GaussianFactor Base;
96  typedef boost::shared_ptr<This> shared_ptr;
97 
102 
103  protected:
104 
105  VerticalBlockMatrix Ab_; // the block view of the full matrix
106  noiseModel::Diagonal::shared_ptr model_; // Gaussian noise model with diagonal covariance matrix
107 
108  public:
109 
111  explicit JacobianFactor(const GaussianFactor& gf);
112 
114  JacobianFactor(const JacobianFactor& jf) : Base(jf), Ab_(jf.Ab_), model_(jf.model_) {}
115 
117  explicit JacobianFactor(const HessianFactor& hf);
118 
120  JacobianFactor();
121 
123  explicit JacobianFactor(const Vector& b_in);
124 
126  JacobianFactor(Key i1, const Matrix& A1,
127  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
128 
130  JacobianFactor(Key i1, const Matrix& A1,
131  Key i2, const Matrix& A2,
132  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
133 
135  JacobianFactor(Key i1, const Matrix& A1, Key i2,
136  const Matrix& A2, Key i3, const Matrix& A3,
137  const Vector& b, const SharedDiagonal& model = SharedDiagonal());
138 
142  template<typename TERMS>
143  JacobianFactor(const TERMS& terms, const Vector& b, const SharedDiagonal& model = SharedDiagonal());
144 
149  template<typename KEYS>
151  const KEYS& keys, const VerticalBlockMatrix& augmentedMatrix, const SharedDiagonal& sigmas = SharedDiagonal());
152 
157  explicit JacobianFactor(
158  const GaussianFactorGraph& graph);
159 
164  explicit JacobianFactor(
165  const GaussianFactorGraph& graph,
166  const VariableSlots& p_variableSlots);
167 
172  explicit JacobianFactor(
173  const GaussianFactorGraph& graph,
174  const Ordering& ordering);
175 
180  explicit JacobianFactor(
181  const GaussianFactorGraph& graph,
182  const Ordering& ordering,
183  const VariableSlots& p_variableSlots);
184 
186  ~JacobianFactor() override {}
187 
190  return boost::static_pointer_cast<GaussianFactor>(
191  boost::make_shared<JacobianFactor>(*this));
192  }
193 
194  // Implementing Testable interface
195  void print(const std::string& s = "",
196  const KeyFormatter& formatter = DefaultKeyFormatter) const override;
197  bool equals(const GaussianFactor& lf, double tol = 1e-9) const override;
198 
199  Vector unweighted_error(const VectorValues& c) const;
200  Vector error_vector(const VectorValues& c) const;
201  double error(const VectorValues& c) const override;
211  Matrix augmentedInformation() const override;
212 
216  Matrix information() const override;
217 
219  using Base::hessianDiagonal;
220 
222  void hessianDiagonalAdd(VectorValues& d) const override;
223 
225  void hessianDiagonal(double* d) const override;
226 
228  std::map<Key,Matrix> hessianBlockDiagonal() const override;
229 
233  std::pair<Matrix, Vector> jacobian() const override;
234 
238  std::pair<Matrix, Vector> jacobianUnweighted() const;
239 
243  Matrix augmentedJacobian() const override;
244 
248  Matrix augmentedJacobianUnweighted() const;
249 
251  const VerticalBlockMatrix& matrixObject() const { return Ab_; }
252 
255 
261  GaussianFactor::shared_ptr negate() const override;
262 
264  bool empty() const override { return size() == 0 /*|| rows() == 0*/; }
265 
267  bool isConstrained() const {
268  return model_ && model_->isConstrained();
269  }
270 
274  DenseIndex getDim(const_iterator variable) const override {
275  return Ab_(variable - begin()).cols();
276  }
277 
281  size_t rows() const { return Ab_.rows(); }
282 
286  size_t cols() const { return Ab_.cols(); }
287 
289  const SharedDiagonal& get_model() const { return model_; }
290 
292  SharedDiagonal& get_model() { return model_; }
293 
295  const constBVector getb() const { return Ab_(size()).col(0); }
296 
298  constABlock getA(const_iterator variable) const { return Ab_(variable - begin()); }
299 
301  constABlock getA() const { return Ab_.range(0, size()); }
302 
304  BVector getb() { return Ab_(size()).col(0); }
305 
307  ABlock getA(iterator variable) { return Ab_(variable - begin()); }
308 
310  ABlock getA() { return Ab_.range(0, size()); }
311 
317  void updateHessian(const KeyVector& keys, SymmetricBlockMatrix* info) const override;
318 
320  Vector operator*(const VectorValues& x) const;
321 
324  void transposeMultiplyAdd(double alpha, const Vector& e,
325  VectorValues& x) const;
326 
328  void multiplyHessianAdd(double alpha, const VectorValues& x,
329  VectorValues& y) const override;
330 
339  void multiplyHessianAdd(double alpha, const double* x, double* y,
340  const std::vector<size_t>& accumulatedDims) const;
341 
343  VectorValues gradientAtZero() const override;
344 
346  void gradientAtZero(double* d) const override;
347 
349  Vector gradient(Key key, const VectorValues& x) const override;
350 
352  JacobianFactor whiten() const;
353 
355  std::pair<boost::shared_ptr<GaussianConditional>, shared_ptr>
356  eliminate(const Ordering& keys);
357 
359  void setModel(bool anyConstrained, const Vector& sigmas);
360 
372  friend GTSAM_EXPORT std::pair<boost::shared_ptr<GaussianConditional>, shared_ptr>
373  EliminateQR(const GaussianFactorGraph& factors, const Ordering& keys);
374 
382  boost::shared_ptr<GaussianConditional> splitConditional(size_t nrFrontals);
383 
384  protected:
385 
387  template<typename TERMS>
388  void fillTerms(const TERMS& terms, const Vector& b, const SharedDiagonal& noiseModel);
389 
390  private:
391 
396  void JacobianFactorHelper(
397  const GaussianFactorGraph& graph,
398  const FastVector<VariableSlots::const_iterator>& orderedSlots);
399 
406  template<class KEYS, class DIMENSIONS>
407  JacobianFactor(const KEYS& keys, const DIMENSIONS& dims, DenseIndex m,
408  const SharedDiagonal& model = SharedDiagonal()) :
409  Base(keys), Ab_(dims.begin(), dims.end(), m, true), model_(model) {
410  }
411 
412  // be very selective on who can access these private methods:
413  template<typename T> friend class ExpressionFactor;
414 
416  friend class boost::serialization::access;
417  template<class ARCHIVE>
418  void save(ARCHIVE & ar, const unsigned int version) const {
419  // TODO(fan): This is a hack for Boost < 1.66
420  // We really need to introduce proper versioning in the archives
421  // As otherwise this will not read objects serialized by older
422  // versions of GTSAM
423  ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
424  ar << BOOST_SERIALIZATION_NVP(Ab_);
425  bool model_null = false;
426  if(model_.get() == nullptr) {
427  model_null = true;
428  ar << boost::serialization::make_nvp("model_null", model_null);
429  } else {
430  ar << boost::serialization::make_nvp("model_null", model_null);
431  ar << BOOST_SERIALIZATION_NVP(model_);
432  }
433  }
434 
435  template<class ARCHIVE>
436  void load(ARCHIVE & ar, const unsigned int version) {
437  // invoke serialization of the base class
438  ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
439  ar >> BOOST_SERIALIZATION_NVP(Ab_);
440  if (version < 1) {
441  ar >> BOOST_SERIALIZATION_NVP(model_);
442  } else {
443  bool model_null;
444  ar >> BOOST_SERIALIZATION_NVP(model_null);
445  if (!model_null) {
446  ar >> BOOST_SERIALIZATION_NVP(model_);
447  }
448  }
449  }
450 
451  BOOST_SERIALIZATION_SPLIT_MEMBER()
452  }; // JacobianFactor
454 template<>
455 struct traits<JacobianFactor> : public Testable<JacobianFactor> {
456 };
457 
458 } // \ namespace gtsam
459 
460 BOOST_CLASS_VERSION(gtsam::JacobianFactor, 1)
461 
462 #include <gtsam/linear/JacobianFactor-inl.h>
463 
464 
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
Matrix3f m
const VerticalBlockMatrix & matrixObject() const
Scalar * y
const Block< const Derived, internal::traits< Derived >::RowsAtCompileTime, 1,!IsRowMajor > ConstColXpr
Definition: BlockMethods.h:15
noiseModel::Diagonal::shared_ptr model
static enum @843 ordering
constABlock getA() const
A matrix with column blocks of pre-defined sizes. Used in JacobianFactor and GaussianConditional.
noiseModel::Diagonal::shared_ptr model_
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
A factor with a quadratic error function - a Gaussian.
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
size_t rows() const
Block< Derived, internal::traits< Derived >::RowsAtCompileTime, 1,!IsRowMajor > ColXpr
Definition: BlockMethods.h:14
GaussianFactorGraph factors(list_of(factor1)(factor2)(factor3))
ABlock::ColXpr BVector
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:45
NonlinearFactorGraph graph
JacobianFactor(const JacobianFactor &jf)
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
SharedDiagonal & get_model()
size_t cols() const
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:67
const KeyFormatter & formatter
Included from all GTSAM files.
VariableSlots describes the structure of a combined factor in terms of where each block comes from in...
else if n * info
void load(ARCHIVE &ar, const unsigned int version)
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
T negate(const T &x)
Definition: packetmath.cpp:27
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
void save(ARCHIVE &ar, const unsigned int version) const
Eigen::VectorXd Vector
Definition: Vector.h:38
GaussianFactor::shared_ptr clone() const override
const constBVector getb() const
constABlock::ConstColXpr constBVector
VerticalBlockMatrix::constBlock constABlock
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
RealScalar alpha
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
bool empty() const override
DenseIndex getDim(const_iterator variable) const override
VerticalBlockMatrix::Block ABlock
JacobianFactor(const KEYS &keys, const DIMENSIONS &dims, DenseIndex m, const SharedDiagonal &model=SharedDiagonal())
constABlock getA(const_iterator variable) const
boost::shared_ptr< Diagonal > shared_ptr
Definition: NoiseModel.h:301
const G & b
Definition: Group.h:83
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
ABlock getA(iterator variable)
const SharedDiagonal & get_model() const
Block range(DenseIndex startBlock, DenseIndex endBlock)
VerticalBlockMatrix & matrixObject()
noiseModel::Diagonal::shared_ptr SharedDiagonal
Definition: NoiseModel.h:736
traits
Definition: chartTesting.h:28
bool isConstrained() const
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
JacobianFactor This
Typedef to this class.
A Gaussian factor using the canonical parameters (information form)
VerticalBlockMatrix Ab_
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
static double error
Definition: testRot3.cpp:39
const G double tol
Definition: Group.h:83
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:67
DenseIndex cols() const
Column size.
const KeyVector keys
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
std::pair< GaussianConditional::shared_ptr, JacobianFactor::shared_ptr > EliminateQR(const GaussianFactorGraph &factors, const Ordering &keys)
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
GaussianFactor Base
Typedef to base class.
DenseIndex rows() const
Row size.


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:23