| Alternative 1 | |
|---|---|
| Error | 0.9 |
| Cost | 19648 |
\[\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1}
\]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
(FPCore (a b) :precision binary64 (if (<= a -38.0) (/ b (+ 2.0 (expm1 a))) (log (+ (exp a) (exp b)))))
double code(double a, double b) {
return log((exp(a) + exp(b)));
}
double code(double a, double b) {
double tmp;
if (a <= -38.0) {
tmp = b / (2.0 + expm1(a));
} else {
tmp = log((exp(a) + exp(b)));
}
return tmp;
}
public static double code(double a, double b) {
return Math.log((Math.exp(a) + Math.exp(b)));
}
public static double code(double a, double b) {
double tmp;
if (a <= -38.0) {
tmp = b / (2.0 + Math.expm1(a));
} else {
tmp = Math.log((Math.exp(a) + Math.exp(b)));
}
return tmp;
}
def code(a, b): return math.log((math.exp(a) + math.exp(b)))
def code(a, b): tmp = 0 if a <= -38.0: tmp = b / (2.0 + math.expm1(a)) else: tmp = math.log((math.exp(a) + math.exp(b))) return tmp
function code(a, b) return log(Float64(exp(a) + exp(b))) end
function code(a, b) tmp = 0.0 if (a <= -38.0) tmp = Float64(b / Float64(2.0 + expm1(a))); else tmp = log(Float64(exp(a) + exp(b))); end return tmp end
code[a_, b_] := N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[a_, b_] := If[LessEqual[a, -38.0], N[(b / N[(2.0 + N[(Exp[a] - 1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\log \left(e^{a} + e^{b}\right)
\begin{array}{l}
\mathbf{if}\;a \leq -38:\\
\;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\
\mathbf{else}:\\
\;\;\;\;\log \left(e^{a} + e^{b}\right)\\
\end{array}
Results
if a < -38Initial program 58.2
Taylor expanded in b around 0 0.1
Simplified0
[Start]0.1 | \[ \log \left(1 + e^{a}\right) + \frac{b}{1 + e^{a}}
\] |
|---|---|
log1p-def [=>]0 | \[ \color{blue}{\mathsf{log1p}\left(e^{a}\right)} + \frac{b}{1 + e^{a}}
\] |
Taylor expanded in b around inf 0.1
Simplified0.1
[Start]0.1 | \[ \frac{b}{1 + e^{a}}
\] |
|---|---|
+-commutative [=>]0.1 | \[ \frac{b}{\color{blue}{e^{a} + 1}}
\] |
metadata-eval [<=]0.1 | \[ \frac{b}{e^{a} + \color{blue}{\left(2 - 1\right)}}
\] |
associate--l+ [<=]0.1 | \[ \frac{b}{\color{blue}{\left(e^{a} + 2\right) - 1}}
\] |
+-commutative [<=]0.1 | \[ \frac{b}{\color{blue}{\left(2 + e^{a}\right)} - 1}
\] |
associate--l+ [=>]0.1 | \[ \frac{b}{\color{blue}{2 + \left(e^{a} - 1\right)}}
\] |
expm1-def [=>]0.1 | \[ \frac{b}{2 + \color{blue}{\mathsf{expm1}\left(a\right)}}
\] |
if -38 < a Initial program 1.3
Final simplification0.7
| Alternative 1 | |
|---|---|
| Error | 0.9 |
| Cost | 19648 |
| Alternative 2 | |
|---|---|
| Error | 1.5 |
| Cost | 19396 |
| Alternative 3 | |
|---|---|
| Error | 1.2 |
| Cost | 19392 |
| Alternative 4 | |
|---|---|
| Error | 1.4 |
| Cost | 13636 |
| Alternative 5 | |
|---|---|
| Error | 1.0 |
| Cost | 13636 |
| Alternative 6 | |
|---|---|
| Error | 1.8 |
| Cost | 7108 |
| Alternative 7 | |
|---|---|
| Error | 27.6 |
| Cost | 6852 |
| Alternative 8 | |
|---|---|
| Error | 1.8 |
| Cost | 6852 |
| Alternative 9 | |
|---|---|
| Error | 27.6 |
| Cost | 6724 |
| Alternative 10 | |
|---|---|
| Error | 27.9 |
| Cost | 6596 |
| Alternative 11 | |
|---|---|
| Error | 56.3 |
| Cost | 192 |
herbie shell --seed 2023040
(FPCore (a b)
:name "symmetry log of sum of exp"
:precision binary64
(log (+ (exp a) (exp b))))