| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 inline 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
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
Definition: log_sum_exp.hpp:8
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–2016, Stan Development Team.