We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
log1mexp
log(1 - exp(x))
1 parent dc9d2c6 commit 53692eeCopy full SHA for 53692ee
include/xsf/log_exp.h
@@ -3,6 +3,7 @@
3
#include <cmath>
4
5
#include "config.h"
6
+#include "error.h"
7
8
namespace xsf {
9
@@ -65,4 +66,22 @@ T log_expit(T x) {
65
66
return -std::log1p(std::exp(-x));
67
};
68
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
87
} // namespace xsf
0 commit comments