Stan Math Library  2.20.0
reverse mode automatic differentiation
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 
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(double x) : val_(x), adj_(0.0) {
59  ChainableStack::instance_->var_stack_.push_back(this);
60  }
61 
62  vari(double x, bool stacked) : val_(x), adj_(0.0) {
63  if (stacked)
64  ChainableStack::instance_->var_stack_.push_back(this);
65  else
67  }
68 
76  virtual ~vari() {
77  // this will never get called
78  }
79 
85  virtual void chain() {}
86 
93  void init_dependent() { adj_ = 1.0; }
94 
100  void set_zero_adjoint() { adj_ = 0.0; }
101 
111  friend std::ostream& operator<<(std::ostream& os, const vari* v) {
112  return os << v->val_ << ":" << v->adj_;
113  }
114 
125  static inline void* operator new(size_t nbytes) {
126  return ChainableStack::instance_->memalloc_.alloc(nbytes);
127  }
128 
140  static inline void operator delete(void* /* ignore arg */) { /* no op */
141  }
142 };
143 
144 } // namespace math
145 } // namespace stan
146 #endif
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: vari.hpp:85
friend std::ostream & operator<<(std::ostream &os, const vari *v)
Insertion operator for vari.
Definition: vari.hpp:111
The variable implementation base class.
Definition: vari.hpp:30
static STAN_THREADS_DEF AutodiffStackStorage * instance_
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
virtual ~vari()
Throw an illegal argument exception.
Definition: vari.hpp:76
const double val_
The value of this variable.
Definition: vari.hpp:38
void set_zero_adjoint()
Set the adjoint value of this variable to 0.
Definition: vari.hpp:100
vari(double x)
Construct a variable implementation from a value.
Definition: vari.hpp:58
void init_dependent()
Initialize the adjoint for this (dependent) variable to 1.
Definition: vari.hpp:93
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
vari(double x, bool stacked)
Definition: vari.hpp:62

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