Stan Math Library  2.15.0
reverse mode automatic differentiation
var.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_CORE_VAR_HPP
2 #define STAN_MATH_REV_CORE_VAR_HPP
3 
7 #include <boost/math/tools/config.hpp>
8 #include <ostream>
9 #include <vector>
10 
11 namespace stan {
12  namespace math {
13 
14  // forward declare
15  static void grad(vari* vi);
16 
30  class var {
31  public:
32  // FIXME: doc what this is for
33  typedef double Scalar;
34 
42  vari * vi_;
43 
54  return (vi_ == static_cast<vari*>(0U));
55  }
56 
64  var() : vi_(static_cast<vari*>(0U)) { }
65 
71  var(vari* vi) : vi_(vi) { } // NOLINT
72 
80  var(float x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
81 
89  var(double x) : vi_(new vari(x)) { } // NOLINT
90 
98  var(long double x) : vi_(new vari(x)) { } // NOLINT
99 
107  var(bool x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
108 
116  var(char x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
117 
125  var(short x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
126 
134  var(int x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
135 
143  var(long x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
144 
152  var(unsigned char x) // NOLINT(runtime/explicit)
153  : vi_(new vari(static_cast<double>(x))) { }
154 
162  // NOLINTNEXTLINE
163  var(unsigned short x) : vi_(new vari(static_cast<double>(x))) { }
164 
172  var(unsigned int x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
173 
181  // NOLINTNEXTLINE
182  var(unsigned long x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
183 
184 #ifdef _WIN64
185 
186  // these two ctors are for Win64 to enable 64-bit signed
187  // and unsigned integers, because long and unsigned long
188  // are still 32-bit
189 
197  var(size_t x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
198 
206  var(ptrdiff_t x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
207 #endif
208 
209 #ifdef BOOST_MATH_USE_FLOAT128
210 
211  // this ctor is for later GCCs that have the __float128
212  // type enabled, because it gets enabled by boost
213 
221  var(__float128 x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
222 
223 #endif
224 
230  inline double val() const {
231  return vi_->val_;
232  }
233 
242  inline double adj() const {
243  return vi_->adj_;
244  }
245 
258  void grad(std::vector<var>& x,
259  std::vector<double>& g) {
260  stan::math::grad(vi_);
261  g.resize(x.size());
262  for (size_t i = 0; i < x.size(); ++i)
263  g[i] = x[i].vi_->adj_;
264  }
265 
272  void grad() {
273  stan::math::grad(vi_);
274  }
275 
276  // POINTER OVERRIDES
277 
290  inline vari& operator*() {
291  return *vi_;
292  }
293 
304  inline vari* operator->() {
305  return vi_;
306  }
307 
308  // COMPOUND ASSIGNMENT OPERATORS
309 
320  inline var& operator+=(const var& b);
321 
332  inline var& operator+=(double b);
333 
345  inline var& operator-=(const var& b);
346 
358  inline var& operator-=(double b);
359 
371  inline var& operator*=(const var& b);
372 
384  inline var& operator*=(double b);
385 
396  inline var& operator/=(const var& b);
397 
409  inline var& operator/=(double b);
410 
419  friend std::ostream& operator<<(std::ostream& os, const var& v) {
420  if (v.vi_ == 0)
421  return os << "uninitialized";
422  return os << v.val();
423  }
424  };
425 
426  }
427 }
428 #endif
var & operator+=(const var &b)
The compound add/assignment operator for variables (C++).
var & operator*=(const var &b)
The compound multiply/assignment operator for variables (C++).
var(unsigned char x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:152
var(long x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:143
var & operator/=(const var &b)
The compound divide/assignment operator for variables (C++).
var(unsigned long x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:182
The variable implementation base class.
Definition: vari.hpp:30
var(vari *vi)
Construct a variable from a pointer to a variable implementation.
Definition: var.hpp:71
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
vari & operator*()
Return a reference to underlying implementation of this variable.
Definition: var.hpp:290
static void grad(vari *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
Definition: grad.hpp:30
var & operator-=(const var &b)
The compound subtract/assignment operator for variables (C++).
const double val_
The value of this variable.
Definition: vari.hpp:38
var(double x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:89
var(bool x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:107
var(char x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:116
bool is_uninitialized()
Return true if this variable has been declared, but not been defined.
Definition: var.hpp:53
var(unsigned int x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:172
var()
Construct a variable for later assignment.
Definition: var.hpp:64
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42
double adj() const
Return the derivative of the root expression with respect to this expression.
Definition: var.hpp:242
var(float x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:80
void grad(std::vector< var > &x, std::vector< double > &g)
Compute the gradient of this (dependent) variable with respect to the specified vector of (independen...
Definition: var.hpp:258
vari * operator->()
Return a pointer to the underlying implementation of this variable.
Definition: var.hpp:304
void grad()
Compute the gradient of this (dependent) variable with respect to all (independent) variables...
Definition: var.hpp:272
var(long double x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:98
var(int x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:134
double Scalar
Definition: var.hpp:33
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
friend std::ostream & operator<<(std::ostream &os, const var &v)
Write the value of this auto-dif variable and its adjoint to the specified output stream...
Definition: var.hpp:419
var(unsigned short x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:163
var(short x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:125
double val() const
Return the value of this variable.
Definition: var.hpp:230

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