OptionalJacobian.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 
20 #pragma once
21 #include <cstddef>
22 #include <functional>
23 #include <gtsam/config.h> // Configuration from CMake
24 #include <Eigen/Dense>
25 #include <optional>
26 #include <stdexcept>
27 #include <string>
28 
29 namespace gtsam {
30 
37 template<int Rows, int Cols>
39 
40 public:
41 
45 
46 private:
47 
49 
50  // Trick from http://eigen.tuxfamily.org/dox/group__TutorialMapClass.html
51  // uses "placement new" to make map_ usurp the memory of the fixed size matrix
52  void usurp(double* data) {
54  }
55 
56  // Private and very dangerous constructor straight from memory
57  OptionalJacobian(double* data) : map_(nullptr) {
58  if (data) usurp(data);
59  }
60 
61  template<int M, int N>
62  friend class OptionalJacobian;
63 
64 public:
65 
68  map_(nullptr) {
69  }
70 
74  OptionalJacobian(std::nullptr_t /*unused*/) :
75  map_(nullptr) {
76  }
77 
80  map_(nullptr) {
81  usurp(fixed.data());
82  }
83 
86  map_(nullptr) {
87  if (fixedPtr)
88  usurp(fixedPtr->data());
89  }
90 
92  OptionalJacobian(Eigen::MatrixXd& dynamic) :
93  map_(nullptr) {
94  dynamic.resize(Rows, Cols); // no malloc if correct size
95  usurp(dynamic.data());
96  }
97 
99  OptionalJacobian(Eigen::MatrixXd* dynamic) :
100  map_(nullptr) {
101  if (dynamic) {
102  dynamic->resize(Rows, Cols); // no malloc if correct size
103  usurp(dynamic->data());
104  }
105  }
106 
111  template<class MATRIX>
113  map_(nullptr) {
114  if (dynamic_ref.rows() == Rows && dynamic_ref.cols() == Cols && !dynamic_ref.IsRowMajor) {
115  usurp(dynamic_ref.data());
116  } else {
117  throw std::invalid_argument(
118  std::string("OptionalJacobian called with wrong dimensions or "
119  "storage order.\n"
120  "Expected: ") +
121  "(" + std::to_string(Rows) + ", " + std::to_string(Cols) + ")");
122  }
123  }
124 
126  OptionalJacobian(std::nullopt_t /*none*/) :
127  map_(nullptr) {
128  }
129 
131  OptionalJacobian(const std::optional<std::reference_wrapper<Eigen::MatrixXd>> optional) :
132  map_(nullptr) {
133  if (optional) {
134  optional->get().resize(Rows, Cols);
135  usurp(optional->get().data());
136  }
137  }
140  // template <typename Derived, bool InnerPanel>
141  // OptionalJacobian(Eigen::Block<Derived,Rows,Cols,InnerPanel> block) : map_(nullptr) { ?? }
142 
144  operator bool() const {
145  return map_.data() != nullptr;
146  }
147 
150  return map_;
151  }
152 
155  return &map_;
156  }
157 
160  // template <int M, int N>
161  // OptionalJacobian<M, N> block(int startRow, int startCol) {
162  // if (*this)
163  // OptionalJacobian<M, N>(map_.block<M, N>(startRow, startCol));
164  // else
165  // return OptionalJacobian<M, N>();
166  // }
167 
171  template <int N>
173  if (*this)
174  return OptionalJacobian<Rows, N>(&map_(0,startCol));
175  else
176  return OptionalJacobian<Rows, N>();
177  }
178 
183 };
184 
185 // The pure dynamic specialization of this is needed to support
186 // variable-sized types. Note that this is designed to work like the
187 // boost optional scheme from GTSAM 3.
188 template<>
190 
191 public:
192 
194  typedef Eigen::MatrixXd Jacobian;
195 
196 private:
197 
199 
200 public:
201 
204  pointer_(nullptr) {
205  }
206 
208  OptionalJacobian(Jacobian* pointer) : pointer_(pointer) {}
209 
211  OptionalJacobian(Jacobian& dynamic) : pointer_(&dynamic) {}
212 
214  OptionalJacobian(std::nullopt_t /*none*/) :
215  pointer_(nullptr) {
216  }
217 
219  OptionalJacobian(const std::optional<std::reference_wrapper<Eigen::MatrixXd>> optional) :
220  pointer_(nullptr) {
221  if (optional) pointer_ = &((*optional).get());
222  }
223 
225  operator bool() const {
226  return pointer_!=nullptr;
227  }
228 
231  return *pointer_;
232  }
233 
235  Jacobian* operator->(){ return pointer_; }
236 };
237 
238 // forward declare
239 template <typename T> struct traits;
240 
246 template <class T, class A>
247 struct MakeJacobian {
249 };
250 
257 template<class T, class A>
261 };
262 
263 } // namespace gtsam
264 
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
gtsam::OptionalJacobian::OptionalJacobian
OptionalJacobian(std::nullopt_t)
Constructor with std::nullopt just makes empty.
Definition: OptionalJacobian.h:126
gtsam::OptionalJacobian::OptionalJacobian
OptionalJacobian(const std::optional< std::reference_wrapper< Eigen::MatrixXd >> optional)
Constructor compatible with old-style derivatives.
Definition: OptionalJacobian.h:131
gtsam::MakeJacobian
: meta-function to generate Jacobian
Definition: OptionalJacobian.h:247
gtsam::OptionalJacobian::OptionalJacobian
OptionalJacobian(Jacobian &fixed)
Constructor that will usurp data of a fixed-size matrix.
Definition: OptionalJacobian.h:79
gtsam::OptionalJacobian< Eigen::Dynamic, Eigen::Dynamic >::operator->
Jacobian * operator->()
TODO: operator->()
Definition: OptionalJacobian.h:235
gtsam::OptionalJacobian< Eigen::Dynamic, Eigen::Dynamic >::OptionalJacobian
OptionalJacobian(Jacobian *pointer)
Construct from pointer to dynamic matrix.
Definition: OptionalJacobian.h:208
gtsam::OptionalJacobian::OptionalJacobian
OptionalJacobian(Eigen::MatrixXd *dynamic)
Constructor that will resize a dynamic matrix (unless already correct)
Definition: OptionalJacobian.h:99
gtsam::OptionalJacobian< Eigen::Dynamic, Eigen::Dynamic >::OptionalJacobian
OptionalJacobian()
View on constructor argument, if given.
Definition: OptionalJacobian.h:203
gtsam::OptionalJacobian< Eigen::Dynamic, Eigen::Dynamic >::pointer_
Jacobian * pointer_
Definition: OptionalJacobian.h:198
gtsam::OptionalJacobian< Eigen::Dynamic, Eigen::Dynamic >::operator*
Jacobian & operator*()
De-reference, like boost optional.
Definition: OptionalJacobian.h:230
gtsam::OptionalJacobian::map_
Eigen::Map< Jacobian > map_
Definition: OptionalJacobian.h:48
data
int data[]
Definition: Map_placement_new.cpp:1
gtsam::MakeOptionalJacobian::type
OptionalJacobian< traits< T >::dimension, traits< A >::dimension > type
Definition: OptionalJacobian.h:260
gtsam::OptionalJacobian::OptionalJacobian
OptionalJacobian(Eigen::MatrixXd &dynamic)
Constructor that will resize a dynamic matrix (unless already correct)
Definition: OptionalJacobian.h:92
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
gtsam::OptionalJacobian< Eigen::Dynamic, Eigen::Dynamic >::Jacobian
Eigen::MatrixXd Jacobian
Jacobian size type.
Definition: OptionalJacobian.h:194
gtsam::OptionalJacobian< Eigen::Dynamic, Eigen::Dynamic >::OptionalJacobian
OptionalJacobian(const std::optional< std::reference_wrapper< Eigen::MatrixXd >> optional)
Constructor for optional matrix reference.
Definition: OptionalJacobian.h:219
gtsam::OptionalJacobian::OptionalJacobian
OptionalJacobian(Eigen::Ref< MATRIX > dynamic_ref)
Constructor from an Eigen::Ref value. Will not usurp if dimension is wrong.
Definition: OptionalJacobian.h:112
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
gtsam::OptionalJacobian::usurp
void usurp(double *data)
View on constructor argument, if given.
Definition: OptionalJacobian.h:52
gtsam::MakeJacobian::type
Eigen::Matrix< double, traits< T >::dimension, traits< A >::dimension > type
Definition: OptionalJacobian.h:248
gtsam::OptionalJacobian::OptionalJacobian
OptionalJacobian()
Default constructor.
Definition: OptionalJacobian.h:67
gtsam::OptionalJacobian::operator->
Eigen::Map< Jacobian > * operator->()
operator->()
Definition: OptionalJacobian.h:154
gtsam::OptionalJacobian< Eigen::Dynamic, Eigen::Dynamic >::OptionalJacobian
OptionalJacobian(Jacobian &dynamic)
Construct from refrence to dynamic matrix.
Definition: OptionalJacobian.h:211
gtsam::OptionalJacobian::cols
OptionalJacobian< Rows, N > cols(int startCol)
Definition: OptionalJacobian.h:172
Eigen::Ref
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:281
gtsam
traits
Definition: SFMdata.h:40
gtsam::traits
Definition: Group.h:36
gtsam::OptionalJacobian< Eigen::Dynamic, Eigen::Dynamic >::OptionalJacobian
OptionalJacobian(std::nullopt_t)
Constructor with std::nullopt just makes empty.
Definition: OptionalJacobian.h:214
gtsam::OptionalJacobian
Definition: OptionalJacobian.h:38
gtsam::OptionalJacobian::operator*
Eigen::Map< Jacobian > & operator*()
De-reference, like boost optional.
Definition: OptionalJacobian.h:149
Eigen::PlainObjectBase::data
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar * data() const
Definition: PlainObjectBase.h:247
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
gtsam::OptionalJacobian::Jacobian
Eigen::Matrix< double, Rows, Cols > Jacobian
Definition: OptionalJacobian.h:44
gtsam::OptionalJacobian::OptionalJacobian
OptionalJacobian(double *data)
Definition: OptionalJacobian.h:57
gtsam::OptionalJacobian::OptionalJacobian
OptionalJacobian(std::nullptr_t)
Definition: OptionalJacobian.h:74
gtsam::OptionalJacobian::OptionalJacobian
OptionalJacobian(Jacobian *fixedPtr)
Constructor that will usurp data of a fixed-size matrix, pointer version.
Definition: OptionalJacobian.h:85
gtsam::MakeOptionalJacobian
: meta-function to generate JacobianTA optional reference Used mainly by Expressions
Definition: OptionalJacobian.h:258
Cols
static const int Cols
Definition: testCallRecord.cpp:32


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:12:24