log_sum_exp.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_ARR_FUN_LOG_SUM_EXP_HPP
2 #define STAN_MATH_FWD_ARR_FUN_LOG_SUM_EXP_HPP
3 
4 #include <stan/math/fwd/core.hpp>
6 #include <vector>
7 
8 namespace stan {
9 namespace math {
10 
11 template <typename T>
12 fvar<T> log_sum_exp(const std::vector<fvar<T> >& v) {
13  using std::exp;
14  std::vector<T> vals(v.size());
15  for (size_t i = 0; i < v.size(); ++i)
16  vals[i] = v[i].val_;
17  T deriv(0.0);
18  T denominator(0.0);
19  for (size_t i = 0; i < v.size(); ++i) {
20  T exp_vi = exp(vals[i]);
21  denominator += exp_vi;
22  deriv += v[i].d_ * exp_vi;
23  }
24  return fvar<T>(log_sum_exp(vals), deriv / denominator);
25 }
26 
27 } // namespace math
28 } // namespace stan
29 #endif
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:12
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
double T
Definition: Xdiff_gwt.C:5