gtsam
Loading...
Searching...
No Matches
RegularHessianFactor.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
25
26#pragma once
27
30#include <gtsam/linear/VectorValues.h> // For VectorValues and Scatter
31#include <vector>
32#include <stdexcept> // For std::invalid_argument
33
34namespace gtsam {
35
56 template<size_t D>
58
59 public:
60
61 typedef Eigen::Matrix<double, D, 1> VectorD;
62 typedef Eigen::Matrix<double, D, D> MatrixD;
63
74 const std::vector<Matrix>& Gs, const std::vector<Vector>& gs, double f) :
75 HessianFactor(js, Gs, gs, f) {
76 checkInvariants();
77 }
78
83 RegularHessianFactor(Key j1, Key j2, const MatrixD& G11, const MatrixD& G12,
84 const VectorD& g1, const MatrixD& G22, const VectorD& g2, double f) :
85 HessianFactor(j1, j2, G11, G12, g1, G22, g2, f) {
86 }
87
93 const MatrixD& G11, const MatrixD& G12, const MatrixD& G13, const VectorD& g1,
94 const MatrixD& G22, const MatrixD& G23, const VectorD& g2,
95 const MatrixD& G33, const VectorD& g3, double f) :
96 HessianFactor(j1, j2, j3, G11, G12, G13, g1, G22, G23, g2, G33, g3, f) {
97 }
98
107 template<typename KEYS>
111 checkInvariants();
112 }
113
123
132 const Scatter& scatter)
133 : HessianFactor(factors, scatter) {
134 checkInvariants();
135 }
136
144 : HessianFactor(factors) {
145 checkInvariants();
146 }
147
148 private:
149
157 void checkInvariants() const {
158 if (info_.cols() != 0 && // Allow zero-key factors (e.g. priors on anchor nodes)
159 info_.cols() != 1 + (info_.nBlocks() - 1) * static_cast<DenseIndex>(D))
160 throw std::invalid_argument(
161 "RegularHessianFactor constructor was given non-regular factors or "
162 "incorrect template dimension D");
163 }
164
165 // Use Eigen magic to access raw memory for efficiency in Hessian products
166 typedef Eigen::Map<VectorD> DMap;
167 typedef Eigen::Map<const VectorD> ConstDMap;
168
169 // Scratch space for multiplyHessianAdd to avoid re-allocation
170 // According to link below this is thread-safe.
171 // http://stackoverflow.com/questions/11160964/multiple-copies-of-the-same-object-c-thread-safe
172 mutable std::vector<VectorD, Eigen::aligned_allocator<VectorD>> y_;
173
175 template <class OffsetPolicy>
176 void multiplyHessianAddImpl(double alpha, const double* x, double* yvalues,
177 const OffsetPolicy& offset) const {
178 const size_t n = size();
179 if (y_.size() != n) y_.resize(n);
180 for (VectorD& yi : y_) yi.setZero();
181
182 for (DenseIndex j = 0; j < static_cast<DenseIndex>(n); ++j) {
183 ConstDMap xj(x + offset(keys_[j]));
184 DenseIndex i = 0;
185 for (; i < j; ++i) {
186 y_[i] += info_.aboveDiagonalBlock(i, j) * xj;
187 }
188 y_[i] += info_.diagonalBlock(j) * xj;
189 for (i = j + 1; i < static_cast<DenseIndex>(n); ++i) {
190 y_[i] += info_.aboveDiagonalBlock(j, i).transpose() * xj;
191 }
192 }
193
194 for (DenseIndex i = 0; i < static_cast<DenseIndex>(n); ++i) {
195 DMap(yvalues + offset(keys_[i])) += alpha * y_[i];
196 }
197 }
198
199 public:
200
208 void multiplyHessianAdd(double alpha, const VectorValues& x,
209 VectorValues& y) const override {
210 // Note: This implementation just calls the base class. The raw memory versions
211 // below are specifically optimized for the regular structure of this class.
212 // Consider using those directly or ensuring the base class implementation
213 // is efficient enough for your use case if calling this version.
215 }
216
227 void multiplyHessianAdd(double alpha, const double* x,
228 double* yvalues) const {
229 multiplyHessianAddImpl(alpha, x, yvalues,
230 [](Key key) { return key * D; });
231 }
232
245 void multiplyHessianAdd(double alpha, const double* x, double* yvalues,
246 const std::vector<size_t>& offsets) const {
247 const auto offset = [&offsets](Key key) {
248 if (offsets.size() < 2 || key >= offsets.size() - 1 ||
249 offsets[key + 1] - offsets[key] != D) {
250 throw std::runtime_error(
251 "RegularHessianFactor::multiplyHessianAdd: Mismatched "
252 "dimension in offset map.");
253 }
254 return offsets[key];
255 };
256 multiplyHessianAddImpl(alpha, x, yvalues, offset);
257 }
258
266 void hessianDiagonal(double* d) const override {
267 // Loop over all variables (diagonal blocks) in the factor
268 const size_t n = size();
269 for (DenseIndex pos = 0; pos < static_cast<DenseIndex>(n); ++pos) {
270 Key j = keys_[pos];
271 // Get the diagonal block G_jj and add its diagonal elements to d
272 DMap(d + D * j) += info_.diagonal(pos);
273 }
274 }
275
284 void gradientAtZero(double* d) const override {
285 // The linear term g is stored as the last block column of info_ (negated).
286 // We add (-g) to d.
287 const size_t n = size();
288 for (DenseIndex pos = 0; pos < static_cast<DenseIndex>(n); ++pos) {
289 Key j = keys_[pos];
290 // info_.aboveDiagonalBlock(pos, n) accesses the block corresponding to g_j
291 DMap(d + D * j) += info_.aboveDiagonalBlock(pos, n);
292 }
293 }
294
295 /* ************************************************************************* */
296
297 }; // end class RegularHessianFactor
298
299 // traits
300 template<size_t D> struct traits<RegularHessianFactor<D> > : public Testable<
301 RegularHessianFactor<D> > {
302 };
303
304} // namespace gtsam
Contains the HessianFactor class, a general quadratic factor.
JacobianFactor class with fixed sized blcoks.
Factor Graph Values.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
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
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
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
DenseIndex cols() const
Column size.
Definition SymmetricBlockMatrix.h:159
constBlock aboveDiagonalBlock(DenseIndex I, DenseIndex J) const
Get block above the diagonal (I, J).
Definition SymmetricBlockMatrix.h:205
DenseIndex nBlocks() const
Block count.
Definition SymmetricBlockMatrix.h:162
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:88
size_t size() const
Definition Factor.h:160
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition GaussianFactorGraph.h:77
Matrix augmentedInformation() const override
Return the augmented information matrix represented by this GaussianFactor.
Definition HessianFactor.cpp:338
SymmetricBlockMatrix info_
The full augmented information matrix, s.t. the quadratic error is 0.5*[x -1]'H[x -1].
Definition HessianFactor.h:104
void multiplyHessianAdd(double alpha, const VectorValues &x, VectorValues &y) const override
y += alpha * A'*A*x
Definition HessianFactor.cpp:487
HessianFactor()
default constructor for I/O
Definition HessianFactor.cpp:75
A HessianFactor where all variables have the same dimension D.
Definition RegularHessianFactor.h:57
RegularHessianFactor(const GaussianFactorGraph &factors)
Construct from a GaussianFactorGraph.
Definition RegularHessianFactor.h:143
void multiplyHessianAdd(double alpha, const VectorValues &x, VectorValues &y) const override
Multiply the Hessian part of the factor times a VectorValues x and add the result to y.
Definition RegularHessianFactor.h:208
RegularHessianFactor(Key j1, Key j2, Key j3, const MatrixD &G11, const MatrixD &G12, const MatrixD &G13, const VectorD &g1, const MatrixD &G22, const MatrixD &G23, const VectorD &g2, const MatrixD &G33, const VectorD &g3, double f)
Construct a ternary factor.
Definition RegularHessianFactor.h:92
void multiplyHessianAdd(double alpha, const double *x, double *yvalues, const std::vector< size_t > &offsets) const
Multiply the Hessian part of the factor times a raw vector x and add the result to y.
Definition RegularHessianFactor.h:245
void multiplyHessianAdd(double alpha, const double *x, double *yvalues) const
Multiply the Hessian part of the factor times a raw vector x and add the result to y.
Definition RegularHessianFactor.h:227
void hessianDiagonal(double *d) const override
Return the diagonal of the Hessian for this factor (Raw memory version).
Definition RegularHessianFactor.h:266
RegularHessianFactor(const KEYS &keys, const SymmetricBlockMatrix &augmentedInformation)
Constructor with an arbitrary number of keys and the augmented information matrix specified as a bloc...
Definition RegularHessianFactor.h:108
void gradientAtZero(double *d) const override
Add the gradient vector (gradient at zero) to a raw memory block d.
Definition RegularHessianFactor.h:284
RegularHessianFactor(const GaussianFactorGraph &factors, const Scatter &scatter)
Construct from a GaussianFactorGraph combined using a Scatter.
Definition RegularHessianFactor.h:131
RegularHessianFactor(const KeyVector &js, const std::vector< Matrix > &Gs, const std::vector< Vector > &gs, double f)
Construct an n-way factor from supplied components.
Definition RegularHessianFactor.h:73
RegularHessianFactor(Key j1, Key j2, const MatrixD &G11, const MatrixD &G12, const VectorD &g1, const MatrixD &G22, const VectorD &g2, double f)
Construct a binary factor.
Definition RegularHessianFactor.h:83
RegularHessianFactor(const RegularJacobianFactor< D > &jf)
Construct a RegularHessianFactor from a RegularJacobianFactor.
Definition RegularHessianFactor.h:120
JacobianFactor with constant sized blocks Provides raw memory access versions of linear operator.
Definition RegularJacobianFactor.h:32
Scatter is an intermediate data structure used when building a HessianFactor incrementally,...
Definition Scatter.h:49
VectorValues represents a collection of vector-valued variables associated each with a unique integer...
Definition VectorValues.h:73