Automatic Differentiation
 
Loading...
Searching...
No Matches

◆ log_sum_exp() [7/13]

template<typename T1 , typename T2 , require_all_not_st_var< T1, T2 > * = nullptr, require_all_stan_scalar_t< T1, T2 > * = nullptr>
return_type_t< T1, T2 > stan::math::log_sum_exp ( const T2 &  a,
const T1 &  b 
)
inline

Calculates the log sum of exponentials without overflow.

\(\log (\exp(a) + \exp(b)) = m + \log(\exp(a-m) + \exp(b-m))\),

where \(m = max(a, b)\).

\[ \mbox{log\_sum\_exp}(x, y) = \begin{cases} \ln(\exp(x)+\exp(y)) & \mbox{if } -\infty\leq x, y \leq \infty \\[6pt] \textrm{NaN} & \mbox{if } x = \textrm{NaN or } y = \textrm{NaN} \end{cases} \]

\[ \frac{\partial\, \mbox{log\_sum\_exp}(x, y)}{\partial x} = \begin{cases} \frac{\exp(x)}{\exp(x)+\exp(y)} & \mbox{if } -\infty\leq x, y \leq \infty \\[6pt] \textrm{NaN} & \mbox{if } x = \textrm{NaN or } y = \textrm{NaN} \end{cases} \]

\[ \frac{\partial\, \mbox{log\_sum\_exp}(x, y)}{\partial y} = \begin{cases} \frac{\exp(y)}{\exp(x)+\exp(y)} & \mbox{if } -\infty\leq x, y \leq \infty \\[6pt] \textrm{NaN} & \mbox{if } x = \textrm{NaN or } y = \textrm{NaN} \end{cases} \]

Template Parameters
T1type of the first variable
T2type of the second variable
Parameters
athe first variable
bthe second variable

Definition at line 53 of file log_sum_exp.hpp.