Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
hessian_times_vector.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_TIMES_VECTOR_HPP
2 #define STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_TIMES_VECTOR_HPP
3 
4 #include <stan/math/fwd/core.hpp>
6 #include <stan/math/rev/core.hpp>
7 #include <vector>
8 
9 namespace stan {
10 
11  namespace math {
12 
13  using Eigen::Dynamic;
14 
15  template <typename F>
16  void
18  const Eigen::Matrix<double, Dynamic, 1>& x,
19  const Eigen::Matrix<double, Dynamic, 1>& v,
20  double& fx,
21  Eigen::Matrix<double, Dynamic, 1>& Hv) {
22  using stan::math::fvar;
23  using stan::math::var;
24  using Eigen::Matrix;
25  start_nested();
26  try {
27  Matrix<var, Dynamic, 1> x_var(x.size());
28  for (int i = 0; i < x_var.size(); ++i)
29  x_var(i) = x(i);
30  var fx_var;
31  var grad_fx_var_dot_v;
32  gradient_dot_vector(f, x_var, v, fx_var, grad_fx_var_dot_v);
33  fx = fx_var.val();
34  stan::math::grad(grad_fx_var_dot_v.vi_);
35  Hv.resize(x.size());
36  for (int i = 0; i < x.size(); ++i)
37  Hv(i) = x_var(i).adj();
38  } catch (const std::exception& e) {
40  throw;
41  }
43  }
44  template <typename T, typename F>
45  void
47  const Eigen::Matrix<T, Dynamic, 1>& x,
48  const Eigen::Matrix<T, Dynamic, 1>& v,
49  T& fx,
50  Eigen::Matrix<T, Dynamic, 1>& Hv) {
51  using Eigen::Matrix;
52  Matrix<T, Dynamic, 1> grad;
53  Matrix<T, Dynamic, Dynamic> H;
54  hessian(f, x, fx, grad, H);
55  Hv = H * v;
56  }
57 
58  } // namespace math
59 } // namespace stan
60 #endif
void gradient_dot_vector(const F &f, const Eigen::Matrix< T1, Dynamic, 1 > &x, const Eigen::Matrix< T2, Dynamic, 1 > &v, T1 &fx, T1 &grad_fx_dot_v)
static void grad(chainable *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
Definition: grad.hpp:30
void hessian_times_vector(const F &f, const Eigen::Matrix< double, Dynamic, 1 > &x, const Eigen::Matrix< double, Dynamic, 1 > &v, double &fx, Eigen::Matrix< double, Dynamic, 1 > &Hv)
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:44
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95
void hessian(const F &f, const Eigen::Matrix< double, Dynamic, 1 > &x, double &fx, Eigen::Matrix< double, Dynamic, 1 > &grad, Eigen::Matrix< double, Dynamic, Dynamic > &H)
Calculate the value, the gradient, and the Hessian, of the specified function at the specified argume...
Definition: hessian.hpp:46
static void recover_memory_nested()
Recover only the memory used for the top nested call.
double val() const
Return the value of this variable.
Definition: var.hpp:234
static void start_nested()
Record the current position so that recover_memory_nested() can find it.

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