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

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
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
stan::math::cumulative_sum
std::vector< T > cumulative_sum(const std::vector< T > &x)
Return the cumulative sum of the specified vector.
Definition: cumulative_sum.hpp:23
stan
Definition: log_sum_exp.hpp:8
Eigen.hpp

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