All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
log_sum_exp.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_ARR_FUN_LOG_SUM_EXP_HPP
#define STAN_MATH_PRIM_ARR_FUN_LOG_SUM_EXP_HPP
#include <cmath>
#include <cstdlib>
#include <limits>
#include <vector>
namespace stan {
namespace math {
double log_sum_exp(const std::vector<double>& x) {
using std::numeric_limits;
using std::log;
using std::exp;
double max = -numeric_limits<double>::infinity();
for (size_t ii = 0; ii < x.size(); ii++)
if (x[ii] > max)
max = x[ii];
double sum = 0.0;
for (size_t ii = 0; ii < x.size(); ii++)
if (x[ii] != -numeric_limits<double>::infinity())
sum += exp(x[ii] - max);
return max + log(sum);
}
}
}
#endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::log_sum_exp
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:14
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::max
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: max.hpp:21

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