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

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