Stan Math Library  2.10.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(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 
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
vari(const double x)
Construct a variable implementation from a value.
Definition: vari.hpp:58
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: vari.hpp:89
friend std::ostream & operator<<(std::ostream &os, const vari *v)
Insertion operator for vari.
Definition: vari.hpp:120
The variable implementation base class.
Definition: vari.hpp:30
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
virtual ~vari()
Throw an illegal argument exception.
Definition: vari.hpp:80
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:107
vari(const double x, bool stacked)
Definition: vari.hpp:64
static std::vector< ChainableT * > var_nochain_stack_
void init_dependent()
Initialize the adjoint for this (dependent) variable to 1.
Definition: vari.hpp:98
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
static std::vector< ChainableT * > var_stack_
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...

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