| Alternative 1 | |
|---|---|
| Accuracy | 89.3% |
| Cost | 20612 |
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
(FPCore (p x) :precision binary64 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -0.5) (+ (/ p x) (* (pow (/ p x) 3.0) -1.5)) (exp (* 0.5 (log1p (+ -0.5 (* 0.5 (/ x (hypot x (* p 2.0))))))))))
double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
double code(double p, double x) {
double tmp;
if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5) {
tmp = (p / x) + (pow((p / x), 3.0) * -1.5);
} else {
tmp = exp((0.5 * log1p((-0.5 + (0.5 * (x / hypot(x, (p * 2.0))))))));
}
return tmp;
}
public static double code(double p, double x) {
return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
public static double code(double p, double x) {
double tmp;
if ((x / Math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5) {
tmp = (p / x) + (Math.pow((p / x), 3.0) * -1.5);
} else {
tmp = Math.exp((0.5 * Math.log1p((-0.5 + (0.5 * (x / Math.hypot(x, (p * 2.0))))))));
}
return tmp;
}
def code(p, x): return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
def code(p, x): tmp = 0 if (x / math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5: tmp = (p / x) + (math.pow((p / x), 3.0) * -1.5) else: tmp = math.exp((0.5 * math.log1p((-0.5 + (0.5 * (x / math.hypot(x, (p * 2.0)))))))) return tmp
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) end
function code(p, x) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -0.5) tmp = Float64(Float64(p / x) + Float64((Float64(p / x) ^ 3.0) * -1.5)); else tmp = exp(Float64(0.5 * log1p(Float64(-0.5 + Float64(0.5 * Float64(x / hypot(x, Float64(p * 2.0)))))))); end return tmp end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[p_, x_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p * N[(4.0 * p), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.5], N[(N[(p / x), $MachinePrecision] + N[(N[Power[N[(p / x), $MachinePrecision], 3.0], $MachinePrecision] * -1.5), $MachinePrecision]), $MachinePrecision], N[Exp[N[(0.5 * N[Log[1 + N[(-0.5 + N[(0.5 * N[(x / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.5:\\
\;\;\;\;\frac{p}{x} + {\left(\frac{p}{x}\right)}^{3} \cdot -1.5\\
\mathbf{else}:\\
\;\;\;\;e^{0.5 \cdot \mathsf{log1p}\left(-0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\right)}\\
\end{array}
Results
| Original | 79.3% |
|---|---|
| Target | 79.3% |
| Herbie | 89.3% |
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -0.5Initial program 17.0%
Applied egg-rr15.7%
[Start]17.0 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\] |
|---|---|
expm1-log1p-u [=>]17.0 | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)\right)}
\] |
expm1-udef [=>]17.0 | \[ \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)} - 1}
\] |
Simplified17.0%
[Start]15.7 | \[ e^{\mathsf{log1p}\left(\sqrt{0.5 + x \cdot \frac{0.5}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1
\] |
|---|---|
expm1-def [=>]15.7 | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{0.5 + x \cdot \frac{0.5}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)\right)}
\] |
expm1-log1p [=>]15.7 | \[ \color{blue}{\sqrt{0.5 + x \cdot \frac{0.5}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}}
\] |
associate-*r/ [=>]17.0 | \[ \sqrt{0.5 + \color{blue}{\frac{x \cdot 0.5}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}}
\] |
Taylor expanded in x around -inf 45.9%
Simplified45.9%
[Start]45.9 | \[ \sqrt{\frac{{p}^{2}}{{x}^{2}} + 0.5 \cdot \frac{-2 \cdot {p}^{4} + -4 \cdot {p}^{4}}{{x}^{4}}}
\] |
|---|---|
unpow2 [=>]45.9 | \[ \sqrt{\frac{\color{blue}{p \cdot p}}{{x}^{2}} + 0.5 \cdot \frac{-2 \cdot {p}^{4} + -4 \cdot {p}^{4}}{{x}^{4}}}
\] |
unpow2 [=>]45.9 | \[ \sqrt{\frac{p \cdot p}{\color{blue}{x \cdot x}} + 0.5 \cdot \frac{-2 \cdot {p}^{4} + -4 \cdot {p}^{4}}{{x}^{4}}}
\] |
distribute-rgt-out [=>]45.9 | \[ \sqrt{\frac{p \cdot p}{x \cdot x} + 0.5 \cdot \frac{\color{blue}{{p}^{4} \cdot \left(-2 + -4\right)}}{{x}^{4}}}
\] |
metadata-eval [=>]45.9 | \[ \sqrt{\frac{p \cdot p}{x \cdot x} + 0.5 \cdot \frac{{p}^{4} \cdot \color{blue}{-6}}{{x}^{4}}}
\] |
Taylor expanded in p around 0 52.4%
Simplified52.4%
[Start]52.4 | \[ \frac{p}{x} + -1.5 \cdot \frac{{p}^{3}}{{x}^{3}}
\] |
|---|---|
associate-*r/ [=>]52.4 | \[ \frac{p}{x} + \color{blue}{\frac{-1.5 \cdot {p}^{3}}{{x}^{3}}}
\] |
Applied egg-rr57.0%
[Start]52.4 | \[ \frac{p}{x} + \frac{-1.5 \cdot {p}^{3}}{{x}^{3}}
\] |
|---|---|
*-commutative [=>]52.4 | \[ \frac{p}{x} + \frac{\color{blue}{{p}^{3} \cdot -1.5}}{{x}^{3}}
\] |
associate-/l* [=>]52.4 | \[ \frac{p}{x} + \color{blue}{\frac{{p}^{3}}{\frac{{x}^{3}}{-1.5}}}
\] |
associate-/r/ [=>]52.4 | \[ \frac{p}{x} + \color{blue}{\frac{{p}^{3}}{{x}^{3}} \cdot -1.5}
\] |
cube-div [<=]57.0 | \[ \frac{p}{x} + \color{blue}{{\left(\frac{p}{x}\right)}^{3}} \cdot -1.5
\] |
if -0.5 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 100.0%
Applied egg-rr100.0%
[Start]100.0 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\] |
|---|---|
pow1/2 [=>]100.0 | \[ \color{blue}{{\left(0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)\right)}^{0.5}}
\] |
pow-to-exp [=>]100.0 | \[ \color{blue}{e^{\log \left(0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)\right) \cdot 0.5}}
\] |
Applied egg-rr100.0%
[Start]100.0 | \[ e^{\left(\log 0.5 + \mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)\right) \cdot 0.5}
\] |
|---|---|
log1p-expm1-u [=>]100.0 | \[ e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log 0.5 + \mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)\right)\right)} \cdot 0.5}
\] |
log1p-udef [=>]100.0 | \[ e^{\color{blue}{\log \left(1 + \mathsf{expm1}\left(\log 0.5 + \mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)\right)\right)} \cdot 0.5}
\] |
expm1-udef [=>]100.0 | \[ e^{\log \left(1 + \color{blue}{\left(e^{\log 0.5 + \mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)} - 1\right)}\right) \cdot 0.5}
\] |
exp-sum [=>]100.0 | \[ e^{\log \left(1 + \left(\color{blue}{e^{\log 0.5} \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)}} - 1\right)\right) \cdot 0.5}
\] |
add-exp-log [<=]100.0 | \[ e^{\log \left(1 + \left(\color{blue}{0.5} \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)} - 1\right)\right) \cdot 0.5}
\] |
log1p-udef [=>]100.0 | \[ e^{\log \left(1 + \left(0.5 \cdot e^{\color{blue}{\log \left(1 + \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)}} - 1\right)\right) \cdot 0.5}
\] |
add-exp-log [<=]100.0 | \[ e^{\log \left(1 + \left(0.5 \cdot \color{blue}{\left(1 + \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)} - 1\right)\right) \cdot 0.5}
\] |
distribute-lft-in [=>]100.0 | \[ e^{\log \left(1 + \left(\color{blue}{\left(0.5 \cdot 1 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)} - 1\right)\right) \cdot 0.5}
\] |
metadata-eval [=>]100.0 | \[ e^{\log \left(1 + \left(\left(\color{blue}{0.5} + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right) - 1\right)\right) \cdot 0.5}
\] |
Simplified100.0%
[Start]100.0 | \[ e^{\log \left(1 + \left(\left(0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right) - 1\right)\right) \cdot 0.5}
\] |
|---|---|
log1p-def [=>]100.0 | \[ e^{\color{blue}{\mathsf{log1p}\left(\left(0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right) - 1\right)} \cdot 0.5}
\] |
+-commutative [=>]100.0 | \[ e^{\mathsf{log1p}\left(\color{blue}{\left(0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} + 0.5\right)} - 1\right) \cdot 0.5}
\] |
associate--l+ [=>]100.0 | \[ e^{\mathsf{log1p}\left(\color{blue}{0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} + \left(0.5 - 1\right)}\right) \cdot 0.5}
\] |
*-commutative [=>]100.0 | \[ e^{\mathsf{log1p}\left(0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, \color{blue}{p \cdot 2}\right)} + \left(0.5 - 1\right)\right) \cdot 0.5}
\] |
metadata-eval [=>]100.0 | \[ e^{\mathsf{log1p}\left(0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)} + \color{blue}{-0.5}\right) \cdot 0.5}
\] |
Final simplification89.3%
| Alternative 1 | |
|---|---|
| Accuracy | 89.3% |
| Cost | 20612 |
| Alternative 2 | |
|---|---|
| Accuracy | 68.4% |
| Cost | 7388 |
| Alternative 3 | |
|---|---|
| Accuracy | 68.3% |
| Cost | 7388 |
| Alternative 4 | |
|---|---|
| Accuracy | 68.2% |
| Cost | 7388 |
| Alternative 5 | |
|---|---|
| Accuracy | 45.2% |
| Cost | 324 |
| Alternative 6 | |
|---|---|
| Accuracy | 36.4% |
| Cost | 64 |
herbie shell --seed 2023133
(FPCore (p x)
:name "Given's Rotation SVD example"
:precision binary64
:pre (and (< 1e-150 (fabs x)) (< (fabs x) 1e+150))
:herbie-target
(sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1.0 (/ (* 2.0 p) x)))))
(sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))