| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

grad_tr_mat_times_hessian.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_MIX_MAT_FUNCTOR_GRAD_TR_MAT_TIMES_HESSIAN_HPP
2 #define STAN_MATH_MIX_MAT_FUNCTOR_GRAD_TR_MAT_TIMES_HESSIAN_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 <stan/math/mix/mat/functor/gradient_dot_vector.hpp>
8 #include <stdexcept>
9 #include <vector>
10
11 namespace stan {
12
13 namespace math {
14
15
16 // FIXME: add other results that are easy to extract
17 // // N * (fwd(2) + bk)
18 template <typename F>
19 void
20 grad_tr_mat_times_hessian(
21 const F& f,
22 const Eigen::Matrix<double, Eigen::Dynamic, 1>& x,
23 const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& M,
24 Eigen::Matrix<double, Eigen::Dynamic, 1>& grad_tr_MH
25 ) {
26 using Eigen::Matrix;
27 using Eigen::Dynamic;
28 start_nested();
29 try {
30 grad_tr_MH.resize(x.size());
31
32 Matrix<var, Dynamic, 1> x_var(x.size());
33 for (int i = 0; i < x.size(); ++i)
34 x_var(i) = x(i);
35
36 Matrix<fvar<var>, Dynamic, 1> x_fvar(x.size());
37
38 var sum(0.0);
39 Matrix<double, Dynamic, 1> M_n(x.size());
40 for (int n = 0; n < x.size(); ++n) {
41 for (int k = 0; k < x.size(); ++k)
42 M_n(k) = M(n, k);
43 for (int k = 0; k < x.size(); ++k)
44 x_fvar(k) = fvar<var>(x_var(k), k == n);
45 fvar<var> fx;
46 fvar<var> grad_fx_dot_v;
47 gradient_dot_vector<fvar<var>, double>(f, x_fvar, M_n, fx,
48 grad_fx_dot_v);
49 sum += grad_fx_dot_v.d_;
50 }
51
52 stan::math::grad(sum.vi_);
53 for (int i = 0; i < x.size(); ++i)
54 grad_tr_MH(i) = x_var(i).adj();
55 } catch (const std::exception& e) {
56 stan::math::recover_memory_nested();
57 throw;
58 }
59 stan::math::recover_memory_nested();
60 }
61
62
63 } // namespace math
64 } // namespace stan
65 #endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
core.hpp
core.hpp
stan::math::fvar::d_
T d_
Definition: fvar.hpp:15
stan
Definition: log_sum_exp.hpp:8
stan::math::grad_tr_mat_times_hessian
void grad_tr_mat_times_hessian(const F &f, const Eigen::Matrix< double, Eigen::Dynamic, 1 > &x, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > &M, Eigen::Matrix< double, Eigen::Dynamic, 1 > &grad_tr_MH)
Definition: grad_tr_mat_times_hessian.hpp:20
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
stan::math::grad
static void grad(vari *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
Definition: grad.hpp:30
Eigen.hpp
gradient_dot_vector.hpp
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95
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:20
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–2016, Stan Development Team.