TMB Documentation  v1.9.11
autodiff.hpp
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 Kasper Kristensen
2 // License: GPL-2
3 
13 namespace autodiff {
14  /* Gradient */
15  template<class Functor, class Type>
16  struct gradient_t
17  {
18  Functor f;
19  gradient_t(Functor f_) : f(f_) {}
20  AD<Type> userfun(vector<AD<Type> > x){
21  return f(x);
22  }
23  vector<Type> operator()(vector<Type> x0){
24  CppAD::vector<AD<Type> > x( x0 );
25  CppAD::vector<AD<Type> > y( 1 );
26  CppAD::Independent(x);
27  y[0] = userfun(x);
28  CppAD::ADFun<Type> F(x, y);
29  CppAD::vector<Type> x_eval(x0);
30  return F.Jacobian(x_eval);
31  }
32  };
33 
66  template<class Functor, class Type>
68 #ifdef CPPAD_FRAMEWORK
69  gradient_t<Functor, Type> f(F);
70  return f(x);
71 #endif
72 #ifdef TMBAD_FRAMEWORK
74  G = G.JacFun();
75  return G(x);
76 #endif
77  }
78 
79  /* Hessian */
80  template<class Functor, class Type>
81  struct hessian_t
82  {
83  Functor f;
84  gradient_t<Functor,AD<Type> > gr;
85  hessian_t(Functor f_) : f(f_), gr(f_) {}
86  vector<AD<Type> > userfun(vector<AD<Type> > x){
87  return gr(x);
88  }
89  matrix<Type> operator()(vector<Type> x0){
90  CppAD::vector<AD<Type> > x( x0 );
91  CppAD::vector<AD<Type> > y( x0 );
92  CppAD::Independent(x);
93  y = userfun(x);
94  CppAD::ADFun<Type> F(x, y);
95  CppAD::vector<Type> x_eval(x0);
96  vector<Type> ans = F.Jacobian(x_eval);
97  return asMatrix(ans, x.size(), x.size());
98  }
99  };
100 
133  template<class Functor, class Type>
135 #ifdef CPPAD_FRAMEWORK
136  hessian_t<Functor, Type> H(f);
137  return H(x);
138 #endif
139 #ifdef TMBAD_FRAMEWORK
141  F = F.JacFun().JacFun();
142  vector<Type> Fx = F(x);
143  return asMatrix(Fx, x.size(), x.size());
144 #endif
145  }
146 
147  /* Jacobian */
148  template<class Functor, class Type>
149  struct jacobian_t
150  {
151  Functor f;
152  jacobian_t(Functor f_) : f(f_) {}
153  vector<AD<Type> > userfun(vector<AD<Type> > x){
154  return f(x);
155  }
156  matrix<Type> operator()(vector<Type> x0){
157  CppAD::vector<AD<Type> > x( x0 );
158  CppAD::Independent(x);
159  CppAD::vector<AD<Type> > y = userfun(x);
160  CppAD::ADFun<Type> F(x, y);
161  CppAD::vector<Type> x_eval(x0);
162  vector<Type> ans = F.Jacobian(x_eval); // By row !
163  return asMatrix(ans, x.size(), y.size()).transpose();
164  }
165  };
166 
202  template<class Functor, class Type>
204 #ifdef CPPAD_FRAMEWORK
205  jacobian_t<Functor, Type> J(f);
206  return J(x);
207 #endif
208 #ifdef TMBAD_FRAMEWORK
210  int m = J.Range();
211  J = J.JacFun();
212  vector<Type> Jx = J(x);
213  return asMatrix(Jx, x.size(), m).transpose();
214 #endif
215  }
216 }
Vector class used by TMB.
Definition: vector.hpp:17
Automatic differentiation (gradient, hessian, jacobian)
Definition: autodiff.hpp:13
matrix< Type > jacobian(Functor f, vector< Type > x)
Calculate jacobian of vector function with vector values.
Definition: autodiff.hpp:203
ADFun JacFun(std::vector< bool > keep_x=std::vector< bool >(0), std::vector< bool > keep_y=std::vector< bool >(0))
Get Jacobian function object.
Definition: TMBad.hpp:680
Matrix class used by TMB.
Definition: vector.hpp:101
matrix< Type > hessian(Functor f, vector< Type > x)
Calculate hessian of vector function with scalar values.
Definition: autodiff.hpp:134
Interoperability with other vector classes.
Definition: TMBad.hpp:57
vector< Type > gradient(Functor F, vector< Type > x)
Calculate gradient of vector function with scalar values.
Definition: autodiff.hpp:67
matrix< Type > asMatrix(const vector< Type > &x, int nr, int nc)
Vector <-> Matrix conversion (for row-major matrices)
Definition: convert.hpp:120
License: GPL v2