Stan Math Library  2.20.0
reverse mode automatic differentiation
fvar.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_CORE_FVAR_HPP
2 #define STAN_MATH_FWD_CORE_FVAR_HPP
3 
6 #include <stan/math/fwd/meta.hpp>
7 #include <ostream>
8 
9 namespace stan {
10 namespace math {
11 
40 template <typename T>
41 struct fvar {
45  T val_;
46 
50  T d_;
51 
57  T val() const { return val_; }
58 
64  T tangent() const { return d_; }
65 
69  fvar() : val_(0.0), d_(0.0) {}
70 
77  fvar(const fvar<T>& x) : val_(x.val_), d_(x.d_) {}
78 
87  fvar(const T& v) : val_(v), d_(0.0) { // NOLINT(runtime/explicit)
88  if (unlikely(is_nan(v)))
89  d_ = v;
90  }
91 
102  template <typename V>
103  fvar(const V& v,
104  typename std::enable_if<ad_promotable<V, T>::value>::type* dummy = 0)
105  : val_(v), d_(0.0) {
106  if (unlikely(is_nan(v)))
107  d_ = v;
108  }
109 
121  template <typename V, typename D>
122  fvar(const V& v, const D& d) : val_(v), d_(d) {
123  if (unlikely(is_nan(v)))
124  d_ = v;
125  }
126 
134  inline fvar<T>& operator+=(const fvar<T>& x2) {
135  val_ += x2.val_;
136  d_ += x2.d_;
137  return *this;
138  }
139 
147  inline fvar<T>& operator+=(double x2) {
148  val_ += x2;
149  return *this;
150  }
151 
159  inline fvar<T>& operator-=(const fvar<T>& x2) {
160  val_ -= x2.val_;
161  d_ -= x2.d_;
162  return *this;
163  }
164 
172  inline fvar<T>& operator-=(double x2) {
173  val_ -= x2;
174  return *this;
175  }
176 
184  inline fvar<T>& operator*=(const fvar<T>& x2) {
185  d_ = d_ * x2.val_ + val_ * x2.d_;
186  val_ *= x2.val_;
187  return *this;
188  }
189 
197  inline fvar<T>& operator*=(double x2) {
198  val_ *= x2;
199  d_ *= x2;
200  return *this;
201  }
202 
210  inline fvar<T>& operator/=(const fvar<T>& x2) {
211  d_ = (d_ * x2.val_ - val_ * x2.d_) / (x2.val_ * x2.val_);
212  val_ /= x2.val_;
213  return *this;
214  }
215 
223  inline fvar<T>& operator/=(double x2) {
224  val_ /= x2;
225  d_ /= x2;
226  return *this;
227  }
228 
235  inline fvar<T>& operator++() {
236  ++val_;
237  return *this;
238  }
239 
246  inline fvar<T> operator++(int /*dummy*/) {
247  fvar<T> result(val_, d_);
248  ++val_;
249  return result;
250  }
251 
258  inline fvar<T>& operator--() {
259  --val_;
260  return *this;
261  }
262 
269  inline fvar<T> operator--(int /*dummy*/) {
270  fvar<T> result(val_, d_);
271  --val_;
272  return result;
273  }
274 
283  friend std::ostream& operator<<(std::ostream& os, const fvar<T>& v) {
284  return os << v.val_;
285  }
286 };
287 } // namespace math
288 } // namespace stan
289 #endif
T tangent() const
Return the tangent (derivative) of this variable.
Definition: fvar.hpp:64
T d_
The tangent (derivative) of this variable.
Definition: fvar.hpp:50
fvar< T > & operator-=(const fvar< T > &x2)
Subtract the specified variable from this variable and return a reference to this variable...
Definition: fvar.hpp:159
fvar< T > & operator/=(double x2)
Divide this value by the the specified variable and return a reference to this variable.
Definition: fvar.hpp:223
fvar(const V &v, const D &d)
Construct a forward variable with the specified value and tangent.
Definition: fvar.hpp:122
fvar< T > operator--(int)
Decrement this variable by one and return a reference to a copy of this variable before it was decrem...
Definition: fvar.hpp:269
fvar(const fvar< T > &x)
Construct a forward variable with value and tangent set to the value and tangent of the specified var...
Definition: fvar.hpp:77
fvar< T > & operator+=(const fvar< T > &x2)
Add the specified variable to this variable and return a reference to this variable.
Definition: fvar.hpp:134
fvar< T > & operator++()
Increment this variable by one and return a reference to this variable after the increment.
Definition: fvar.hpp:235
#define unlikely(x)
Definition: likely.hpp:9
T val_
The value of this variable.
Definition: fvar.hpp:45
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.
Definition: fvar.hpp:103
fvar< T > & operator--()
Decrement this variable by one and return a reference to this variable after the decrement.
Definition: fvar.hpp:258
fvar< T > operator++(int)
Increment this variable by one and return a reference to a copy of this variable before it was increm...
Definition: fvar.hpp:246
fvar< T > & operator-=(double x2)
Subtract the specified value from this variable and return a reference to this variable.
Definition: fvar.hpp:172
T val() const
Return the value of this variable.
Definition: fvar.hpp:57
fvar< T > & operator+=(double x2)
Add the specified value to this variable and return a reference to this variable. ...
Definition: fvar.hpp:147
Template traits metaprogram to determine if a variable of one template type can be promoted to a seco...
fvar(const T &v)
Construct a forward variable with the specified value and zero tangent.
Definition: fvar.hpp:87
fvar< T > & operator*=(const fvar< T > &x2)
Multiply this variable by the the specified variable and return a reference to this variable...
Definition: fvar.hpp:184
fvar()
Construct a forward variable with zero value and tangent.
Definition: fvar.hpp:69
int is_nan(const fvar< T > &x)
Returns 1 if the input&#39;s value is NaN and 0 otherwise.
Definition: is_nan.hpp:20
fvar< T > & operator*=(double x2)
Multiply this variable by the the specified value and return a reference to this variable.
Definition: fvar.hpp:197
fvar< T > & operator/=(const fvar< T > &x2)
Divide this variable by the the specified variable and return a reference to this variable...
Definition: fvar.hpp:210
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

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