All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
hessian_times_vector.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_TIMES_VECTOR_HPP
#define STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_TIMES_VECTOR_HPP
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/rev/core.hpp>
#include <vector>
namespace stan {
namespace math {
using Eigen::Dynamic;
template <typename F>
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) {
using stan::math::fvar;
using stan::math::var;
using Eigen::Matrix;
start_nested();
try {
Matrix<var, Dynamic, 1> x_var(x.size());
for (int i = 0; i < x_var.size(); ++i)
x_var(i) = x(i);
var fx_var;
var grad_fx_var_dot_v;
gradient_dot_vector(f, x_var, v, fx_var, grad_fx_var_dot_v);
fx = fx_var.val();
stan::math::grad(grad_fx_var_dot_v.vi_);
Hv.resize(x.size());
for (int i = 0; i < x.size(); ++i)
Hv(i) = x_var(i).adj();
} catch (const std::exception& e) {
stan::math::recover_memory_nested();
throw;
}
stan::math::recover_memory_nested();
}
template <typename T, typename F>
void
hessian_times_vector(const F& f,
const Eigen::Matrix<T, Dynamic, 1>& x,
const Eigen::Matrix<T, Dynamic, 1>& v,
T& fx,
Eigen::Matrix<T, Dynamic, 1>& Hv) {
using Eigen::Matrix;
Matrix<T, Dynamic, 1> grad;
Matrix<T, Dynamic, Dynamic> H;
hessian(f, x, fx, grad, H);
Hv = H * v;
}
} // namespace math
} // namespace stan
#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.