Stan Math Library  2.20.0
reverse mode automatic differentiation
Public Member Functions | Public Attributes | Friends | List of all members
stan::math::fvar< T > Struct Template Reference

This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type. More...

#include <fvar.hpp>

Public Member Functions

val () const
 Return the value of this variable. More...
 
tangent () const
 Return the tangent (derivative) of this variable. More...
 
 fvar ()
 Construct a forward variable with zero value and tangent. More...
 
 fvar (const fvar< T > &x)
 Construct a forward variable with value and tangent set to the value and tangent of the specified variable. More...
 
 fvar (const T &v)
 Construct a forward variable with the specified value and zero tangent. More...
 
template<typename V >
 fvar (const V &v, typename std::enable_if< ad_promotable< V, T >::value >::type *dummy=0)
 Construct a forward variable with the specified value and zero tangent. More...
 
template<typename V , typename D >
 fvar (const V &v, const D &d)
 Construct a forward variable with the specified value and tangent. More...
 
fvar< T > & operator+= (const fvar< T > &x2)
 Add the specified variable to this variable and return a reference to this variable. More...
 
fvar< T > & operator+= (double x2)
 Add the specified value to this variable and return a reference to this variable. More...
 
fvar< T > & operator-= (const fvar< T > &x2)
 Subtract the specified variable from this variable and return a reference to this variable. More...
 
fvar< T > & operator-= (double x2)
 Subtract the specified value from this variable and return a reference to this variable. More...
 
fvar< T > & operator*= (const fvar< T > &x2)
 Multiply this variable by the the specified variable and return a reference to this variable. More...
 
fvar< T > & operator*= (double x2)
 Multiply this variable by the the specified value and return a reference to this variable. More...
 
fvar< T > & operator/= (const fvar< T > &x2)
 Divide this variable by the the specified variable and return a reference to this variable. More...
 
fvar< T > & operator/= (double x2)
 Divide this value by the the specified variable and return a reference to this variable. More...
 
fvar< T > & operator++ ()
 Increment this variable by one and return a reference to this variable after the increment. More...
 
fvar< T > operator++ (int)
 Increment this variable by one and return a reference to a copy of this variable before it was incremented. More...
 
fvar< T > & operator-- ()
 Decrement this variable by one and return a reference to this variable after the decrement. More...
 
fvar< T > operator-- (int)
 Decrement this variable by one and return a reference to a copy of this variable before it was decremented. More...
 

Public Attributes

val_
 The value of this variable. More...
 
d_
 The tangent (derivative) of this variable. More...
 

Friends

std::ostream & operator<< (std::ostream &os, const fvar< T > &v)
 Write the value of the specified variable to the specified output stream, returning a reference to the output stream. More...
 

Detailed Description

template<typename T>
struct stan::math::fvar< T >

This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.

When performing operations on instances of this class, all operands should be either primitive integer or double values or dual numbers representing derivatives in the same direction. The typical use case is to have a unit length directional derivative in the direction of a single independent variable.

By using reverse-mode automatic derivative variables, second-order derivatives may be calculated. By using fvar<<var> instances, third-order derivatives may be calculated. These are called mixed-mode automatic differentiation variable in Stan.

Specialized functionals that perform differentiation on functors may be found in the matrix subdirectories of the reverse, forward, and mixed-mode directories.

The Wikipedia page on automatic differentiation describes how forward-mode automatic differentiation works mathematically in terms of dual numbers.

Template Parameters
Ttype of value and tangent

Definition at line 41 of file fvar.hpp.

Constructor & Destructor Documentation

◆ fvar() [1/5]

template<typename T>
stan::math::fvar< T >::fvar ( )
inline

Construct a forward variable with zero value and tangent.

Definition at line 69 of file fvar.hpp.

◆ fvar() [2/5]

template<typename T>
stan::math::fvar< T >::fvar ( const fvar< T > &  x)
inline

Construct a forward variable with value and tangent set to the value and tangent of the specified variable.

Parameters
[in]xvariable to be copied

Definition at line 77 of file fvar.hpp.

◆ fvar() [3/5]

template<typename T>
stan::math::fvar< T >::fvar ( const T &  v)
inline

Construct a forward variable with the specified value and zero tangent.

Template Parameters
Vtype of value (must be assignable to the value and tangent type T)
Parameters
[in]vvalue

Definition at line 87 of file fvar.hpp.

◆ fvar() [4/5]

template<typename T>
template<typename V >
stan::math::fvar< T >::fvar ( const V &  v,
typename std::enable_if< ad_promotable< V, T >::value >::type *  dummy = 0 
)
inline

Construct a forward variable with the specified value and zero tangent.

Template Parameters
Vtype of value (must be assignable to the value and tangent type T)
Parameters
[in]vvalue
[in]dummyvalue given by default with enable-if metaprogramming

Definition at line 103 of file fvar.hpp.

◆ fvar() [5/5]

template<typename T>
template<typename V , typename D >
stan::math::fvar< T >::fvar ( const V &  v,
const D &  d 
)
inline

Construct a forward variable with the specified value and tangent.

Template Parameters
Vtype of value (must be assignable to the value and tangent type T)
Dtype of tangent (must be assignable to the value and tangent type T)
Parameters
[in]vvalue
[in]dtangent

Definition at line 122 of file fvar.hpp.

Member Function Documentation

◆ operator*=() [1/2]

template<typename T>
fvar<T>& stan::math::fvar< T >::operator*= ( const fvar< T > &  x2)
inline

Multiply this variable by the the specified variable and return a reference to this variable.

Parameters
[in]x2variable to multiply
Returns
reference to this variable after multiplication

Definition at line 184 of file fvar.hpp.

◆ operator*=() [2/2]

template<typename T>
fvar<T>& stan::math::fvar< T >::operator*= ( double  x2)
inline

Multiply this variable by the the specified value and return a reference to this variable.

Parameters
[in]x2value to multiply
Returns
reference to this variable after multiplication

Definition at line 197 of file fvar.hpp.

◆ operator++() [1/2]

template<typename T>
fvar<T>& stan::math::fvar< T >::operator++ ( )
inline

Increment this variable by one and return a reference to this variable after the increment.

Returns
reference to this variable after increment

Definition at line 235 of file fvar.hpp.

◆ operator++() [2/2]

template<typename T>
fvar<T> stan::math::fvar< T >::operator++ ( int  )
inline

Increment this variable by one and return a reference to a copy of this variable before it was incremented.

Returns
reference to copy of this variable before increment

Definition at line 246 of file fvar.hpp.

◆ operator+=() [1/2]

template<typename T>
fvar<T>& stan::math::fvar< T >::operator+= ( const fvar< T > &  x2)
inline

Add the specified variable to this variable and return a reference to this variable.

Parameters
[in]x2variable to add
Returns
reference to this variable after addition

Definition at line 134 of file fvar.hpp.

◆ operator+=() [2/2]

template<typename T>
fvar<T>& stan::math::fvar< T >::operator+= ( double  x2)
inline

Add the specified value to this variable and return a reference to this variable.

Parameters
[in]x2value to add
Returns
reference to this variable after addition

Definition at line 147 of file fvar.hpp.

◆ operator--() [1/2]

template<typename T>
fvar<T>& stan::math::fvar< T >::operator-- ( )
inline

Decrement this variable by one and return a reference to this variable after the decrement.

Returns
reference to this variable after decrement

Definition at line 258 of file fvar.hpp.

◆ operator--() [2/2]

template<typename T>
fvar<T> stan::math::fvar< T >::operator-- ( int  )
inline

Decrement this variable by one and return a reference to a copy of this variable before it was decremented.

Returns
reference to copy of this variable before decrement

Definition at line 269 of file fvar.hpp.

◆ operator-=() [1/2]

template<typename T>
fvar<T>& stan::math::fvar< T >::operator-= ( const fvar< T > &  x2)
inline

Subtract the specified variable from this variable and return a reference to this variable.

Parameters
[in]x2variable to subtract
Returns
reference to this variable after subtraction

Definition at line 159 of file fvar.hpp.

◆ operator-=() [2/2]

template<typename T>
fvar<T>& stan::math::fvar< T >::operator-= ( double  x2)
inline

Subtract the specified value from this variable and return a reference to this variable.

Parameters
[in]x2value to add
Returns
reference to this variable after subtraction

Definition at line 172 of file fvar.hpp.

◆ operator/=() [1/2]

template<typename T>
fvar<T>& stan::math::fvar< T >::operator/= ( const fvar< T > &  x2)
inline

Divide this variable by the the specified variable and return a reference to this variable.

Parameters
[in]x2variable to divide this variable by
Returns
reference to this variable after division

Definition at line 210 of file fvar.hpp.

◆ operator/=() [2/2]

template<typename T>
fvar<T>& stan::math::fvar< T >::operator/= ( double  x2)
inline

Divide this value by the the specified variable and return a reference to this variable.

Parameters
[in]x2value to divide this variable by
Returns
reference to this variable after division

Definition at line 223 of file fvar.hpp.

◆ tangent()

template<typename T>
T stan::math::fvar< T >::tangent ( ) const
inline

Return the tangent (derivative) of this variable.

Returns
tangent of this variable

Definition at line 64 of file fvar.hpp.

◆ val()

template<typename T>
T stan::math::fvar< T >::val ( ) const
inline

Return the value of this variable.

Returns
value of this variable

Definition at line 57 of file fvar.hpp.

Friends And Related Function Documentation

◆ operator<<

template<typename T>
std::ostream& operator<< ( std::ostream &  os,
const fvar< T > &  v 
)
friend

Write the value of the specified variable to the specified output stream, returning a reference to the output stream.

Parameters
[in,out]osstream for writing value
[in]vvariable whose value is written
Returns
reference to the specified output stream

Definition at line 283 of file fvar.hpp.

Member Data Documentation

◆ d_

template<typename T>
T stan::math::fvar< T >::d_

The tangent (derivative) of this variable.

Definition at line 50 of file fvar.hpp.

◆ val_

template<typename T>
T stan::math::fvar< T >::val_

The value of this variable.

Definition at line 45 of file fvar.hpp.


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

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