1 #ifndef STAN_MATH_REV_ARR_FUN_LOG_SUM_EXP_HPP
2 #define STAN_MATH_REV_ARR_FUN_LOG_SUM_EXP_HPP
14 double log_sum_exp_as_double(
const std::vector<var>& x) {
15 using std::numeric_limits;
18 double max = -numeric_limits<double>::infinity();
19 for (
size_t i = 0; i < x.size(); ++i)
23 for (
size_t i = 0; i < x.size(); ++i)
24 if (x[i] != -numeric_limits<double>::infinity())
25 sum +=
exp(x[i].val() -
max);
26 return max +
log(sum);
29 class log_sum_exp_vector_vari :
public op_vector_vari {
31 explicit log_sum_exp_vector_vari(
const std::vector<var>& x) :
32 op_vector_vari(log_sum_exp_as_double(x), x) {
35 for (
size_t i = 0; i <
size_; ++i) {
46 return var(
new log_sum_exp_vector_vari(x));
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
fvar< T > log(const fvar< T > &x)
Independent (input) and dependent (output) variables for gradients.
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
fvar< T > exp(const fvar< T > &x)
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
double calculate_chain(double x, double val)