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_FWD_MAT_META_OPERANDS_AND_PARTIALS_HPP
2 #define STAN_MATH_FWD_MAT_META_OPERANDS_AND_PARTIALS_HPP
3 
8 #include <vector>
9 
10 namespace stan {
11 namespace math {
12 namespace internal {
13 // Vectorized Univariate
14 template <typename Dx>
15 class ops_partials_edge<Dx, std::vector<fvar<Dx> > > {
16  public:
17  typedef std::vector<fvar<Dx> > Op;
18  typedef Eigen::Matrix<Dx, -1, 1> partials_t;
19  partials_t partials_; // For univariate use-cases
21  explicit ops_partials_edge(const Op& ops)
22  : partials_(partials_t::Zero(ops.size())),
23  partials_vec_(partials_),
24  operands_(ops) {}
25 
26  private:
27  template <typename, typename, typename, typename, typename, typename>
29  const Op& operands_;
30 
31  Dx dx() {
32  Dx derivative(0);
33  for (size_t i = 0; i < this->operands_.size(); ++i) {
34  derivative += this->partials_[i] * this->operands_[i].d_;
35  }
36  return derivative;
37  }
38 };
39 
40 template <typename Dx, int R, int C>
41 class ops_partials_edge<Dx, Eigen::Matrix<fvar<Dx>, R, C> > {
42  public:
43  typedef Eigen::Matrix<Dx, R, C> partials_t;
44  typedef Eigen::Matrix<fvar<Dx>, R, C> Op;
45  partials_t partials_; // For univariate use-cases
47  explicit ops_partials_edge(const Op& ops)
48  : partials_(partials_t::Zero(ops.rows(), ops.cols())),
49  partials_vec_(partials_),
50  operands_(ops) {}
51 
52  private:
53  template <typename, typename, typename, typename, typename, typename>
55  const Op& operands_;
56 
57  Dx dx() {
58  Dx derivative(0);
59  for (int i = 0; i < this->operands_.size(); ++i) {
60  derivative += this->partials_(i) * this->operands_(i).d_;
61  }
62  return derivative;
63  }
64 };
65 
66 // Multivariate; vectors of eigen types
67 template <typename Dx, int R, int C>
68 class ops_partials_edge<Dx, std::vector<Eigen::Matrix<fvar<Dx>, R, C> > > {
69  public:
70  typedef std::vector<Eigen::Matrix<fvar<Dx>, R, C> > Op;
71  typedef Eigen::Matrix<Dx, -1, -1> partial_t;
72  std::vector<partial_t> partials_vec_;
73  explicit ops_partials_edge(const Op& ops)
74  : partials_vec_(ops.size()), operands_(ops) {
75  for (size_t i = 0; i < ops.size(); ++i) {
76  partials_vec_[i] = partial_t::Zero(ops[i].rows(), ops[i].cols());
77  }
78  }
79 
80  private:
81  template <typename, typename, typename, typename, typename, typename>
83  const Op& operands_;
84 
85  Dx dx() {
86  Dx derivative(0);
87  for (size_t i = 0; i < this->operands_.size(); ++i) {
88  for (int j = 0; j < this->operands_[i].size(); ++j) {
89  derivative += this->partials_vec_[i](j) * this->operands_[i](j).d_;
90  }
91  }
92  return derivative;
93  }
94 };
95 
96 template <typename Dx>
97 class ops_partials_edge<Dx, std::vector<std::vector<fvar<Dx> > > > {
98  public:
99  typedef std::vector<std::vector<fvar<Dx> > > Op;
100  typedef std::vector<Dx> partial_t;
101  std::vector<partial_t> partials_vec_;
102  explicit ops_partials_edge(const Op& ops)
103  : partials_vec_(length(ops)), operands_(ops) {
104  for (size_t i = 0; i < length(ops); ++i) {
105  partials_vec_[i] = partial_t(length(ops[i]), 0.0);
106  }
107  }
108 
109  private:
110  template <typename, typename, typename, typename, typename, typename>
112  const Op& operands_;
113 
114  Dx dx() {
115  Dx derivative(0);
116  for (size_t i = 0; i < this->operands_.size(); ++i) {
117  for (size_t j = 0; j < this->operands_[i].size(); ++j) {
118  derivative += this->partials_vec_[i][j] * this->operands_[i][j].d_;
119  }
120  }
121  return derivative;
122  }
123 };
124 } // namespace internal
125 } // namespace math
126 } // namespace stan
127 #endif
int rows(const Eigen::Matrix< T, R, C > &m)
Return the number of rows in the specified matrix, vector, or row vector.
Definition: rows.hpp:20
An edge holds both the operands and its associated partial derivatives.
This template builds partial derivatives with respect to a set of operands.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
Definition: length.hpp:16
(Expert) Numerical traits for algorithmic differentiation variables.
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:24
empty_broadcast_array< ViewElt, Op > partials_
int cols(const Eigen::Matrix< T, R, C > &m)
Return the number of columns in the specified matrix, vector, or row vector.
Definition: cols.hpp:20
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.