Stan  1.1
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
hessian.hpp
Go to the documentation of this file.
1 #ifndef __STAN__AGRAD__HESSIAN_HPP__
2 #define __STAN__AGRAD__HESSIAN_HPP__
3 
4 #include <vector>
5 
6 #include <stan/agrad/agrad.hpp>
7 #include <stan/agrad/fvar.hpp>
8 
9 #define EIGEN_DENSEBASE_PLUGIN "stan/math/EigenDenseBaseAddons.hpp"
10 #include <Eigen/Dense>
11 
12 namespace stan {
13 
14  namespace agrad {
15 
16  // f: vector<T> -> T
17  // Hv = H(f(x)) * v; return f(x)
18 
19  template <class F>
20  double
21  hessian(const F& f,
22  const std::vector<double>& x,
23  const std::vector<double>& v,
24  std::vector<double>& Hv) {
25 
26  using stan::agrad::var;
27  using stan::agrad::fvar;
28 
29  std::vector<var> x_var(x.size());
30  for (int i = 0; i < x_var.size(); ++i)
31  x_var[i] = x[i];
32 
33  std::vector<fvar<var> > x_fvar(x.size());
34  for (int i = 0; i < x_fvar.size(); ++i)
35  x_fvar[i] = fvar<var>(x_var[i], v[i]);
36  fvar<var> y = f(x_fvar);
37  y.d_.grad(x_var,Hv);
38  return y.val_.val();
39  }
40 
41  }
42 }
43 
44 
45 #endif

     [ Stan Home Page ] © 2011–2013, Stan Development Team.