| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
OperandsAndPartials.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_META_OPERANDSANDPARTIALS_HPP
2 #define STAN_MATH_PRIM_SCAL_META_OPERANDSANDPARTIALS_HPP
3
4 #include <stan/math/fwd/core.hpp>
5 #include <stan/math/fwd/scal/meta/is_fvar.hpp>
6 #include <stan/math/fwd/scal/meta/partials_type.hpp>
7 #include <stan/math/prim/scal/meta/is_vector.hpp>
8 #include <stan/math/prim/mat/meta/is_vector.hpp>
9 #include <stan/math/prim/arr/meta/is_vector.hpp>
10 #include <stan/math/prim/arr/meta/length.hpp>
11 #include <stan/math/prim/mat/meta/length.hpp>
12 #include <stan/math/prim/scal/meta/length.hpp>
13 #include <stan/math/prim/mat/meta/is_vector_like.hpp>
14 #include <stan/math/prim/scal/meta/is_vector_like.hpp>
15 #include <stan/math/prim/scal/meta/VectorView.hpp>
16 #include <stan/math/prim/scal/meta/is_constant_struct.hpp>
17 #include <stan/math/prim/scal/meta/contains_fvar.hpp>
18 #include <stan/math/prim/scal/meta/partials_return_type.hpp>
19 #include <stan/math/prim/scal/meta/partials_type.hpp>
20 #include <stan/math/prim/scal/meta/return_type.hpp>
21 #include <stan/math/rev/core.hpp>
22 #include <stan/math/rev/scal/meta/is_var.hpp>
23 #include <stan/math/rev/scal/meta/partials_type.hpp>
24
25 namespace stan {
26 namespace math {
27
28 class partials_vari : public vari {
29 private:
30 const size_t N_;
31 vari** operands_;
32 double* partials_;
33 public:
34 partials_vari(double value,
35 size_t N,
36 vari** operands, double* partials)
37 : vari(value),
38 N_(N),
39 operands_(operands),
40 partials_(partials) { }
41 void chain() {
42 for (size_t n = 0; n < N_; ++n)
43 operands_[n]->adj_ += adj_ * partials_[n];
44 }
45 };
46
47 namespace {
48 template<typename T1, typename T2, typename T3,
49 bool is_vec = is_vector<T2>::value,
50 bool is_const = is_constant_struct<T2>::value>
51 struct incr_deriv {
52 inline T3 incr(T1 d_x, const T2& x_d ) {
53 return 0;
54 }
55 };
56 template<typename T1, typename T2, typename T3>
57 struct incr_deriv<T1, T2, T3, false, false> {
58 inline T3 incr(T1 d_x, const T2& x_d) {
59 return d_x[0]*x_d.d_;
60 }
61 };
62 template<typename T1, typename T2, typename T3>
63 struct incr_deriv<T1, T2, T3, true, false> {
64 inline T3 incr(T1 d_x, const T2& x_d) {
65 T3 temp = 0;
66 for (size_t n = 0; n < length(x_d); n++)
67 temp += d_x[n] * x_d[n].d_;
68 return temp;
69 }
70 };
71
72 template<typename T_return_type, typename T_partials_return,
73 typename T1, typename T2, typename T3, typename T4,
74 typename T5, typename T6,
75 bool is_fvar = stan::contains_fvar<T_return_type>::value,
76 bool is_const = stan::is_constant_struct<T_return_type>::value>
77 struct partials_to_var {
78 inline
79 T_return_type to_var(double logp, size_t /* nvaris */,
80 vari** /* all_varis */,
81 T_partials_return* /* all_partials */,
82 const T1& x1, const T2& x2, const T3& x3,
83 const T4& x4, const T5& x5, const T6& x6,
84 VectorView<T_partials_return,
85 is_vector<T1>::value,
86 is_constant_struct<T1>::value> d_x1,
87 VectorView<T_partials_return,
88 is_vector<T2>::value,
89 is_constant_struct<T2>::value> d_x2,
90 VectorView<T_partials_return,
91 is_vector<T3>::value,
92 is_constant_struct<T3>::value> d_x3,
93 VectorView<T_partials_return,
94 is_vector<T4>::value,
95 is_constant_struct<T4>::value> d_x4,
96 VectorView<T_partials_return,
97 is_vector<T5>::value,
98 is_constant_struct<T5>::value> d_x5,
99 VectorView<T_partials_return,
100 is_vector<T6>::value,
101 is_constant_struct<T6>::value> d_x6) {
102 return logp;
103 }
104 };
105
106 template<typename T_return_type, typename T_partials_return,
107 typename T1, typename T2, typename T3, typename T4,
108 typename T5, typename T6>
109 struct partials_to_var<T_return_type, T_partials_return,
110 T1, T2, T3, T4, T5, T6,
111 false, false> {
112 inline T_return_type to_var(T_partials_return logp, size_t nvaris,
113 vari** all_varis,
114 T_partials_return* all_partials,
115 const T1& x1, const T2& x2, const T3& x3,
116 const T4& x4, const T5& x5, const T6& x6,
117 VectorView<T_partials_return,
118 is_vector<T1>::value,
119 is_constant_struct<T1>::value> d_x1,
120 VectorView<T_partials_return,
121 is_vector<T2>::value,
122 is_constant_struct<T2>::value> d_x2,
123 VectorView<T_partials_return,
124 is_vector<T3>::value,
125 is_constant_struct<T3>::value> d_x3,
126 VectorView<T_partials_return,
127 is_vector<T4>::value,
128 is_constant_struct<T4>::value> d_x4,
129 VectorView<T_partials_return,
130 is_vector<T5>::value,
131 is_constant_struct<T5>::value> d_x5,
132 VectorView<T_partials_return,
133 is_vector<T6>::value,
134 is_constant_struct<T6>::value> d_x6) {
135 return var(new partials_vari(logp, nvaris, all_varis,
136 all_partials));
137 }
138 };
139
140 template<typename T_return_type, typename T_partials_return,
141 typename T1, typename T2, typename T3, typename T4,
142 typename T5, typename T6>
143 struct partials_to_var<T_return_type, T_partials_return,
144 T1, T2, T3, T4, T5, T6,
145 true, false> {
146 inline T_return_type to_var(T_partials_return logp, size_t nvaris,
147 vari** all_varis,
148 T_partials_return* all_partials,
149 const T1& x1, const T2& x2, const T3& x3,
150 const T4& x4, const T5& x5, const T6& x6,
151 VectorView<T_partials_return,
152 is_vector<T1>::value,
153 is_constant_struct<T1>::value> d_x1,
154 VectorView<T_partials_return,
155 is_vector<T2>::value,
156 is_constant_struct<T2>::value> d_x2,
157 VectorView<T_partials_return,
158 is_vector<T3>::value,
159 is_constant_struct<T3>::value> d_x3,
160 VectorView<T_partials_return,
161 is_vector<T4>::value,
162 is_constant_struct<T4>::value> d_x4,
163 VectorView<T_partials_return,
164 is_vector<T5>::value,
165 is_constant_struct<T5>::value> d_x5,
166 VectorView<T_partials_return,
167 is_vector<T6>::value,
168 is_constant_struct<T6>::value> d_x6) {
169 T_partials_return temp_deriv = 0;
170 temp_deriv += incr_deriv<VectorView<T_partials_return,
171 is_vector<T1>::value,
172 is_constant_struct<T1>::value>,
173 T1, T_partials_return>().incr(d_x1, x1);
174 temp_deriv += incr_deriv<VectorView<T_partials_return,
175 is_vector<T2>::value,
176 is_constant_struct<T2>::value>,
177 T2, T_partials_return>().incr(d_x2, x2);
178 temp_deriv += incr_deriv<VectorView<T_partials_return,
179 is_vector<T3>::value,
180 is_constant_struct<T3>::value>,
181 T3, T_partials_return>().incr(d_x3, x3);
182 temp_deriv += incr_deriv<VectorView<T_partials_return,
183 is_vector<T4>::value,
184 is_constant_struct<T4>::value>,
185 T4, T_partials_return>().incr(d_x4, x4);
186 temp_deriv += incr_deriv<VectorView<T_partials_return,
187 is_vector<T5>::value,
188 is_constant_struct<T5>::value>,
189 T5, T_partials_return>().incr(d_x5, x5);
190 temp_deriv += incr_deriv<VectorView<T_partials_return,
191 is_vector<T6>::value,
192 is_constant_struct<T6>::value>,
193 T6, T_partials_return>().incr(d_x6, x6);
194 return stan::math::fvar<T_partials_return>(logp, temp_deriv);
195 }
196 };
197
198 template<typename T,
199 bool is_vec = is_vector<T>::value,
200 bool is_const = is_constant_struct<T>::value,
201 bool contain_fvar = contains_fvar<T>::value>
202 struct set_varis {
203 inline size_t set(vari** /*varis*/, const T& /*x*/) {
204 return 0U;
205 }
206 };
207 template<typename T>
208 struct set_varis<T, true, false, false> {
209 inline size_t set(vari** varis, const T& x) {
210 for (size_t n = 0; n < length(x); n++)
211 varis[n] = x[n].vi_;
212 return length(x);
213 }
214 };
215 template<typename T>
216 struct set_varis<T, true, false, true> {
217 inline size_t set(vari** varis, const T& x) {
218 for (size_t n = 0; n < length(x); n++)
219 varis[n] = 0;
220 return length(x);
221 }
222 };
223 template<>
224 struct set_varis<var, false, false, false> {
225 inline size_t set(vari** varis, const var& x) {
226 varis[0] = x.vi_;
227 return (1);
228 }
229 };
230 }
231
236 template<typename T1 = double, typename T2 = double, typename T3 = double,
237 typename T4 = double, typename T5 = double, typename T6 = double>
238 struct OperandsAndPartials {
239 typedef
240 typename stan::partials_return_type<T1, T2, T3, T4, T5, T6>::type
241 T_partials_return;
242
243 typedef
244 typename stan::return_type<T1, T2, T3, T4, T5, T6>::type T_return_type;
245
246 static const bool all_constant = is_constant<T_return_type>::value;
247 size_t nvaris;
248 vari** all_varis;
249 T_partials_return* all_partials;
250
251 VectorView<T_partials_return,
252 is_vector<T1>::value,
253 is_constant_struct<T1>::value> d_x1;
254 VectorView<T_partials_return,
255 is_vector<T2>::value,
256 is_constant_struct<T2>::value> d_x2;
257 VectorView<T_partials_return,
258 is_vector<T3>::value,
259 is_constant_struct<T3>::value> d_x3;
260 VectorView<T_partials_return,
261 is_vector<T4>::value,
262 is_constant_struct<T4>::value> d_x4;
263 VectorView<T_partials_return,
264 is_vector<T5>::value,
265 is_constant_struct<T5>::value> d_x5;
266 VectorView<T_partials_return,
267 is_vector<T6>::value,
268 is_constant_struct<T6>::value> d_x6;
269
270 OperandsAndPartials(const T1& x1 = 0, const T2& x2 = 0, const T3& x3 = 0,
271 const T4& x4 = 0, const T5& x5 = 0, const T6& x6 = 0)
272 : nvaris(!is_constant_struct<T1>::value * length(x1) +
273 !is_constant_struct<T2>::value * length(x2) +
274 !is_constant_struct<T3>::value * length(x3) +
275 !is_constant_struct<T4>::value * length(x4) +
276 !is_constant_struct<T5>::value * length(x5) +
277 !is_constant_struct<T6>::value * length(x6)),
278 all_varis(static_cast<vari**>
279 (chainable::operator new
280 (sizeof(vari*) * nvaris))),
281 all_partials(static_cast<T_partials_return*>
282 (chainable::operator new
283 (sizeof(T_partials_return) * nvaris))),
284 d_x1(all_partials),
285 d_x2(all_partials
286 + (!is_constant_struct<T1>::value) * length(x1)),
287 d_x3(all_partials
288 + (!is_constant_struct<T1>::value) * length(x1)
289 + (!is_constant_struct<T2>::value) * length(x2)),
290 d_x4(all_partials
291 + (!is_constant_struct<T1>::value) * length(x1)
292 + (!is_constant_struct<T2>::value) * length(x2)
293 + (!is_constant_struct<T3>::value) * length(x3)),
294 d_x5(all_partials
295 + (!is_constant_struct<T1>::value) * length(x1)
296 + (!is_constant_struct<T2>::value) * length(x2)
297 + (!is_constant_struct<T3>::value) * length(x3)
298 + (!is_constant_struct<T4>::value) * length(x4)),
299 d_x6(all_partials
300 + (!is_constant_struct<T1>::value) * length(x1)
301 + (!is_constant_struct<T2>::value) * length(x2)
302 + (!is_constant_struct<T3>::value) * length(x3)
303 + (!is_constant_struct<T4>::value) * length(x4)
304 + (!is_constant_struct<T5>::value) * length(x5)) {
305 size_t base = 0;
306 if (!is_constant_struct<T1>::value)
307 base += set_varis<T1>().set(&all_varis[base], x1);
308 if (!is_constant_struct<T2>::value)
309 base += set_varis<T2>().set(&all_varis[base], x2);
310 if (!is_constant_struct<T3>::value)
311 base += set_varis<T3>().set(&all_varis[base], x3);
312 if (!is_constant_struct<T4>::value)
313 base += set_varis<T4>().set(&all_varis[base], x4);
314 if (!is_constant_struct<T5>::value)
315 base += set_varis<T5>().set(&all_varis[base], x5);
316 if (!is_constant_struct<T6>::value)
317 set_varis<T6>().set(&all_varis[base], x6);
318 std::fill(all_partials, all_partials+nvaris, 0);
319 }
320
321 T_return_type
322 to_var(T_partials_return logp,
323 const T1& x1 = 0, const T2& x2 = 0, const T3& x3 = 0,
324 const T4& x4 = 0, const T5& x5 = 0, const T6& x6 = 0) {
325 return partials_to_var
326 <T_return_type, T_partials_return, T1,
327 T2, T3, T4, T5, T6>().to_var(logp, nvaris, all_varis,
328 all_partials,
329 x1, x2, x3, x4, x5, x6, d_x1, d_x2,
330 d_x3, d_x4, d_x5, d_x6);
331 }
332 };
333
334
335 }
336 }
337
338
339 #endif
stan::math::OperandsAndPartials::d_x6
VectorView< T_partials_return, is_vector< T6 >::value, is_constant_struct< T6 >::value > d_x6
Definition: OperandsAndPartials.hpp:268
stan::math::OperandsAndPartials::T_return_type
stan::return_type< T1, T2, T3, T4, T5, T6 >::type T_return_type
Definition: OperandsAndPartials.hpp:244
stan::is_constant
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Definition: is_constant.hpp:22
stan::math::OperandsAndPartials::all_partials
T_partials_return * all_partials
Definition: OperandsAndPartials.hpp:249
core.hpp
core.hpp
stan::math::chainable
Abstract base class for variable implementations that handles memory management and applying the chai...
Definition: chainable.hpp:14
is_vector.hpp
is_vector_like.hpp
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:28
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
partials_type.hpp
stan::math::partials_vari::chain
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: OperandsAndPartials.hpp:41
length.hpp
is_vector.hpp
stan::math::OperandsAndPartials::T_partials_return
stan::partials_return_type< T1, T2, T3, T4, T5, T6 >::type T_partials_return
Definition: OperandsAndPartials.hpp:241
stan::math::OperandsAndPartials::to_var
T_return_type to_var(T_partials_return logp, 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:322
return_type.hpp
stan::is_vector
Definition: is_vector.hpp:10
partials_type.hpp
stan::math::to_var
var to_var(const double &x)
Converts argument to an automatic differentiation variable.
Definition: to_var.hpp:21
stan::return_type::type
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Definition: return_type.hpp:27
VectorView.hpp
stan::math::OperandsAndPartials::d_x1
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Definition: OperandsAndPartials.hpp:253
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:16
is_fvar.hpp
is_vector_like.hpp
stan::math::OperandsAndPartials::d_x3
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
Definition: OperandsAndPartials.hpp:259
stan::math::OperandsAndPartials::d_x4
VectorView< T_partials_return, is_vector< T4 >::value, is_constant_struct< T4 >::value > d_x4
Definition: OperandsAndPartials.hpp:262
stan::math::fill
void fill(T &x, const S &y)
Fill the specified container with the specified value.
Definition: fill.hpp:22
stan::math::OperandsAndPartials
A variable implementation that stores operands and derivatives with respect to the variable...
Definition: OperandsAndPartials.hpp:238
stan::math::partials_vari
Definition: OperandsAndPartials.hpp:28
stan::math::partials_vari::partials_vari
partials_vari(double value, size_t N, vari **operands, double *partials)
Definition: OperandsAndPartials.hpp:34
partials_return_type.hpp
length.hpp
partials_type.hpp
stan::math::OperandsAndPartials::d_x2
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
Definition: OperandsAndPartials.hpp:256
is_var.hpp
is_constant_struct.hpp
stan::math::OperandsAndPartials::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:270
stan::math::vari::adj_
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:42
stan::math::OperandsAndPartials::nvaris
size_t nvaris
Definition: OperandsAndPartials.hpp:247
stan::math::OperandsAndPartials::d_x5
VectorView< T_partials_return, is_vector< T5 >::value, is_constant_struct< T5 >::value > d_x5
Definition: OperandsAndPartials.hpp:265
stan::contains_fvar
Metaprogram to calculate the base scalar return type resulting from promoting all the scalar types of...
Definition: contains_fvar.hpp:19
stan::math::OperandsAndPartials::all_constant
static const bool all_constant
Definition: OperandsAndPartials.hpp:246
is_vector.hpp
stan::VectorView
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
stan::math::OperandsAndPartials::all_varis
vari ** all_varis
Definition: OperandsAndPartials.hpp:248
stan::partials_return_type::type
boost::math::tools::promote_args< typename partials_type< typename scalar_type< T1 >::type >::type, typename partials_type< typename scalar_type< T2 >::type >::type, typename partials_type< typename scalar_type< T3 >::type >::type, typename partials_type< typename scalar_type< T4 >::type >::type, typename partials_type< typename scalar_type< T5 >::type >::type, typename partials_type< typename scalar_type< T6 >::type >::type >::type type
Definition: partials_return_type.hpp:26
contains_fvar.hpp
stan::math::fvar
Definition: fvar.hpp:13
length.hpp

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