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

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