Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
AutoDiffJacobian.h
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_AUTODIFF_JACOBIAN_H
12#define EIGEN_AUTODIFF_JACOBIAN_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19template <typename Functor>
20class AutoDiffJacobian : public Functor {
21 public:
22 AutoDiffJacobian() : Functor() {}
23 AutoDiffJacobian(const Functor& f) : Functor(f) {}
24
25 // forward constructors
26 template <typename... T>
27 AutoDiffJacobian(const T&... Values) : Functor(Values...) {}
28
29 typedef typename Functor::InputType InputType;
30 typedef typename Functor::ValueType ValueType;
31 typedef typename ValueType::Scalar Scalar;
32
33 enum { InputsAtCompileTime = InputType::RowsAtCompileTime, ValuesAtCompileTime = ValueType::RowsAtCompileTime };
34
35 typedef Matrix<Scalar, ValuesAtCompileTime, InputsAtCompileTime> JacobianType;
36 typedef typename JacobianType::Index Index;
37
38 typedef Matrix<Scalar, InputsAtCompileTime, 1> DerivativeType;
39 typedef AutoDiffScalar<DerivativeType> ActiveScalar;
40
41 typedef Matrix<ActiveScalar, InputsAtCompileTime, 1> ActiveInput;
42 typedef Matrix<ActiveScalar, ValuesAtCompileTime, 1> ActiveValue;
43
44 // Some compilers don't accept variadic parameters after a default parameter,
45 // i.e., we can't just write _jac=0 but we need to overload operator():
46 EIGEN_STRONG_INLINE void operator()(const InputType& x, ValueType* v) const { this->operator()(x, v, 0); }
47 template <typename... ParamsType>
48 void operator()(const InputType& x, ValueType* v, JacobianType* _jac, const ParamsType&... Params) const {
49 eigen_assert(v != 0);
50
51 if (!_jac) {
52 Functor::operator()(x, v, Params...);
53 return;
54 }
55
56 JacobianType& jac = *_jac;
57
58 ActiveInput ax = x.template cast<ActiveScalar>();
59 ActiveValue av(jac.rows());
60
61 EIGEN_IF_CONSTEXPR (InputsAtCompileTime == Dynamic)
62 for (Index j = 0; j < jac.rows(); j++) av[j].derivatives().resize(x.rows());
63
64 for (Index i = 0; i < jac.cols(); i++) ax[i].derivatives() = DerivativeType::Unit(x.rows(), i);
65
66 Functor::operator()(ax, &av, Params...);
67
68 for (Index i = 0; i < jac.rows(); i++) {
69 (*v)[i] = av[i].value();
70 jac.row(i) = av[i].derivatives();
71 }
72 }
73};
74
75} // namespace Eigen
76
77#endif // EIGEN_AUTODIFF_JACOBIAN_H
Namespace containing all symbols from the Eigen library.