Automatic Differentiation
 
Loading...
Searching...
No Matches
cumulative_sum.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_CUMULATIVE_SUM_HPP
2#define STAN_MATH_PRIM_FUN_CUMULATIVE_SUM_HPP
3
6#include <vector>
7#include <numeric>
8#include <functional>
9
10namespace stan {
11namespace math {
12
24template <typename T>
25inline std::vector<T> cumulative_sum(const std::vector<T>& x) {
26 std::vector<T> result(x.size());
27 if (x.size() == 0) {
28 return result;
29 }
30 std::partial_sum(x.begin(), x.end(), result.begin(), std::plus<T>());
31 return result;
32}
33
48template <typename EigVec, require_eigen_vector_t<EigVec>* = nullptr,
49 require_not_st_var<EigVec>* = nullptr>
50inline auto cumulative_sum(const EigVec& m) {
51 using T_scalar = value_type_t<EigVec>;
52 Eigen::Matrix<T_scalar, EigVec::RowsAtCompileTime, EigVec::ColsAtCompileTime>
53 result(m.rows(), m.cols());
54 if (m.size() == 0) {
55 return result;
56 }
57 const auto& m_ref = to_ref(m);
58 result.coeffRef(0) = m_ref.coeff(0);
59 for (Eigen::Index i = 1; i < m_ref.size(); ++i) {
60 result.coeffRef(i) = m_ref.coeff(i) + result.coeffRef(i - 1);
61 }
62 return result;
63}
64
65} // namespace math
66} // namespace stan
67
68#endif
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
auto cumulative_sum(T_vec &&v)
Return the cumulative sum of the specified vector.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...