Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
log_sum_exp.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_ARR_FUN_LOG_SUM_EXP_HPP
2 #define STAN_MATH_PRIM_ARR_FUN_LOG_SUM_EXP_HPP
3 
4 #include <cmath>
5 #include <cstdlib>
6 #include <limits>
7 #include <vector>
8 
9 namespace stan {
10  namespace math {
11 
24  double log_sum_exp(const std::vector<double>& x) {
25  using std::numeric_limits;
26  using std::log;
27  using std::exp;
28  double max = -numeric_limits<double>::infinity();
29  for (size_t ii = 0; ii < x.size(); ii++)
30  if (x[ii] > max)
31  max = x[ii];
32 
33  double sum = 0.0;
34  for (size_t ii = 0; ii < x.size(); ii++)
35  if (x[ii] != -numeric_limits<double>::infinity())
36  sum += exp(x[ii] - max);
37 
38  return max + log(sum);
39  }
40 
41  }
42 }
43 
44 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:14
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
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.