Stan Math Library  2.20.0
reverse mode automatic differentiation
operands_and_partials.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_META_OPERANDS_AND_PARTIALS_HPP
2 #define STAN_MATH_REV_SCAL_META_OPERANDS_AND_PARTIALS_HPP
3 
11 
12 namespace stan {
13 namespace math {
14 namespace internal {
15 template <>
16 class ops_partials_edge<double, var> {
17  public:
18  double partial_;
20  explicit ops_partials_edge(const var& op)
21  : partial_(0), partials_(partial_), operand_(op) {}
22 
23  private:
24  template <typename, typename, typename, typename, typename, typename>
26  const var& operand_;
27 
28  void dump_partials(double* partials) { *partials = this->partial_; }
29  void dump_operands(vari** varis) { *varis = this->operand_.vi_; }
30  int size() const { return 1; }
31 };
32 } // namespace internal
33 
67 template <typename Op1, typename Op2, typename Op3, typename Op4, typename Op5>
68 class operands_and_partials<Op1, Op2, Op3, Op4, Op5, var> {
69  public:
75 
76  explicit operands_and_partials(const Op1& o1) : edge1_(o1) {}
77  operands_and_partials(const Op1& o1, const Op2& o2)
78  : edge1_(o1), edge2_(o2) {}
79  operands_and_partials(const Op1& o1, const Op2& o2, const Op3& o3)
80  : edge1_(o1), edge2_(o2), edge3_(o3) {}
81  operands_and_partials(const Op1& o1, const Op2& o2, const Op3& o3,
82  const Op4& o4)
83  : edge1_(o1), edge2_(o2), edge3_(o3), edge4_(o4) {}
84  operands_and_partials(const Op1& o1, const Op2& o2, const Op3& o3,
85  const Op4& o4, const Op5& o5)
86  : edge1_(o1), edge2_(o2), edge3_(o3), edge4_(o4), edge5_(o5) {}
87 
101  var build(double value) {
102  size_t size = edge1_.size() + edge2_.size() + edge3_.size() + edge4_.size()
103  + edge5_.size();
104  vari** varis
106  double* partials
108  int idx = 0;
109  edge1_.dump_operands(&varis[idx]);
110  edge1_.dump_partials(&partials[idx]);
111  edge2_.dump_operands(&varis[idx += edge1_.size()]);
112  edge2_.dump_partials(&partials[idx]);
113  edge3_.dump_operands(&varis[idx += edge2_.size()]);
114  edge3_.dump_partials(&partials[idx]);
115  edge4_.dump_operands(&varis[idx += edge3_.size()]);
116  edge4_.dump_partials(&partials[idx]);
117  edge5_.dump_operands(&varis[idx += edge4_.size()]);
118  edge5_.dump_partials(&partials[idx]);
119 
120  return var(new precomputed_gradients_vari(value, size, varis, partials));
121  }
122 };
123 } // namespace math
124 } // namespace stan
125 #endif
operands_and_partials(const Op1 &o1, const Op2 &o2, const Op3 &o3, const Op4 &o4)
An edge holds both the operands and its associated partial derivatives.
operands_and_partials(const Op1 &o1, const Op2 &o2, const Op3 &o3)
This template builds partial derivatives with respect to a set of operands.
The variable implementation base class.
Definition: vari.hpp:30
static STAN_THREADS_DEF AutodiffStackStorage * instance_
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
A variable implementation taking a sequence of operands and partial derivatives with respect to the o...
operands_and_partials(const Op1 &o1, const Op2 &o2, const Op3 &o3, const Op4 &o4, const Op5 &o5)
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
var build(double value)
Build the node to be stored on the autodiff graph.
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17

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