Stan Math Library  2.15.0
reverse mode automatic differentiation
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 
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;
61  deriv += increment_derivative<T, T1, D1>()(x1, d_x1);
63  deriv += increment_derivative<T, T2, D2>()(x2, d_x2);
65  deriv += increment_derivative<T, T3, D3>()(x3, d_x3);
67  deriv += increment_derivative<T, T4, D4>()(x4, d_x4);
69  deriv += increment_derivative<T, T5, D5>()(x5, d_x5);
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> > {
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,
118  VectorView<T_partials_return,
121  VectorView<T_partials_return,
124  VectorView<T_partials_return,
127  VectorView<T_partials_return,
130  VectorView<T_partials_return,
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 
177  delete[] all_partials;
178  }
179  };
180 
181  }
182 }
183 #endif
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
This class builds partial derivatives with respect to a set of operands.
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
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
VectorView< T_partials_return, is_vector< T5 >::value, is_constant_struct< T5 >::value > d_x5
VectorView< T_partials_return, is_vector< T4 >::value, is_constant_struct< T4 >::value > d_x4
void fill(std::vector< T > &x, const S &y)
Fill the specified container with the specified value.
Definition: fill.hpp:22
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
VectorView< T_partials_return, is_vector< T6 >::value, is_constant_struct< T6 >::value > d_x6
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)
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
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1

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