Stan Math Library  2.20.0
reverse mode automatic differentiation
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 
5 #include <stan/math/fwd/core.hpp>
7 
8 namespace stan {
9 namespace math {
10 
11 template <typename T, int R, int C>
12 fvar<T> log_sum_exp(const Eigen::Matrix<fvar<T>, R, C>& v) {
13  Eigen::Matrix<T, R, C> vals = v.val();
14  Eigen::Matrix<T, R, C> exp_vals = vals.array().exp();
15 
16  return fvar<T>(log_sum_exp(vals),
17  v.d().cwiseProduct(exp_vals).sum() / exp_vals.sum());
18 }
19 
20 } // namespace math
21 } // namespace stan
22 #endif
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:12
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

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