| Alternative 1 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 9952 |

(FPCore (x s) :precision binary32 (/ (exp (/ (- (fabs x)) s)) (* (* s (+ 1.0 (exp (/ (- (fabs x)) s)))) (+ 1.0 (exp (/ (- (fabs x)) s))))))
(FPCore (x s) :precision binary32 (/ 1.0 (* s (+ 2.0 (* 2.0 (expm1 (log1p (cosh (/ x s)))))))))
float code(float x, float s) {
return expf((-fabsf(x) / s)) / ((s * (1.0f + expf((-fabsf(x) / s)))) * (1.0f + expf((-fabsf(x) / s))));
}
float code(float x, float s) {
return 1.0f / (s * (2.0f + (2.0f * expm1f(log1pf(coshf((x / s)))))));
}
function code(x, s) return Float32(exp(Float32(Float32(-abs(x)) / s)) / Float32(Float32(s * Float32(Float32(1.0) + exp(Float32(Float32(-abs(x)) / s)))) * Float32(Float32(1.0) + exp(Float32(Float32(-abs(x)) / s))))) end
function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(2.0) + Float32(Float32(2.0) * expm1(log1p(cosh(Float32(x / s)))))))) end
\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}
\frac{1}{s \cdot \left(2 + 2 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\cosh \left(\frac{x}{s}\right)\right)\right)\right)}
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
Initial program 99.6%
Simplified99.8%
[Start]99.6% | \[ \frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}
\] |
|---|---|
*-lft-identity [<=]99.6% | \[ \color{blue}{1 \cdot \frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}}
\] |
associate-*r/ [=>]99.6% | \[ \color{blue}{\frac{1 \cdot e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}}
\] |
associate-*l* [=>]99.7% | \[ \frac{1 \cdot e^{\frac{-\left|x\right|}{s}}}{\color{blue}{s \cdot \left(\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)}}
\] |
times-frac [=>]99.7% | \[ \color{blue}{\frac{1}{s} \cdot \frac{e^{\frac{-\left|x\right|}{s}}}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}}
\] |
associate-*r/ [=>]99.7% | \[ \color{blue}{\frac{\frac{1}{s} \cdot e^{\frac{-\left|x\right|}{s}}}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}}
\] |
associate-/l* [=>]99.7% | \[ \color{blue}{\frac{\frac{1}{s}}{\frac{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}{e^{\frac{-\left|x\right|}{s}}}}}
\] |
distribute-frac-neg [=>]99.7% | \[ \frac{\frac{1}{s}}{\frac{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}{e^{\color{blue}{-\frac{\left|x\right|}{s}}}}}
\] |
exp-neg [=>]99.7% | \[ \frac{\frac{1}{s}}{\frac{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}{\color{blue}{\frac{1}{e^{\frac{\left|x\right|}{s}}}}}}
\] |
Taylor expanded in s around 0 99.8%
Simplified99.8%
[Start]99.8% | \[ \frac{1}{s \cdot \left(e^{-1 \cdot \frac{\left|x\right|}{s}} + \left(2 + e^{\frac{\left|x\right|}{s}}\right)\right)}
\] |
|---|---|
+-commutative [=>]99.8% | \[ \frac{1}{s \cdot \color{blue}{\left(\left(2 + e^{\frac{\left|x\right|}{s}}\right) + e^{-1 \cdot \frac{\left|x\right|}{s}}\right)}}
\] |
mul-1-neg [=>]99.8% | \[ \frac{1}{s \cdot \left(\left(2 + e^{\frac{\left|x\right|}{s}}\right) + e^{\color{blue}{-\frac{\left|x\right|}{s}}}\right)}
\] |
distribute-frac-neg [<=]99.8% | \[ \frac{1}{s \cdot \left(\left(2 + e^{\frac{\left|x\right|}{s}}\right) + e^{\color{blue}{\frac{-\left|x\right|}{s}}}\right)}
\] |
Applied egg-rr80.2%
[Start]99.8% | \[ \frac{1}{s \cdot \left(\left(2 + e^{\frac{\left|x\right|}{s}}\right) + e^{\frac{-\left|x\right|}{s}}\right)}
\] |
|---|---|
expm1-log1p-u [=>]99.8% | \[ \frac{1}{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(s \cdot \left(\left(2 + e^{\frac{\left|x\right|}{s}}\right) + e^{\frac{-\left|x\right|}{s}}\right)\right)\right)}}
\] |
expm1-udef [=>]80.2% | \[ \frac{1}{\color{blue}{e^{\mathsf{log1p}\left(s \cdot \left(\left(2 + e^{\frac{\left|x\right|}{s}}\right) + e^{\frac{-\left|x\right|}{s}}\right)\right)} - 1}}
\] |
associate-+l+ [=>]80.2% | \[ \frac{1}{e^{\mathsf{log1p}\left(s \cdot \color{blue}{\left(2 + \left(e^{\frac{\left|x\right|}{s}} + e^{\frac{-\left|x\right|}{s}}\right)\right)}\right)} - 1}
\] |
distribute-frac-neg [=>]80.2% | \[ \frac{1}{e^{\mathsf{log1p}\left(s \cdot \left(2 + \left(e^{\frac{\left|x\right|}{s}} + e^{\color{blue}{-\frac{\left|x\right|}{s}}}\right)\right)\right)} - 1}
\] |
cosh-undef [=>]80.2% | \[ \frac{1}{e^{\mathsf{log1p}\left(s \cdot \left(2 + \color{blue}{2 \cdot \cosh \left(\frac{\left|x\right|}{s}\right)}\right)\right)} - 1}
\] |
Simplified99.8%
[Start]80.2% | \[ \frac{1}{e^{\mathsf{log1p}\left(s \cdot \left(2 + 2 \cdot \cosh \left(\frac{\left|x\right|}{s}\right)\right)\right)} - 1}
\] |
|---|---|
expm1-def [=>]99.8% | \[ \frac{1}{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(s \cdot \left(2 + 2 \cdot \cosh \left(\frac{\left|x\right|}{s}\right)\right)\right)\right)}}
\] |
expm1-log1p [=>]99.8% | \[ \frac{1}{\color{blue}{s \cdot \left(2 + 2 \cdot \cosh \left(\frac{\left|x\right|}{s}\right)\right)}}
\] |
Applied egg-rr99.8%
[Start]99.8% | \[ \frac{1}{s \cdot \left(2 + 2 \cdot \cosh \left(\frac{\left|x\right|}{s}\right)\right)}
\] |
|---|---|
expm1-log1p-u [=>]99.8% | \[ \frac{1}{s \cdot \left(2 + 2 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cosh \left(\frac{\left|x\right|}{s}\right)\right)\right)}\right)}
\] |
add-sqr-sqrt [=>]47.2% | \[ \frac{1}{s \cdot \left(2 + 2 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\cosh \left(\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}\right)\right)\right)\right)}
\] |
fabs-sqr [=>]47.2% | \[ \frac{1}{s \cdot \left(2 + 2 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\cosh \left(\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}\right)\right)\right)\right)}
\] |
add-sqr-sqrt [<=]99.8% | \[ \frac{1}{s \cdot \left(2 + 2 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\cosh \left(\frac{\color{blue}{x}}{s}\right)\right)\right)\right)}
\] |
Final simplification99.8%
| Alternative 1 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 9952 |
| Alternative 2 | |
|---|---|
| Accuracy | 81.7% |
| Cost | 3684 |
| Alternative 3 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 3616 |
| Alternative 4 | |
|---|---|
| Accuracy | 99.5% |
| Cost | 3552 |
| Alternative 5 | |
|---|---|
| Accuracy | 76.1% |
| Cost | 416 |
| Alternative 6 | |
|---|---|
| Accuracy | 64.0% |
| Cost | 361 |
| Alternative 7 | |
|---|---|
| Accuracy | 64.0% |
| Cost | 360 |
| Alternative 8 | |
|---|---|
| Accuracy | 65.7% |
| Cost | 352 |
| Alternative 9 | |
|---|---|
| Accuracy | 62.8% |
| Cost | 297 |
| Alternative 10 | |
|---|---|
| Accuracy | 27.1% |
| Cost | 96 |
herbie shell --seed 2023165
(FPCore (x s)
:name "Logistic distribution"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ (exp (/ (- (fabs x)) s)) (* (* s (+ 1.0 (exp (/ (- (fabs x)) s)))) (+ 1.0 (exp (/ (- (fabs x)) s))))))