1 #ifndef STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_HPP 2 #define STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_HPP 45 const Eigen::Matrix<double, Eigen::Dynamic, 1>& x,
47 Eigen::Matrix<double, Eigen::Dynamic, 1>&
grad,
48 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& H) {
49 H.resize(x.size(), x.size());
50 grad.resize(x.size());
52 for (
int i = 0; i < x.size(); ++i) {
54 Eigen::Matrix<fvar<var>, Eigen::Dynamic, 1> x_fvar(x.size());
55 for (
int j = 0; j < x.size(); ++j)
59 if (i == 0) fx = fx_fvar.
val_.
val();
61 for (
int j = 0; j < x.size(); ++j)
62 H(i, j) = x_fvar(j).val_.adj();
65 }
catch (
const std::exception&
e) {
71 template <
typename T,
typename F>
74 const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
76 Eigen::Matrix<T, Eigen::Dynamic, 1>&
grad,
77 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& H) {
78 H.resize(x.size(), x.size());
79 grad.resize(x.size());
80 Eigen::Matrix<fvar<fvar<T> >, Eigen::Dynamic, 1> x_fvar(x.size());
81 for (
int i = 0; i < x.size(); ++i) {
82 for (
int j = i; j < x.size(); ++j) {
83 for (
int k = 0; k < x.size(); ++k)
88 fx = fx_fvar.
val_.val_;
91 H(i, j) = fx_fvar.
d_.d_;
void hessian(const F &f, const Eigen::Matrix< double, Eigen::Dynamic, 1 > &x, double &fx, Eigen::Matrix< double, Eigen::Dynamic, 1 > &grad, Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > &H)
Calculate the value, the gradient, and the Hessian, of the specified function at the specified argume...
static void grad(vari *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
vari * vi_
Pointer to the implementation of this variable.
double e()
Return the base of the natural logarithm.
static void recover_memory_nested()
Recover only the memory used for the top nested call.
static void start_nested()
Record the current position so that recover_memory_nested() can find it.
double val() const
Return the value of this variable.