gtsam
Loading...
Searching...
No Matches
JacobianFactor-inl.h
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
22
23#include <array>
24
25#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 13
26#pragma GCC diagnostic warning "-Wstringop-overread"
27#endif
28
29namespace gtsam {
30
31 /* ************************************************************************* */
32 template <typename TERMS>
33 JacobianFactor::JacobianFactor(const TERMS& terms, const Vector& b,
34 const SharedDiagonal& model) {
35 fillTerms(terms, b, model);
36 }
37
38 /* ************************************************************************* */
39 template <int M, int N1, typename>
40 JacobianFactor::JacobianFactor(Key i1, const Eigen::Matrix<double, M, N1>& A1,
41 const Eigen::Matrix<double, M, 1>& b,
42 const SharedDiagonal& model)
43 : Base(std::array<Key, 1>{{i1}}) {
44 const DenseIndex rows = static_cast<DenseIndex>(b.rows());
45 if (model && (DenseIndex)model->dim() != rows)
46 throw InvalidNoiseModel(rows, model->dim());
47
48 const std::array<size_t, 1> dims = {
49 static_cast<size_t>(A1.cols())};
50 Ab_ = VerticalBlockMatrix(dims, rows, true);
51 Ab_(0) = A1;
52 getb() = b;
53 model_ = model;
54 }
55
56 /* ************************************************************************* */
57 template <int M, int N1, int N2, typename>
58 JacobianFactor::JacobianFactor(Key i1, const Eigen::Matrix<double, M, N1>& A1,
59 Key i2, const Eigen::Matrix<double, M, N2>& A2,
60 const Eigen::Matrix<double, M, 1>& b,
61 const SharedDiagonal& model)
62 : Base(std::array<Key, 2>{{i1, i2}}) {
63 const DenseIndex rows = static_cast<DenseIndex>(b.rows());
64 if (model && (DenseIndex)model->dim() != rows)
65 throw InvalidNoiseModel(rows, model->dim());
66
67 const std::array<size_t, 2> dims = {
68 static_cast<size_t>(A1.cols()),
69 static_cast<size_t>(A2.cols())};
70 Ab_ = VerticalBlockMatrix(dims, rows, true);
71 Ab_(0) = A1;
72 Ab_(1) = A2;
73 getb() = b;
74 model_ = model;
75 }
76
77 /* ************************************************************************* */
78 template <int M, int N1, int N2, int N3, typename>
79 JacobianFactor::JacobianFactor(Key i1, const Eigen::Matrix<double, M, N1>& A1,
80 Key i2, const Eigen::Matrix<double, M, N2>& A2,
81 Key i3, const Eigen::Matrix<double, M, N3>& A3,
82 const Eigen::Matrix<double, M, 1>& b,
83 const SharedDiagonal& model)
84 : Base(std::array<Key, 3>{{i1, i2, i3}}) {
85 const DenseIndex rows = static_cast<DenseIndex>(b.rows());
86 if (model && (DenseIndex)model->dim() != rows)
87 throw InvalidNoiseModel(rows, model->dim());
88
89 const std::array<size_t, 3> dims = {
90 static_cast<size_t>(A1.cols()),
91 static_cast<size_t>(A2.cols()),
92 static_cast<size_t>(A3.cols())};
93 Ab_ = VerticalBlockMatrix(dims, rows, true);
94 Ab_(0) = A1;
95 Ab_(1) = A2;
96 Ab_(2) = A3;
97 getb() = b;
98 model_ = model;
99 }
100
101 /* ************************************************************************* */
102 template <typename KEYS>
104 const VerticalBlockMatrix& augmentedMatrix,
105 const SharedDiagonal& model)
106 : Base(keys), Ab_(augmentedMatrix), model_(model) {
107 checkAb(model, augmentedMatrix);
108 }
109
110 /* ************************************************************************* */
111 template <typename KEYS>
113 VerticalBlockMatrix&& augmentedMatrix,
114 const SharedDiagonal& model)
115 : Base(keys), Ab_(std::move(augmentedMatrix)), model_(model) {
116 checkAb(model, Ab_);
117 }
118
119 /* ************************************************************************* */
120 template<typename TERMS>
121 void JacobianFactor::fillTerms(const TERMS& terms, const Vector& b, const SharedDiagonal& noiseModel)
122 {
123 // Check noise model dimension
124 if(noiseModel && (DenseIndex)noiseModel->dim() != b.size())
125 throw InvalidNoiseModel(b.size(), noiseModel->dim());
126
127 // Resize base class key vector
128 Base::keys_.resize(terms.size());
129
130 // Get dimensions of matrices
131 std::vector<size_t> dimensions;
132 dimensions.reserve(terms.size());
133 for(typename TERMS::const_iterator it = terms.begin(); it != terms.end(); ++it) {
134 const std::pair<Key, Matrix>& term = *it;
135 const Matrix& Ai = term.second;
136 dimensions.push_back(Ai.cols());
137 }
138
139 // Construct block matrix
140 Ab_ = VerticalBlockMatrix(dimensions, b.size(), true);
141
142 // Check and add terms
143 DenseIndex i = 0; // For block index
144 for(typename TERMS::const_iterator it = terms.begin(); it != terms.end(); ++it) {
145 const std::pair<Key, Matrix>& term = *it;
146 Key key = term.first;
147 const Matrix& Ai = term.second;
148
149 // Check block rows
150 if(Ai.rows() != Ab_.rows())
151 throw InvalidMatrixBlock(Ab_.rows(), Ai.rows());
152
153 // Assign key and matrix
154 Base::keys_[i] = key;
155 Ab_(i) = Ai;
156
157 // Increment block index
158 ++ i;
159 }
160
161 // Assign RHS vector
162 getb() = b;
163
164 // Assign noise model
165 model_ = noiseModel;
166 }
167
168} // gtsam
Exceptions that may be thrown by linear solver components.
STL namespace.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition types.h:49
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
All noise models live in the noiseModel namespace.
Definition LossFunctions.cpp:33
This class stores a dense matrix and allows it to be accessed as a collection of vertical blocks.
Definition VerticalBlockMatrix.h:47
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
const constBVector getb() const
Get a view of the r.h.s.
Definition JacobianFactor.h:344
JacobianFactor()
default constructor for I/O
Definition JacobianFactor.cpp:48
void fillTerms(const TERMS &terms, const Vector &b, const SharedDiagonal &noiseModel)
Internal function to fill blocks and set dimensions.
Definition JacobianFactor-inl.h:121
GaussianFactor Base
Typedef to base class.
Definition JacobianFactor.h:96
void checkAb(const SharedDiagonal &model, const VerticalBlockMatrix &augmentedMatrix) const
Common code between VerticalBlockMatrix constructors.
Definition JacobianFactor.cpp:155
An exception indicating that the noise model dimension passed into a JacobianFactor has a different d...
Definition linearExceptions.h:128
An exception indicating that a matrix block passed into a JacobianFactor has a different dimensionali...
Definition linearExceptions.h:143