Stan Math Library  2.20.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 #include <complex>
11 #include <string>
12 #include <exception>
13 
14 namespace stan {
15 namespace math {
16 
17 // forward declare
18 static void grad(vari* vi);
19 
33 class var {
34  public:
35  // FIXME: doc what this is for
36  typedef double Scalar;
37 
46 
56  bool is_uninitialized() { return (vi_ == static_cast<vari*>(nullptr)); }
57 
65  var() : vi_(static_cast<vari*>(0U)) {}
66 
72  var(vari* vi) : vi_(vi) {} // NOLINT
73 
81  var(float x) : vi_(new vari(static_cast<double>(x))) {} // NOLINT
82 
90  var(double x) : vi_(new vari(x)) {} // NOLINT
91 
99  var(long double x) : vi_(new vari(x)) {} // NOLINT
100 
108  var(bool x) : vi_(new vari(static_cast<double>(x))) {} // NOLINT
109 
117  var(char x) : vi_(new vari(static_cast<double>(x))) {} // NOLINT
118 
126  var(short x) : vi_(new vari(static_cast<double>(x))) {} // NOLINT
127 
135  var(int x) : vi_(new vari(static_cast<double>(x))) {} // NOLINT
136 
144  var(long x) : vi_(new vari(static_cast<double>(x))) {} // NOLINT
145 
153  var(unsigned char x) // NOLINT(runtime/explicit)
154  : vi_(new vari(static_cast<double>(x))) {}
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 
193  explicit var(const std::complex<double>& x) {
194  if (imag(x) == 0) {
195  vi_ = new vari(real(x));
196  } else {
197  std::stringstream ss;
198  ss << "Imaginary part of std::complex used to construct var"
199  << " must be zero. Found real part = " << real(x) << " and "
200  << " found imaginary part = " << imag(x) << std::endl;
201  std::string msg = ss.str();
202  throw std::invalid_argument(msg);
203  }
204  }
205 
214  explicit var(const std::complex<float>& x) {
215  if (imag(x) == 0) {
216  vi_ = new vari(static_cast<double>(real(x)));
217  } else {
218  std::stringstream ss;
219  ss << "Imaginary part of std::complex used to construct var"
220  << " must be zero. Found real part = " << real(x) << " and "
221  << " found imaginary part = " << imag(x) << std::endl;
222  std::string msg = ss.str();
223  throw std::invalid_argument(msg);
224  }
225  }
226 
235  explicit var(const std::complex<long double>& x) {
236  if (imag(x) == 0) {
237  vi_ = new vari(static_cast<double>(real(x)));
238  } else {
239  std::stringstream ss;
240  ss << "Imaginary part of std::complex used to construct var"
241  << " must be zero. Found real part = " << real(x) << " and "
242  << " found imaginary part = " << imag(x) << std::endl;
243  std::string msg = ss.str();
244  throw std::invalid_argument(msg);
245  }
246  }
247 
248 #ifdef _WIN64
249 
250  // these two ctors are for Win64 to enable 64-bit signed
251  // and unsigned integers, because long and unsigned long
252  // are still 32-bit
253 
261  var(size_t x) : vi_(new vari(static_cast<double>(x))) {} // NOLINT
262 
270  var(ptrdiff_t x) : vi_(new vari(static_cast<double>(x))) {} // NOLINT
271 #endif
272 
273 #ifdef BOOST_MATH_USE_FLOAT128
274 
275  // this ctor is for later GCCs that have the __float128
276  // type enabled, because it gets enabled by boost
277 
285  var(__float128 x) : vi_(new vari(static_cast<double>(x))) {} // NOLINT
286 
287 #endif
288 
294  inline double val() const { return vi_->val_; }
295 
304  inline double adj() const { return vi_->adj_; }
305 
318  void grad(std::vector<var>& x, std::vector<double>& g) {
319  stan::math::grad(vi_);
320  g.resize(x.size());
321  for (size_t i = 0; i < x.size(); ++i)
322  g[i] = x[i].vi_->adj_;
323  }
324 
331  void grad() { stan::math::grad(vi_); }
332 
333  // POINTER OVERRIDES
334 
347  inline vari& operator*() { return *vi_; }
348 
359  inline vari* operator->() { return vi_; }
360 
361  // COMPOUND ASSIGNMENT OPERATORS
362 
373  inline var& operator+=(const var& b);
374 
385  inline var& operator+=(double b);
386 
398  inline var& operator-=(const var& b);
399 
411  inline var& operator-=(double b);
412 
424  inline var& operator*=(const var& b);
425 
437  inline var& operator*=(double b);
438 
449  inline var& operator/=(const var& b);
450 
462  inline var& operator/=(double b);
463 
472  friend std::ostream& operator<<(std::ostream& os, const var& v) {
473  if (v.vi_ == nullptr)
474  return os << "uninitialized";
475  return os << v.val();
476  }
477 };
478 
479 } // namespace math
480 } // namespace stan
481 #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:153
var(long x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:144
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(const std::complex< float > &x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:214
var(vari *vi)
Construct a variable from a pointer to a variable implementation.
Definition: var.hpp:72
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
vari & operator*()
Return a reference to underlying implementation of this variable.
Definition: var.hpp:347
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:90
var(const std::complex< double > &x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:193
var(bool x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:108
var(char x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:117
bool is_uninitialized()
Return true if this variable has been declared, but not been defined.
Definition: var.hpp:56
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:45
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw an invalid_argument exception with a consistently formatted message.
double adj() const
Return the derivative of the root expression with respect to this expression.
Definition: var.hpp:304
var(float x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:81
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:318
vari * operator->()
Return a pointer to the underlying implementation of this variable.
Definition: var.hpp:359
void grad()
Compute the gradient of this (dependent) variable with respect to all (independent) variables...
Definition: var.hpp:331
var(long double x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:99
var(int x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:135
double Scalar
Definition: var.hpp:36
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:472
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:126
double val() const
Return the value of this variable.
Definition: var.hpp:294
var(const std::complex< long double > &x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:235

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