Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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 
7 #include <ostream>
8 
9 namespace stan {
10  namespace math {
11 
12  // forward declaration of var
13  class var;
14 
28  class vari : public chainable {
29  private:
30  friend class var;
31 
32  public:
36  const double val_;
37 
42  double adj_;
43 
56  explicit vari(const double x):
57  val_(x),
58  adj_(0.0) {
59  ChainableStack::var_stack_.push_back(this);
60  }
61 
62  vari(const double x, bool stacked):
63  val_(x),
64  adj_(0.0) {
65  if (stacked)
66  ChainableStack::var_stack_.push_back(this);
67  else
68  ChainableStack::var_nochain_stack_.push_back(this);
69  }
70 
78  virtual ~vari() {
79  // throw std::logic_error("vari destruction handled automatically");
80  }
81 
87  virtual void init_dependent() {
88  adj_ = 1.0; // droot/droot = 1
89  }
90 
94  virtual void set_zero_adjoint() {
95  adj_ = 0.0;
96  }
97 
107  friend std::ostream& operator<<(std::ostream& os, const vari* v) {
108  return os << v->val_ << ":" << v->adj_;
109  }
110  };
111 
112  }
113 }
114 #endif
vari(const double x)
Construct a variable implementation from a value.
Definition: vari.hpp:56
friend std::ostream & operator<<(std::ostream &os, const vari *v)
Insertion operator for vari.
Definition: vari.hpp:107
Abstract base class for variable implementations that handles memory management and applying the chai...
Definition: chainable.hpp:14
The variable implementation base class.
Definition: vari.hpp:28
virtual void init_dependent()
Initialize the adjoint for this (dependent) variable to 1.
Definition: vari.hpp:87
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
virtual ~vari()
Throw an illegal argument exception.
Definition: vari.hpp:78
const double val_
The value of this variable.
Definition: vari.hpp:36
vari(const double x, bool stacked)
Definition: vari.hpp:62
static std::vector< ChainableT * > var_nochain_stack_
virtual void set_zero_adjoint()
Set the adjoint value of this variable to 0.
Definition: vari.hpp:94
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:42
static std::vector< ChainableT * > var_stack_

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