Stan Math Library  2.14.0
reverse mode automatic differentiation
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 
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;
92  double* all_partials;
93 
94  VectorView<double,
97  VectorView<double,
100  VectorView<double,
103  VectorView<double,
106  VectorView<double,
109  VectorView<double,
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;
161  base += set_varis<T1>().set(&all_varis[base], x1);
163  base += set_varis<T2>().set(&all_varis[base], x2);
165  base += set_varis<T3>().set(&all_varis[base], x3);
167  base += set_varis<T4>().set(&all_varis[base], x4);
169  base += set_varis<T5>().set(&all_varis[base], x5);
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
VectorView< double, is_vector< T4 >::value, is_constant_struct< T4 >::value > d_x4
The variable implementation base class.
Definition: vari.hpp:30
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
stan::math::var value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
VectorView< double, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
This class builds partial derivatives with respect to a set of operands.
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.
VectorView< double, is_vector< T5 >::value, is_constant_struct< T5 >::value > d_x5
VectorView< double, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
void fill(std::vector< T > &x, const S &y)
Fill the specified container with the specified value.
Definition: fill.hpp:22
VectorView< double, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
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
int N_
VectorView< double, is_vector< T6 >::value, is_constant_struct< T6 >::value > d_x6

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