Stan Math Library  2.20.0
reverse mode automatic differentiation
log_sum_exp.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_LOG_SUM_EXP_HPP
2 #define STAN_MATH_REV_MAT_FUN_LOG_SUM_EXP_HPP
3 
4 #include <stan/math/rev/meta.hpp>
5 #include <stan/math/rev/core.hpp>
9 #include <limits>
10 
11 namespace stan {
12 namespace math {
13 
14 namespace internal {
15 
16 // these function and the following class just translate
17 // log_sum_exp for std::vector for Eigen::Matrix
18 
19 template <int R, int C>
20 inline double log_sum_exp_as_double(const Eigen::Matrix<var, R, C>& x) {
21  const double max = x.val().maxCoeff();
22  return max + std::log((x.val().array() - max).exp().sum());
23 }
24 
26  public:
27  template <int R, int C>
28  explicit log_sum_exp_matrix_vari(const Eigen::Matrix<var, R, C>& x)
30  void chain() {
31  Eigen::Map<vector_vi> vis_map(vis_, size_);
32  vis_map.adj().array() += adj_ * (vis_map.val().array() - val_).exp();
33  }
34 };
35 } // namespace internal
36 
42 template <int R, int C>
43 inline var log_sum_exp(const Eigen::Matrix<var, R, C>& x) {
45 }
46 
47 } // namespace math
48 } // namespace stan
49 #endif
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
double log_sum_exp_as_double(const std::vector< var > &x)
Definition: log_sum_exp.hpp:14
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:12
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: log_sum_exp.hpp:30
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: max.hpp:21
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
log_sum_exp_matrix_vari(const Eigen::Matrix< var, R, C > &x)
Definition: log_sum_exp.hpp:28

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