| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

vari.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_CORE_VARI_HPP
2 #define STAN_MATH_REV_CORE_VARI_HPP
3
4 #include <stan/math/rev/core/chainable_alloc.hpp>
5 #include <stan/math/rev/core/chainablestack.hpp>
6 #include <ostream>
7
8 namespace stan {
9 namespace math {
10
11 // forward declaration of var
12 class var;
13
30 class vari {
31 private:
32 friend class var;
33
34 public:
38 const double val_;
39
44 double adj_;
45
58 explicit vari(const double x):
59 val_(x),
60 adj_(0.0) {
61 ChainableStack::var_stack_.push_back(this);
62 }
63
64 vari(const double x, bool stacked):
65 val_(x),
66 adj_(0.0) {
67 if (stacked)
68 ChainableStack::var_stack_.push_back(this);
69 else
70 ChainableStack::var_nochain_stack_.push_back(this);
71 }
72
80 virtual ~vari() {
81 // this will never get called
82 }
83
89 virtual void chain() {
90 }
91
98 void init_dependent() {
99 adj_ = 1.0;
100 }
101
107 void set_zero_adjoint() {
108 adj_ = 0.0;
109 }
110
120 friend std::ostream& operator<<(std::ostream& os, const vari* v) {
121 return os << v->val_ << ":" << v->adj_;
122 }
123
134 static inline void* operator new(size_t nbytes) {
135 return ChainableStack::memalloc_.alloc(nbytes);
136 }
137
149 static inline void operator delete(void* /* ignore arg */) {
150 /* no op */
151 }
152 };
153
154 }
155 }
156 #endif
stan::math::vari::vari
vari(const double x)
Construct a variable implementation from a value.
Definition: vari.hpp:58
stan::math::vari::chain
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: vari.hpp:89
stan::math::vari::operator<<
friend std::ostream & operator<<(std::ostream &os, const vari *v)
Insertion operator for vari.
Definition: vari.hpp:120
stan
Definition: log_sum_exp.hpp:8
stan::math::AutodiffStackStorage::memalloc_
static stack_alloc memalloc_
Definition: autodiffstackstorage.hpp:16
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:30
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
stan::math::vari::~vari
virtual ~vari()
Throw an illegal argument exception.
Definition: vari.hpp:80
stan::math::vari::val_
const double val_
The value of this variable.
Definition: vari.hpp:38
stan::math::vari::set_zero_adjoint
void set_zero_adjoint()
Set the adjoint value of this variable to 0.
Definition: vari.hpp:107
stan::math::vari::vari
vari(const double x, bool stacked)
Definition: vari.hpp:64
stan::math::AutodiffStackStorage::var_nochain_stack_
static std::vector< ChainableT * > var_nochain_stack_
Definition: autodiffstackstorage.hpp:14
chainable_alloc.hpp
stan::math::vari::init_dependent
void init_dependent()
Initialize the adjoint for this (dependent) variable to 1.
Definition: vari.hpp:98
stan::math::vari::adj_
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
chainablestack.hpp
stan::math::AutodiffStackStorage::var_stack_
static std::vector< ChainableT * > var_stack_
Definition: autodiffstackstorage.hpp:13
stan::math::stack_alloc::alloc
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Definition: stack_alloc.hpp:162

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