1 #ifndef STAN_MATH_REV_FUN_LOG1M_EXP_HPP
2 #define STAN_MATH_REV_FUN_LOG1M_EXP_HPP
3 
4 #include <stan/math/rev/meta.hpp>
5 #include <stan/math/rev/core.hpp>
6 #include <stan/math/prim/fun/expm1.hpp>
7 #include <stan/math/prim/fun/log1m_exp.hpp>
8 
9 namespace stan {
10 namespace math {
11 
12 /**
13  * Return the log of 1 minus the exponential of the specified
14  * variable.
15  *
16  * <p>The derivative of <code>log(1 - exp(x))</code> with respect
17  * to <code>x</code> is <code>-1 / expm1(-x)</code>.
18  *
19  * @tparam T Arithmetic or a type inheriting from `EigenBase`.
20  * @param[in] x Argument.
21  * @return Natural logarithm of one minus the exponential of the
22  * argument.
23  */
24 template <typename T, require_stan_scalar_or_eigen_t<T>* = nullptr>
log1m_exp(const var_value<T> & x)25 inline auto log1m_exp(const var_value<T>& x) {
26   return make_callback_var(log1m_exp(x.val()), [x](auto& vi) mutable {
27     as_array_or_scalar(x.adj())
28         -= as_array_or_scalar(vi.adj()) / as_array_or_scalar(expm1(-x.val()));
29   });
30 }
31 
32 }  // namespace math
33 }  // namespace stan
34 #endif
35