Stan Math Library  2.12.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) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
153 
161  // NOLINTNEXTLINE
162  var(unsigned short x) : vi_(new vari(static_cast<double>(x))) { }
163 
171  var(unsigned int x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
172 
180  // NOLINTNEXTLINE
181  var(unsigned long x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
182 
183 #ifdef _WIN64
184 
185  // these two ctors are for Win64 to enable 64-bit signed
186  // and unsigned integers, because long and unsigned long
187  // are still 32-bit
188 
196  var(size_t x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
197 
205  var(ptrdiff_t x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
206 #endif
207 
208 #ifdef BOOST_MATH_USE_FLOAT128
209 
210  // this ctor is for later GCCs that have the __float128
211  // type enabled, because it gets enabled by boost
212 
220  var(__float128 x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
221 
222 #endif
223 
229  inline double val() const {
230  return vi_->val_;
231  }
232 
241  inline double adj() const {
242  return vi_->adj_;
243  }
244 
257  void grad(std::vector<var>& x,
258  std::vector<double>& g) {
259  stan::math::grad(vi_);
260  g.resize(x.size());
261  for (size_t i = 0; i < x.size(); ++i)
262  g[i] = x[i].vi_->adj_;
263  }
264 
271  void grad() {
272  stan::math::grad(vi_);
273  }
274 
275  // POINTER OVERRIDES
276 
289  inline vari& operator*() {
290  return *vi_;
291  }
292 
303  inline vari* operator->() {
304  return vi_;
305  }
306 
307  // COMPOUND ASSIGNMENT OPERATORS
308 
319  inline var& operator+=(const var& b);
320 
331  inline var& operator+=(const double b);
332 
344  inline var& operator-=(const var& b);
345 
357  inline var& operator-=(const double b);
358 
370  inline var& operator*=(const var& b);
371 
383  inline var& operator*=(const double b);
384 
395  inline var& operator/=(const var& b);
396 
408  inline var& operator/=(const double b);
409 
418  friend std::ostream& operator<<(std::ostream& os, const var& v) {
419  if (v.vi_ == 0)
420  return os << "uninitialized";
421  return os << v.val();
422  }
423  };
424 
425  }
426 }
427 #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:181
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:289
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:171
var()
Construct a variable for later assignment.
Definition: var.hpp:64
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42
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:257
vari * operator->()
Return a pointer to the underlying implementation of this variable.
Definition: var.hpp:303
void grad()
Compute the gradient of this (dependent) variable with respect to all (independent) variables...
Definition: var.hpp:271
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
double val() const
Return the value of this variable.
Definition: var.hpp:229
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:418
double adj() const
Return the derivative of the root expression with respect to this expression.
Definition: var.hpp:241
var(unsigned short x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:162
var(short x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:125

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