Stan Math Library: stan/math/fwd/mat/fun/log_sum_exp.hpp Source File

| Stan Math Library 2.10.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_MAT_FUN_LOG_SUM_EXP_HPP
2 #define STAN_MATH_FWD_MAT_FUN_LOG_SUM_EXP_HPP
3
4 #include <stan/math/fwd/core.hpp>
5 #include <stan/math/prim/mat/fun/log_sum_exp.hpp>
6 #include <stan/math/prim/mat/fun/Eigen.hpp>
7 #include <stan/math/fwd/scal/fun/log.hpp>
8 #include <stan/math/fwd/scal/fun/exp.hpp>
9 #include <vector>
10
11 namespace stan {
12
13 namespace math {
14
15 // FIXME: cut-and-paste from fwd/log_sum_exp.hpp; should
16 // be able to generalize
17 template <typename T, int R, int C>
18 fvar<T>
19 log_sum_exp(const Eigen::Matrix<fvar<T>, R, C>& v) {
20 using stan::math::log_sum_exp;
21 using stan::math::log_sum_exp;
22 using stan::math::exp;
23 using std::exp;
24 using stan::math::log;
25 using std::log;
26
27 Eigen::Matrix<T, 1, Eigen::Dynamic> vals(v.size());
28 for (int i = 0; i < v.size(); ++i)
29 vals[i] = v(i).val_;
30 T deriv(0.0);
31 T denominator(0.0);
32 for (int i = 0; i < v.size(); ++i) {
33 T exp_vi = exp(vals[i]);
34 denominator += exp_vi;
35 deriv += v(i).d_ * exp_vi;
36 }
37 return fvar<T>(log_sum_exp(vals), deriv / denominator);
38 }
39
40 }
41 }
42 #endif
core.hpp
log.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::log_sum_exp
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:14
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
Eigen.hpp
log_sum_exp.hpp
exp.hpp
stan::math::fvar
Definition: fvar.hpp:13

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