var.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_CORE_VAR_HPP
#define STAN_MATH_REV_CORE_VAR_HPP
#include <stan/math/rev/core/vari.hpp>
#include <stan/math/rev/core/grad.hpp>
#include <stan/math/rev/core/chainable_alloc.hpp>
#include <boost/math/tools/config.hpp>
#include <ostream>
#include <vector>
namespace stan {
namespace math {
// forward declare
static void grad(vari* vi);
class var {
public:
// FIXME: doc what this is for
typedef double Scalar;
vari * vi_;
bool is_uninitialized() {
return (vi_ == static_cast<vari*>(0U));
}
var() : vi_(static_cast<vari*>(0U)) { }
var(vari* vi) : vi_(vi) { } // NOLINT
var(float x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
var(double x) : vi_(new vari(x)) { } // NOLINT
var(long double x) : vi_(new vari(x)) { } // NOLINT
var(bool x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
var(char x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
var(short x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
var(int x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
var(long x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
var(unsigned char x) // NOLINT(runtime/explicit)
: vi_(new vari(static_cast<double>(x))) { }
// NOLINTNEXTLINE
var(unsigned short x) : vi_(new vari(static_cast<double>(x))) { }
var(unsigned int x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
// NOLINTNEXTLINE
var(unsigned long x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
#ifdef _WIN64
// these two ctors are for Win64 to enable 64-bit signed
// and unsigned integers, because long and unsigned long
// are still 32-bit
var(size_t x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
var(ptrdiff_t x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
#endif
#ifdef BOOST_MATH_USE_FLOAT128
// this ctor is for later GCCs that have the __float128
// type enabled, because it gets enabled by boost
var(__float128 x) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
#endif
inline double val() const {
return vi_->val_;
}
inline double adj() const {
return vi_->adj_;
}
void grad(std::vector<var>& x,
std::vector<double>& g) {
stan::math::grad(vi_);
g.resize(x.size());
for (size_t i = 0; i < x.size(); ++i)
g[i] = x[i].vi_->adj_;
}
void grad() {
stan::math::grad(vi_);
}
// POINTER OVERRIDES
inline vari& operator*() {
return *vi_;
}
inline vari* operator->() {
return vi_;
}
// COMPOUND ASSIGNMENT OPERATORS
inline var& operator+=(const var& b);
inline var& operator+=(double b);
inline var& operator-=(const var& b);
inline var& operator-=(double b);
inline var& operator*=(const var& b);
inline var& operator*=(double b);
inline var& operator/=(const var& b);
inline var& operator/=(double b);
friend std::ostream& operator<<(std::ostream& os, const var& v) {
if (v.vi_ == 0)
return os << "uninitialized";
return os << v.val();
}
};
}
}
#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:152
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:143
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:182
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:71
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
stan::math::var::operator*
vari & operator*()
Return a reference to underlying implementation of this variable.
Definition: var.hpp:290
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:89
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:107
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:116
stan::math::var::is_uninitialized
bool is_uninitialized()
Return true if this variable has been declared, but not been defined.
Definition: var.hpp:53
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:172
stan::math::var::var
var()
Construct a variable for later assignment.
Definition: var.hpp:64
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42
chainable_alloc.hpp
stan::math::var::adj
double adj() const
Return the derivative of the root expression with respect to this expression.
Definition: var.hpp:242
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:80
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:258
stan::math::var::operator->
vari * operator->()
Return a pointer to the underlying implementation of this variable.
Definition: var.hpp:304
stan::math::var::grad
void grad()
Compute the gradient of this (dependent) variable with respect to all (independent) variables...
Definition: var.hpp:272
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:98
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:134
stan::math::var::Scalar
double Scalar
Definition: var.hpp:33
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::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:419
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:163
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:125
stan::math::var::val
double val() const
Return the value of this variable.
Definition: var.hpp:230

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