1 #ifndef STAN_MATH_PRIM_FUN_SUM_HPP
2 #define STAN_MATH_PRIM_FUN_SUM_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 #include <cstddef>
7 #include <numeric>
8 #include <vector>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Returns specified input value.
15  *
16  * @tparam T Type of element.
17  * @param m Specified value.
18  * @return Same value (the sum of one value).
19  */
20 template <typename T, require_stan_scalar_t<T>* = nullptr>
sum(T && m)21 inline T sum(T&& m) {
22   return std::forward<T>(m);
23 }
24 
25 /**
26  * Return the sum of the values in the specified standard vector.
27  *
28  * @tparam T Type of elements summed.
29  * @param m Standard vector to sum.
30  * @return Sum of elements.
31  */
32 template <typename T, require_not_var_t<T>* = nullptr>
sum(const std::vector<T> & m)33 inline T sum(const std::vector<T>& m) {
34   return std::accumulate(m.begin(), m.end(), T{0});
35 }
36 
37 /**
38  * Returns the sum of the coefficients of the specified
39  * Eigen Matrix, Array or expression.
40  *
41  * @tparam T Type of argument
42  * @param m argument
43  * @return Sum of coefficients of argument.
44  */
45 template <typename T, require_eigen_vt<std::is_arithmetic, T>* = nullptr>
sum(const T & m)46 inline value_type_t<T> sum(const T& m) {
47   return m.sum();
48 }
49 
50 }  // namespace math
51 }  // namespace stan
52 
53 #endif
54