Stan Math Library  2.12.0
reverse mode automatic differentiation
Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
stan::math::vari Class Reference

The variable implementation base class. More...

#include <vari.hpp>

Inheritance diagram for stan::math::vari:
stan::math::cholesky_decompose_v_vari stan::math::gevv_vvv_vari stan::math::multiply_mat_vari< TA, RA, CA, TB, CB > stan::math::multiply_mat_vari< double, 1, CA, TB, 1 > stan::math::multiply_mat_vari< double, RA, CA, TB, CB > stan::math::multiply_mat_vari< TA, 1, CA, double, 1 > stan::math::multiply_mat_vari< TA, 1, CA, TB, 1 > stan::math::multiply_mat_vari< TA, RA, CA, double, CB > stan::math::op_ddv_vari stan::math::op_dv_vari stan::math::op_dvd_vari stan::math::op_dvv_vari stan::math::op_matrix_vari stan::math::op_v_vari stan::math::op_vd_vari stan::math::op_vdd_vari stan::math::op_vdv_vari stan::math::op_vector_vari stan::math::op_vv_vari stan::math::op_vvd_vari stan::math::op_vvv_vari stan::math::precomputed_gradients_vari stan::math::stored_gradient_vari stan::math::sum_v_vari

Public Member Functions

 vari (const double x)
 Construct a variable implementation from a value. More...
 
 vari (const double x, bool stacked)
 
virtual ~vari ()
 Throw an illegal argument exception. More...
 
virtual void chain ()
 Apply the chain rule to this variable based on the variables on which it depends. More...
 
void init_dependent ()
 Initialize the adjoint for this (dependent) variable to 1. More...
 
void set_zero_adjoint ()
 Set the adjoint value of this variable to 0. More...
 

Static Public Member Functions

static void * operator new (size_t nbytes)
 Allocate memory from the underlying memory pool. More...
 
static void operator delete (void *)
 Delete a pointer from the underlying memory pool. More...
 

Public Attributes

const double val_
 The value of this variable. More...
 
double adj_
 The adjoint of this variable, which is the partial derivative of this variable with respect to the root variable. More...
 

Friends

class var
 
std::ostream & operator<< (std::ostream &os, const vari *v)
 Insertion operator for vari. More...
 

Detailed Description

The variable implementation base class.

This class is complete (not abstract) and may be used for constants.

A variable implementation is constructed with a constant value. It also stores the adjoint for storing the partial derivative with respect to the root of the derivative tree.

The chain() method applies the chain rule. Concrete extensions of this class will represent base variables or the result of operations such as addition or subtraction. These extended classes will store operand variables and propagate derivative information via an implementation of chain().

Definition at line 30 of file vari.hpp.

Constructor & Destructor Documentation

stan::math::vari::vari ( const double  x)
inlineexplicit

Construct a variable implementation from a value.

The adjoint is initialized to zero.

All constructed variables are added to the stack. Variables should be constructed before variables on which they depend to insure proper partial derivative propagation. During derivative propagation, the chain() method of each variable will be called in the reverse order of construction.

Parameters
xValue of the constructed variable.

Definition at line 58 of file vari.hpp.

stan::math::vari::vari ( const double  x,
bool  stacked 
)
inline

Definition at line 64 of file vari.hpp.

virtual stan::math::vari::~vari ( )
inlinevirtual

Throw an illegal argument exception.

Warning: Destructors should never called for var objects.

Exceptions
Logicexception always.

Definition at line 80 of file vari.hpp.

Member Function Documentation

virtual void stan::math::vari::chain ( )
inlinevirtual
void stan::math::vari::init_dependent ( )
inline

Initialize the adjoint for this (dependent) variable to 1.

This operation is applied to the dependent variable before propagating derivatives, setting the derivative of the result with respect to itself to be 1.

Definition at line 98 of file vari.hpp.

static void stan::math::vari::operator delete ( void *  )
inlinestatic

Delete a pointer from the underlying memory pool.

This no-op implementation enables a subclass to throw exceptions in its constructor. An exception thrown in the constructor of a subclass will result in an error being raised, which is in turn caught and calls delete().

See the discussion of "plugging the memory leak" in: http://www.parashift.com/c++-faq/memory-pools.html

Definition at line 149 of file vari.hpp.

static void* stan::math::vari::operator new ( size_t  nbytes)
inlinestatic

Allocate memory from the underlying memory pool.

This memory is is managed as a whole externally.

Warning: Classes should not be allocated with this operator if they have non-trivial destructors.

Parameters
nbytesNumber of bytes to allocate.
Returns
Pointer to allocated bytes.

Definition at line 134 of file vari.hpp.

void stan::math::vari::set_zero_adjoint ( )
inline

Set the adjoint value of this variable to 0.

This is used to reset adjoints before propagating derivatives again (for example in a Jacobian calculation).

Definition at line 107 of file vari.hpp.

Friends And Related Function Documentation

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

Insertion operator for vari.

Prints the current value and the adjoint value.

Parameters
os[in, out] ostream to modify
v[in] vari object to print.
Returns
The modified ostream.

Definition at line 120 of file vari.hpp.

friend class var
friend

Definition at line 32 of file vari.hpp.

Member Data Documentation

double stan::math::vari::adj_

The adjoint of this variable, which is the partial derivative of this variable with respect to the root variable.

Definition at line 44 of file vari.hpp.

const double stan::math::vari::val_

The value of this variable.

Definition at line 38 of file vari.hpp.


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

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