Stan Math Library  4.9.0 Automatic Differentiation
Searching...
No Matches
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 <ostream>
7#include <type_traits>
8
9namespace stan {
10namespace math {
11
39template <typename T>
40struct fvar {
44 using Scalar = std::decay_t<T>;
45
50
56 Scalar val() const { return val_; }
57
62
68 Scalar d() const { return d_; }
69
73 fvar() : val_(0.0), d_(0.0) {} // NOLINT
74
82 template <typename V, std::enable_if_t<ad_promotable<V, T>::value>* = nullptr,
84 fvar(const V& v) : val_(v), d_(0) {} // NOLINT(runtime/explicit)
85
95 template <typename V, typename D>
96 fvar(const V& v, const D& d) : val_(v), d_(d) {}
97
105 inline fvar<T>& operator+=(const fvar<T>& x2) {
106 val_ += x2.val_;
107 d_ += x2.d_;
108 return *this;
109 }
110
118 inline fvar<T>& operator+=(double x2) {
119 val_ += x2;
120 return *this;
121 }
122
130 inline fvar<T>& operator-=(const fvar<T>& x2) {
131 val_ -= x2.val_;
132 d_ -= x2.d_;
133 return *this;
134 }
135
143 inline fvar<T>& operator-=(double x2) {
144 val_ -= x2;
145 return *this;
146 }
147
155 inline fvar<T>& operator*=(const fvar<T>& x2) {
156 d_ = d_ * x2.val_ + val_ * x2.d_;
157 val_ *= x2.val_;
158 return *this;
159 }
160
168 inline fvar<T>& operator*=(double x2) {
169 val_ *= x2;
170 d_ *= x2;
171 return *this;
172 }
173
181 inline fvar<T>& operator/=(const fvar<T>& x2) {
182 d_ = (d_ * x2.val_ - val_ * x2.d_) / (x2.val_ * x2.val_);
183 val_ /= x2.val_;
184 return *this;
185 }
186
194 inline fvar<T>& operator/=(double x2) {
195 val_ /= x2;
196 d_ /= x2;
197 return *this;
198 }
199
206 inline fvar<T>& operator++() {
207 ++val_;
208 return *this;
209 }
210
217 inline fvar<T> operator++(int /*dummy*/) {
218 fvar<T> result(val_, d_);
219 ++val_;
220 return result;
221 }
222
229 inline fvar<T>& operator--() {
230 --val_;
231 return *this;
232 }
233
240 inline fvar<T> operator--(int /*dummy*/) {
241 fvar<T> result(val_, d_);
242 --val_;
243 return result;
244 }
245
254 friend std::ostream& operator<<(std::ostream& os, const fvar<T>& v) {
255 return os << v.val_;
256 }
257};
258
259} // namespace math
260} // namespace stan
261#endif
require_not_t< std::is_same< std::decay_t< T >, std::decay_t< S > > > require_not_same_t
Require types T and S does not satisfy std::is_same.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
fvar< T > & operator/=(double x2)
Divide this value by the the specified variable and return a reference to this variable.
Definition fvar.hpp:194
Scalar val() const
Return the value of this variable.
Definition fvar.hpp:56
fvar< T > & operator--()
Decrement this variable by one and return a reference to this variable after the decrement.
Definition fvar.hpp:229
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:181
fvar(const V &v)
Construct a forward variable with the specified value and zero tangent.
Definition fvar.hpp:84
fvar< T > & operator*=(double x2)
Multiply this variable by the the specified value and return a reference to this variable.
Definition fvar.hpp:168
fvar< T > & operator-=(const fvar< T > &x2)
Subtract the specified variable from this variable and return a reference to this variable.
Definition fvar.hpp:130
Scalar d() const
Return the tangent (derivative) of this variable.
Definition fvar.hpp:68
fvar(const V &v, const D &d)
Construct a forward variable with the specified value and tangent.
Definition fvar.hpp:96
fvar< T > & operator-=(double x2)
Subtract the specified value from this variable and return a reference to this variable.
Definition fvar.hpp:143
std::decay_t< T > Scalar
The type of values and tangents.
Definition fvar.hpp:44
fvar< T > & operator++()
Increment this variable by one and return a reference to this variable after the increment.
Definition fvar.hpp:206
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:217
fvar< T > & operator+=(const fvar< T > &x2)
Add the specified variable to this variable and return a reference to this variable.
Definition fvar.hpp:105
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:240
fvar()
Construct a forward variable with zero value and tangent.
Definition fvar.hpp:73
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:155
Scalar val_
The value of this variable.
Definition fvar.hpp:49
fvar< T > & operator+=(double x2)
Add the specified value to this variable and return a reference to this variable.
Definition fvar.hpp:118
Scalar d_
The tangent (derivative) of this variable.
Definition fvar.hpp:61
friend 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 th...
Definition fvar.hpp:254
This template class represents scalars used in forward-mode automatic differentiation,...
Definition fvar.hpp:40