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_PRIM_MAT_FUN_LOG_SUM_EXP_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_LOG_SUM_EXP_HPP
3 
5 #include <vector>
6 #include <cmath>
7 
8 namespace stan {
9 namespace math {
10 
25 template <int R, int C>
26 double log_sum_exp(const Eigen::Matrix<double, R, C>& x) {
27  const double max = x.maxCoeff();
28  return max + std::log((x.array() - max).exp().sum());
29 }
30 
31 } // namespace math
32 } // namespace stan
33 
34 #endif
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:12
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: max.hpp:21

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