blob: 33b6c393f49aa14ba01e86da6389edca5cf33753 [file] [log] [blame]
Brian Silverman72890c22015-09-19 14:37:37 -04001// 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
13namespace Eigen
14{
15
16template<typename Functor> class AutoDiffJacobian : public Functor
17{
18public:
19 AutoDiffJacobian() : Functor() {}
20 AutoDiffJacobian(const Functor& f) : Functor(f) {}
21
22 // forward constructors
Austin Schuh189376f2018-12-20 22:11:15 +110023#if EIGEN_HAS_VARIADIC_TEMPLATES
24 template<typename... T>
25 AutoDiffJacobian(const T& ...Values) : Functor(Values...) {}
26#else
Brian Silverman72890c22015-09-19 14:37:37 -040027 template<typename T0>
28 AutoDiffJacobian(const T0& a0) : Functor(a0) {}
29 template<typename T0, typename T1>
30 AutoDiffJacobian(const T0& a0, const T1& a1) : Functor(a0, a1) {}
31 template<typename T0, typename T1, typename T2>
32 AutoDiffJacobian(const T0& a0, const T1& a1, const T2& a2) : Functor(a0, a1, a2) {}
Austin Schuh189376f2018-12-20 22:11:15 +110033#endif
Brian Silverman72890c22015-09-19 14:37:37 -040034
35 typedef typename Functor::InputType InputType;
36 typedef typename Functor::ValueType ValueType;
Austin Schuh189376f2018-12-20 22:11:15 +110037 typedef typename ValueType::Scalar Scalar;
38
39 enum {
40 InputsAtCompileTime = InputType::RowsAtCompileTime,
41 ValuesAtCompileTime = ValueType::RowsAtCompileTime
42 };
43
44 typedef Matrix<Scalar, ValuesAtCompileTime, InputsAtCompileTime> JacobianType;
Brian Silverman72890c22015-09-19 14:37:37 -040045 typedef typename JacobianType::Index Index;
46
Austin Schuh189376f2018-12-20 22:11:15 +110047 typedef Matrix<Scalar, InputsAtCompileTime, 1> DerivativeType;
Brian Silverman72890c22015-09-19 14:37:37 -040048 typedef AutoDiffScalar<DerivativeType> ActiveScalar;
49
Brian Silverman72890c22015-09-19 14:37:37 -040050 typedef Matrix<ActiveScalar, InputsAtCompileTime, 1> ActiveInput;
51 typedef Matrix<ActiveScalar, ValuesAtCompileTime, 1> ActiveValue;
52
Austin Schuh189376f2018-12-20 22:11:15 +110053#if EIGEN_HAS_VARIADIC_TEMPLATES
54 // Some compilers don't accept variadic parameters after a default parameter,
55 // i.e., we can't just write _jac=0 but we need to overload operator():
56 EIGEN_STRONG_INLINE
57 void operator() (const InputType& x, ValueType* v) const
58 {
59 this->operator()(x, v, 0);
60 }
61 template<typename... ParamsType>
62 void operator() (const InputType& x, ValueType* v, JacobianType* _jac,
63 const ParamsType&... Params) const
64#else
Brian Silverman72890c22015-09-19 14:37:37 -040065 void operator() (const InputType& x, ValueType* v, JacobianType* _jac=0) const
Austin Schuh189376f2018-12-20 22:11:15 +110066#endif
Brian Silverman72890c22015-09-19 14:37:37 -040067 {
68 eigen_assert(v!=0);
Austin Schuh189376f2018-12-20 22:11:15 +110069
Brian Silverman72890c22015-09-19 14:37:37 -040070 if (!_jac)
71 {
Austin Schuh189376f2018-12-20 22:11:15 +110072#if EIGEN_HAS_VARIADIC_TEMPLATES
73 Functor::operator()(x, v, Params...);
74#else
Brian Silverman72890c22015-09-19 14:37:37 -040075 Functor::operator()(x, v);
Austin Schuh189376f2018-12-20 22:11:15 +110076#endif
Brian Silverman72890c22015-09-19 14:37:37 -040077 return;
78 }
79
80 JacobianType& jac = *_jac;
81
82 ActiveInput ax = x.template cast<ActiveScalar>();
83 ActiveValue av(jac.rows());
84
85 if(InputsAtCompileTime==Dynamic)
86 for (Index j=0; j<jac.rows(); j++)
Austin Schuh189376f2018-12-20 22:11:15 +110087 av[j].derivatives().resize(x.rows());
Brian Silverman72890c22015-09-19 14:37:37 -040088
89 for (Index i=0; i<jac.cols(); i++)
Austin Schuh189376f2018-12-20 22:11:15 +110090 ax[i].derivatives() = DerivativeType::Unit(x.rows(),i);
Brian Silverman72890c22015-09-19 14:37:37 -040091
Austin Schuh189376f2018-12-20 22:11:15 +110092#if EIGEN_HAS_VARIADIC_TEMPLATES
93 Functor::operator()(ax, &av, Params...);
94#else
Brian Silverman72890c22015-09-19 14:37:37 -040095 Functor::operator()(ax, &av);
Austin Schuh189376f2018-12-20 22:11:15 +110096#endif
Brian Silverman72890c22015-09-19 14:37:37 -040097
98 for (Index i=0; i<jac.rows(); i++)
99 {
100 (*v)[i] = av[i].value();
101 jac.row(i) = av[i].derivatives();
102 }
103 }
Brian Silverman72890c22015-09-19 14:37:37 -0400104};
105
106}
107
108#endif // EIGEN_AUTODIFF_JACOBIAN_H