1 #ifndef STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_HPP
2 #define STAN_MATH_MIX_MAT_FUNCTOR_HESSIAN_HPP
46 const Eigen::Matrix<double, Eigen::Dynamic, 1>& x,
48 Eigen::Matrix<double, Eigen::Dynamic, 1>&
grad,
49 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& H) {
50 H.resize(x.size(), x.size());
51 grad.resize(x.size());
53 for (
int i = 0; i < x.size(); ++i) {
55 Eigen::Matrix<fvar<var>, Eigen::Dynamic, 1> x_fvar(x.size());
56 for (
int j = 0; j < x.size(); ++j)
60 if (i == 0) fx = fx_fvar.
val_.
val();
62 for (
int j = 0; j < x.size(); ++j)
63 H(i, j) = x_fvar(j).val_.adj();
66 }
catch (
const std::exception&
e) {
72 template <
typename T,
typename F>
75 const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
77 Eigen::Matrix<T, Eigen::Dynamic, 1>&
grad,
78 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& H) {
79 H.resize(x.size(), x.size());
80 grad.resize(x.size());
81 Eigen::Matrix<fvar<fvar<T> >, Eigen::Dynamic, 1> x_fvar(x.size());
82 for (
int i = 0; i < x.size(); ++i) {
83 for (
int j = i; j < x.size(); ++j) {
84 for (
int k = 0; k < x.size(); ++k)
89 fx = fx_fvar.
val_.val_;
92 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.
double val() const
Return the value of this variable.
static void start_nested()
Record the current position so that recover_memory_nested() can find it.