Stan Math Library: stan/math/fwd/scal/meta/OperandsAndPartials.hpp Source File

| Stan Math Library 2.10.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 // These are helpers to the OperandsAndPartials specialization for
13 // stan::math::fvar
14 namespace {
15 template <typename T_derivative,
16 typename T,
17 typename T_partials,
18 bool is_vec = is_vector<T>::value,
19 bool is_const = is_constant_struct<T>::value>
20 struct increment_derivative {
21 inline T_derivative operator()(const T& x,
22 const T_partials& d_dx) {
23 return 0;
24 }
25 };
26
27 template <typename T_derivative,
28 typename T,
29 typename T_partials>
30 struct increment_derivative<T_derivative, T, T_partials, false, false> {
31 inline T_derivative operator()(const T& x,
32 const T_partials& d_dx) {
33 return d_dx[0] * x.d_;
34 }
35 };
36
37 template <typename T_derivative,
38 typename T,
39 typename T_partials>
40 struct increment_derivative<T_derivative, T, T_partials, true, false> {
41 inline T_derivative operator()(const T& x,
42 const T_partials& d_dx) {
43 T_derivative derivative(0);
44 for (size_t n = 0; n < length(x); n++)
45 derivative += d_dx[n] * x[n].d_;
46 return derivative;
47 }
48 };
49
50 template <typename T,
51 typename T1, typename D1, typename T2, typename D2,
52 typename T3, typename D3, typename T4, typename D4,
53 typename T5, typename D5, typename T6, typename D6>
54 fvar<T> partials_to_fvar(T& logp,
55 const T1& x1, D1& d_x1,
56 const T2& x2, D2& d_x2,
57 const T3& x3, D3& d_x3,
58 const T4& x4, D4& d_x4,
59 const T5& x5, D5& d_x5,
60 const T6& x6, D6& d_x6) {
61 T deriv = 0;
62 if (!is_constant_struct<T1>::value)
63 deriv += increment_derivative<T, T1, D1>()(x1, d_x1);
64 if (!is_constant_struct<T2>::value)
65 deriv += increment_derivative<T, T2, D2>()(x2, d_x2);
66 if (!is_constant_struct<T3>::value)
67 deriv += increment_derivative<T, T3, D3>()(x3, d_x3);
68 if (!is_constant_struct<T4>::value)
69 deriv += increment_derivative<T, T4, D4>()(x4, d_x4);
70 if (!is_constant_struct<T5>::value)
71 deriv += increment_derivative<T, T5, D5>()(x5, d_x5);
72 if (!is_constant_struct<T6>::value)
73 deriv += increment_derivative<T, T6, D6>()(x6, d_x6);
74 return stan::math::fvar<T>(logp, deriv);
75 }
76 }
77
78
101 template<typename T1, typename T2, typename T3,
102 typename T4, typename T5, typename T6,
103 typename T_partials_return>
104 struct OperandsAndPartials<T1, T2, T3, T4, T5, T6,
105 typename stan::math::fvar<T_partials_return> > {
106 typedef typename stan::math::fvar<T_partials_return> T_return_type;
107
108 const T1& x1_;
109 const T2& x2_;
110 const T3& x3_;
111 const T4& x4_;
112 const T5& x5_;
113 const T6& x6_;
114
115 size_t n_partials;
116 T_partials_return* all_partials;
117
118
119 VectorView<T_partials_return,
120 is_vector<T1>::value,
121 is_constant_struct<T1>::value> d_x1;
122 VectorView<T_partials_return,
123 is_vector<T2>::value,
124 is_constant_struct<T2>::value> d_x2;
125 VectorView<T_partials_return,
126 is_vector<T3>::value,
127 is_constant_struct<T3>::value> d_x3;
128 VectorView<T_partials_return,
129 is_vector<T4>::value,
130 is_constant_struct<T4>::value> d_x4;
131 VectorView<T_partials_return,
132 is_vector<T5>::value,
133 is_constant_struct<T5>::value> d_x5;
134 VectorView<T_partials_return,
135 is_vector<T6>::value,
136 is_constant_struct<T6>::value> d_x6;
137
138 OperandsAndPartials(const T1& x1 = 0, const T2& x2 = 0, const T3& x3 = 0,
139 const T4& x4 = 0, const T5& x5 = 0, const T6& x6 = 0)
140 : x1_(x1), x2_(x2), x3_(x3), x4_(x4), x5_(x5), x6_(x6),
141 n_partials(!is_constant_struct<T1>::value * length(x1) +
142 !is_constant_struct<T2>::value * length(x2) +
143 !is_constant_struct<T3>::value * length(x3) +
144 !is_constant_struct<T4>::value * length(x4) +
145 !is_constant_struct<T5>::value * length(x5) +
146 !is_constant_struct<T6>::value * length(x6)),
147 all_partials(new T_partials_return[n_partials]),
148 d_x1(all_partials),
149 d_x2(all_partials
150 + (!is_constant_struct<T1>::value) * length(x1)),
151 d_x3(all_partials
152 + (!is_constant_struct<T1>::value) * length(x1)
153 + (!is_constant_struct<T2>::value) * length(x2)),
154 d_x4(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 d_x5(all_partials
159 + (!is_constant_struct<T1>::value) * length(x1)
160 + (!is_constant_struct<T2>::value) * length(x2)
161 + (!is_constant_struct<T3>::value) * length(x3)
162 + (!is_constant_struct<T4>::value) * length(x4)),
163 d_x6(all_partials
164 + (!is_constant_struct<T1>::value) * length(x1)
165 + (!is_constant_struct<T2>::value) * length(x2)
166 + (!is_constant_struct<T3>::value) * length(x3)
167 + (!is_constant_struct<T4>::value) * length(x4)
168 + (!is_constant_struct<T5>::value) * length(x5)) {
169 std::fill(all_partials, all_partials + n_partials, 0);
170 }
171
172 T_return_type
173 value(T_partials_return value) {
174 return partials_to_fvar(value,
175 x1_, d_x1, x2_, d_x2,
176 x3_, d_x3, x4_, d_x4,
177 x5_, d_x4, x6_, d_x5);
178 }
179
180 ~OperandsAndPartials() {
181 delete[] all_partials;
182 }
183 };
184
185
186 }
187 }
188 #endif
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::n_partials
size_t n_partials
Definition: OperandsAndPartials.hpp:115
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::value
T_return_type value(T_partials_return value)
Definition: OperandsAndPartials.hpp:173
core.hpp
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::d_x2
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
Definition: OperandsAndPartials.hpp:124
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::d_x1
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Definition: OperandsAndPartials.hpp:121
stan
Definition: log_sum_exp.hpp:8
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::x4_
const T4 & x4_
Definition: OperandsAndPartials.hpp:111
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::d_x5
VectorView< T_partials_return, is_vector< T5 >::value, is_constant_struct< T5 >::value > d_x5
Definition: OperandsAndPartials.hpp:133
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:68
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::x2_
const T2 & x2_
Definition: OperandsAndPartials.hpp:109
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::d_x4
VectorView< T_partials_return, is_vector< T4 >::value, is_constant_struct< T4 >::value > d_x4
Definition: OperandsAndPartials.hpp:130
is_constant_struct.hpp
stan::is_vector
Definition: is_vector.hpp:10
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::T_return_type
stan::math::fvar< T_partials_return > T_return_type
Definition: OperandsAndPartials.hpp:106
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, typename stan::math::fvar< T_partials_return > >::x1_
const T1 & x1_
Definition: OperandsAndPartials.hpp:108
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::x6_
const T6 & x6_
Definition: OperandsAndPartials.hpp:113
stan::math::OperandsAndPartials
This class builds partial derivatives with respect to a set of operands.
Definition: OperandsAndPartials.hpp:38
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:26
OperandsAndPartials.hpp
length.hpp
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::d_x3
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
Definition: OperandsAndPartials.hpp:127
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::~OperandsAndPartials
~OperandsAndPartials()
Definition: OperandsAndPartials.hpp:180
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, typename stan::math::fvar< T_partials_return > >::x3_
const T3 & x3_
Definition: OperandsAndPartials.hpp:110
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::x5_
const T5 & x5_
Definition: OperandsAndPartials.hpp:112
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::fvar< T_partials_return > >::d_x6
VectorView< T_partials_return, is_vector< T6 >::value, is_constant_struct< T6 >::value > d_x6
Definition: OperandsAndPartials.hpp:136
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, typename stan::math::fvar< T_partials_return > >::all_partials
T_partials_return * all_partials
Definition: OperandsAndPartials.hpp:116
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, typename stan::math::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:138
stan::math::fvar
Definition: fvar.hpp:13

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