All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
cumulative_sum.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_FUN_CUMULATIVE_SUM_HPP
#define STAN_MATH_PRIM_MAT_FUN_CUMULATIVE_SUM_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <vector>
namespace stan {
namespace math {
template <typename T>
inline std::vector<T>
cumulative_sum(const std::vector<T>& x) {
std::vector<T> result(x.size());
if (x.size() == 0)
return result;
result[0] = x[0];
for (size_t i = 1; i < result.size(); ++i)
result[i] = x[i] + result[i-1];
return result;
}
template <typename T, int R, int C>
inline Eigen::Matrix<T, R, C>
cumulative_sum(const Eigen::Matrix<T, R, C>& m) {
Eigen::Matrix<T, R, C> result(m.rows(), m.cols());
if (m.size() == 0)
return result;
result(0) = m(0);
for (int i = 1; i < result.size(); ++i)
result(i) = m(i) + result(i-1);
return result;
}
}
}
#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
Eigen.hpp

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