Stan Math Library  2.11.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 
13  namespace math {
14 
15  // forward declare
16  static void grad(vari* vi);
17 
31  class var {
32  public:
33  // FIXME: doc what this is for
34  typedef double Scalar;
35 
43  vari * vi_;
44 
55  return (vi_ == static_cast<vari*>(0U));
56  }
57 
65  var() : vi_(static_cast<vari*>(0U)) { }
66 
67 
73  var(vari* vi) : vi_(vi) { } // NOLINT
74 
82  var(float x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
83 
91  var(double x) : vi_(new vari(x)) { } // NOLINT
92 
100  var(long double x) : vi_(new vari(x)) { } // NOLINT
101 
109  var(bool x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
110 
118  var(char x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
119 
127  var(short x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
128 
136  var(int x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
137 
145  var(long x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
146 
154  var(unsigned char x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
155 
163  // NOLINTNEXTLINE
164  var(unsigned short x) : vi_(new vari(static_cast<double>(x))) { }
165 
173  var(unsigned int x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
174 
182  // NOLINTNEXTLINE
183  var(unsigned long x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
184 
185 #ifdef _WIN64
186 
187  // these two ctors are for Win64 to enable 64-bit signed
188  // and unsigned integers, because long and unsigned long
189  // are still 32-bit
190 
198  var(size_t x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
199 
200 
208  var(ptrdiff_t x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
209 #endif
210 
211 
212 #ifdef BOOST_MATH_USE_FLOAT128
213 
214  // this ctor is for later GCCs that have the __float128
215  // type enabled, because it gets enabled by boost
216 
224  var(__float128 x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
225 
226 #endif
227 
233  inline double val() const {
234  return vi_->val_;
235  }
236 
245  inline double adj() const {
246  return vi_->adj_;
247  }
248 
261  void grad(std::vector<var>& x,
262  std::vector<double>& g) {
263  stan::math::grad(vi_);
264  g.resize(x.size());
265  for (size_t i = 0; i < x.size(); ++i)
266  g[i] = x[i].vi_->adj_;
267  }
268 
275  void grad() {
276  stan::math::grad(vi_);
277  }
278 
279  // POINTER OVERRIDES
280 
293  inline vari& operator*() {
294  return *vi_;
295  }
296 
307  inline vari* operator->() {
308  return vi_;
309  }
310 
311  // COMPOUND ASSIGNMENT OPERATORS
312 
323  inline var& operator+=(const var& b);
324 
335  inline var& operator+=(const double b);
336 
348  inline var& operator-=(const var& b);
349 
361  inline var& operator-=(const double b);
362 
374  inline var& operator*=(const var& b);
375 
387  inline var& operator*=(const double b);
388 
399  inline var& operator/=(const var& b);
400 
412  inline var& operator/=(const double b);
413 
422  friend std::ostream& operator<<(std::ostream& os, const var& v) {
423  if (v.vi_ == 0)
424  return os << "uninitialized";
425  return os << v.val();
426  }
427  };
428 
429  }
430 }
431 #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:154
var(long x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:145
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:183
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:73
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
vari & operator*()
Return a reference to underlying implementation of this variable.
Definition: var.hpp:293
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:91
var(bool x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:109
var(char x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:118
bool is_uninitialized()
Return true if this variable has been declared, but not been defined.
Definition: var.hpp:54
var(unsigned int x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:173
var()
Construct a variable for later assignment.
Definition: var.hpp:65
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:43
var(float x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:82
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:261
vari * operator->()
Return a pointer to the underlying implementation of this variable.
Definition: var.hpp:307
void grad()
Compute the gradient of this (dependent) variable with respect to all (independent) variables...
Definition: var.hpp:275
var(long double x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:100
var(int x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:136
double Scalar
Definition: var.hpp:34
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:233
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:422
double adj() const
Return the derivative of the root expression with respect to this expression.
Definition: var.hpp:245
var(unsigned short x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:164
var(short x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:127

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