Stan Math Library  2.20.0
reverse mode automatic differentiation
sum.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_SUM_HPP
2 #define STAN_MATH_REV_MAT_FUN_SUM_HPP
3 
4 #include <stan/math/rev/meta.hpp>
6 #include <stan/math/rev/core.hpp>
8 
9 namespace stan {
10 namespace math {
11 
17 class sum_eigen_v_vari : public sum_v_vari {
18  protected:
19  template <typename Derived>
20  inline static double sum_of_val(const Eigen::DenseBase<Derived>& v) {
21  double result = 0;
22  for (int i = 0; i < v.size(); i++)
23  result += v(i).vi_->val_;
24  return result;
25  }
26 
27  public:
28  template <int R1, int C1>
29  explicit sum_eigen_v_vari(const Eigen::Matrix<var, R1, C1>& v1)
30  : sum_v_vari(
31  sum_of_val(v1),
32  reinterpret_cast<vari**>(ChainableStack::instance_->memalloc_.alloc(
33  v1.size() * sizeof(vari*))),
34  v1.size()) {
35  for (size_t i = 0; i < length_; i++)
36  v_[i] = v1(i).vi_;
37  }
38 };
39 
49 template <int R, int C>
50 inline var sum(const Eigen::Matrix<var, R, C>& m) {
51  if (m.size() == 0)
52  return 0.0;
53  return var(new sum_eigen_v_vari(m));
54 }
55 
56 } // namespace math
57 } // namespace stan
58 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
sum_eigen_v_vari(const Eigen::Matrix< var, R1, C1 > &v1)
Definition: sum.hpp:29
static double sum_of_val(const Eigen::DenseBase< Derived > &v)
Definition: sum.hpp:20
The variable implementation base class.
Definition: vari.hpp:30
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
friend class var
Definition: vari.hpp:32
Class for representing sums with constructors for Eigen.
Definition: sum.hpp:17
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
This struct always provides access to the autodiff stack using the singleton pattern.
Class for sums of variables constructed with standard vectors.
Definition: sum.hpp:14

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