Stan Math Library: stan/math/prim/mat/fun/accumulator.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

accumulator.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_ACCUMULATOR_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_ACCUMULATOR_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/fun/sum.hpp>
6 #include <boost/utility/enable_if.hpp>
7 #include <boost/type_traits/is_arithmetic.hpp>
8 #include <boost/type_traits/is_same.hpp>
9 #include <vector>
10
11 namespace stan {
12 namespace math {
13
24 template <typename T>
25 class accumulator {
26 private:
27 std::vector<T> buf_;
28
29 public:
33 accumulator()
34 : buf_() {
35 }
36
40 ~accumulator() { }
41
52 template <typename S>
53 typename boost::enable_if<boost::is_arithmetic<S>, void>::type
54 add(S x) {
55 buf_.push_back(static_cast<T>(x));
56 }
57
70 template <typename S>
71 typename boost::disable_if<boost::is_arithmetic<S>,
72 typename boost::enable_if<boost::is_same<S, T>,
73 void>::type >::type
74 add(const S& x) {
75 buf_.push_back(x);
76 }
77
87 template <typename S, int R, int C>
88 void add(const Eigen::Matrix<S, R, C>& m) {
89 for (int i = 0; i < m.size(); ++i)
90 add(m(i));
91 }
92
102 template <typename S>
103 void add(const std::vector<S>& xs) {
104 for (size_t i = 0; i < xs.size(); ++i)
105 add(xs[i]);
106 }
107
113 T sum() const {
114 using math::sum;
115 return sum(buf_);
116 }
117 };
118
119
120
121
122 }
123 }
124
125 #endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
stan::math::accumulator::sum
T sum() const
Return the sum of the accumulated values.
Definition: accumulator.hpp:113
stan
Definition: log_sum_exp.hpp:8
stan::math::accumulator::accumulator
accumulator()
Construct an accumulator.
Definition: accumulator.hpp:33
stan::math::accumulator::add
boost::disable_if< boost::is_arithmetic< S >, typename boost::enable_if< boost::is_same< S, T >, void >::type >::type add(const S &x)
Add the specified non-arithmetic value to the buffer.
Definition: accumulator.hpp:74
Eigen.hpp
stan::math::accumulator
Class to accumulate values and eventually return their sum.
Definition: accumulator.hpp:25
sum.hpp
stan::math::accumulator::add
void add(const std::vector< S > &xs)
Recursively add each entry in the specified standard vector to the buffer.
Definition: accumulator.hpp:103
stan::math::accumulator::add
void add(const Eigen::Matrix< S, R, C > &m)
Add each entry in the specified matrix, vector, or row vector of values to the buffer.
Definition: accumulator.hpp:88
stan::math::accumulator::~accumulator
~accumulator()
Destroy an accumulator.
Definition: accumulator.hpp:40
stan::math::accumulator::add
boost::enable_if< boost::is_arithmetic< S >, void >::type add(S x)
Add the specified arithmetic type value to the buffer after static casting it to the class type T...
Definition: accumulator.hpp:54

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