1 #ifndef STAN_MATH_REV_CORE_VAR_HPP 2 #define STAN_MATH_REV_CORE_VAR_HPP 7 #include <boost/math/tools/config.hpp> 18 static void grad(vari* vi);
81 var(
float x) : vi_(new
vari(static_cast<double>(x))) {}
108 var(
bool x) : vi_(new
vari(static_cast<double>(x))) {}
117 var(
char x) : vi_(new
vari(static_cast<double>(x))) {}
126 var(
short x) : vi_(new
vari(static_cast<double>(x))) {}
135 var(
int x) : vi_(new
vari(static_cast<double>(x))) {}
144 var(
long x) : vi_(new
vari(static_cast<double>(x))) {}
154 : vi_(new
vari(static_cast<double>(x))) {}
164 var(
unsigned short x) : vi_(new
vari(static_cast<double>(x))) {}
173 var(
unsigned int x) : vi_(new
vari(static_cast<double>(x))) {}
183 var(
unsigned long x) : vi_(new
vari(static_cast<double>(x))) {}
193 explicit var(
const std::complex<double>& x) {
195 vi_ =
new vari(real(x));
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();
214 explicit var(
const std::complex<float>& x) {
216 vi_ =
new vari(static_cast<double>(real(x)));
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();
235 explicit var(
const std::complex<long double>& x) {
237 vi_ =
new vari(static_cast<double>(real(x)));
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();
261 var(
size_t x) :
vi_(
new vari(static_cast<double>(x))) {}
270 var(ptrdiff_t x) :
vi_(
new vari(static_cast<double>(x))) {}
273 #ifdef BOOST_MATH_USE_FLOAT128 285 var(__float128 x) :
vi_(
new vari(static_cast<double>(x))) {}
294 inline double val()
const {
return vi_->
val_; }
304 inline double adj()
const {
return vi_->
adj_; }
318 void grad(std::vector<var>& x, std::vector<double>& g) {
321 for (
size_t i = 0; i < x.size(); ++i)
322 g[i] = x[i].vi_->
adj_;
473 if (v.
vi_ ==
nullptr)
474 return os <<
"uninitialized";
475 return os << v.
val();
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...
var(long x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
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...
The variable implementation base class.
var(const std::complex< float > &x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
var(vari *vi)
Construct a variable from a pointer to a variable implementation.
Independent (input) and dependent (output) variables for gradients.
vari & operator*()
Return a reference to underlying implementation of this variable.
static void grad(vari *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
var & operator-=(const var &b)
The compound subtract/assignment operator for variables (C++).
const double val_
The value of this variable.
var(double x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
var(const std::complex< double > &x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
var(bool x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
var(char x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
bool is_uninitialized()
Return true if this variable has been declared, but not been defined.
var(unsigned int x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
var()
Construct a variable for later assignment.
vari * vi_
Pointer to the implementation of this variable.
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.
var(float x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
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...
vari * operator->()
Return a pointer to the underlying implementation of this variable.
void grad()
Compute the gradient of this (dependent) variable with respect to all (independent) variables...
var(long double x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
var(int x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
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...
var(unsigned short x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
var(short x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...
double val() const
Return the value of this variable.
var(const std::complex< long double > &x)
Construct a variable from the specified arithmetic argument by constructing a new vari with the argum...