| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

grad.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_CORE_GRAD_HPP
2 #define STAN_MATH_REV_CORE_GRAD_HPP
3
4 #include <stan/math/rev/core/chainable_alloc.hpp>
5 #include <stan/math/rev/core/chainablestack.hpp>
6 #include <stan/math/rev/core/empty_nested.hpp>
7 #include <stan/math/rev/core/nested_size.hpp>
8 #include <stan/math/rev/core/vari.hpp>
9 #include <vector>
10
11 namespace stan {
12 namespace math {
13
30 static void grad(vari* vi) {
31 // simple reference implementation (intended as doc):
32 // vi->init_dependent();
33 // size_t end = var_stack_.size();
34 // size_t begin = empty_nested() ? 0 : end - nested_size();
35 // for (size_t i = end; --i > begin; )
36 // var_stack_[i]->chain();
37
38 typedef std::vector<vari*>::reverse_iterator it_t;
39 vi->init_dependent();
40 it_t begin = ChainableStack::var_stack_.rbegin();
41 it_t end = empty_nested()
42 ? ChainableStack::var_stack_.rend() : begin + nested_size();
43 for (it_t it = begin; it < end; ++it) {
44 (*it)->chain();
45 }
46 }
47
48 }
49 }
50
51 #endif
stan::math::empty_nested
static bool empty_nested()
Return true if there is no nested autodiff being executed.
Definition: empty_nested.hpp:12
vari.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:30
nested_size.hpp
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
empty_nested.hpp
chainable_alloc.hpp
stan::math::nested_size
static size_t nested_size()
Definition: nested_size.hpp:10
stan::math::vari::init_dependent
void init_dependent()
Initialize the adjoint for this (dependent) variable to 1.
Definition: vari.hpp:98
chainablestack.hpp
stan::math::AutodiffStackStorage::var_stack_
static std::vector< ChainableT * > var_stack_
Definition: autodiffstackstorage.hpp:13

[ Stan Home Page ] © 2011–2016, Stan Development Team.