Automatic Differentiation
 
Loading...
Searching...
No Matches
log_diff_exp.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_LOG_DIFF_EXP_HPP
2#define STAN_MATH_REV_FUN_LOG_DIFF_EXP_HPP
3
9
10namespace stan {
11namespace math {
12
13namespace internal {
15 public:
17 : op_vv_vari(log_diff_exp(avi->val_, bvi->val_), avi, bvi) {}
18 void chain() {
19 avi_->adj_ -= adj_ / expm1(bvi_->val_ - avi_->val_);
20 bvi_->adj_ -= adj_ / expm1(avi_->val_ - bvi_->val_);
21 }
22};
24 public:
25 log_diff_exp_vd_vari(vari* avi, double b)
26 : op_vd_vari(log_diff_exp(avi->val_, b), avi, b) {}
27 void chain() {
28 if (val_ == NEGATIVE_INFTY) {
29 avi_->adj_ += (bd_ == NEGATIVE_INFTY) ? adj_ : adj_ * INFTY;
30 } else {
31 avi_->adj_ -= adj_ / expm1(bd_ - avi_->val_);
32 }
33 }
34};
36 public:
37 log_diff_exp_dv_vari(double a, vari* bvi)
38 : op_dv_vari(log_diff_exp(a, bvi->val_), a, bvi) {}
39 void chain() {
40 if (val_ == NEGATIVE_INFTY) {
41 bvi_->adj_ -= adj_ * INFTY;
42 } else {
43 bvi_->adj_ -= adj_ / expm1(ad_ - bvi_->val_);
44 }
45 }
46};
47} // namespace internal
48
56inline var log_diff_exp(const var& a, const var& b) {
57 return var(new internal::log_diff_exp_vv_vari(a.vi_, b.vi_));
58}
59
67inline var log_diff_exp(const var& a, double b) {
68 return var(new internal::log_diff_exp_vd_vari(a.vi_, b));
69}
70
78inline var log_diff_exp(double a, const var& b) {
79 return var(new internal::log_diff_exp_dv_vari(a, b.vi_));
80}
81
82} // namespace math
83} // namespace stan
84#endif
fvar< T > expm1(const fvar< T > &x)
Definition expm1.hpp:14
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
fvar< T > log_diff_exp(const fvar< T > &x1, const fvar< T > &x2)
var_value< double > var
Definition var.hpp:1187
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 ...