Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
cumulative_sum.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CUMULATIVE_SUM_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CUMULATIVE_SUM_HPP
3 
5 #include <vector>
6 
7 namespace stan {
8  namespace math {
9 
21  template <typename T>
22  inline std::vector<T>
23  cumulative_sum(const std::vector<T>& x) {
24  std::vector<T> result(x.size());
25  if (x.size() == 0)
26  return result;
27  result[0] = x[0];
28  for (size_t i = 1; i < result.size(); ++i)
29  result[i] = x[i] + result[i-1];
30  return result;
31  }
32 
47  template <typename T, int R, int C>
48  inline Eigen::Matrix<T, R, C>
49  cumulative_sum(const Eigen::Matrix<T, R, C>& m) {
50  Eigen::Matrix<T, R, C> result(m.rows(), m.cols());
51  if (m.size() == 0)
52  return result;
53  result(0) = m(0);
54  for (int i = 1; i < result.size(); ++i)
55  result(i) = m(i) + result(i-1);
56  return result;
57  }
58  }
59 }
60 #endif
std::vector< T > cumulative_sum(const std::vector< T > &x)
Return the cumulative sum of the specified vector.

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