1 #ifndef STAN__MATH__FUNCTIONS__LOG_SUM_EXP_HPP
2 #define STAN__MATH__FUNCTIONS__LOG_SUM_EXP_HPP
6 #include <boost/math/tools/promotion.hpp>
47 template <
typename T1,
typename T2>
48 inline typename boost::math::tools::promote_args<T1,T2>::type
69 using std::numeric_limits;
72 double max = -numeric_limits<double>::infinity();
73 for (
size_t ii = 0; ii < x.size(); ii++)
78 for (
size_t ii = 0; ii < x.size(); ii++)
79 if (x[ii] != -numeric_limits<double>::infinity())
80 sum +=
exp(x[ii] - max);
82 return max +
log(sum);
boost::math::tools::promote_args< T >::type log1p_exp(const T a)
Calculates the log of 1 plus the exponential of the specified value without overflow.
boost::math::tools::promote_args< T1, T2 >::type log_sum_exp(const T2 &a, const T1 &b)
Calculates the log sum of exponetials without overflow.
Eigen::Matrix< T, Rows, Cols > exp(const Eigen::Matrix< T, Rows, Cols > &m)
Return the element-wise exponentiation of the matrix or vector.
double max(const double a, const double b)
Eigen::Matrix< T, Rows, Cols > log(const Eigen::Matrix< T, Rows, Cols > &m)
Return the element-wise logarithm of the matrix or vector.
double sum(std::vector< double > &x)
fvar< T > log(const fvar< T > &x)
fvar< T > exp(const fvar< T > &x)