Stan Math Library  2.6.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
Public Types | Public Member Functions | Public Attributes | Friends | List of all members
stan::math::var Class Reference

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

#include <var.hpp>

Public Types

typedef double Scalar
 

Public Member Functions

bool is_uninitialized ()
 Return true if this variable has been declared, but not been defined. More...
 
 var ()
 Construct a variable for later assignment. More...
 
 var (vari *vi)
 Construct a variable from a pointer to a variable implementation. More...
 
 var (float x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. More...
 
 var (double x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument as a value and a zero adjoint. More...
 
 var (long double x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. More...
 
 var (bool x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. More...
 
 var (char x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. More...
 
 var (short x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. More...
 
 var (int x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. More...
 
 var (long x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. More...
 
 var (unsigned char x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. More...
 
 var (unsigned short x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. More...
 
 var (unsigned int x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. More...
 
 var (unsigned long x)
 Construct a variable from the specified arithmetic argument by constructing a new vari with the argument cast to double, and a zero adjoint. 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 the gradient of this (dependent) variable with respect to all (independent) variables. 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...
 

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 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 32 of file var.hpp.

Member Typedef Documentation

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

Definition at line 35 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 66 of file var.hpp.

stan::math::var::var ( vari vi)
inline

Construct a variable from a pointer to a variable implementation.

Parameters
viVariable implementation.

Definition at line 74 of file var.hpp.

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 83 of file var.hpp.

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 92 of file var.hpp.

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 101 of file var.hpp.

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 110 of file var.hpp.

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 119 of file var.hpp.

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 128 of file var.hpp.

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 137 of file var.hpp.

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 146 of file var.hpp.

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 155 of file var.hpp.

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 165 of file var.hpp.

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 174 of file var.hpp.

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 184 of file var.hpp.

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 246 of file var.hpp.

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 262 of file var.hpp.

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 276 of file var.hpp.

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 55 of file var.hpp.

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 294 of file var.hpp.

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.

var & stan::math::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 15 of file operator_multiply_equal.hpp.

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.

var & stan::math::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 15 of file operator_plus_equal.hpp.

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.

var & stan::math::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 15 of file operator_minus_equal.hpp.

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 308 of file var.hpp.

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.

var & stan::math::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 15 of file operator_divide_equal.hpp.

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

Return the value of this variable.

Returns
The value of this variable.

Definition at line 234 of file var.hpp.

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 423 of file var.hpp.

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 44 of file var.hpp.


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

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