| 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
jacobian.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUNCTOR_JACOBIAN_HPP
2 #define STAN_MATH_REV_MAT_FUNCTOR_JACOBIAN_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/rev/core.hpp>
6 #include <vector>
7
8 namespace stan {
9
10 namespace math {
11 using Eigen::Dynamic;
12
13 template <typename F>
14 void
15 jacobian(const F& f,
16 const Eigen::Matrix<double, Dynamic, 1>& x,
17 Eigen::Matrix<double, Dynamic, 1>& fx,
18 Eigen::Matrix<double, Dynamic, Dynamic>& J) {
19 using Eigen::Matrix;
20 using stan::math::var;
21 start_nested();
22 try {
23 Matrix<var, Dynamic, 1> x_var(x.size());
24 for (int k = 0; k < x.size(); ++k)
25 x_var(k) = x(k);
26 Matrix<var, Dynamic, 1> fx_var = f(x_var);
27 fx.resize(fx_var.size());
28 for (int i = 0; i < fx_var.size(); ++i)
29 fx(i) = fx_var(i).val();
30 J.resize(x.size(), fx_var.size());
31 for (int i = 0; i < fx_var.size(); ++i) {
32 if (i > 0)
33 set_zero_all_adjoints();
34 grad(fx_var(i).vi_);
35 for (int k = 0; k < x.size(); ++k)
36 J(k, i) = x_var(k).adj();
37 }
38 } catch (const std::exception& e) {
39 stan::math::recover_memory_nested();
40 throw;
41 }
42 stan::math::recover_memory_nested();
43 }
44
45 }
46 }
47 #endif
core.hpp
stan::math::set_zero_all_adjoints
static void set_zero_all_adjoints()
Reset all adjoint values in the stack to zero.
Definition: set_zero_all_adjoints.hpp:14
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::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
Eigen.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:21
stan::math::jacobian
void jacobian(const F &f, const Eigen::Matrix< T, Dynamic, 1 > &x, Eigen::Matrix< T, Dynamic, 1 > &fx, Eigen::Matrix< T, Dynamic, Dynamic > &J)
Definition: jacobian.hpp:16
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 Home Page ] © 2011–2015, Stan Development Team.