Automatic Differentiation
 
Loading...
Searching...
No Matches
log_sum_exp_signed.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_LOG_SUM_EXP_SIGNED_HPP
2#define STAN_MATH_PRIM_FUN_LOG_SUM_EXP_SIGNED_HPP
3
9#include <cmath>
10#include <vector>
11
12namespace stan {
13namespace math {
14
26template <typename T1, typename T2,
27 require_all_stan_scalar_t<T1, T2>* = nullptr>
28inline std::tuple<return_type_t<T1, T2>, int> log_sum_exp_signed(const T1& a,
29 int a_sign,
30 const T2& b,
31 int b_sign) {
32 if (a_sign == b_sign) {
33 return std::make_tuple(log_sum_exp(a, b), a_sign);
34 }
35 bool a_larger = (a > b);
36 return std::make_tuple(a_larger ? log_diff_exp(a, b) : log_diff_exp(b, a),
37 a_larger ? a_sign : b_sign);
38}
39
40} // namespace math
41} // namespace stan
42
43#endif
std::tuple< return_type_t< T1, T2 >, int > log_sum_exp_signed(const T1 &a, int a_sign, const T2 &b, int b_sign)
Calculates the log sum of exponentials without overflow, accounting for the signs of the inputs.
fvar< T > log_diff_exp(const fvar< T > &x1, const fvar< T > &x2)
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 ...