| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

OperandsAndPartials.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_SCAL_META_OPERANDSANDPARTIALS_HPP
2 #define STAN_MATH_FWD_SCAL_META_OPERANDSANDPARTIALS_HPP
3
4 #include <stan/math/prim/scal/meta/is_constant_struct.hpp>
5 #include <stan/math/prim/scal/meta/length.hpp>
6 #include <stan/math/prim/scal/meta/OperandsAndPartials.hpp>
7 #include <stan/math/fwd/core.hpp>
8
9 namespace stan {
10 namespace math {
11
12 namespace {
13 template <typename T_derivative,
14 typename T,
15 typename T_partials,
16 bool is_vec = is_vector<T>::value,
17 bool is_const = is_constant_struct<T>::value>
18 struct increment_derivative {
19 inline T_derivative operator()(const T& x,
20 const T_partials& d_dx) {
21 return 0;
22 }
23 };
24
25 template <typename T_derivative,
26 typename T,
27 typename T_partials>
28 struct increment_derivative<T_derivative, T, T_partials, false, false> {
29 inline T_derivative operator()(const T& x,
30 const T_partials& d_dx) {
31 return d_dx[0] * x.d_;
32 }
33 };
34
35 template <typename T_derivative,
36 typename T,
37 typename T_partials>
38 struct increment_derivative<T_derivative, T, T_partials, true, false> {
39 inline T_derivative operator()(const T& x,
40 const T_partials& d_dx) {
41 T_derivative derivative(0);
42 for (size_t n = 0; n < length(x); n++)
43 derivative += d_dx[n] * x[n].d_;
44 return derivative;
45 }
46 };
47
48 template <typename T,
49 typename T1, typename D1, typename T2, typename D2,
50 typename T3, typename D3, typename T4, typename D4,
51 typename T5, typename D5, typename T6, typename D6>
52 fvar<T> partials_to_fvar(T& logp,
53 const T1& x1, D1& d_x1,
54 const T2& x2, D2& d_x2,
55 const T3& x3, D3& d_x3,
56 const T4& x4, D4& d_x4,
57 const T5& x5, D5& d_x5,
58 const T6& x6, D6& d_x6) {
59 T deriv = 0;
60 if (!is_constant_struct<T1>::value)
61 deriv += increment_derivative<T, T1, D1>()(x1, d_x1);
62 if (!is_constant_struct<T2>::value)
63 deriv += increment_derivative<T, T2, D2>()(x2, d_x2);
64 if (!is_constant_struct<T3>::value)
65 deriv += increment_derivative<T, T3, D3>()(x3, d_x3);
66 if (!is_constant_struct<T4>::value)
67 deriv += increment_derivative<T, T4, D4>()(x4, d_x4);
68 if (!is_constant_struct<T5>::value)
69 deriv += increment_derivative<T, T5, D5>()(x5, d_x5);
70 if (!is_constant_struct<T6>::value)
71 deriv += increment_derivative<T, T6, D6>()(x6, d_x6);
72 return fvar<T>(logp, deriv);
73 }
74 }
75
98 template<typename T1, typename T2, typename T3,
99 typename T4, typename T5, typename T6,
100 typename T_partials_return>
101 struct OperandsAndPartials<T1, T2, T3, T4, T5, T6,
102 fvar<T_partials_return> > {
103 typedef fvar<T_partials_return> T_return_type;
104
105 const T1& x1_;
106 const T2& x2_;
107 const T3& x3_;
108 const T4& x4_;
109 const T5& x5_;
110 const T6& x6_;
111
112 size_t n_partials;
113 T_partials_return* all_partials;
114
115 VectorView<T_partials_return,
116 is_vector<T1>::value,
117 is_constant_struct<T1>::value> d_x1;
118 VectorView<T_partials_return,
119 is_vector<T2>::value,
120 is_constant_struct<T2>::value> d_x2;
121 VectorView<T_partials_return,
122 is_vector<T3>::value,
123 is_constant_struct<T3>::value> d_x3;
124 VectorView<T_partials_return,
125 is_vector<T4>::value,
126 is_constant_struct<T4>::value> d_x4;
127 VectorView<T_partials_return,
128 is_vector<T5>::value,
129 is_constant_struct<T5>::value> d_x5;
130 VectorView<T_partials_return,
131 is_vector<T6>::value,
132 is_constant_struct<T6>::value> d_x6;
133
134 OperandsAndPartials(const T1& x1 = 0, const T2& x2 = 0, const T3& x3 = 0,
135 const T4& x4 = 0, const T5& x5 = 0, const T6& x6 = 0)
136 : x1_(x1), x2_(x2), x3_(x3), x4_(x4), x5_(x5), x6_(x6),
137 n_partials(!is_constant_struct<T1>::value * length(x1) +
138 !is_constant_struct<T2>::value * length(x2) +
139 !is_constant_struct<T3>::value * length(x3) +
140 !is_constant_struct<T4>::value * length(x4) +
141 !is_constant_struct<T5>::value * length(x5) +
142 !is_constant_struct<T6>::value * length(x6)),
143 all_partials(new T_partials_return[n_partials]),
144 d_x1(all_partials),
145 d_x2(all_partials
146 + (!is_constant_struct<T1>::value) * length(x1)),
147 d_x3(all_partials
148 + (!is_constant_struct<T1>::value) * length(x1)
149 + (!is_constant_struct<T2>::value) * length(x2)),
150 d_x4(all_partials
151 + (!is_constant_struct<T1>::value) * length(x1)
152 + (!is_constant_struct<T2>::value) * length(x2)
153 + (!is_constant_struct<T3>::value) * length(x3)),
154 d_x5(all_partials
155 + (!is_constant_struct<T1>::value) * length(x1)
156 + (!is_constant_struct<T2>::value) * length(x2)
157 + (!is_constant_struct<T3>::value) * length(x3)
158 + (!is_constant_struct<T4>::value) * length(x4)),
159 d_x6(all_partials
160 + (!is_constant_struct<T1>::value) * length(x1)
161 + (!is_constant_struct<T2>::value) * length(x2)
162 + (!is_constant_struct<T3>::value) * length(x3)
163 + (!is_constant_struct<T4>::value) * length(x4)
164 + (!is_constant_struct<T5>::value) * length(x5)) {
165 std::fill(all_partials, all_partials + n_partials, 0);
166 }
167
168 T_return_type
169 value(T_partials_return value) {
170 return partials_to_fvar(value,
171 x1_, d_x1, x2_, d_x2,
172 x3_, d_x3, x4_, d_x4,
173 x5_, d_x4, x6_, d_x5);
174 }
175
176 ~OperandsAndPartials() {
177 delete[] all_partials;
178 }
179 };
180
181 }
182 }
183 #endif
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::~OperandsAndPartials
~OperandsAndPartials()
Definition: OperandsAndPartials.hpp:176
core.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::d_x5
VectorView< T_partials_return, is_vector< T5 >::value, is_constant_struct< T5 >::value > d_x5
Definition: OperandsAndPartials.hpp:129
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::d_x3
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
Definition: OperandsAndPartials.hpp:123
stan::math::OperandsAndPartials::value
T_return_type value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
Definition: OperandsAndPartials.hpp:67
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::x3_
const T3 & x3_
Definition: OperandsAndPartials.hpp:107
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::value
T_return_type value(T_partials_return value)
Definition: OperandsAndPartials.hpp:169
is_constant_struct.hpp
stan::is_vector
Definition: is_vector.hpp:10
stan::is_constant_struct
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: is_constant_struct.hpp:13
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::x1_
const T1 & x1_
Definition: OperandsAndPartials.hpp:105
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::n_partials
size_t n_partials
Definition: OperandsAndPartials.hpp:112
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::OperandsAndPartials
OperandsAndPartials(const T1 &x1=0, const T2 &x2=0, const T3 &x3=0, const T4 &x4=0, const T5 &x5=0, const T6 &x6=0)
Definition: OperandsAndPartials.hpp:134
stan::math::OperandsAndPartials
This class builds partial derivatives with respect to a set of operands.
Definition: OperandsAndPartials.hpp:37
stan::math::derivative
void derivative(const F &f, const T &x, T &fx, T &dfx_dx)
Return the derivative of the specified univariate function at the specified argument.
Definition: derivative.hpp:25
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::d_x1
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Definition: OperandsAndPartials.hpp:117
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::x4_
const T4 & x4_
Definition: OperandsAndPartials.hpp:108
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::d_x4
VectorView< T_partials_return, is_vector< T4 >::value, is_constant_struct< T4 >::value > d_x4
Definition: OperandsAndPartials.hpp:126
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::x5_
const T5 & x5_
Definition: OperandsAndPartials.hpp:109
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::all_partials
T_partials_return * all_partials
Definition: OperandsAndPartials.hpp:113
OperandsAndPartials.hpp
length.hpp
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::d_x6
VectorView< T_partials_return, is_vector< T6 >::value, is_constant_struct< T6 >::value > d_x6
Definition: OperandsAndPartials.hpp:132
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::x6_
const T6 & x6_
Definition: OperandsAndPartials.hpp:110
stan::math::fill
void fill(std::vector< T > &x, const S &y)
Fill the specified container with the specified value.
Definition: fill.hpp:22
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::x2_
const T2 & x2_
Definition: OperandsAndPartials.hpp:106
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::T_return_type
fvar< T_partials_return > T_return_type
Definition: OperandsAndPartials.hpp:103
stan::VectorView
VectorView is a template expression that is constructed with a container or scalar, which it then allows to be used as an array using operator[].
Definition: VectorView.hpp:48
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, fvar< T_partials_return > >::d_x2
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
Definition: OperandsAndPartials.hpp:120
stan::math::fvar
Definition: fvar.hpp:12

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