Stan  1.0
probability, sampling & optimization
Public Types | Public Member Functions | Public Attributes | Friends | List of all members
stan::agrad::var Class Reference

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

#include <agrad.hpp>

Public Types

typedef double Scalar
 
typedef double Scalar
 

Public Member Functions

bool is_uninitialized ()
 Return true if this variable has been declared, but not been defined. More...
 
 var (vari *vi)
 Construct a variable from a pointer to a variable implementation. More...
 
 var ()
 Construct a variable for later assignment. More...
 
 var (bool b)
 Construct a variable by static casting the specified value to double. More...
 
 var (char c)
 Construct a variable by static casting the specified value to double. More...
 
 var (short n)
 Construct a variable by static casting the specified value to double. More...
 
 var (unsigned short n)
 Construct a variable by static casting the specified value to double. More...
 
 var (int n)
 Construct a variable by static casting the specified value to double. More...
 
 var (unsigned int n)
 Construct a variable by static casting the specified value to double. More...
 
 var (long int n)
 Construct a variable by static casting the specified value to double. More...
 
 var (unsigned long int n)
 Construct a variable by static casting the specified value to double. More...
 
 var (unsigned long long n)
 Construct a variable by static casting the specified value to double. More...
 
 var (long long n)
 Construct a variable by static casting the specified value to double. More...
 
 var (float x)
 Construct a variable by static casting the specified value to double. More...
 
 var (double x)
 Construct a variable with the specified value. More...
 
 var (long double x)
 Construct a variable by static casting the specified value to double. More...
 
double val () const
 Return the value of this variable. More...
 
double adj () const
 Return the derivative of the root expression with respect to this expression. More...
 
void grad (std::vector< var > &x, std::vector< double > &g)
 Compute the gradient of this (dependent) variable with respect to the specified vector of (independent) variables, assigning the specified vector to the gradient. More...
 
void grad ()
 Compute gradients of this dependent variable with respect to all variables on which it depends. More...
 
varioperator* ()
 Return a reference to underlying implementation of this variable. More...
 
varioperator-> ()
 Return a pointer to the underlying implementation of this variable. More...
 
varoperator+= (const var &b)
 The compound add/assignment operator for variables (C++). More...
 
varoperator+= (const double b)
 The compound add/assignment operator for scalars (C++). More...
 
varoperator-= (const var &b)
 The compound subtract/assignment operator for variables (C++). More...
 
varoperator-= (const double b)
 The compound subtract/assignment operator for scalars (C++). More...
 
varoperator*= (const var &b)
 The compound multiply/assignment operator for variables (C++). More...
 
varoperator*= (const double b)
 The compound multiply/assignment operator for scalars (C++). More...
 
varoperator/= (const var &b)
 The compound divide/assignment operator for variables (C++). More...
 
varoperator/= (const double b)
 The compound divide/assignment operator for scalars (C++). More...
 
 var (vari *vi)
 Construct a variable from a pointer to a variable implementation. More...
 
 var ()
 Construct a variable for later assignment. More...
 
 var (bool b)
 Construct a variable by static casting the specified value to double. More...
 
 var (char c)
 Construct a variable by static casting the specified value to double. More...
 
 var (short n)
 Construct a variable by static casting the specified value to double. More...
 
 var (unsigned short n)
 Construct a variable by static casting the specified value to double. More...
 
 var (int n)
 Construct a variable by static casting the specified value to double. More...
 
 var (unsigned int n)
 Construct a variable by static casting the specified value to double. More...
 
 var (long int n)
 Construct a variable by static casting the specified value to double. More...
 
 var (unsigned long int n)
 Construct a variable by static casting the specified value to double. More...
 
 var (float x)
 Construct a variable by static casting the specified value to double. More...
 
 var (double x)
 Construct a variable with the specified value. More...
 
 var (long double x)
 Construct a variable by static casting the specified value to double. More...
 
double val () const
 Return the value of this variable. More...
 
void grad (std::vector< var > &x, std::vector< double > &g)
 Compute the gradient of this dependent variable with respect to the specified vector of independent variables, assigning the gradients componentwise to the specified vector of gradients. More...
 
void grad ()
 Compute gradients of this dependent variable with respect to all variables on which it depends. More...
 
varoperator+= (const var &b)
 The compound add/assignment operator for variables (C++). More...
 
varoperator+= (const double &b)
 The compound add/assignment operator for scalars (C++). More...
 
varoperator-= (const var &b)
 The compound subtract/assignment operator for variables (C++). More...
 
varoperator-= (const double &b)
 The compound subtract/assignment operator for scalars (C++). More...
 
varoperator*= (const var &b)
 The compound multiply/assignment operator for variables (C++). More...
 
varoperator*= (const double &b)
 The compound multiply/assignment operator for scalars (C++). More...
 
varoperator/= (const var &b)
 The compound divide/assignment operator for variables (C++). More...
 
varoperator/= (const double &b)
 The compound divide/assignment operator for scalars (C++). More...
 

Public Attributes

varivi_
 Pointer to the implementation of this variable. More...
 

Friends

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. More...
 

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 agrad::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 agrad::var values objects.

An agrad::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 agrad::var values objects.

Definition at line 214 of file agrad.hpp.

Member Typedef Documentation

◆ Scalar [1/2]

typedef double stan::agrad::var::Scalar

Definition at line 218 of file agrad.hpp.

◆ Scalar [2/2]

typedef double stan::agrad::var::Scalar

Definition at line 703 of file agrad_thread_safe.hpp.

Constructor & Destructor Documentation

◆ var() [1/28]

stan::agrad::var::var ( vari vi)
inlineexplicit

Construct a variable from a pointer to a variable implementation.

Parameters
viVariable implementation.

Definition at line 247 of file agrad.hpp.

◆ var() [2/28]

stan::agrad::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 258 of file agrad.hpp.

◆ var() [3/28]

stan::agrad::var::var ( bool  b)
inline

Construct a variable by static casting the specified value to double.

Parameters
bValue.

Definition at line 268 of file agrad.hpp.

◆ var() [4/28]

stan::agrad::var::var ( char  c)
inline

Construct a variable by static casting the specified value to double.

Parameters
cValue.

Definition at line 278 of file agrad.hpp.

◆ var() [5/28]

stan::agrad::var::var ( short  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 288 of file agrad.hpp.

◆ var() [6/28]

stan::agrad::var::var ( unsigned short  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 298 of file agrad.hpp.

◆ var() [7/28]

stan::agrad::var::var ( int  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 308 of file agrad.hpp.

◆ var() [8/28]

stan::agrad::var::var ( unsigned int  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 318 of file agrad.hpp.

◆ var() [9/28]

stan::agrad::var::var ( long int  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 328 of file agrad.hpp.

◆ var() [10/28]

stan::agrad::var::var ( unsigned long int  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 338 of file agrad.hpp.

◆ var() [11/28]

stan::agrad::var::var ( unsigned long long  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 348 of file agrad.hpp.

◆ var() [12/28]

stan::agrad::var::var ( long long  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 358 of file agrad.hpp.

◆ var() [13/28]

stan::agrad::var::var ( float  x)
inline

Construct a variable by static casting the specified value to double.

Parameters
xValue.

Definition at line 368 of file agrad.hpp.

◆ var() [14/28]

stan::agrad::var::var ( double  x)
inline

Construct a variable with the specified value.

Parameters
xValue of the variable.

Definition at line 377 of file agrad.hpp.

◆ var() [15/28]

stan::agrad::var::var ( long double  x)
inline

Construct a variable by static casting the specified value to double.

Parameters
xValue.

Definition at line 387 of file agrad.hpp.

◆ var() [16/28]

stan::agrad::var::var ( vari vi)
inlineexplicit

Construct a variable from a pointer to a variable implementation.

Parameters
viVariable implementation.

Definition at line 719 of file agrad_thread_safe.hpp.

◆ var() [17/28]

stan::agrad::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 730 of file agrad_thread_safe.hpp.

◆ var() [18/28]

stan::agrad::var::var ( bool  b)
inline

Construct a variable by static casting the specified value to double.

Parameters
bValue.

Definition at line 740 of file agrad_thread_safe.hpp.

◆ var() [19/28]

stan::agrad::var::var ( char  c)
inline

Construct a variable by static casting the specified value to double.

Parameters
cValue.

Definition at line 750 of file agrad_thread_safe.hpp.

◆ var() [20/28]

stan::agrad::var::var ( short  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 760 of file agrad_thread_safe.hpp.

◆ var() [21/28]

stan::agrad::var::var ( unsigned short  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 770 of file agrad_thread_safe.hpp.

◆ var() [22/28]

stan::agrad::var::var ( int  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 780 of file agrad_thread_safe.hpp.

◆ var() [23/28]

stan::agrad::var::var ( unsigned int  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 790 of file agrad_thread_safe.hpp.

◆ var() [24/28]

stan::agrad::var::var ( long int  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 800 of file agrad_thread_safe.hpp.

◆ var() [25/28]

stan::agrad::var::var ( unsigned long int  n)
inline

Construct a variable by static casting the specified value to double.

Parameters
nValue.

Definition at line 810 of file agrad_thread_safe.hpp.

◆ var() [26/28]

stan::agrad::var::var ( float  x)
inline

Construct a variable by static casting the specified value to double.

Parameters
xValue.

Definition at line 820 of file agrad_thread_safe.hpp.

◆ var() [27/28]

stan::agrad::var::var ( double  x)
inline

Construct a variable with the specified value.

Parameters
xValue of the variable.

Definition at line 829 of file agrad_thread_safe.hpp.

◆ var() [28/28]

stan::agrad::var::var ( long double  x)
inline

Construct a variable by static casting the specified value to double.

Parameters
xValue.

Definition at line 839 of file agrad_thread_safe.hpp.

Member Function Documentation

◆ adj()

double stan::agrad::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 value for this variable.

Definition at line 408 of file agrad.hpp.

◆ grad() [1/4]

void stan::agrad::var::grad ( )
inline

Compute gradients of this dependent variable with respect to all variables on which it depends.


Memory is recovered, but not freed after this operation, calling recover_memory(); see free_all() to release resources back to the system rather than saving them for reuse).

Until the next creation of a stan::agrad::var instance, the gradient values will be available from an instance x of stan::agrad::var via x.adj(). It may be slightly more efficient to do this without the intermediate creation and population of two vectors as done in the two-argument form grad(std::vector<var>&, std::vector<double>&).

Definition at line 449 of file agrad.hpp.

◆ grad() [2/4]

void stan::agrad::var::grad ( )
inline

Compute gradients of this dependent variable with respect to all variables on which it depends.


Memory is recovered, but not freed after this operation, calling vari::recover_memory(); see vari::free_all() to release resources back to the system rather than saving them for reuse).

Until the next creation of a stan::agrad::var instance, the gradient values will be available from an instance x of stan::agrad::var via x.vi_->adj_. It may be slightly more efficient to do this without the intermediate creation and population of two vectors as done in the two-argument form grad(std::vector<var>&, std::vector<double>&).

Definition at line 889 of file agrad_thread_safe.hpp.

◆ grad() [3/4]

void stan::agrad::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.

After the computation of the gradient and value, memory is recovered.

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

Definition at line 424 of file agrad.hpp.

◆ grad() [4/4]

void stan::agrad::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 gradients componentwise to the specified vector of gradients.

After the computation of the gradient and value, memory is recovered.

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

Definition at line 864 of file agrad_thread_safe.hpp.

◆ is_uninitialized()

bool stan::agrad::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 238 of file agrad.hpp.

◆ operator*()

vari& stan::agrad::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 468 of file agrad.hpp.

◆ operator*=() [1/4]

var& stan::agrad::var::operator*= ( const 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 985 of file agrad_thread_safe.hpp.

◆ operator*=() [2/4]

var & stan::agrad::var::operator*= ( const 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 2283 of file agrad.hpp.

◆ operator*=() [3/4]

var & stan::agrad::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 2278 of file agrad.hpp.

◆ operator*=() [4/4]

var& stan::agrad::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 969 of file agrad_thread_safe.hpp.

◆ operator+=() [1/4]

var& stan::agrad::var::operator+= ( const 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 921 of file agrad_thread_safe.hpp.

◆ operator+=() [2/4]

var & stan::agrad::var::operator+= ( const 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 2259 of file agrad.hpp.

◆ operator+=() [3/4]

var & stan::agrad::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 2254 of file agrad.hpp.

◆ operator+=() [4/4]

var& stan::agrad::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 906 of file agrad_thread_safe.hpp.

◆ operator-=() [1/4]

var& stan::agrad::var::operator-= ( const 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 953 of file agrad_thread_safe.hpp.

◆ operator-=() [2/4]

var & stan::agrad::var::operator-= ( const 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 2271 of file agrad.hpp.

◆ operator-=() [3/4]

var & stan::agrad::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 2266 of file agrad.hpp.

◆ operator-=() [4/4]

var& stan::agrad::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 937 of file agrad_thread_safe.hpp.

◆ operator->()

vari* stan::agrad::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 482 of file agrad.hpp.

◆ operator/=() [1/4]

var& stan::agrad::var::operator/= ( const 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 1016 of file agrad_thread_safe.hpp.

◆ operator/=() [2/4]

var & stan::agrad::var::operator/= ( const 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 2295 of file agrad.hpp.

◆ operator/=() [3/4]

var & stan::agrad::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 2290 of file agrad.hpp.

◆ operator/=() [4/4]

var& stan::agrad::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 1000 of file agrad_thread_safe.hpp.

◆ val() [1/2]

double stan::agrad::var::val ( ) const
inline

Return the value of this variable.

Returns
The value of this variable.

Definition at line 396 of file agrad.hpp.

◆ val() [2/2]

double stan::agrad::var::val ( ) const
inline

Return the value of this variable.

Returns
The value of this variable.

Definition at line 848 of file agrad_thread_safe.hpp.

Friends And Related Function Documentation

◆ operator<<

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 597 of file agrad.hpp.

Member Data Documentation

◆ vi_

vari * stan::agrad::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 227 of file agrad.hpp.


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

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