| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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>
5 #include <stan/math/prim/arr/fun/log_sum_exp.hpp>
6 #include <vector>
7
8 namespace stan {
9 namespace math {
10
11 template <typename T>
12 fvar<T>
13 log_sum_exp(const std::vector<fvar<T> >& v) {
14 using std::exp;
15 std::vector<T> vals(v.size());
16 for (size_t i = 0; i < v.size(); ++i)
17 vals[i] = v[i].val_;
18 T deriv(0.0);
19 T denominator(0.0);
20 for (size_t i = 0; i < v.size(); ++i) {
21 T exp_vi = exp(vals[i]);
22 denominator += exp_vi;
23 deriv += v[i].d_ * exp_vi;
24 }
25 return fvar<T>(log_sum_exp(vals), deriv / denominator);
26 }
27
28 }
29 }
30 #endif
core.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::log_sum_exp
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:13
log_sum_exp.hpp
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::fvar
Definition: fvar.hpp:12

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