| 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_REV_SCAL_META_OPERANDSANDPARTIALS_HPP
2 #define STAN_MATH_REV_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/prim/scal/meta/VectorView.hpp>
8 #include <stan/math/rev/core.hpp>
9
10 namespace stan {
11 namespace math {
12
13 namespace {
14 class partials_vari : public vari {
15 private:
16 const size_t N_;
17 vari** operands_;
18 double* partials_;
19 public:
20 partials_vari(double value,
21 size_t N,
22 vari** operands, double* partials)
23 : vari(value),
24 N_(N),
25 operands_(operands),
26 partials_(partials) { }
27 void chain() {
28 for (size_t n = 0; n < N_; ++n)
29 operands_[n]->adj_ += adj_ * partials_[n];
30 }
31 };
32
33 var partials_to_var(double logp, size_t nvaris,
34 vari** all_varis,
35 double* all_partials) {
36 return var(new partials_vari(logp, nvaris, all_varis,
37 all_partials));
38 }
39
40 template<typename T,
41 bool is_vec = is_vector<T>::value,
42 bool is_const = is_constant_struct<T>::value>
43 struct set_varis {
44 inline size_t set(vari** /*varis*/, const T& /*x*/) {
45 return 0U;
46 }
47 };
48 template<typename T>
49 struct set_varis<T, true, false> {
50 inline size_t set(vari** varis, const T& x) {
51 for (size_t n = 0; n < length(x); n++)
52 varis[n] = x[n].vi_;
53 return length(x);
54 }
55 };
56 template<>
57 struct set_varis<var, false, false> {
58 inline size_t set(vari** varis, const var& x) {
59 varis[0] = x.vi_;
60 return (1);
61 }
62 };
63 }
64
87 template<typename T1, typename T2, typename T3,
88 typename T4, typename T5, typename T6>
89 struct OperandsAndPartials<T1, T2, T3, T4, T5, T6, stan::math::var> {
90 size_t nvaris;
91 vari** all_varis;
92 double* all_partials;
93
94 VectorView<double,
95 is_vector<T1>::value,
96 is_constant_struct<T1>::value> d_x1;
97 VectorView<double,
98 is_vector<T2>::value,
99 is_constant_struct<T2>::value> d_x2;
100 VectorView<double,
101 is_vector<T3>::value,
102 is_constant_struct<T3>::value> d_x3;
103 VectorView<double,
104 is_vector<T4>::value,
105 is_constant_struct<T4>::value> d_x4;
106 VectorView<double,
107 is_vector<T5>::value,
108 is_constant_struct<T5>::value> d_x5;
109 VectorView<double,
110 is_vector<T6>::value,
111 is_constant_struct<T6>::value> d_x6;
112
123 OperandsAndPartials(const T1& x1 = 0, const T2& x2 = 0, const T3& x3 = 0,
124 const T4& x4 = 0, const T5& x5 = 0, const T6& x6 = 0)
125 : nvaris(!is_constant_struct<T1>::value * length(x1) +
126 !is_constant_struct<T2>::value * length(x2) +
127 !is_constant_struct<T3>::value * length(x3) +
128 !is_constant_struct<T4>::value * length(x4) +
129 !is_constant_struct<T5>::value * length(x5) +
130 !is_constant_struct<T6>::value * length(x6)),
131 // TODO(carpenter): replace with array allocation fun
132 all_varis(static_cast<vari**>
133 (vari::operator new
134 (sizeof(vari*) * nvaris))),
135 all_partials(static_cast<double*>
136 (vari::operator new
137 (sizeof(double) * nvaris))),
138 d_x1(all_partials),
139 d_x2(all_partials
140 + (!is_constant_struct<T1>::value) * length(x1)),
141 d_x3(all_partials
142 + (!is_constant_struct<T1>::value) * length(x1)
143 + (!is_constant_struct<T2>::value) * length(x2)),
144 d_x4(all_partials
145 + (!is_constant_struct<T1>::value) * length(x1)
146 + (!is_constant_struct<T2>::value) * length(x2)
147 + (!is_constant_struct<T3>::value) * length(x3)),
148 d_x5(all_partials
149 + (!is_constant_struct<T1>::value) * length(x1)
150 + (!is_constant_struct<T2>::value) * length(x2)
151 + (!is_constant_struct<T3>::value) * length(x3)
152 + (!is_constant_struct<T4>::value) * length(x4)),
153 d_x6(all_partials
154 + (!is_constant_struct<T1>::value) * length(x1)
155 + (!is_constant_struct<T2>::value) * length(x2)
156 + (!is_constant_struct<T3>::value) * length(x3)
157 + (!is_constant_struct<T4>::value) * length(x4)
158 + (!is_constant_struct<T5>::value) * length(x5)) {
159 size_t base = 0;
160 if (!is_constant_struct<T1>::value)
161 base += set_varis<T1>().set(&all_varis[base], x1);
162 if (!is_constant_struct<T2>::value)
163 base += set_varis<T2>().set(&all_varis[base], x2);
164 if (!is_constant_struct<T3>::value)
165 base += set_varis<T3>().set(&all_varis[base], x3);
166 if (!is_constant_struct<T4>::value)
167 base += set_varis<T4>().set(&all_varis[base], x4);
168 if (!is_constant_struct<T5>::value)
169 base += set_varis<T5>().set(&all_varis[base], x5);
170 if (!is_constant_struct<T6>::value)
171 set_varis<T6>().set(&all_varis[base], x6);
172 std::fill(all_partials, all_partials+nvaris, 0);
173 }
174
183 stan::math::var value(double value) {
184 return partials_to_var(value, nvaris, all_varis,
185 all_partials);
186 }
187 };
188
189 }
190 }
191 #endif
core.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, stan::math::var >::all_varis
vari ** all_varis
Definition: OperandsAndPartials.hpp:91
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, stan::math::var >::d_x4
VectorView< double, is_vector< T4 >::value, is_constant_struct< T4 >::value > d_x4
Definition: OperandsAndPartials.hpp:105
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:30
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, stan::math::var >::value
stan::math::var value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
Definition: OperandsAndPartials.hpp:183
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
operands_
vari ** operands_
Definition: OperandsAndPartials.hpp:17
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
partials_
double * partials_
Definition: OperandsAndPartials.hpp:18
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, stan::math::var >::d_x1
VectorView< double, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Definition: OperandsAndPartials.hpp:96
N_
const size_t N_
Definition: OperandsAndPartials.hpp:16
stan::math::OperandsAndPartials
This class builds partial derivatives with respect to a set of operands.
Definition: OperandsAndPartials.hpp:37
VectorView.hpp
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, stan::math::var >::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)
Constructor.
Definition: OperandsAndPartials.hpp:123
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, stan::math::var >::d_x5
VectorView< double, is_vector< T5 >::value, is_constant_struct< T5 >::value > d_x5
Definition: OperandsAndPartials.hpp:108
OperandsAndPartials.hpp
length.hpp
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, stan::math::var >::d_x2
VectorView< double, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
Definition: OperandsAndPartials.hpp:99
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, stan::math::var >::d_x3
VectorView< double, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
Definition: OperandsAndPartials.hpp:102
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, stan::math::var >::all_partials
double * all_partials
Definition: OperandsAndPartials.hpp:92
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, stan::math::var >::nvaris
size_t nvaris
Definition: OperandsAndPartials.hpp:90
stan::math::OperandsAndPartials< T1, T2, T3, T4, T5, T6, stan::math::var >::d_x6
VectorView< double, is_vector< T6 >::value, is_constant_struct< T6 >::value > d_x6
Definition: OperandsAndPartials.hpp:111

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