Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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 
8 #include <boost/math/tools/config.hpp>
9 #include <ostream>
10 #include <vector>
11 
12 namespace stan {
13 
14  namespace math {
15 
16  // forward declare
17  static void grad(chainable* vi);
18 
32  class var {
33  public:
34  // FIXME: doc what this is for
35  typedef double Scalar;
36 
44  vari * vi_;
45 
56  return (vi_ == static_cast<vari*>(0U));
57  }
58 
66  var() : vi_(static_cast<vari*>(0U)) { }
67 
68 
74  var(vari* vi) : vi_(vi) { } // NOLINT
75 
83  var(float x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
84 
92  var(double x) : vi_(new vari(x)) { } // NOLINT
93 
101  var(long double x) : vi_(new vari(x)) { } // NOLINT
102 
110  var(bool x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
111 
119  var(char x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
120 
128  var(short x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
129 
137  var(int x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
138 
146  var(long x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
147 
155  var(unsigned char x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
156 
164  // NOLINTNEXTLINE
165  var(unsigned short x) : vi_(new vari(static_cast<double>(x))) { }
166 
174  var(unsigned int x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
175 
183  // NOLINTNEXTLINE
184  var(unsigned long x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
185 
186 #ifdef _WIN64
187 
188  // these two ctors are for Win64 to enable 64-bit signed
189  // and unsigned integers, because long and unsigned long
190  // are still 32-bit
191 
199  var(size_t x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
200 
201 
209  var(ptrdiff_t x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
210 #endif
211 
212 
213 #ifdef BOOST_MATH_USE_FLOAT128
214 
215  // this ctor is for later GCCs that have the __float128
216  // type enabled, because it gets enabled by boost
217 
225  var(__float128 x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
226 
227 #endif
228 
234  inline double val() const {
235  return vi_->val_;
236  }
237 
246  inline double adj() const {
247  return vi_->adj_;
248  }
249 
262  void grad(std::vector<var>& x,
263  std::vector<double>& g) {
264  stan::math::grad(vi_); // defined in chainable.hpp
265  g.resize(x.size());
266  for (size_t i = 0; i < x.size(); ++i)
267  g[i] = x[i].vi_->adj_;
268  }
269 
276  void grad() {
277  stan::math::grad(vi_); // defined in chainable.hpp
278  }
279 
280  // POINTER OVERRIDES
281 
294  inline vari& operator*() {
295  return *vi_;
296  }
297 
308  inline vari* operator->() {
309  return vi_;
310  }
311 
312  // COMPOUND ASSIGNMENT OPERATORS
313 
324  inline var& operator+=(const var& b);
325 
336  inline var& operator+=(const double b);
337 
349  inline var& operator-=(const var& b);
350 
362  inline var& operator-=(const double b);
363 
375  inline var& operator*=(const var& b);
376 
388  inline var& operator*=(const double b);
389 
400  inline var& operator/=(const var& b);
401 
413  inline var& operator/=(const double b);
414 
423  friend std::ostream& operator<<(std::ostream& os, const var& v) {
424  if (v.vi_ == 0)
425  return os << "uninitialized";
426  return os << v.val();
427  }
428  };
429 
430  }
431 }
432 #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:155
var(long x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:146
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:184
The variable implementation base class.
Definition: vari.hpp:28
static void grad(chainable *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
Definition: grad.hpp:30
var(vari *vi)
Construct a variable from a pointer to a variable implementation.
Definition: var.hpp:74
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
vari & operator*()
Return a reference to underlying implementation of this variable.
Definition: var.hpp:294
var & operator-=(const var &b)
The compound subtract/assignment operator for variables (C++).
const double val_
The value of this variable.
Definition: vari.hpp:36
var(double x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:92
var(bool x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:110
var(char x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:119
bool is_uninitialized()
Return true if this variable has been declared, but not been defined.
Definition: var.hpp:55
var(unsigned int x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:174
var()
Construct a variable for later assignment.
Definition: var.hpp:66
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:44
var(float x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:83
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:262
vari * operator->()
Return a pointer to the underlying implementation of this variable.
Definition: var.hpp:308
void grad()
Compute the gradient of this (dependent) variable with respect to all (independent) variables...
Definition: var.hpp:276
var(long double x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:101
var(int x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:137
double Scalar
Definition: var.hpp:35
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:42
double val() const
Return the value of this variable.
Definition: var.hpp:234
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:423
double adj() const
Return the derivative of the root expression with respect to this expression.
Definition: var.hpp:246
var(unsigned short x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:165
var(short x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:128

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