NumericalDiff.h
Go to the documentation of this file.
1 // -*- coding: utf-8
2 // vim: set fileencoding=utf-8
3 
4 // This file is part of Eigen, a lightweight C++ template library
5 // for linear algebra.
6 //
7 // Copyright (C) 2009 Thomas Capricelli <orzel@freehackers.org>
8 //
9 // This Source Code Form is subject to the terms of the Mozilla
10 // Public License v. 2.0. If a copy of the MPL was not distributed
11 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 
13 #ifndef EIGEN_NUMERICAL_DIFF_H
14 #define EIGEN_NUMERICAL_DIFF_H
15 
16 namespace Eigen {
17 
21 };
22 
23 
35 template<typename _Functor, NumericalDiffMode mode=Forward>
36 class NumericalDiff : public _Functor
37 {
38 public:
39  typedef _Functor Functor;
40  typedef typename Functor::Scalar Scalar;
41  typedef typename Functor::InputType InputType;
42  typedef typename Functor::ValueType ValueType;
43  typedef typename Functor::JacobianType JacobianType;
44 
45  NumericalDiff(Scalar _epsfcn=0.) : Functor(), epsfcn(_epsfcn) {}
46  NumericalDiff(const Functor& f, Scalar _epsfcn=0.) : Functor(f), epsfcn(_epsfcn) {}
47 
48  // forward constructors
49  template<typename T0>
50  NumericalDiff(const T0& a0) : Functor(a0), epsfcn(0) {}
51  template<typename T0, typename T1>
52  NumericalDiff(const T0& a0, const T1& a1) : Functor(a0, a1), epsfcn(0) {}
53  template<typename T0, typename T1, typename T2>
54  NumericalDiff(const T0& a0, const T1& a1, const T2& a2) : Functor(a0, a1, a2), epsfcn(0) {}
55 
56  enum {
57  InputsAtCompileTime = Functor::InputsAtCompileTime,
58  ValuesAtCompileTime = Functor::ValuesAtCompileTime
59  };
60 
64  int df(const InputType& _x, JacobianType &jac) const
65  {
66  using std::sqrt;
67  using std::abs;
68  /* Local variables */
69  Scalar h;
70  int nfev=0;
71  const typename InputType::Index n = _x.size();
72  const Scalar eps = sqrt(((std::max)(epsfcn,NumTraits<Scalar>::epsilon() )));
73  ValueType val1, val2;
74  InputType x = _x;
75  // TODO : we should do this only if the size is not already known
76  val1.resize(Functor::values());
77  val2.resize(Functor::values());
78 
79  // initialization
80  switch(mode) {
81  case Forward:
82  // compute f(x)
83  Functor::operator()(x, val1); nfev++;
84  break;
85  case Central:
86  // do nothing
87  break;
88  default:
89  eigen_assert(false);
90  };
91 
92  // Function Body
93  for (int j = 0; j < n; ++j) {
94  h = eps * abs(x[j]);
95  if (h == 0.) {
96  h = eps;
97  }
98  switch(mode) {
99  case Forward:
100  x[j] += h;
101  Functor::operator()(x, val2);
102  nfev++;
103  x[j] = _x[j];
104  jac.col(j) = (val2-val1)/h;
105  break;
106  case Central:
107  x[j] += h;
108  Functor::operator()(x, val2); nfev++;
109  x[j] -= 2*h;
110  Functor::operator()(x, val1); nfev++;
111  x[j] = _x[j];
112  jac.col(j) = (val2-val1)/(2*h);
113  break;
114  default:
115  eigen_assert(false);
116  };
117  }
118  return nfev;
119  }
120 private:
121  Scalar epsfcn;
122 
124 };
125 
126 } // end namespace Eigen
127 
128 //vim: ai ts=4 sts=4 et sw=4
129 #endif // EIGEN_NUMERICAL_DIFF_H
130 
Functor::ValueType ValueType
Definition: NumericalDiff.h:42
IntermediateState sqrt(const Expression &arg)
Functor::JacobianType JacobianType
Definition: NumericalDiff.h:43
NumericalDiff(const T0 &a0)
Definition: NumericalDiff.h:50
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: matrix.hpp:471
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
NumericalDiffMode
Definition: NumericalDiff.h:18
NumericalDiff(Scalar _epsfcn=0.)
Definition: NumericalDiff.h:45
EIGEN_STRONG_INLINE const CwiseUnaryOp< internal::scalar_abs_op< Scalar >, const Derived > abs() const
Functor::InputType InputType
Definition: NumericalDiff.h:41
Functor::Scalar Scalar
Definition: NumericalDiff.h:40
NumericalDiff(const T0 &a0, const T1 &a1)
Definition: NumericalDiff.h:52
NumericalDiff(const Functor &f, Scalar _epsfcn=0.)
Definition: NumericalDiff.h:46
int df(const InputType &_x, JacobianType &jac) const
Definition: NumericalDiff.h:64
NumericalDiff(const T0 &a0, const T1 &a1, const T2 &a2)
Definition: NumericalDiff.h:54
NumericalDiff & operator=(const NumericalDiff &)
#define eigen_assert(x)


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:54