Stan Math Library: stan/math/fwd/core/fvar.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/scal/meta/likely.hpp>
5 #include <boost/math/special_functions/fpclassify.hpp>
6 #include <ostream>
7
8 namespace stan {
9
10 namespace math {
11
12 template <typename T>
13 struct fvar {
14 T val_; // value
15 T d_; // tangent (aka derivative)
16
17 T val() const { return val_; }
18 T tangent() const { return d_; }
19
20 typedef fvar value_type;
21
22 fvar() : val_(0.0), d_(0.0) { }
23
24 fvar(const fvar<T>& x)
25 : val_(x.val_), d_(x.d_) {
26 }
27
28 // TV and TD must be assignable to T
29 template <typename TV, typename TD>
30 fvar(const TV& val, const TD& deriv) : val_(val), d_(deriv) {
31 if (unlikely(boost::math::isnan(val)))
32 d_ = val;
33 }
34
35 // TV must be assignable to T
36 template <typename TV>
37 fvar(const TV& val) // NOLINT
38 : val_(val), d_(0.0) {
39 if (unlikely(boost::math::isnan(val)))
40 d_ = val;
41 }
42
43
44 inline
45 fvar<T>&
46 operator+=(const fvar<T>& x2) {
47 val_ += x2.val_;
48 d_ += x2.d_;
49 return *this;
50 }
51
52 inline
53 fvar<T>&
54 operator+=(double x2) {
55 val_ += x2;
56 return *this;
57 }
58
59 inline
60 fvar<T>&
61 operator-=(const fvar<T>& x2) {
62 val_ -= x2.val_;
63 d_ -= x2.d_;
64 return *this;
65 }
66
67 inline
68 fvar<T>&
69 operator-=(double x2) {
70 val_ -= x2;
71 return *this;
72 }
73
74 inline
75 fvar<T>&
76 operator*=(const fvar<T>& x2) {
77 d_ = d_ * x2.val_ + val_ * x2.d_;
78 val_ *= x2.val_;
79 return *this;
80 }
81
82 inline
83 fvar<T>&
84 operator*=(double x2) {
85 val_ *= x2;
86 d_ *= x2;
87 return *this;
88 }
89
90 // SPEEDUP: specialize for T2 == var with d_ function
91
92 inline
93 fvar<T>&
94 operator/=(const fvar<T>& x2) {
95 d_ = (d_ * x2.val_ - val_ * x2.d_) / (x2.val_ * x2.val_);
96 val_ /= x2.val_;
97 return *this;
98 }
99
100 inline
101 fvar<T>&
102 operator/=(double x2) {
103 val_ /= x2;
104 d_ /= x2;
105 return *this;
106 }
107
108 inline
109 fvar<T>&
110 operator++() {
111 ++val_;
112 return *this;
113 }
114
115 inline
116 fvar<T>
117 operator++(int /*dummy*/) {
118 fvar<T> result(val_, d_);
119 ++val_;
120 return result;
121 }
122
123 inline
124 fvar<T>&
125 operator--() {
126 --val_;
127 return *this;
128 }
129 inline
130 fvar<T>
131 operator--(int /*dummy*/) {
132 fvar<T> result(val_, d_);
133 --val_;
134 return result;
135 }
136
137 friend
138 std::ostream&
139 operator<<(std::ostream& os, const fvar<T>& v) {
140 return os << v.val_;
141 }
142 };
143 }
144 }
145 #endif
stan::math::fvar::d_
T d_
Definition: fvar.hpp:15
stan
Definition: log_sum_exp.hpp:8
stan::math::fvar::operator-=
fvar< T > & operator-=(const fvar< T > &x2)
Definition: fvar.hpp:61
stan::math::fvar::operator/=
fvar< T > & operator/=(double x2)
Definition: fvar.hpp:102
stan::math::fvar::tangent
T tangent() const
Definition: fvar.hpp:18
stan::math::fvar::operator--
fvar< T > operator--(int)
Definition: fvar.hpp:131
stan::math::fvar::fvar
fvar(const fvar< T > &x)
Definition: fvar.hpp:24
stan::math::fvar::operator+=
fvar< T > & operator+=(const fvar< T > &x2)
Definition: fvar.hpp:46
boost::math::isnan
bool isnan(const stan::math::var &v)
Checks if the given number is NaN.
Definition: boost_isnan.hpp:22
unlikely
#define unlikely(x)
Definition: likely.hpp:9
stan::math::fvar::operator++
fvar< T > & operator++()
Definition: fvar.hpp:110
stan::math::fvar::val_
T val_
Definition: fvar.hpp:14
stan::math::fvar::value_type
fvar value_type
Definition: fvar.hpp:20
stan::math::fvar::operator--
fvar< T > & operator--()
Definition: fvar.hpp:125
stan::math::fvar::operator++
fvar< T > operator++(int)
Definition: fvar.hpp:117
likely.hpp
stan::math::fvar::operator-=
fvar< T > & operator-=(double x2)
Definition: fvar.hpp:69
stan::math::fvar::fvar
fvar(const TV &val)
Definition: fvar.hpp:37
stan::math::fvar::val
T val() const
Definition: fvar.hpp:17
stan::math::fvar::operator+=
fvar< T > & operator+=(double x2)
Definition: fvar.hpp:54
stan::math::fvar::operator*=
fvar< T > & operator*=(const fvar< T > &x2)
Definition: fvar.hpp:76
stan::math::fvar::fvar
fvar()
Definition: fvar.hpp:22
stan::math::fvar::operator*=
fvar< T > & operator*=(double x2)
Definition: fvar.hpp:84
stan::math::fvar::fvar
fvar(const TV &val, const TD &deriv)
Definition: fvar.hpp:30
stan::math::fvar::operator/=
fvar< T > & operator/=(const fvar< T > &x2)
Definition: fvar.hpp:94
stan::math::fvar
Definition: fvar.hpp:13

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