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 
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 
34 namespace gtsam {
35 
56  template<size_t D>
58 
59  public:
60 
63 
74  const std::vector<Matrix>& Gs, const std::vector<Vector>& gs, double f) :
75  HessianFactor(js, Gs, gs, f) {
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 
121  : HessianFactor(jf) {
122  }
123 
132  const Scatter& scatter)
133  : HessianFactor(factors, scatter) {
134  checkInvariants();
135  }
136 
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
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 
174  public:
175 
183  void multiplyHessianAdd(double alpha, const VectorValues& x,
184  VectorValues& y) const override {
185  // Note: This implementation just calls the base class. The raw memory versions
186  // below are specifically optimized for the regular structure of this class.
187  // Consider using those directly or ensuring the base class implementation
188  // is efficient enough for your use case if calling this version.
190  }
191 
202  void multiplyHessianAdd(double alpha, const double* x,
203  double* yvalues) const {
204  // Ensure scratch space is properly sized
205  const size_t n = size();
206  if (y_.size() != n) {
207  y_.resize(n);
208  }
209  for (VectorD& yi : y_)
210  yi.setZero();
211 
212  // Loop over columns (j) of the Hessian H=info_
213  // Accessing x only once per column j
214  // Fill temporary y_ vector corresponding to rows i
215  for (DenseIndex j = 0; j < static_cast<DenseIndex>(n); ++j) {
216  Key key_j = keys_[j];
217  ConstDMap xj(x + key_j * D); // Map to the j-th block of x
218 
219  DenseIndex i = 0;
220  // Off-diagonal blocks G_ij * x_j for i < j
221  for (; i < j; ++i) {
222  y_[i] += info_.aboveDiagonalBlock(i, j) * xj;
223  }
224  // Diagonal block G_jj * x_j
225  y_[i] += info_.diagonalBlock(j) * xj;
226  // Off-diagonal blocks G_ij * x_j for i > j (using transpose G_ji^T * x_j)
227  for (i = j + 1; i < static_cast<DenseIndex>(n); ++i) {
228  y_[i] += info_.aboveDiagonalBlock(j, i).transpose() * xj;
229  }
230  }
231 
232  // Add accumulated results from y_ to the output yvalues
233  for (DenseIndex i = 0; i < static_cast<DenseIndex>(n); ++i) {
234  Key key_i = keys_[i];
235  DMap map_yi(yvalues + key_i * D); // Map to the i-th block of yvalues
236  map_yi += alpha * y_[i];
237  }
238  }
239 
252  void multiplyHessianAdd(double alpha, const double* x, double* yvalues,
253  const std::vector<size_t>& offsets) const {
254  // Ensure scratch space is properly sized
255  const size_t n = size();
256  if (y_.size() != n) {
257  y_.resize(n);
258  }
259  for (VectorD& yi : y_)
260  yi.setZero();
261 
262  // Loop over columns (j) of the Hessian H=info_
263  for (DenseIndex j = 0; j < static_cast<DenseIndex>(n); ++j) {
264  Key key_j = keys_[j];
265  size_t offset_j = offsets[key_j];
266  // Ensure block size matches D (redundant if checkInvariants worked, but safe)
267  size_t dim_j = offsets[key_j + 1] - offset_j;
268  if (dim_j != D) throw std::runtime_error("RegularHessianFactor::multiplyHessianAdd: Mismatched dimension in offset map.");
269  ConstDMap xj(x + offset_j); // Map to the j-th block of x using offset
270 
271  DenseIndex i = 0;
272  // Off-diagonal blocks G_ij * x_j for i < j
273  for (; i < j; ++i) {
274  y_[i] += info_.aboveDiagonalBlock(i, j) * xj;
275  }
276  // Diagonal block G_jj * x_j
277  y_[i] += info_.diagonalBlock(j) * xj;
278  // Off-diagonal blocks G_ij * x_j for i > j (using transpose G_ji^T * x_j)
279  for (i = j + 1; i < static_cast<DenseIndex>(n); ++i) {
280  y_[i] += info_.aboveDiagonalBlock(j, i).transpose() * xj;
281  }
282  }
283 
284  // Add accumulated results from y_ to the output yvalues using offsets
285  for (DenseIndex i = 0; i < static_cast<DenseIndex>(n); ++i) {
286  Key key_i = keys_[i];
287  size_t offset_i = offsets[key_i];
288  size_t dim_i = offsets[key_i + 1] - offset_i;
289  if (dim_i != D) throw std::runtime_error("RegularHessianFactor::multiplyHessianAdd: Mismatched dimension in offset map.");
290  DMap map_yi(yvalues + offset_i); // Map to the i-th block of yvalues using offset
291  map_yi += alpha * y_[i];
292  }
293  }
294 
302  void hessianDiagonal(double* d) const override {
303  // Loop over all variables (diagonal blocks) in the factor
304  const size_t n = size();
306  Key j = keys_[pos];
307  // Get the diagonal block G_jj and add its diagonal elements to d
308  DMap(d + D * j) += info_.diagonal(pos);
309  }
310  }
311 
320  void gradientAtZero(double* d) const override {
321  // The linear term g is stored as the last block column of info_ (negated).
322  // We add (-g) to d.
323  const size_t n = size();
325  Key j = keys_[pos];
326  // info_.aboveDiagonalBlock(pos, n) accesses the block corresponding to g_j
327  DMap(d + D * j) += info_.aboveDiagonalBlock(pos, n);
328  }
329  }
330 
331  /* ************************************************************************* */
332 
333  }; // end class RegularHessianFactor
334 
335  // traits
336  template<size_t D> struct traits<RegularHessianFactor<D> > : public Testable<
337  RegularHessianFactor<D> > {
338  };
339 
340 } // namespace gtsam
gtsam::RegularHessianFactor::DMap
Eigen::Map< VectorD > DMap
Definition: RegularHessianFactor.h:166
D
MatrixXcd D
Definition: EigenSolver_EigenSolver_MatrixType.cpp:14
gtsam::RegularHessianFactor::RegularHessianFactor
RegularHessianFactor(Key j1, Key j2, const MatrixD &G11, const MatrixD &G12, const VectorD &g1, const MatrixD &G22, const VectorD &g2, double f)
Definition: RegularHessianFactor.h:83
gtsam::HessianFactor::augmentedInformation
Matrix augmentedInformation() const override
Definition: HessianFactor.cpp:277
alpha
RealScalar alpha
Definition: level1_cplx_impl.h:147
gtsam::HessianFactor
A Gaussian factor using the canonical parameters (information form)
Definition: HessianFactor.h:99
d
static const double d[K][N]
Definition: igam.h:11
g1
Pose3 g1(Rot3(), Point3(100.0, 0.0, 300.0))
gtsam::RegularHessianFactor
A HessianFactor where all variables have the same dimension D.
Definition: RegularHessianFactor.h:57
simple_graph::factors
const GaussianFactorGraph factors
Definition: testJacobianFactor.cpp:213
x
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
Definition: gnuplot_common_settings.hh:12
HessianFactor.h
Contains the HessianFactor class, a general quadratic factor.
gtsam::HessianFactor::multiplyHessianAdd
void multiplyHessianAdd(double alpha, const VectorValues &x, VectorValues &y) const override
Definition: HessianFactor.cpp:384
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:91
gtsam::RegularHessianFactor::checkInvariants
void checkInvariants() const
Check if the factor has regular block structure.
Definition: RegularHessianFactor.h:157
n
int n
Definition: BiCGSTAB_simple.cpp:1
gtsam::HessianFactor::info_
SymmetricBlockMatrix info_
The full augmented information matrix, s.t. the quadratic error is 0.5*[x -1]'H[x -1].
Definition: HessianFactor.h:102
gtsam::GaussianFactorGraph
Definition: GaussianFactorGraph.h:73
gtsam::RegularHessianFactor::RegularHessianFactor
RegularHessianFactor(const RegularJacobianFactor< D > &jf)
Construct a RegularHessianFactor from a RegularJacobianFactor.
Definition: RegularHessianFactor.h:120
gtsam::VectorValues
Definition: VectorValues.h:74
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::RegularHessianFactor::hessianDiagonal
void hessianDiagonal(double *d) const override
Return the diagonal of the Hessian for this factor (Raw memory version).
Definition: RegularHessianFactor.h:302
g2
Pose3 g2(g1.expmap(h *V1_g1))
gtsam::RegularHessianFactor::RegularHessianFactor
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)
Definition: RegularHessianFactor.h:92
RegularJacobianFactor.h
JacobianFactor class with fixed sized blcoks.
gtsam::Scatter
Definition: Scatter.h:49
VectorValues.h
Factor Graph Values.
gtsam::SymmetricBlockMatrix::nBlocks
DenseIndex nBlocks() const
Block count.
Definition: SymmetricBlockMatrix.h:122
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
gtsam::RegularHessianFactor::ConstDMap
Eigen::Map< const VectorD > ConstDMap
Definition: RegularHessianFactor.h:167
y
Scalar * y
Definition: level1_cplx_impl.h:124
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
gtsam::RegularHessianFactor::VectorD
Eigen::Matrix< double, D, 1 > VectorD
Definition: RegularHessianFactor.h:61
gtsam::RegularHessianFactor::y_
std::vector< VectorD, Eigen::aligned_allocator< VectorD > > y_
Definition: RegularHessianFactor.h:172
gtsam::SymmetricBlockMatrix::cols
DenseIndex cols() const
Column size.
Definition: SymmetricBlockMatrix.h:119
gtsam::RegularHessianFactor::multiplyHessianAdd
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:202
gtsam
traits
Definition: ABC.h:17
gtsam::Testable
Definition: Testable.h:152
gtsam::Factor::keys_
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:88
gtsam::traits
Definition: Group.h:36
gtsam::Factor::keys
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:143
gtsam::DenseIndex
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:103
gtsam::RegularHessianFactor::gradientAtZero
void gradientAtZero(double *d) const override
Add the gradient vector (gradient at zero) to a raw memory block d.
Definition: RegularHessianFactor.h:320
gtsam::RegularHessianFactor::RegularHessianFactor
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
gtsam::SymmetricBlockMatrix::diagonal
Vector diagonal(DenseIndex J) const
Get the diagonal of the J'th diagonal block.
Definition: SymmetricBlockMatrix.h:147
gtsam::SymmetricBlockMatrix::diagonalBlock
Eigen::SelfAdjointView< Block, Eigen::Upper > diagonalBlock(DenseIndex J)
Return the J'th diagonal block as a self adjoint view.
Definition: SymmetricBlockMatrix.h:137
gtsam::RegularHessianFactor::RegularHessianFactor
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
offsets
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 y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set offsets
Definition: gnuplot_common_settings.hh:27
gtsam::RegularJacobianFactor
Definition: RegularJacobianFactor.h:32
gtsam::RegularHessianFactor::RegularHessianFactor
RegularHessianFactor(const GaussianFactorGraph &factors, const Scatter &scatter)
Construct from a GaussianFactorGraph combined using a Scatter.
Definition: RegularHessianFactor.h:131
gtsam::RegularHessianFactor::multiplyHessianAdd
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:183
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
gtsam::RegularHessianFactor::multiplyHessianAdd
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:252
gtsam::RegularHessianFactor::MatrixD
Eigen::Matrix< double, D, D > MatrixD
Definition: RegularHessianFactor.h:62
gtsam::SymmetricBlockMatrix::aboveDiagonalBlock
constBlock aboveDiagonalBlock(DenseIndex I, DenseIndex J) const
Get block above the diagonal (I, J).
Definition: SymmetricBlockMatrix.h:152
pos
Definition: example-NearestNeighbor.cpp:32
gtsam::Factor::size
size_t size() const
Definition: Factor.h:160
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::SymmetricBlockMatrix
Definition: SymmetricBlockMatrix.h:53
j1
double j1(double x)
Definition: j1.c:174
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::RegularHessianFactor::RegularHessianFactor
RegularHessianFactor(const GaussianFactorGraph &factors)
Construct from a GaussianFactorGraph.
Definition: RegularHessianFactor.h:143


gtsam
Author(s):
autogenerated on Wed May 28 2025 03:02:57