Stan Math Library  2.20.0
reverse mode automatic differentiation
sum.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_SUM_HPP
2 #define STAN_MATH_FWD_MAT_FUN_SUM_HPP
3 
4 #include <stan/math/fwd/core.hpp>
7 
8 namespace stan {
9 namespace math {
10 
20 template <typename T, int R, int C>
21 inline fvar<T> sum(const Eigen::Matrix<fvar<T>, R, C>& m) {
22  if (m.size() == 0)
23  return 0.0;
24  Eigen::Matrix<T, Eigen::Dynamic, 1> vals(m.size());
25  Eigen::Matrix<T, Eigen::Dynamic, 1> tans(m.size());
26  for (int i = 0; i < m.size(); ++i) {
27  vals(i) = m(i).val();
28  tans(i) = m(i).tangent();
29  }
30  return fvar<T>(sum(vals), sum(tans));
31 }
32 
33 } // namespace math
34 } // namespace stan
35 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

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