| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 39240 |

(FPCore (x y) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
(FPCore (x y)
:precision binary64
(if (<= (* -2.0 x) -2.0)
(cbrt (pow (+ (/ 2.0 (+ (pow (exp x) -2.0) 1.0)) -1.0) 3.0))
(if (<= (* -2.0 x) 0.002)
(+
(* -0.3333333333333333 (pow x 3.0))
(+ x (* 0.13333333333333333 (pow x 5.0))))
(log (exp (expm1 (- (log 2.0) (log1p (exp (* -2.0 x))))))))))double code(double x, double y) {
return (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
}
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -2.0) {
tmp = cbrt(pow(((2.0 / (pow(exp(x), -2.0) + 1.0)) + -1.0), 3.0));
} else if ((-2.0 * x) <= 0.002) {
tmp = (-0.3333333333333333 * pow(x, 3.0)) + (x + (0.13333333333333333 * pow(x, 5.0)));
} else {
tmp = log(exp(expm1((log(2.0) - log1p(exp((-2.0 * x)))))));
}
return tmp;
}
public static double code(double x, double y) {
return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
public static double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -2.0) {
tmp = Math.cbrt(Math.pow(((2.0 / (Math.pow(Math.exp(x), -2.0) + 1.0)) + -1.0), 3.0));
} else if ((-2.0 * x) <= 0.002) {
tmp = (-0.3333333333333333 * Math.pow(x, 3.0)) + (x + (0.13333333333333333 * Math.pow(x, 5.0)));
} else {
tmp = Math.log(Math.exp(Math.expm1((Math.log(2.0) - Math.log1p(Math.exp((-2.0 * x)))))));
}
return tmp;
}
function code(x, y) return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0) end
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -2.0) tmp = cbrt((Float64(Float64(2.0 / Float64((exp(x) ^ -2.0) + 1.0)) + -1.0) ^ 3.0)); elseif (Float64(-2.0 * x) <= 0.002) tmp = Float64(Float64(-0.3333333333333333 * (x ^ 3.0)) + Float64(x + Float64(0.13333333333333333 * (x ^ 5.0)))); else tmp = log(exp(expm1(Float64(log(2.0) - log1p(exp(Float64(-2.0 * x))))))); end return tmp end
code[x_, y_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -2.0], N[Power[N[Power[N[(N[(2.0 / N[(N[Power[N[Exp[x], $MachinePrecision], -2.0], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], 3.0], $MachinePrecision], 1/3], $MachinePrecision], If[LessEqual[N[(-2.0 * x), $MachinePrecision], 0.002], N[(N[(-0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(x + N[(0.13333333333333333 * N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[N[Exp[N[(Exp[N[(N[Log[2.0], $MachinePrecision] - N[Log[1 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]]]
\frac{2}{1 + e^{-2 \cdot x}} - 1
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -2:\\
\;\;\;\;\sqrt[3]{{\left(\frac{2}{{\left(e^{x}\right)}^{-2} + 1} + -1\right)}^{3}}\\
\mathbf{elif}\;-2 \cdot x \leq 0.002:\\
\;\;\;\;-0.3333333333333333 \cdot {x}^{3} + \left(x + 0.13333333333333333 \cdot {x}^{5}\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(e^{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{-2 \cdot x}\right)\right)}\right)\\
\end{array}
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
if (*.f64 -2 x) < -2Initial program 100.0%
Applied egg-rr100.0%
[Start]100.0% | \[ \frac{2}{1 + e^{-2 \cdot x}} - 1
\] |
|---|---|
add-sqr-sqrt [=>]100.0% | \[ \color{blue}{\sqrt{\frac{2}{1 + e^{-2 \cdot x}} - 1} \cdot \sqrt{\frac{2}{1 + e^{-2 \cdot x}} - 1}}
\] |
pow2 [=>]100.0% | \[ \color{blue}{{\left(\sqrt{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)}^{2}}
\] |
add-exp-log [=>]100.0% | \[ {\left(\sqrt{\color{blue}{e^{\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)}} - 1}\right)}^{2}
\] |
expm1-def [=>]100.0% | \[ {\left(\sqrt{\color{blue}{\mathsf{expm1}\left(\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)\right)}}\right)}^{2}
\] |
log-div [=>]100.0% | \[ {\left(\sqrt{\mathsf{expm1}\left(\color{blue}{\log 2 - \log \left(1 + e^{-2 \cdot x}\right)}\right)}\right)}^{2}
\] |
log1p-udef [<=]100.0% | \[ {\left(\sqrt{\mathsf{expm1}\left(\log 2 - \color{blue}{\mathsf{log1p}\left(e^{-2 \cdot x}\right)}\right)}\right)}^{2}
\] |
exp-prod [=>]100.0% | \[ {\left(\sqrt{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(\color{blue}{{\left(e^{-2}\right)}^{x}}\right)\right)}\right)}^{2}
\] |
Taylor expanded in x around inf 100.0%
Simplified100.0%
[Start]100.0% | \[ {\left(\sqrt{\mathsf{expm1}\left(\log 2 - \log \left(1 + e^{-2 \cdot x}\right)\right)}\right)}^{2}
\] |
|---|---|
log1p-def [=>]100.0% | \[ {\left(\sqrt{\mathsf{expm1}\left(\log 2 - \color{blue}{\mathsf{log1p}\left(e^{-2 \cdot x}\right)}\right)}\right)}^{2}
\] |
*-commutative [=>]100.0% | \[ {\left(\sqrt{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{\color{blue}{x \cdot -2}}\right)\right)}\right)}^{2}
\] |
Applied egg-rr100.0%
[Start]100.0% | \[ {\left(\sqrt{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{x \cdot -2}\right)\right)}\right)}^{2}
\] |
|---|---|
unpow2 [=>]100.0% | \[ \color{blue}{\sqrt{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{x \cdot -2}\right)\right)} \cdot \sqrt{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{x \cdot -2}\right)\right)}}
\] |
add-sqr-sqrt [<=]100.0% | \[ \color{blue}{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{x \cdot -2}\right)\right)}
\] |
add-cbrt-cube [=>]100.0% | \[ \color{blue}{\sqrt[3]{\left(\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{x \cdot -2}\right)\right) \cdot \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{x \cdot -2}\right)\right)\right) \cdot \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{x \cdot -2}\right)\right)}}
\] |
pow3 [=>]100.0% | \[ \sqrt[3]{\color{blue}{{\left(\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{x \cdot -2}\right)\right)\right)}^{3}}}
\] |
expm1-udef [=>]100.0% | \[ \sqrt[3]{{\color{blue}{\left(e^{\log 2 - \mathsf{log1p}\left(e^{x \cdot -2}\right)} - 1\right)}}^{3}}
\] |
sub-neg [=>]100.0% | \[ \sqrt[3]{{\color{blue}{\left(e^{\log 2 - \mathsf{log1p}\left(e^{x \cdot -2}\right)} + \left(-1\right)\right)}}^{3}}
\] |
exp-diff [=>]100.0% | \[ \sqrt[3]{{\left(\color{blue}{\frac{e^{\log 2}}{e^{\mathsf{log1p}\left(e^{x \cdot -2}\right)}}} + \left(-1\right)\right)}^{3}}
\] |
add-exp-log [<=]100.0% | \[ \sqrt[3]{{\left(\frac{\color{blue}{2}}{e^{\mathsf{log1p}\left(e^{x \cdot -2}\right)}} + \left(-1\right)\right)}^{3}}
\] |
log1p-udef [=>]100.0% | \[ \sqrt[3]{{\left(\frac{2}{e^{\color{blue}{\log \left(1 + e^{x \cdot -2}\right)}}} + \left(-1\right)\right)}^{3}}
\] |
add-exp-log [<=]100.0% | \[ \sqrt[3]{{\left(\frac{2}{\color{blue}{1 + e^{x \cdot -2}}} + \left(-1\right)\right)}^{3}}
\] |
+-commutative [=>]100.0% | \[ \sqrt[3]{{\left(\frac{2}{\color{blue}{e^{x \cdot -2} + 1}} + \left(-1\right)\right)}^{3}}
\] |
exp-prod [=>]100.0% | \[ \sqrt[3]{{\left(\frac{2}{\color{blue}{{\left(e^{x}\right)}^{-2}} + 1} + \left(-1\right)\right)}^{3}}
\] |
metadata-eval [=>]100.0% | \[ \sqrt[3]{{\left(\frac{2}{{\left(e^{x}\right)}^{-2} + 1} + \color{blue}{-1}\right)}^{3}}
\] |
if -2 < (*.f64 -2 x) < 2e-3Initial program 9.2%
Taylor expanded in x around 0 100.0%
if 2e-3 < (*.f64 -2 x) Initial program 99.9%
Applied egg-rr100.0%
[Start]99.9% | \[ \frac{2}{1 + e^{-2 \cdot x}} - 1
\] |
|---|---|
add-log-exp [=>]99.9% | \[ \color{blue}{\log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)}
\] |
add-exp-log [=>]99.9% | \[ \log \left(e^{\color{blue}{e^{\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)}} - 1}\right)
\] |
expm1-def [=>]99.9% | \[ \log \left(e^{\color{blue}{\mathsf{expm1}\left(\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)\right)}}\right)
\] |
log-div [=>]99.9% | \[ \log \left(e^{\mathsf{expm1}\left(\color{blue}{\log 2 - \log \left(1 + e^{-2 \cdot x}\right)}\right)}\right)
\] |
log1p-udef [<=]100.0% | \[ \log \left(e^{\mathsf{expm1}\left(\log 2 - \color{blue}{\mathsf{log1p}\left(e^{-2 \cdot x}\right)}\right)}\right)
\] |
exp-prod [=>]100.0% | \[ \log \left(e^{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(\color{blue}{{\left(e^{-2}\right)}^{x}}\right)\right)}\right)
\] |
Taylor expanded in x around inf 99.9%
Simplified100.0%
[Start]0.0% | \[ {\left(\sqrt{\mathsf{expm1}\left(\log 2 - \log \left(1 + e^{-2 \cdot x}\right)\right)}\right)}^{2}
\] |
|---|---|
log1p-def [=>]0.0% | \[ {\left(\sqrt{\mathsf{expm1}\left(\log 2 - \color{blue}{\mathsf{log1p}\left(e^{-2 \cdot x}\right)}\right)}\right)}^{2}
\] |
*-commutative [=>]0.0% | \[ {\left(\sqrt{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{\color{blue}{x \cdot -2}}\right)\right)}\right)}^{2}
\] |
Final simplification100.0%
| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 39240 |
| Alternative 2 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 26436 |
| Alternative 3 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 20296 |
| Alternative 4 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 14025 |
| Alternative 5 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 7497 |
| Alternative 6 | |
|---|---|
| Accuracy | 99.7% |
| Cost | 7048 |
| Alternative 7 | |
|---|---|
| Accuracy | 99.4% |
| Cost | 328 |
| Alternative 8 | |
|---|---|
| Accuracy | 75.8% |
| Cost | 196 |
| Alternative 9 | |
|---|---|
| Accuracy | 27.6% |
| Cost | 64 |
herbie shell --seed 2023263
(FPCore (x y)
:name "Logistic function from Lakshay Garg"
:precision binary64
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))