Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vari.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__VARI_HPP
2 #define STAN__AGRAD__REV__VARI_HPP
3 
4 #include <ostream>
5 
8 
9 namespace stan {
10  namespace agrad {
11 
12  // forward declaration of var
13  class var;
14 
28  class vari : public chainable {
29  private:
30  friend class var;
31 
32  public:
33 
37  const double val_;
38 
43  double adj_;
44 
57  vari(const double x):
58  val_(x),
59  adj_(0.0) {
60  var_stack_.push_back(this);
61  }
62 
63  vari(const double x,bool stacked):
64  val_(x),
65  adj_(0.0) {
66  if (stacked)
67  var_stack_.push_back(this);
68  else
69  var_nochain_stack_.push_back(this);
70  }
71 
79  virtual ~vari() {
80  // throw std::logic_error("vari destruction handled automatically");
81  }
82 
88  virtual void init_dependent() {
89  adj_ = 1.0; // droot/droot = 1
90  }
91 
95  virtual void set_zero_adjoint() {
96  adj_ = 0.0;
97  }
98 
108  friend std::ostream& operator<<(std::ostream& os, const vari* v) {
109  return os << v << " " << v->val_ << " : " << v->adj_;
110  }
111  };
112 
113  }
114 }
115 #endif
virtual void init_dependent()
Initialize the adjoint for this (dependent) variable to 1.
Definition: vari.hpp:88
Abstract base class for variable implementations that handles memory management and applying the chai...
Definition: chainable.hpp:14
const double val_
The value of this variable.
Definition: vari.hpp:37
vari(const double x, bool stacked)
Definition: vari.hpp:63
vari(const double x)
Construct a variable implementation from a value.
Definition: vari.hpp:57
The variable implementation base class.
Definition: vari.hpp:28
std::vector< chainable * > var_stack_
Definition: var_stack.cpp:13
virtual ~vari()
Throw an illegal argument exception.
Definition: vari.hpp:79
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:27
virtual void set_zero_adjoint()
Set the adjoint value of this variable to 0.
Definition: vari.hpp:95
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:43
std::vector< chainable * > var_nochain_stack_
Definition: var_stack.cpp:14
friend std::ostream & operator<<(std::ostream &os, const vari *v)
Insertion operator for vari.
Definition: vari.hpp:108

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