Skip to content

Commit 53692ee

Browse files
steppimdhaber
andcommitted
MAINT: special: Add log1mexp for log(1 - exp(x)) (#22439)
* MAINT: special: add private function _log1mexp * TST: special: add tests for log1mexp * MAINT: stats: replace Python _log1mexp with ufunc --------- Co-authored-by: Matt Haberland <[email protected]>
1 parent dc9d2c6 commit 53692ee

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

include/xsf/log_exp.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cmath>
44

55
#include "config.h"
6+
#include "error.h"
67

78
namespace xsf {
89

@@ -65,4 +66,22 @@ T log_expit(T x) {
6566
return -std::log1p(std::exp(-x));
6667
};
6768

69+
70+
/* Compute log(1 - exp(x)). */
71+
template <typename T>
72+
T log1mexp(T x) {
73+
if (x > 0) {
74+
set_error("_log1mexp", SF_ERROR_DOMAIN, NULL);
75+
return std::numeric_limits<T>::quiet_NaN();
76+
}
77+
if (x == 0) {
78+
set_error("_log1mexp", SF_ERROR_SINGULAR, NULL);
79+
return -std::numeric_limits<T>::infinity();
80+
}
81+
if (x < -1) {
82+
return std::log1p(-std::exp(x));
83+
}
84+
return std::log(-std::expm1(x));
85+
}
86+
6887
} // namespace xsf

0 commit comments

Comments
 (0)