autodiff_chain_jacobian.h
Go to the documentation of this file.
1 // Copyright (C) 2018
2 //
3 // This Source Code Form is subject to the terms of the Mozilla
4 // Public License v. 2.0. If a copy of the MPL was not distributed
5 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 //
7 // Modified from unsupported/Eigen/src/AutoDiff/AutoDiffJacobian.h
8 
9 #ifndef EIGEN_AUTODIFF_CHAIN_JACOBIAN_H_
10 #define EIGEN_AUTODIFF_CHAIN_JACOBIAN_H_
11 
14 
15 namespace Eigen
16 {
17 template <typename Functor>
19 {
20 public:
23 // forward constructors
24 #if EIGEN_HAS_VARIADIC_TEMPLATES
25  template <typename... T>
26  AutoDiffChainJacobian(const T &... Values) : Functor(Values...)
27  {
28  }
29 #else
30  template <typename T0>
31  AutoDiffChainJacobian(const T0 &a0) : Functor(a0)
32  {
33  }
34  template <typename T0, typename T1>
35  AutoDiffChainJacobian(const T0 &a0, const T1 &a1) : Functor(a0, a1)
36  {
37  }
38  template <typename T0, typename T1, typename T2>
39  AutoDiffChainJacobian(const T0 &a0, const T1 &a1, const T2 &a2) : Functor(a0, a1, a2)
40  {
41  }
42 #endif
43 
44  typedef typename Functor::InputType InputType;
45  typedef typename Functor::ValueType ValueType;
46  typedef typename ValueType::Scalar Scalar;
47 
48  enum
49  {
50  InputsAtCompileTime = InputType::RowsAtCompileTime,
51  ValuesAtCompileTime = ValueType::RowsAtCompileTime,
52  JacobianInputsAtCompileTime = Functor::JacobianColsAtCompileTime // JacobianInputsAtCompileTime no longer have to match InputsAtCompileTime
53  };
54 
55  typedef Matrix<Scalar, ValuesAtCompileTime, JacobianInputsAtCompileTime> JacobianType;
56 
57  typedef Matrix<Scalar, InputsAtCompileTime, JacobianInputsAtCompileTime> InputJacobianType; // Jacobian.cols() matches InputJacobian.cols()
58  typedef typename JacobianType::Index Index;
59 
60  typedef Matrix<Scalar, JacobianInputsAtCompileTime, 1> DerivativeType; // Derivative rows() matches InputJacobian.cols()
62 
63  typedef Matrix<ActiveScalar, InputsAtCompileTime, 1> ActiveInput;
64  typedef Matrix<ActiveScalar, ValuesAtCompileTime, 1> ActiveValue;
65 
66 #if EIGEN_HAS_VARIADIC_TEMPLATES
67  // Some compilers don't accept variadic parameters after a default parameter,
68  // i.e., we can't just write _jac=0 but we need to overload operator():
69  EIGEN_STRONG_INLINE
70  void operator()(const InputType &x, ValueType &v) const
71  {
72  this->operator()(x, v);
73  }
74 
75  template <typename... ParamsType>
76  void operator()(const InputType &x, ValueType &v, const ParamsType &... Params) const
77  {
78  this->operator()(x, v, Params...);
79  }
80 
81  template <typename... ParamsType>
82  void operator()(const InputType &x, ValueType &v, JacobianType &jac, const ParamsType &... Params) const
83  {
84  this->operator()(x, v, jac, nullptr, Params...);
85  }
86 
87  template <typename... ParamsType>
88  void operator()(const InputType &x, ValueType &v, JacobianType &jac, const InputJacobianType &ijac,
89  const ParamsType &... Params) const
90  {
91  this->operator()(x, v, jac, &ijac, Params...);
92  }
93 
94  // Optional parameter InputJacobian (_ijac)
95  template <typename... ParamsType>
96  void operator()(const InputType &x, ValueType &v, JacobianType &jac, const InputJacobianType *_ijac = 0,
97  const ParamsType &... Params) const
98 #else
99  EIGEN_STRONG_INLINE
100  void operator()(const InputType &x, ValueType &v) const
101  {
102  this->operator()(x, v);
103  }
104 
105  void operator()(const InputType &x, ValueType &v, JacobianType &jac) const
106  {
107  this->operator()(x, v, jac, nullptr);
108  }
109 
110  void operator()(const InputType &x, ValueType &v, JacobianType &jac, const InputJacobianType &ijac) const
111  {
112  this->operator()(x, v, jac, &ijac);
113  }
114 
115  void operator()(const InputType &x, ValueType &v, JacobianType &jac = 0, const InputJacobianType *_ijac = 0) const
116 #endif
117  {
118  ActiveInput ax = x.template cast<ActiveScalar>();
119  ActiveValue av(jac.rows());
120 
121  if (!_ijac)
122  {
124 
125  if (InputsAtCompileTime == Dynamic)
126  for (Index j = 0; j < jac.rows(); ++j)
127  av[j].derivatives().resize(x.rows());
128 
129  for (Index i = 0; i < jac.cols(); ++i)
130  ax[i].derivatives() = DerivativeType::Unit(x.rows(), i);
131  }
132  else
133  {
134  // If specified, copy derivatives from InputJacobian
135  const InputJacobianType &ijac = *_ijac;
136 
137  if (InputsAtCompileTime == Dynamic)
138  for (Index j = 0; j < jac.rows(); ++j)
139  av[j].derivatives().resize(ijac.cols());
140 
141  for (Index i = 0; i < x.rows(); ++i)
142  ax[i].derivatives() = ijac.row(i);
143  }
144 
145 #if EIGEN_HAS_VARIADIC_TEMPLATES
146  Functor::operator()(ax, av, Params...);
147 #else
148  Functor::operator()(ax, av);
149 #endif
150  for (Index i = 0; i < jac.rows(); ++i)
151  {
152  v[i] = av[i].value();
153  jac.row(i) = av[i].derivatives();
154  }
155  }
156 };
157 
158 } // namespace Eigen
159 
160 #endif // EIGEN_AUTODIFF_CHAIN_JACOBIAN_H_
Matrix< Scalar, ValuesAtCompileTime, JacobianInputsAtCompileTime > JacobianType
AutoDiffChainJacobian(const T0 &a0, const T1 &a1, const T2 &a2)
EIGEN_STRONG_INLINE void operator()(const InputType &x, ValueType &v) const
void operator()(const InputType &x, ValueType &v, JacobianType &jac) const
FunctorBase< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::Dynamic > Functor
Definition: functor.h:49
void operator()(const InputType &x, ValueType &v, JacobianType &jac=0, const InputJacobianType *_ijac=0) const
AutoDiffScalar< DerivativeType > ActiveScalar
Matrix< Scalar, InputsAtCompileTime, JacobianInputsAtCompileTime > InputJacobianType
void operator()(const InputType &x, ValueType &v, JacobianType &jac, const InputJacobianType &ijac) const
AutoDiffChainJacobian(const T0 &a0, const T1 &a1)
Matrix< ActiveScalar, InputsAtCompileTime, 1 > ActiveInput
Matrix< ActiveScalar, ValuesAtCompileTime, 1 > ActiveValue
Matrix< Scalar, JacobianInputsAtCompileTime, 1 > DerivativeType
double x


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:34:49