1#ifndef STAN_MATH_PRIM_FUN_LOG_SUM_EXP_HPP
2#define STAN_MATH_PRIM_FUN_LOG_SUM_EXP_HPP
51template <
typename T1,
typename T2, require_all_not_st_var<T1, T2>* =
nullptr,
52 require_all_stan_scalar_t<T1, T2>* =
nullptr>
81template <
typename T, require_container_st<std::is_arithmetic, T>* =
nullptr>
83 return apply_vector_unary<T>::reduce(x, [&](
const auto& v) {
85 return NEGATIVE_INFTY;
87 const auto& v_ref =
to_ref(v);
88 const double max = v_ref.maxCoeff();
89 if (!std::isfinite(
max)) {
92 return max + std::log((v_ref.array() -
max).exp().sum());
106template <
typename T1,
typename T2, require_any_container_t<T1, T2>* =
nullptr>
109 a, b, [](
const auto& c,
const auto& d) {
return log_sum_exp(c, d); });
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
static constexpr double NEGATIVE_INFTY
Negative infinity.
auto max(T1 x, T2 y)
Returns the maximum value of the two specified scalar arguments.
fvar< T > log1p_exp(const fvar< T > &x)
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
auto apply_scalar_binary(const T1 &x, const T2 &y, const F &f)
Base template function for vectorization of binary scalar functions defined by applying a functor to ...
static constexpr double INFTY
Positive infinity.
fvar< T > log_sum_exp(const fvar< T > &x1, const fvar< T > &x2)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...