Stan Math Library  2.20.0
reverse mode automatic differentiation
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 #include <numeric>
7 #include <functional>
8 
9 namespace stan {
10 namespace math {
11 
23 template <typename T>
24 inline std::vector<T> cumulative_sum(const std::vector<T>& x) {
25  std::vector<T> result(x.size());
26  if (x.size() == 0)
27  return result;
28  std::partial_sum(x.begin(), x.end(), result.begin(), std::plus<T>());
29  return result;
30 }
31 
46 template <typename T, int R, int C>
47 inline Eigen::Matrix<T, R, C> cumulative_sum(const Eigen::Matrix<T, R, C>& m) {
48  Eigen::Matrix<T, R, C> result(m.rows(), m.cols());
49  if (m.size() == 0)
50  return result;
51  std::partial_sum(m.data(), m.data() + m.size(), result.data(),
52  std::plus<T>());
53  return result;
54 }
55 } // namespace math
56 } // namespace stan
57 #endif
std::vector< T > cumulative_sum(const std::vector< T > &x)
Return the cumulative sum of the specified vector.

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