AutoDiffJacobian.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_AUTODIFF_JACOBIAN_H
11 #define EIGEN_AUTODIFF_JACOBIAN_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen
16 {
17 
18 template<typename Functor> class AutoDiffJacobian : public Functor
19 {
20 public:
22  AutoDiffJacobian(const Functor& f) : Functor(f) {}
23 
24  // forward constructors
25  template<typename... T>
26  AutoDiffJacobian(const T& ...Values) : Functor(Values...) {}
27 
28  typedef typename Functor::InputType InputType;
29  typedef typename Functor::ValueType ValueType;
30  typedef typename ValueType::Scalar Scalar;
31 
32  enum {
33  InputsAtCompileTime = InputType::RowsAtCompileTime,
34  ValuesAtCompileTime = ValueType::RowsAtCompileTime
35  };
36 
38  typedef typename JacobianType::Index Index;
39 
42 
45 
46  // Some compilers don't accept variadic parameters after a default parameter,
47  // i.e., we can't just write _jac=0 but we need to overload operator():
48  EIGEN_STRONG_INLINE
49  void operator() (const InputType& x, ValueType* v) const
50  {
51  this->operator()(x, v, 0);
52  }
53  template<typename... ParamsType>
54  void operator() (const InputType& x, ValueType* v, JacobianType* _jac,
55  const ParamsType&... Params) const
56  {
57  eigen_assert(v!=0);
58 
59  if (!_jac)
60  {
61  Functor::operator()(x, v, Params...);
62  return;
63  }
64 
65  JacobianType& jac = *_jac;
66 
67  ActiveInput ax = x.template cast<ActiveScalar>();
68  ActiveValue av(jac.rows());
69 
71  for (Index j=0; j<jac.rows(); j++)
72  av[j].derivatives().resize(x.rows());
73 
74  for (Index i=0; i<jac.cols(); i++)
75  ax[i].derivatives() = DerivativeType::Unit(x.rows(),i);
76 
77  Functor::operator()(ax, &av, Params...);
78 
79  for (Index i=0; i<jac.rows(); i++)
80  {
81  (*v)[i] = av[i].value();
82  jac.row(i) = av[i].derivatives();
83  }
84  }
85 };
86 
87 }
88 
89 #endif // EIGEN_AUTODIFF_JACOBIAN_H
Array< int, Dynamic, 1 > v
int i
#define eigen_assert(x)
Functor::InputType InputType
AutoDiffJacobian(const T &...Values)
ValueType::Scalar Scalar
Functor::ValueType ValueType
AutoDiffScalar< DerivativeType > ActiveScalar
Matrix< Scalar, InputsAtCompileTime, 1 > DerivativeType
Matrix< Scalar, ValuesAtCompileTime, InputsAtCompileTime > JacobianType
Matrix< ActiveScalar, ValuesAtCompileTime, 1 > ActiveValue
JacobianType::Index Index
Matrix< ActiveScalar, InputsAtCompileTime, 1 > ActiveInput
AutoDiffJacobian(const Functor &f)
void operator()(const InputType &x, ValueType *v) const
A scalar type replacement with automatic differentiation capability.
CoeffReturnType value() const
static const BasisReturnType Unit(Index i)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
const int Dynamic
Eigen::Index Index
std::ptrdiff_t j