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
8#include <vector>
9
10namespace stan {
11namespace math {
12
21template <typename T>
22inline fvar<T> sum(const std::vector<fvar<T>>& m) {
23 if (m.size() == 0) {
24 return 0.0;
25 }
26 std::vector<T> vals(m.size());
27 std::vector<T> tans(m.size());
28 for (size_t i = 0; i < m.size(); ++i) {
29 vals[i] = m[i].val();
30 tans[i] = m[i].d();
31 }
32 return fvar<T>(sum(vals), sum(tans));
33}
34
43template <typename T, require_eigen_vt<is_fvar, T>* = nullptr>
44inline value_type_t<T> sum(const T& m) {
45 if (m.size() == 0) {
46 return 0.0;
47 }
48 const Eigen::Ref<const plain_type_t<T>>& m_ref = m;
49 return {sum(m_ref.val()), sum(m_ref.d())};
50}
51
52} // namespace math
53} // namespace stan
54#endif
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
This template class represents scalars used in forward-mode automatic differentiation,...
Definition fvar.hpp:40