Automatic Differentiation
 
Loading...
Searching...
No Matches
sum.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_SUM_HPP
2#define STAN_MATH_FWD_FUN_SUM_HPP
3
9#include <vector>
10
11namespace stan {
12namespace math {
13
22template <typename T, require_fvar_t<T>* = nullptr>
23inline auto sum(const std::vector<T>& m) {
24 if (m.size() == 0) {
25 return T(0.0);
26 }
27 std::vector<partials_type_t<T>> vals(m.size());
28 std::vector<partials_type_t<T>> tans(m.size());
29 for (size_t i = 0; i < m.size(); ++i) {
30 vals[i] = m[i].val();
31 tans[i] = m[i].d();
32 }
33 return T(sum(vals), sum(tans));
34}
35
44template <typename T, require_eigen_vt<is_fvar, T>* = nullptr>
45inline value_type_t<T> sum(const T& m) {
46 if (m.size() == 0) {
47 return 0.0;
48 }
49 const Eigen::Ref<const plain_type_t<T>>& m_ref = m;
50 return {sum(m_ref.val()), sum(m_ref.d())};
51}
52
53} // namespace math
54} // namespace stan
55#endif
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...