| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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>
5 #include <stan/math/prim/mat/fun/Eigen.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
17 hessian_times_vector(const F& f,
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) {
39 stan::math::recover_memory_nested();
40 throw;
41 }
42 stan::math::recover_memory_nested();
43 }
44 template <typename T, typename F>
45 void
46 hessian_times_vector(const F& f,
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
core.hpp
core.hpp
stan::math::gradient_dot_vector
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)
Definition: gradient_dot_vector.hpp:19
stan::math::grad
static void grad(chainable *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
Definition: grad.hpp:30
stan::math::hessian_times_vector
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)
Definition: hessian_times_vector.hpp:17
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:44
Eigen.hpp
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95
stan::math::hessian
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
stan::math::recover_memory_nested
static void recover_memory_nested()
Recover only the memory used for the top nested call.
Definition: recover_memory_nested.hpp:21
stan::math::var::val
double val() const
Return the value of this variable.
Definition: var.hpp:234
stan::math::start_nested
static void start_nested()
Record the current position so that recover_memory_nested() can find it.
Definition: start_nested.hpp:13
stan::math::fvar
Definition: fvar.hpp:13

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