| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

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...
vari & | operator* ()
| Return a reference to underlying implementation of this variable. More...
vari * | operator-> ()
| Return a pointer to the underlying implementation of this variable. More...
var & | operator+= (const var &b)
| The compound add/assignment operator for variables (C++). More...
var & | operator+= (const double b)
| The compound add/assignment operator for scalars (C++). More...
var & | operator-= (const var &b)
| The compound subtract/assignment operator for variables (C++). More...
var & | operator-= (const double b)
| The compound subtract/assignment operator for scalars (C++). More...
var & | operator*= (const var &b)
| The compound multiply/assignment operator for variables (C++). More...
var & | operator*= (const double b)
| The compound multiply/assignment operator for scalars (C++). More...
var & | operator/= (const var &b)
| The compound divide/assignment operator for variables (C++). More...
var & | operator/= (const double b)
| The compound divide/assignment operator for scalars (C++). More...

## Public Attributes
vari * | vi_
| 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 31 of file var.hpp.

## Member Typedef Documentation

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

Definition at line 34 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.

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

Construct a variable from a pointer to a variable implementation.

Parameters

  vi | Variable implementation.

Definition at line 73 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

  x | Value of the variable.

Definition at line 82 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

  x | Value of the variable.

Definition at line 91 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

  x | Value of the variable.

Definition at line 100 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

  x | Value of the variable.

Definition at line 109 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

  x | Value of the variable.

Definition at line 118 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

  x | Value of the variable.

Definition at line 127 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

  x | Value of the variable.

Definition at line 136 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

  x | Value of the variable.

Definition at line 145 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

  x | Value of the variable.

Definition at line 154 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

  x | Value of the variable.

Definition at line 164 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

  x | Value of the variable.

Definition at line 173 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

  x | Value of the variable.

Definition at line 183 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 245 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

  x | Vector of independent variables.
  g | Gradient vector of partial derivatives of this variable with respect to x.

Definition at line 261 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 275 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 54 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 293 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

  b | The 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

  b | The 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

  b | The 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

  b | The 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

  b | The 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

  b | The 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 307 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

  b | The 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

  b | The 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 233 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

  os | Output stream to which to write.
  v | Variable to write.

Returns
  Reference to the specified output stream.

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

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

- stan/math/rev/core/var.hpp
- stan/math/rev/core/operator_divide_equal.hpp
- stan/math/rev/core/operator_minus_equal.hpp
- stan/math/rev/core/operator_multiply_equal.hpp
- stan/math/rev/core/operator_plus_equal.hpp

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