All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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.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(chainable* 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) : vi_(new vari(static_cast<double>(x))) { } // NOLINT
// 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_); // defined in chainable.hpp
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_); // defined in chainable.hpp
}
// POINTER OVERRIDES
inline vari& operator*() {
return *vi_;
}
inline vari* operator->() {
return vi_;
}
// COMPOUND ASSIGNMENT OPERATORS
inline var& operator+=(const var& b);
inline var& operator+=(const double b);
inline var& operator-=(const var& b);
inline var& operator-=(const double b);
inline var& operator*=(const var& b);
inline var& operator*=(const double b);
inline var& operator/=(const var& b);
inline var& operator/=(const 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:155
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:146
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:184
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:28
chainable.hpp
stan::math::grad
static void grad(chainable *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
Definition: grad.hpp:30
grad.hpp
stan::math::var::var
var(vari *vi)
Construct a variable from a pointer to a variable implementation.
Definition: var.hpp:74
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
stan::math::var::operator*
vari & operator*()
Return a reference to underlying implementation of this variable.
Definition: var.hpp:294
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:36
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:92
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:110
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:119
stan::math::var::is_uninitialized
bool is_uninitialized()
Return true if this variable has been declared, but not been defined.
Definition: var.hpp:55
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:174
stan::math::var::var
var()
Construct a variable for later assignment.
Definition: var.hpp:66
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:44
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:83
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:262
stan::math::var::operator->
vari * operator->()
Return a pointer to the underlying implementation of this variable.
Definition: var.hpp:308
stan::math::var::grad
void grad()
Compute the gradient of this (dependent) variable with respect to all (independent) variables...
Definition: var.hpp:276
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:101
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:137
stan::math::var::Scalar
double Scalar
Definition: var.hpp:35
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:42
stan::math::var::val
double val() const
Return the value of this variable.
Definition: var.hpp:234
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:423
stan::math::var::adj
double adj() const
Return the derivative of the root expression with respect to this expression.
Definition: var.hpp:246
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:165
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:128

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