Stan Math Library: stan/math/rev/core/var.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/rev/core/vari.hpp>
5 #include <stan/math/rev/core/grad.hpp>
6 #include <stan/math/rev/core/chainable_alloc.hpp>
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
54 bool is_uninitialized() {
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
stan::math::var::operator+=
var & operator+=(const var &b)
The compound add/assignment operator for variables (C++).
Definition: operator_plus_equal.hpp:10
stan::math::var::operator*=
var & operator*=(const var &b)
The compound multiply/assignment operator for variables (C++).
Definition: operator_multiply_equal.hpp:10
stan::math::var::var
var(unsigned char x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:154
vari.hpp
stan::math::var::var
var(long x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:145
stan
Definition: log_sum_exp.hpp:8
stan::math::var::operator/=
var & operator/=(const var &b)
The compound divide/assignment operator for variables (C++).
Definition: operator_divide_equal.hpp:10
stan::math::var::var
var(unsigned long x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:183
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:30
grad.hpp
stan::math::var::var
var(vari *vi)
Construct a variable from a pointer to a variable implementation.
Definition: var.hpp:73
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
stan::math::var::operator*
vari & operator*()
Return a reference to underlying implementation of this variable.
Definition: var.hpp:293
stan::math::grad
static void grad(vari *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
Definition: grad.hpp:30
stan::math::var::operator-=
var & operator-=(const var &b)
The compound subtract/assignment operator for variables (C++).
Definition: operator_minus_equal.hpp:10
stan::math::vari::val_
const double val_
The value of this variable.
Definition: vari.hpp:38
stan::math::var::var
var(double x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:91
stan::math::var::var
var(bool x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:109
stan::math::var::var
var(char x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:118
stan::math::var::is_uninitialized
bool is_uninitialized()
Return true if this variable has been declared, but not been defined.
Definition: var.hpp:54
stan::math::var::var
var(unsigned int x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:173
stan::math::var::var
var()
Construct a variable for later assignment.
Definition: var.hpp:65
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:43
chainable_alloc.hpp
stan::math::var::var
var(float x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:82
stan::math::var::grad
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
stan::math::var::operator->
vari * operator->()
Return a pointer to the underlying implementation of this variable.
Definition: var.hpp:307
stan::math::var::grad
void grad()
Compute the gradient of this (dependent) variable with respect to all (independent) variables...
Definition: var.hpp:275
stan::math::var::var
var(long double x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:100
stan::math::var::var
var(int x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:136
stan::math::var::Scalar
double Scalar
Definition: var.hpp:34
stan::math::vari::adj_
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
stan::math::var::val
double val() const
Return the value of this variable.
Definition: var.hpp:233
stan::math::var::operator<<
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
stan::math::var::adj
double adj() const
Return the derivative of the root expression with respect to this expression.
Definition: var.hpp:245
stan::math::var::var
var(unsigned short x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
Definition: var.hpp:164
stan::math::var::var
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.