Public Types | Public Member Functions | Public Attributes | Friends | List of all members
stan::math::var Class Reference

#include "stan/math/rev/core/var.hpp"

Public Types

typedef double Scalar
 

Public Member Functions

bool is_uninitialized ()
 
 var ()
 
 var (vari *vi)
 
 var (float x)
 
 var (double x)
 
 var (long double x)
 
 var (bool x)
 
 var (char x)
 
 var (short x)
 
 var (int x)
 
 var (long x)
 
 var (unsigned char x)
 
 var (unsigned short x)
 
 var (unsigned int x)
 
 var (unsigned long x)
 
 var (const std::complex< double > &x)
 
 var (const std::complex< float > &x)
 
 var (const std::complex< long double > &x)
 
double val () const
 
double adj () const
 
void grad (std::vector< var > &x, std::vector< double > &g)
 
void grad ()
 
varioperator* ()
 
varioperator-> ()
 
varoperator+= (const var &b)
 
varoperator+= (double b)
 
varoperator-= (const var &b)
 
varoperator-= (double b)
 
varoperator*= (const var &b)
 
varoperator*= (double b)
 
varoperator/= (const var &b)
 
varoperator/= (double b)
 

Public Attributes

varivi_
 

Friends

std::ostream & operator<< (std::ostream &os, const var &v)
 

Detailed Description

Independent (input) and dependent (output) variables for gradients.

This class acts as a smart pointer, with resources managed by an agenda-based memory manager scoped to a single gradient calculation.

An var is constructed with a double and used like any other scalar. Arithmetical functions like negation, addition, and subtraction, as well as a range of mathematical functions like exponentiation and powers are overridden to operate on var values objects.

Definition at line 33 of file var.hpp.

Member Typedef Documentation

typedef double stan::math::var::Scalar

Definition at line 36 of file var.hpp.

Constructor & Destructor Documentation

stan::math::var::var ( )
inline

Construct a variable for later assignment.

This is implemented as a no-op, leaving the underlying implementation dangling. Before an assignment, the behavior is thus undefined just as for a basic double.

Definition at line 65 of file var.hpp.

Referenced by var().

65 : vi_(static_cast<vari*>(0U)) {}
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( vari vi)
inline

Construct a variable from a pointer to a variable implementation.

Parameters
viVariable implementation.

Definition at line 72 of file var.hpp.

72 : vi_(vi) {} // NOLINT
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( float  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 81 of file var.hpp.

81 : vi_(new vari(static_cast<double>(x))) {} // NOLINT
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( double  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument as a value and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 90 of file var.hpp.

90 : vi_(new vari(x)) {} // NOLINT
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( long double  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 99 of file var.hpp.

99 : vi_(new vari(x)) {} // NOLINT
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( bool  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 108 of file var.hpp.

108 : vi_(new vari(static_cast<double>(x))) {} // NOLINT
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( char  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 117 of file var.hpp.

117 : vi_(new vari(static_cast<double>(x))) {} // NOLINT
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( short  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 126 of file var.hpp.

126 : vi_(new vari(static_cast<double>(x))) {} // NOLINT
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( int  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 135 of file var.hpp.

135 : vi_(new vari(static_cast<double>(x))) {} // NOLINT
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( long  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 144 of file var.hpp.

144 : vi_(new vari(static_cast<double>(x))) {} // NOLINT
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( unsigned char  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 153 of file var.hpp.

154  : vi_(new vari(static_cast<double>(x))) {}
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( unsigned short  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 164 of file var.hpp.

164 : vi_(new vari(static_cast<double>(x))) {}
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( unsigned int  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 173 of file var.hpp.

173 : vi_(new vari(static_cast<double>(x))) {} // NOLINT
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( unsigned long  x)
inline

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint.

Parameters
xValue of the variable.

Definition at line 183 of file var.hpp.

183 : vi_(new vari(static_cast<double>(x))) {} // NOLINT
vari * vi_
Definition: var.hpp:45
stan::math::var::var ( const std::complex< double > &  x)
inlineexplicit

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. Only works if the imaginary part is zero.

Parameters
xValue of the variable.

Definition at line 193 of file var.hpp.

References allTimeWatchdog::endl, stan::math::invalid_argument(), lem_server::msg, and ss.

193  {
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  }
Float_t ss
Definition: plot.C:24
vari * vi_
Definition: var.hpp:45
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
stan::math::var::var ( const std::complex< float > &  x)
inlineexplicit

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. Only works if the imaginary part is zero.

Parameters
xValue of the variable.

Definition at line 214 of file var.hpp.

References allTimeWatchdog::endl, stan::math::invalid_argument(), lem_server::msg, and ss.

214  {
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  }
Float_t ss
Definition: plot.C:24
vari * vi_
Definition: var.hpp:45
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
stan::math::var::var ( const std::complex< long double > &  x)
inlineexplicit

Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. Only works if the imaginary part is zero.

Parameters
xValue of the variable.

Definition at line 235 of file var.hpp.

References allTimeWatchdog::endl, stan::math::invalid_argument(), lem_server::msg, ss, var(), vi_, and submit_syst::x.

235  {
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  }
Float_t ss
Definition: plot.C:24
vari * vi_
Definition: var.hpp:45
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154

Member Function Documentation

double stan::math::var::adj ( ) const
inline

Return the derivative of the root expression with respect to this expression. This method only works after one of the grad() methods has been called.

Returns
Adjoint for this variable.

Definition at line 304 of file var.hpp.

References stan::math::vari::adj_.

304 { return vi_->adj_; }
vari * vi_
Definition: var.hpp:45
double adj_
Definition: vari.hpp:44
void stan::math::var::grad ( std::vector< var > &  x,
std::vector< double > &  g 
)
inline

Compute the gradient of this (dependent) variable with respect to the specified vector of (independent) variables, assigning the specified vector to the gradient.

The grad() function does not recover memory. In Stan 2.4 and earlier, this function did recover memory.

Parameters
xVector of independent variables.
gGradient vector of partial derivatives of this variable with respect to x.

Definition at line 318 of file var.hpp.

References stan::math::vari::adj_, stan::math::grad(), and MECModelEnuComparisons::i.

Referenced by stan::math::gradient_of_f().

318  {
320  g.resize(x.size());
321  for (size_t i = 0; i < x.size(); ++i)
322  g[i] = x[i].vi_->adj_;
323  }
static void grad(vari *vi)
Definition: grad.hpp:30
vari * vi_
Definition: var.hpp:45
double adj_
Definition: vari.hpp:44
void stan::math::var::grad ( )
inline

Compute the gradient of this (dependent) variable with respect to all (independent) variables.

The grad() function does not recover memory.

Definition at line 331 of file var.hpp.

References stan::math::grad().

331 { stan::math::grad(vi_); }
static void grad(vari *vi)
Definition: grad.hpp:30
vari * vi_
Definition: var.hpp:45
bool stan::math::var::is_uninitialized ( )
inline

Return true if this variable has been declared, but not been defined. Any attempt to use an undefined variable's value or adjoint will result in a segmentation fault.

Returns
true if this variable does not yet have a defined variable.

Definition at line 56 of file var.hpp.

Referenced by stan::math::is_uninitialized().

56 { return (vi_ == static_cast<vari*>(nullptr)); }
vari * vi_
Definition: var.hpp:45
vari& stan::math::var::operator* ( )
inline

Return a reference to underlying implementation of this variable.

If x is of type var, then applying this operator, *x, has the same behavior as *(x.vi_).

Warning: The returned reference does not track changes to this variable.

Returns
variable

Definition at line 347 of file var.hpp.

References vi_.

347 { return *vi_; }
vari * vi_
Definition: var.hpp:45
var & stan::math::var::operator*= ( const var b)
inline

The compound multiply/assignment operator for variables (C++).

If this variable is a and the argument is the variable b, then (a *= b) behaves exactly the same way as (a = a * b). Note that the result is an assignable lvalue.

Parameters
bThe variable to multiply this variable by.
Returns
The result of multiplying this variable by the specified variable.

Definition at line 10 of file operator_multiply_equal.hpp.

References vi_.

Referenced by operator->().

10  {
11  vi_ = new multiply_vv_vari(vi_, b.vi_);
12  return *this;
13 }
vari * vi_
Definition: var.hpp:45
const hit & b
Definition: hits.cxx:21
var & stan::math::var::operator*= ( double  b)
inline

The compound multiply/assignment operator for scalars (C++).

If this variable is a and the argument is the scalar b, then (a *= b) behaves exactly the same way as (a = a * b). Note that the result is an assignable lvalue.

Parameters
bThe scalar to multiply this variable by.
Returns
The result of multplying this variable by the specified variable.

Definition at line 15 of file operator_multiply_equal.hpp.

References vi_.

15  {
16  if (b == 1.0)
17  return *this;
18  vi_ = new multiply_vd_vari(vi_, b);
19  return *this;
20 }
vari * vi_
Definition: var.hpp:45
const hit & b
Definition: hits.cxx:21
var & stan::math::var::operator+= ( const var b)
inline

The compound add/assignment operator for variables (C++).

If this variable is a and the argument is the variable b, then (a += b) behaves exactly the same way as (a = a + b), creating an intermediate variable representing (a + b).

Parameters
bThe variable to add to this variable.
Returns
The result of adding the specified variable to this variable.

Definition at line 10 of file operator_plus_equal.hpp.

References vi_.

Referenced by operator->().

10  {
11  vi_ = new add_vv_vari(vi_, b.vi_);
12  return *this;
13 }
vari * vi_
Definition: var.hpp:45
const hit & b
Definition: hits.cxx:21
var & stan::math::var::operator+= ( double  b)
inline

The compound add/assignment operator for scalars (C++).

If this variable is a and the argument is the scalar b, then (a += b) behaves exactly the same way as (a = a + b). Note that the result is an assignable lvalue.

Parameters
bThe scalar to add to this variable.
Returns
The result of adding the specified variable to this variable.

Definition at line 15 of file operator_plus_equal.hpp.

References vi_.

15  {
16  if (b == 0.0)
17  return *this;
18  vi_ = new add_vd_vari(vi_, b);
19  return *this;
20 }
vari * vi_
Definition: var.hpp:45
const hit & b
Definition: hits.cxx:21
var & stan::math::var::operator-= ( const var b)
inline

The compound subtract/assignment operator for variables (C++).

If this variable is a and the argument is the variable b, then (a -= b) behaves exactly the same way as (a = a - b). Note that the result is an assignable lvalue.

Parameters
bThe variable to subtract from this variable.
Returns
The result of subtracting the specified variable from this variable.

Definition at line 10 of file operator_minus_equal.hpp.

References vi_.

Referenced by operator->().

10  {
11  vi_ = new subtract_vv_vari(vi_, b.vi_);
12  return *this;
13 }
vari * vi_
Definition: var.hpp:45
const hit & b
Definition: hits.cxx:21
var & stan::math::var::operator-= ( double  b)
inline

The compound subtract/assignment operator for scalars (C++).

If this variable is a and the argument is the scalar b, then (a -= b) behaves exactly the same way as (a = a - b). Note that the result is an assignable lvalue.

Parameters
bThe scalar to subtract from this variable.
Returns
The result of subtracting the specified variable from this variable.

Definition at line 15 of file operator_minus_equal.hpp.

References vi_.

15  {
16  if (b == 0.0)
17  return *this;
18  vi_ = new subtract_vd_vari(vi_, b);
19  return *this;
20 }
vari * vi_
Definition: var.hpp:45
const hit & b
Definition: hits.cxx:21
vari* stan::math::var::operator-> ( )
inline

Return a pointer to the underlying implementation of this variable.

If x is of type var, then applying this operator, x->, behaves the same way as x.vi_->.

Warning: The returned result does not track changes to this variable.

Definition at line 359 of file var.hpp.

References b, operator*=(), operator+=(), operator-=(), operator/=(), and vi_.

359 { return vi_; }
vari * vi_
Definition: var.hpp:45
var & stan::math::var::operator/= ( const var b)
inline

The compound divide/assignment operator for variables (C++). If this variable is a and the argument is the variable b, then (a /= b) behaves exactly the same way as (a = a / b). Note that the result is an assignable lvalue.

Parameters
bThe variable to divide this variable by.
Returns
The result of dividing this variable by the specified variable.

Definition at line 10 of file operator_divide_equal.hpp.

References vi_.

Referenced by operator->().

10  {
11  vi_ = new divide_vv_vari(vi_, b.vi_);
12  return *this;
13 }
vari * vi_
Definition: var.hpp:45
const hit & b
Definition: hits.cxx:21
var & stan::math::var::operator/= ( double  b)
inline

The compound divide/assignment operator for scalars (C++).

If this variable is a and the argument is the scalar b, then (a /= b) behaves exactly the same way as (a = a / b). Note that the result is an assignable lvalue.

Parameters
bThe scalar to divide this variable by.
Returns
The result of dividing this variable by the specified variable.

Definition at line 15 of file operator_divide_equal.hpp.

References vi_.

15  {
16  if (b == 1.0)
17  return *this;
18  vi_ = new divide_vd_vari(vi_, b);
19  return *this;
20 }
vari * vi_
Definition: var.hpp:45
const hit & b
Definition: hits.cxx:21
double stan::math::var::val ( ) const
inline

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const var v 
)
friend

Write the value of this auto-dif variable and its adjoint to the specified output stream.

Parameters
osOutput stream to which to write.
vVariable to write.
Returns
Reference to the specified output stream.

Definition at line 472 of file var.hpp.

472  {
473  if (v.vi_ == nullptr)
474  return os << "uninitialized";
475  return os << v.val();
476  }

Member Data Documentation

vari* stan::math::var::vi_

Pointer to the implementation of this variable.

This value should not be modified, but may be accessed in var operators to construct vari instances.

Definition at line 45 of file var.hpp.

Referenced by stan::math::acos(), stan::math::acosh(), stan::math::as_bool(), stan::math::asin(), stan::math::asinh(), stan::math::atan(), stan::math::atan2(), stan::math::atanh(), stan::math::bessel_first_kind(), stan::math::bessel_second_kind(), stan::math::binary_log_loss(), stan::math::cbrt(), stan::math::ceil(), stan::math::cos(), stan::math::cosh(), stan::math::digamma(), stan::math::internal::ops_partials_edge< double, var >::dump_operands(), stan::math::erf(), stan::math::erfc(), stan::math::gevv_vvv_vari::eval_gevv(), stan::math::exp(), stan::math::exp2(), stan::math::expm1(), stan::math::fabs(), stan::math::falling_factorial(), stan::math::fdim(), stan::math::floor(), stan::math::fma(), stan::math::fmax(), stan::math::fmin(), stan::math::fmod(), stan::math::gamma_p(), stan::math::gamma_q(), stan::math::grad(), stan::math::gradient(), stan::math::hessian(), stan::math::hessian_times_vector(), stan::math::hypot(), stan::math::ibeta(), stan::math::inc_beta(), stan::math::inv(), stan::math::inv_cloglog(), stan::math::inv_logit(), stan::math::inv_Phi(), stan::math::inv_sqrt(), stan::math::inv_square(), stan::math::lgamma(), stan::math::lmgamma(), stan::math::log(), stan::math::log10(), stan::math::log1m(), stan::math::log1m_exp(), stan::math::log1m_inv_logit(), stan::math::log1p(), stan::math::log1p_exp(), stan::math::log2(), stan::math::log_diff_exp(), stan::math::log_falling_factorial(), stan::math::log_inv_logit(), stan::math::log_inv_logit_diff(), stan::math::log_rising_factorial(), stan::math::log_sum_exp(), stan::math::logit(), stan::math::modified_bessel_first_kind(), stan::math::modified_bessel_second_kind(), stan::math::multiply_log(), stan::math::operator*(), operator*(), operator*=(), stan::math::operator+(), stan::math::operator++(), operator+=(), stan::math::operator-(), stan::math::operator--(), operator-=(), operator->(), stan::math::operator/(), operator/=(), stan::math::owens_t(), stan::math::Phi(), stan::math::Phi_approx(), stan::math::pow(), stan::math::rising_factorial(), stan::math::round(), stan::math::sin(), stan::math::sinh(), stan::math::sqrt(), stan::math::square(), stan::math::squared_distance(), stan::math::step(), stan::math::tan(), stan::math::tanh(), stan::math::tgamma(), stan::math::trunc(), stan::math::value_of(), stan::math::value_of_rec(), and var().


The documentation for this class was generated from the following files: