| Alternative 1 | |
|---|---|
| Error | 98.9% |
| Cost | 25924.00 |
\[\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0:\\
\;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\
\mathbf{else}:\\
\;\;\;\;\log \left(e^{a} + e^{b}\right)\\
\end{array}
\]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.0) (/ b (+ 2.0 (expm1 a))) (log1p (+ (exp a) (expm1 b)))))
double code(double a, double b) {
return log((exp(a) + exp(b)));
}
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.0) {
tmp = b / (2.0 + expm1(a));
} else {
tmp = log1p((exp(a) + expm1(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 (Math.exp(a) <= 0.0) {
tmp = b / (2.0 + Math.expm1(a));
} else {
tmp = Math.log1p((Math.exp(a) + Math.expm1(b)));
}
return tmp;
}
def code(a, b): return math.log((math.exp(a) + math.exp(b)))
def code(a, b): tmp = 0 if math.exp(a) <= 0.0: tmp = b / (2.0 + math.expm1(a)) else: tmp = math.log1p((math.exp(a) + math.expm1(b))) return tmp
function code(a, b) return log(Float64(exp(a) + exp(b))) end
function code(a, b) tmp = 0.0 if (exp(a) <= 0.0) tmp = Float64(b / Float64(2.0 + expm1(a))); else tmp = log1p(Float64(exp(a) + expm1(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[N[Exp[a], $MachinePrecision], 0.0], N[(b / N[(2.0 + N[(Exp[a] - 1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[1 + N[(N[Exp[a], $MachinePrecision] + N[(Exp[b] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\log \left(e^{a} + e^{b}\right)
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0:\\
\;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right)\\
\end{array}
Results
if (exp.f64 a) < 0.0Initial program 9.3
Taylor expanded in b around 0 100.0
Simplified100.0
[Start]100.0 | \[ \log \left(1 + e^{a}\right) + \frac{b}{1 + e^{a}}
\] |
|---|---|
log1p-def [=>]100.0 | \[ \color{blue}{\mathsf{log1p}\left(e^{a}\right)} + \frac{b}{1 + e^{a}}
\] |
Taylor expanded in b around inf 100.0
Simplified100.0
[Start]100.0 | \[ \frac{b}{1 + e^{a}}
\] |
|---|---|
+-commutative [=>]100.0 | \[ \frac{b}{\color{blue}{e^{a} + 1}}
\] |
metadata-eval [<=]100.0 | \[ \frac{b}{e^{a} + \color{blue}{\left(2 - 1\right)}}
\] |
associate--l+ [<=]100.0 | \[ \frac{b}{\color{blue}{\left(e^{a} + 2\right) - 1}}
\] |
+-commutative [<=]100.0 | \[ \frac{b}{\color{blue}{\left(2 + e^{a}\right)} - 1}
\] |
associate--l+ [=>]100.0 | \[ \frac{b}{\color{blue}{2 + \left(e^{a} - 1\right)}}
\] |
expm1-def [=>]100.0 | \[ \frac{b}{2 + \color{blue}{\mathsf{expm1}\left(a\right)}}
\] |
if 0.0 < (exp.f64 a) Initial program 97.8
Applied egg-rr96.4
Simplified98.1
[Start]96.4 | \[ \log \left(\sqrt{e^{a} + e^{b}}\right) + \log \left(\sqrt{e^{a} + e^{b}}\right)
\] |
|---|---|
log-prod [<=]95.6 | \[ \color{blue}{\log \left(\sqrt{e^{a} + e^{b}} \cdot \sqrt{e^{a} + e^{b}}\right)}
\] |
rem-square-sqrt [=>]97.8 | \[ \log \color{blue}{\left(e^{a} + e^{b}\right)}
\] |
log1p-expm1 [<=]97.8 | \[ \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log \left(e^{a} + e^{b}\right)\right)\right)}
\] |
expm1-def [<=]97.8 | \[ \mathsf{log1p}\left(\color{blue}{e^{\log \left(e^{a} + e^{b}\right)} - 1}\right)
\] |
rem-exp-log [=>]97.8 | \[ \mathsf{log1p}\left(\color{blue}{\left(e^{a} + e^{b}\right)} - 1\right)
\] |
associate--l+ [=>]98.1 | \[ \mathsf{log1p}\left(\color{blue}{e^{a} + \left(e^{b} - 1\right)}\right)
\] |
expm1-def [=>]98.1 | \[ \mathsf{log1p}\left(e^{a} + \color{blue}{\mathsf{expm1}\left(b\right)}\right)
\] |
Final simplification99.1
| Alternative 1 | |
|---|---|
| Error | 98.9% |
| Cost | 25924.00 |
| Alternative 2 | |
|---|---|
| Error | 98.3% |
| Cost | 20036.00 |
| Alternative 3 | |
|---|---|
| Error | 98.1% |
| Cost | 19652.00 |
| Alternative 4 | |
|---|---|
| Error | 98.4% |
| Cost | 19648.00 |
| Alternative 5 | |
|---|---|
| Error | 97.6% |
| Cost | 19396.00 |
| Alternative 6 | |
|---|---|
| Error | 97.6% |
| Cost | 19396.00 |
| Alternative 7 | |
|---|---|
| Error | 57.1% |
| Cost | 6852.00 |
| Alternative 8 | |
|---|---|
| Error | 97.4% |
| Cost | 6852.00 |
| Alternative 9 | |
|---|---|
| Error | 56.5% |
| Cost | 6724.00 |
| Alternative 10 | |
|---|---|
| Error | 56.0% |
| Cost | 6596.00 |
| Alternative 11 | |
|---|---|
| Error | 12.0% |
| Cost | 192.00 |
herbie shell --seed 2023097
(FPCore (a b)
:name "symmetry log of sum of exp"
:precision binary64
(log (+ (exp a) (exp b))))