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) {
53  new (&map_) Eigen::Map<Jacobian>(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 
79  OptionalJacobian(Jacobian& fixed) :
80  map_(nullptr) {
81  usurp(fixed.data());
82  }
83 
85  OptionalJacobian(Jacobian* fixedPtr) :
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 
198  Jacobian* pointer_;
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 
230  Jacobian& operator*() {
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 
void usurp(double *data)
View on constructor argument, if given.
OptionalJacobian< traits< T >::dimension, traits< A >::dimension > type
OptionalJacobian(const std::optional< std::reference_wrapper< Eigen::MatrixXd >> optional)
Constructor compatible with old-style derivatives.
OptionalJacobian(double *data)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
: meta-function to generate Jacobian
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
OptionalJacobian< Rows, N > cols(int startCol)
Eigen::Matrix< double, traits< T >::dimension, traits< A >::dimension > type
OptionalJacobian(std::nullptr_t)
int data[]
OptionalJacobian()
Default constructor.
static const int Cols
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:281
traits
Definition: chartTesting.h:28
OptionalJacobian(Eigen::MatrixXd *dynamic)
Constructor that will resize a dynamic matrix (unless already correct)
OptionalJacobian(std::nullopt_t)
Constructor with std::nullopt just makes empty.
OptionalJacobian(Jacobian &dynamic)
Construct from refrence to dynamic matrix.
OptionalJacobian(std::nullopt_t)
Constructor with std::nullopt just makes empty.
OptionalJacobian(Jacobian &fixed)
Constructor that will usurp data of a fixed-size matrix.
: meta-function to generate JacobianTA optional reference Used mainly by Expressions ...
OptionalJacobian(Jacobian *pointer)
Construct from pointer to dynamic matrix.
const int Dynamic
Definition: Constants.h:22
Eigen::Map< Jacobian > * operator->()
operator->()
OptionalJacobian()
View on constructor argument, if given.
The matrix class, also used for vectors and row-vectors.
OptionalJacobian(Eigen::MatrixXd &dynamic)
Constructor that will resize a dynamic matrix (unless already correct)
OptionalJacobian(Jacobian *fixedPtr)
Constructor that will usurp data of a fixed-size matrix, pointer version.
Eigen::Matrix< double, Rows, Cols > Jacobian
OptionalJacobian(const std::optional< std::reference_wrapper< Eigen::MatrixXd >> optional)
Constructor for optional matrix reference.
Jacobian & operator*()
De-reference, like boost optional.
Eigen::Map< Jacobian > & operator*()
De-reference, like boost optional.
OptionalJacobian(Eigen::Ref< MATRIX > dynamic_ref)
Constructor from an Eigen::Ref value. Will not usurp if dimension is wrong.
Eigen::Map< Jacobian > map_


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:59