AdaptAutoDiff.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 
19 #pragma once
20 
21 #include <gtsam/base/VectorSpace.h>
24 
25 namespace gtsam {
26 
34 template <typename FUNCTOR, int M, int N1, int N2>
38 
42 
43  FUNCTOR f;
44 
45  public:
46  VectorT operator()(const Vector1& v1, const Vector2& v2,
48  OptionalJacobian<M, N2> H2 = {}) {
50 
51  bool success;
52  VectorT result;
53 
54  if (H1 || H2) {
55  // Get derivatives with AutoDiff
56  const double* parameters[] = {v1.data(), v2.data()};
57  double rowMajor1[M * N1] = {}, rowMajor2[M * N2] = {}; // on the stack
58  double* jacobians[] = {rowMajor1, rowMajor2};
59  success = AutoDiff<FUNCTOR, double, N1, N2>::Differentiate(
60  f, parameters, M, result.data(), jacobians);
61 
62  // Convert from row-major to columnn-major
63  // TODO: if this is a bottleneck (probably not!) fix Autodiff to be
64  // Column-Major
65  if (H1) *H1 = Eigen::Map<RowMajor1>(rowMajor1);
66  if (H2) *H2 = Eigen::Map<RowMajor2>(rowMajor2);
67 
68  } else {
69  // Apply the mapping, to get result
70  success = f(v1.data(), v2.data(), result.data());
71  }
72  if (!success)
73  throw std::runtime_error(
74  "AdaptAutoDiff: function call resulted in failure");
75  return result;
76  }
77 };
78 
79 } // namespace gtsam
Eigen::Matrix< double, M, 1 > VectorT
Definition: AdaptAutoDiff.h:39
VectorT operator()(const Vector1 &v1, const Vector2 &v2, OptionalJacobian< M, N1 > H1={}, OptionalJacobian< M, N2 > H2={})
Definition: AdaptAutoDiff.h:46
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51
Vector v2
Vector v1
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
Eigen::Matrix< double, M, N1, Eigen::RowMajor > RowMajor1
Definition: AdaptAutoDiff.h:36
Values result
Eigen::Matrix< double, N1, 1 > Vector1
Definition: AdaptAutoDiff.h:40
static ConjugateGradientParameters parameters
Eigen::Matrix< double, N2, 1 > Vector2
Definition: AdaptAutoDiff.h:41
traits
Definition: chartTesting.h:28
Special class for optional Jacobian arguments.
The matrix class, also used for vectors and row-vectors.
Eigen::Matrix< double, M, N2, Eigen::RowMajor > RowMajor2
Definition: AdaptAutoDiff.h:37


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:33:53