Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
accumulator.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__MATRIX__ACCUMULATOR_HPP
2 #define STAN__MATH__MATRIX__ACCUMULATOR_HPP
3 
4 #include <vector>
5 #include <boost/utility/enable_if.hpp>
6 #include <boost/type_traits/is_arithmetic.hpp>
7 #include <boost/type_traits/is_same.hpp>
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:
34  : buf_() {
35  }
36 
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 
126 #endif
T sum() const
Return the sum of the accumulated values.
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
accumulator()
Construct an accumulator.
Definition: accumulator.hpp:33
double sum(std::vector< double > &x)
Definition: sum.hpp:10
Class to accumulate values and eventually return their sum.
Definition: accumulator.hpp:25
void add(const std::vector< S > &xs)
Recursively add each entry in the specified standard vector to the buffer.
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
~accumulator()
Destroy an accumulator.
Definition: accumulator.hpp:40
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–2014, Stan Development Team.