| 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_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 namespace math {
13
14 // FIXME: cut-and-paste from fwd/log_sum_exp.hpp; should
15 // be able to generalize
16 template <typename T, int R, int C>
17 fvar<T>
18 log_sum_exp(const Eigen::Matrix<fvar<T>, R, C>& v) {
19 using std::exp;
20 using std::log;
21
22 Eigen::Matrix<T, 1, Eigen::Dynamic> vals(v.size());
23 for (int i = 0; i < v.size(); ++i)
24 vals[i] = v(i).val_;
25 T deriv(0.0);
26 T denominator(0.0);
27 for (int i = 0; i < v.size(); ++i) {
28 T exp_vi = exp(vals[i]);
29 denominator += exp_vi;
30 deriv += v(i).d_ * exp_vi;
31 }
32 return fvar<T>(log_sum_exp(vals), deriv / denominator);
33 }
34
35 }
36 }
37 #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:14
stan::math::log_sum_exp
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:13
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:12

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