Automatic Differentiation
 
Loading...
Searching...
No Matches
log_diff_exp.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_LOG_DIFF_EXP_HPP
2#define STAN_MATH_FWD_FUN_LOG_DIFF_EXP_HPP
3
9
10namespace stan {
11namespace math {
12
13template <typename T>
14inline fvar<T> log_diff_exp(const fvar<T>& x1, const fvar<T>& x2) {
15 if (x1.val_ <= x2.val_) {
16 if (x1.val_ < INFTY && x1.val_ == x2.val_) {
18 }
20 }
21 return fvar<T>(
22 log_diff_exp(x1.val_, x2.val_),
23 -(x1.d_ / expm1(x2.val_ - x1.val_) + x2.d_ / expm1(x1.val_ - x2.val_)));
24}
25
26template <typename T1, typename T2, require_arithmetic_t<T1>* = nullptr>
27inline fvar<T2> log_diff_exp(const T1& x1, const fvar<T2>& x2) {
28 if (x1 <= x2.val_) {
29 if (x1 < INFTY && x1 == x2.val_) {
31 }
33 }
34 return fvar<T2>(log_diff_exp(x1, x2.val_), -x2.d_ / expm1(x1 - x2.val_));
35}
36
37template <typename T1, typename T2, require_arithmetic_t<T2>* = nullptr>
38inline fvar<T1> log_diff_exp(const fvar<T1>& x1, const T2& x2) {
39 if (x1.val_ <= x2) {
40 if (x1.val_ < INFTY && x1.val_ == x2) {
41 if (x2 == NEGATIVE_INFTY) {
42 return fvar<T1>(NEGATIVE_INFTY, x1.d_);
43 }
44 return fvar<T1>(NEGATIVE_INFTY, x1.d_ * INFTY);
45 }
47 }
48 return fvar<T1>(log_diff_exp(x1.val_, x2), -x1.d_ / expm1(x2 - x1.val_));
49}
50} // namespace math
51} // namespace stan
52#endif
static constexpr double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition constants.hpp:56
fvar< T > expm1(const fvar< T > &x)
Definition expm1.hpp:13
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
fvar< T > log_diff_exp(const fvar< T > &x1, const fvar< T > &x2)
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
Scalar val_
The value of this variable.
Definition fvar.hpp:49
Scalar d_
The tangent (derivative) of this variable.
Definition fvar.hpp:61
This template class represents scalars used in forward-mode automatic differentiation,...
Definition fvar.hpp:40