All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
log_sum_exp.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_FUN_LOG_SUM_EXP_HPP
#define STAN_MATH_PRIM_MAT_FUN_LOG_SUM_EXP_HPP
#include <stan/math/prim/scal/fun/log1p.hpp>
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <boost/math/tools/promotion.hpp>
#include <limits>
#include <vector>
namespace stan {
namespace math {
template <int R, int C>
double log_sum_exp(const Eigen::Matrix<double, R, C>& x) {
using std::numeric_limits;
using std::log;
using std::exp;
double max = -numeric_limits<double>::infinity();
for (int i = 0; i < x.size(); i++)
if (x(i) > max)
max = x(i);
double sum = 0.0;
for (int i = 0; i < x.size(); i++)
if (x(i) != -numeric_limits<double>::infinity())
sum += exp(x(i) - 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
Eigen.hpp
stan::math::max
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: max.hpp:21
log1p.hpp

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