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 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_AUTODIFF_JACOBIAN_H
26 #define EIGEN_AUTODIFF_JACOBIAN_H
27 
28 namespace Eigen
29 {
30 
31 template<typename Functor> class AutoDiffJacobian : public Functor
32 {
33 public:
34  AutoDiffJacobian() : Functor() {}
35  AutoDiffJacobian(const Functor& f) : Functor(f) {}
36 
37  // forward constructors
38  template<typename T0>
39  AutoDiffJacobian(const T0& a0) : Functor(a0) {}
40  template<typename T0, typename T1>
41  AutoDiffJacobian(const T0& a0, const T1& a1) : Functor(a0, a1) {}
42  template<typename T0, typename T1, typename T2>
43  AutoDiffJacobian(const T0& a0, const T1& a1, const T2& a2) : Functor(a0, a1, a2) {}
44 
45  enum {
46  InputsAtCompileTime = Functor::InputsAtCompileTime,
47  ValuesAtCompileTime = Functor::ValuesAtCompileTime
48  };
49 
50  typedef typename Functor::InputType InputType;
51  typedef typename Functor::ValueType ValueType;
52  typedef typename Functor::JacobianType JacobianType;
53  typedef typename JacobianType::Scalar Scalar;
54  typedef typename JacobianType::Index Index;
55 
56  typedef Matrix<Scalar,InputsAtCompileTime,1> DerivativeType;
57  typedef AutoDiffScalar<DerivativeType> ActiveScalar;
58 
59 
60  typedef Matrix<ActiveScalar, InputsAtCompileTime, 1> ActiveInput;
61  typedef Matrix<ActiveScalar, ValuesAtCompileTime, 1> ActiveValue;
62 
63  void operator() (const InputType& x, ValueType* v, JacobianType* _jac=0) const
64  {
65  eigen_assert(v!=0);
66  if (!_jac)
67  {
68  Functor::operator()(x, v);
69  return;
70  }
71 
72  JacobianType& jac = *_jac;
73 
74  ActiveInput ax = x.template cast<ActiveScalar>();
75  ActiveValue av(jac.rows());
76 
77  if(InputsAtCompileTime==Dynamic)
78  for (Index j=0; j<jac.rows(); j++)
79  av[j].derivatives().resize(this->inputs());
80 
81  for (Index i=0; i<jac.cols(); i++)
82  ax[i].derivatives() = DerivativeType::Unit(this->inputs(),i);
83 
84  Functor::operator()(ax, &av);
85 
86  for (Index i=0; i<jac.rows(); i++)
87  {
88  (*v)[i] = av[i].value();
89  jac.row(i) = av[i].derivatives();
90  }
91  }
92 protected:
93 
94 };
95 
96 }
97 
98 #endif // EIGEN_AUTODIFF_JACOBIAN_H