Public Member Functions | Public Attributes | Friends | List of all members
stan::math::fvar< T > Struct Template Reference

#include "stan/math/fwd/core/fvar.hpp"

Public Member Functions

T val () const
 
T tangent () const
 
 fvar ()
 
 fvar (const fvar< T > &x)
 
 fvar (const T &v)
 
template<typename V >
 fvar (const V &v, typename boost::enable_if_c< ad_promotable< V, T >::value >::type *dummy=0)
 
template<typename V , typename D >
 fvar (const V &v, const D &d)
 
fvar< T > & operator+= (const fvar< T > &x2)
 
fvar< T > & operator+= (double x2)
 
fvar< T > & operator-= (const fvar< T > &x2)
 
fvar< T > & operator-= (double x2)
 
fvar< T > & operator*= (const fvar< T > &x2)
 
fvar< T > & operator*= (double x2)
 
fvar< T > & operator/= (const fvar< T > &x2)
 
fvar< T > & operator/= (double x2)
 
fvar< T > & operator++ ()
 
fvar< Toperator++ (int)
 
fvar< T > & operator-- ()
 
fvar< Toperator-- (int)
 

Public Attributes

T val_
 
T d_
 

Friends

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

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 42 of file fvar.hpp.

Constructor & Destructor Documentation

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

Construct a forward variable with zero value and tangent.

Definition at line 70 of file fvar.hpp.

70 : val_(0.0), d_(0.0) {}
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 78 of file fvar.hpp.

78 : val_(x.val_), d_(x.d_) {}
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 88 of file fvar.hpp.

References stan::math::is_nan(), unlikely, and registry_explorer::v.

88  : val_(v), d_(0.0) { // NOLINT(runtime/explicit)
89  if (unlikely(is_nan(v)))
90  d_ = v;
91  }
#define unlikely(expr)
Definition: lz4.cxx:121
int is_nan(const fvar< T > &x)
Definition: is_nan.hpp:19
template<typename T>
template<typename V >
stan::math::fvar< T >::fvar ( const V &  v,
typename boost::enable_if_c< 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 104 of file fvar.hpp.

References stan::math::is_nan(), unlikely, and registry_explorer::v.

106  : val_(v), d_(0.0) {
107  if (unlikely(is_nan(v)))
108  d_ = v;
109  }
#define unlikely(expr)
Definition: lz4.cxx:121
int is_nan(const fvar< T > &x)
Definition: is_nan.hpp:19
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 123 of file fvar.hpp.

References stan::math::is_nan(), unlikely, and registry_explorer::v.

123  : val_(v), d_(d) {
124  if (unlikely(is_nan(v)))
125  d_ = v;
126  }
Float_t d
Definition: plot.C:236
#define unlikely(expr)
Definition: lz4.cxx:121
int is_nan(const fvar< T > &x)
Definition: is_nan.hpp:19

Member Function Documentation

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 185 of file fvar.hpp.

References stan::math::fvar< T >::d_, and stan::math::fvar< T >::val_.

185  {
186  d_ = d_ * x2.val_ + val_ * x2.d_;
187  val_ *= x2.val_;
188  return *this;
189  }
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 198 of file fvar.hpp.

References submit_syst::x2.

198  {
199  val_ *= x2;
200  d_ *= x2;
201  return *this;
202  }
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 236 of file fvar.hpp.

References stan::math::fvar< T >::val_.

236  {
237  ++val_;
238  return *this;
239  }
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 247 of file fvar.hpp.

References fillBadChanDBTables::result, and stan::math::fvar< T >::val_.

247  {
248  fvar<T> result(val_, d_);
249  ++val_;
250  return result;
251  }
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 135 of file fvar.hpp.

References stan::math::fvar< T >::d_, and stan::math::fvar< T >::val_.

135  {
136  val_ += x2.val_;
137  d_ += x2.d_;
138  return *this;
139  }
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 148 of file fvar.hpp.

References submit_syst::x2.

148  {
149  val_ += x2;
150  return *this;
151  }
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 259 of file fvar.hpp.

References stan::math::fvar< T >::val_.

259  {
260  --val_;
261  return *this;
262  }
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 270 of file fvar.hpp.

References fillBadChanDBTables::result, and stan::math::fvar< T >::val_.

270  {
271  fvar<T> result(val_, d_);
272  --val_;
273  return result;
274  }
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 160 of file fvar.hpp.

References stan::math::fvar< T >::d_, and stan::math::fvar< T >::val_.

160  {
161  val_ -= x2.val_;
162  d_ -= x2.d_;
163  return *this;
164  }
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 173 of file fvar.hpp.

References submit_syst::x2.

173  {
174  val_ -= x2;
175  return *this;
176  }
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 211 of file fvar.hpp.

References stan::math::fvar< T >::d_, and stan::math::fvar< T >::val_.

211  {
212  d_ = (d_ * x2.val_ - val_ * x2.d_) / (x2.val_ * x2.val_);
213  val_ /= x2.val_;
214  return *this;
215  }
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 224 of file fvar.hpp.

References submit_syst::x2.

224  {
225  val_ /= x2;
226  d_ /= x2;
227  return *this;
228  }
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 65 of file fvar.hpp.

References stan::math::fvar< T >::d_.

65 { return d_; }
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 58 of file fvar.hpp.

References stan::math::fvar< T >::val_.

Referenced by stan::math::is_inf(), and stan::math::is_nan().

58 { return val_; }

Friends And Related Function Documentation

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 284 of file fvar.hpp.

284  {
285  return os << v.val_;
286  }

Member Data Documentation

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

The tangent (derivative) of this variable.

Definition at line 51 of file fvar.hpp.

Referenced by stan::math::abs(), stan::math::acos(), stan::math::acosh(), stan::math::asin(), stan::math::asinh(), stan::math::atan(), stan::math::atan2(), stan::math::atanh(), stan::math::bessel_first_kind(), stan::math::bessel_second_kind(), stan::math::binary_log_loss(), stan::math::binomial_coefficient_log(), stan::math::cbrt(), stan::math::cos(), stan::math::cosh(), stan::math::derivative(), stan::math::determinant(), stan::math::digamma(), stan::math::internal::ops_partials_edge< Dx, fvar< Dx > >::dx(), stan::math::erf(), stan::math::erfc(), stan::math::exp(), stan::math::exp2(), stan::math::expm1(), stan::math::fabs(), stan::math::falling_factorial(), stan::math::fdim(), stan::math::fma(), stan::math::fmax(), stan::math::fmin(), stan::math::fmod(), stan::math::gamma_p(), stan::math::gamma_q(), stan::math::grad_hessian(), stan::math::grad_tr_mat_times_hessian(), stan::math::gradient(), stan::math::gradient_dot_vector(), stan::math::hessian(), stan::math::hypot(), stan::math::inc_beta(), stan::math::inv(), stan::math::inv_cloglog(), stan::math::inv_logit(), stan::math::inv_Phi(), stan::math::inv_sqrt(), stan::math::inv_square(), stan::math::jacobian(), stan::math::lbeta(), stan::math::lgamma(), stan::math::lmgamma(), stan::math::log(), stan::math::log10(), stan::math::log1m(), stan::math::log1m_exp(), stan::math::log1m_inv_logit(), stan::math::log1p(), stan::math::log1p_exp(), stan::math::log2(), stan::math::log_diff_exp(), stan::math::log_falling_factorial(), stan::math::log_inv_logit(), stan::math::log_inv_logit_diff(), stan::math::log_mix(), stan::math::log_rising_factorial(), stan::math::log_sum_exp(), stan::math::logit(), stan::math::modified_bessel_first_kind(), stan::math::modified_bessel_second_kind(), stan::math::multiply_log(), stan::math::operator*(), stan::math::fvar< T >::operator*=(), stan::math::operator+(), stan::math::fvar< T >::operator+=(), stan::math::operator-(), stan::math::fvar< T >::operator-=(), stan::math::operator/(), stan::math::fvar< T >::operator/=(), stan::math::owens_t(), stan::math::partial_derivative(), stan::math::Phi(), stan::math::pow(), stan::math::rising_factorial(), stan::math::sin(), stan::math::sinh(), stan::math::sqrt(), stan::math::square(), stan::math::tan(), stan::math::fvar< T >::tangent(), stan::math::tanh(), and stan::math::tgamma().

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

The value of this variable.

Definition at line 46 of file fvar.hpp.

Referenced by stan::math::abs(), stan::math::acos(), stan::math::acosh(), stan::math::asin(), stan::math::asinh(), stan::math::atan(), stan::math::atan2(), stan::math::atanh(), stan::math::bessel_first_kind(), stan::math::bessel_second_kind(), stan::math::binary_log_loss(), stan::math::binomial_coefficient_log(), stan::math::cbrt(), stan::math::ceil(), stan::math::cos(), stan::math::cosh(), stan::math::derivative(), stan::math::determinant(), stan::math::digamma(), stan::math::erf(), stan::math::erfc(), stan::math::exp(), stan::math::exp2(), stan::math::expm1(), stan::math::fabs(), stan::math::falling_factorial(), stan::math::fdim(), stan::math::floor(), stan::math::fma(), stan::math::fmax(), stan::math::fmin(), stan::math::fmod(), stan::math::gamma_p(), stan::math::gamma_q(), stan::math::gradient(), stan::math::gradient_dot_vector(), stan::math::hessian(), stan::math::hypot(), stan::math::inc_beta(), stan::math::inv(), stan::math::inv_cloglog(), stan::math::inv_logit(), stan::math::inv_Phi(), stan::math::inv_sqrt(), stan::math::inv_square(), stan::math::jacobian(), stan::math::lbeta(), stan::math::lgamma(), stan::math::lmgamma(), stan::math::log(), stan::math::log10(), stan::math::log1m(), stan::math::log1m_exp(), stan::math::log1m_inv_logit(), stan::math::log1p(), stan::math::log1p_exp(), stan::math::log2(), stan::math::log_diff_exp(), stan::math::log_falling_factorial(), stan::math::log_inv_logit(), stan::math::log_inv_logit_diff(), stan::math::log_mix(), stan::math::log_rising_factorial(), stan::math::log_sum_exp(), stan::math::logit(), stan::math::modified_bessel_first_kind(), stan::math::modified_bessel_second_kind(), stan::math::multiply_log(), stan::math::operator!(), stan::math::operator!=(), stan::math::operator&&(), stan::math::operator*(), stan::math::fvar< T >::operator*=(), stan::math::operator+(), stan::math::fvar< T >::operator++(), stan::math::fvar< T >::operator+=(), stan::math::operator-(), stan::math::fvar< T >::operator--(), stan::math::fvar< T >::operator-=(), stan::math::operator/(), stan::math::fvar< T >::operator/=(), stan::math::operator==(), stan::math::operator>(), stan::math::operator>=(), stan::math::operator||(), stan::math::owens_t(), stan::math::partial_derivative(), stan::math::Phi(), stan::math::pow(), stan::math::primitive_value(), stan::math::rising_factorial(), stan::math::round(), stan::math::sin(), stan::math::sinh(), stan::math::sqrt(), stan::math::square(), stan::math::tan(), stan::math::tanh(), stan::math::tgamma(), stan::math::trunc(), stan::math::fvar< T >::val(), stan::math::value_of(), and stan::math::value_of_rec().


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