Stan Math Library  2.10.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  // 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> > {
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,
122  VectorView<T_partials_return,
125  VectorView<T_partials_return,
128  VectorView<T_partials_return,
131  VectorView<T_partials_return,
134  VectorView<T_partials_return,
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 
181  delete[] all_partials;
182  }
183  };
184 
185 
186  }
187 }
188 #endif
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
VectorView< T_partials_return, is_vector< T5 >::value, is_constant_struct< T5 >::value > d_x5
T_return_type value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
VectorView< T_partials_return, is_vector< T4 >::value, is_constant_struct< T4 >::value > d_x4
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:26
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
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< T6 >::value, is_constant_struct< T6 >::value > d_x6
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
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)

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