| Alternative 1 | |
|---|---|
| Accuracy | 68.4% |
| Cost | 7124 |
(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)))) -1.0) (/ (- (sqrt (* p p))) x) (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* p 2.0) x)))))))
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)))) <= -1.0) {
tmp = -sqrt((p * p)) / x;
} else {
tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x)))));
}
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)))) <= -1.0) {
tmp = -Math.sqrt((p * p)) / x;
} else {
tmp = Math.sqrt((0.5 * (1.0 + (x / Math.hypot((p * 2.0), x)))));
}
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)))) <= -1.0: tmp = -math.sqrt((p * p)) / x else: tmp = math.sqrt((0.5 * (1.0 + (x / math.hypot((p * 2.0), x))))) 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)))) <= -1.0) tmp = Float64(Float64(-sqrt(Float64(p * p))) / x); else tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(p * 2.0), x))))); end return tmp end
function tmp = code(p, x) tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))); end
function tmp_2 = code(p, x) tmp = 0.0; if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) tmp = -sqrt((p * p)) / x; else tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x))))); end tmp_2 = 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], -1.0], N[((-N[Sqrt[N[(p * p), $MachinePrecision]], $MachinePrecision]) / x), $MachinePrecision], N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(p * 2.0), $MachinePrecision] ^ 2 + x ^ 2], $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 -1:\\
\;\;\;\;\frac{-\sqrt{p \cdot p}}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\
\end{array}
Results
| Original | 78.8% |
|---|---|
| Target | 78.8% |
| Herbie | 91.1% |
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1Initial program 15.0%
Taylor expanded in x around -inf 55.2%
Simplified55.2%
[Start]55.2 | \[ -1 \cdot \frac{\sqrt{2} \cdot \left(\sqrt{0.5} \cdot p\right)}{x}
\] |
|---|---|
mul-1-neg [=>]55.2 | \[ \color{blue}{-\frac{\sqrt{2} \cdot \left(\sqrt{0.5} \cdot p\right)}{x}}
\] |
associate-/l* [=>]55.0 | \[ -\color{blue}{\frac{\sqrt{2}}{\frac{x}{\sqrt{0.5} \cdot p}}}
\] |
associate-/r/ [=>]55.2 | \[ -\color{blue}{\frac{\sqrt{2}}{x} \cdot \left(\sqrt{0.5} \cdot p\right)}
\] |
distribute-lft-neg-in [=>]55.2 | \[ \color{blue}{\left(-\frac{\sqrt{2}}{x}\right) \cdot \left(\sqrt{0.5} \cdot p\right)}
\] |
*-commutative [=>]55.2 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot \color{blue}{\left(p \cdot \sqrt{0.5}\right)}
\] |
Applied egg-rr64.4%
[Start]55.2 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot \left(p \cdot \sqrt{0.5}\right)
\] |
|---|---|
add-sqr-sqrt [=>]48.2 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot \color{blue}{\left(\sqrt{p \cdot \sqrt{0.5}} \cdot \sqrt{p \cdot \sqrt{0.5}}\right)}
\] |
sqrt-unprod [=>]64.1 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot \color{blue}{\sqrt{\left(p \cdot \sqrt{0.5}\right) \cdot \left(p \cdot \sqrt{0.5}\right)}}
\] |
pow1/2 [=>]64.1 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot \color{blue}{{\left(\left(p \cdot \sqrt{0.5}\right) \cdot \left(p \cdot \sqrt{0.5}\right)\right)}^{0.5}}
\] |
*-commutative [=>]64.1 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot {\left(\color{blue}{\left(\sqrt{0.5} \cdot p\right)} \cdot \left(p \cdot \sqrt{0.5}\right)\right)}^{0.5}
\] |
*-commutative [=>]64.1 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot {\left(\left(\sqrt{0.5} \cdot p\right) \cdot \color{blue}{\left(\sqrt{0.5} \cdot p\right)}\right)}^{0.5}
\] |
swap-sqr [=>]64.0 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot {\color{blue}{\left(\left(\sqrt{0.5} \cdot \sqrt{0.5}\right) \cdot \left(p \cdot p\right)\right)}}^{0.5}
\] |
add-sqr-sqrt [<=]64.4 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot {\left(\color{blue}{0.5} \cdot \left(p \cdot p\right)\right)}^{0.5}
\] |
Simplified64.4%
[Start]64.4 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot {\left(0.5 \cdot \left(p \cdot p\right)\right)}^{0.5}
\] |
|---|---|
unpow1/2 [=>]64.4 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot \color{blue}{\sqrt{0.5 \cdot \left(p \cdot p\right)}}
\] |
associate-*r* [=>]64.4 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot \sqrt{\color{blue}{\left(0.5 \cdot p\right) \cdot p}}
\] |
*-commutative [=>]64.4 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot \sqrt{\color{blue}{p \cdot \left(0.5 \cdot p\right)}}
\] |
Applied egg-rr64.8%
[Start]64.4 | \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot \sqrt{p \cdot \left(0.5 \cdot p\right)}
\] |
|---|---|
distribute-lft-neg-out [=>]64.4 | \[ \color{blue}{-\frac{\sqrt{2}}{x} \cdot \sqrt{p \cdot \left(0.5 \cdot p\right)}}
\] |
neg-sub0 [=>]64.4 | \[ \color{blue}{0 - \frac{\sqrt{2}}{x} \cdot \sqrt{p \cdot \left(0.5 \cdot p\right)}}
\] |
associate-*l/ [=>]64.5 | \[ 0 - \color{blue}{\frac{\sqrt{2} \cdot \sqrt{p \cdot \left(0.5 \cdot p\right)}}{x}}
\] |
pow1/2 [=>]64.5 | \[ 0 - \frac{\color{blue}{{2}^{0.5}} \cdot \sqrt{p \cdot \left(0.5 \cdot p\right)}}{x}
\] |
pow1/2 [=>]64.5 | \[ 0 - \frac{{2}^{0.5} \cdot \color{blue}{{\left(p \cdot \left(0.5 \cdot p\right)\right)}^{0.5}}}{x}
\] |
pow-prod-down [=>]64.8 | \[ 0 - \frac{\color{blue}{{\left(2 \cdot \left(p \cdot \left(0.5 \cdot p\right)\right)\right)}^{0.5}}}{x}
\] |
*-commutative [=>]64.8 | \[ 0 - \frac{{\left(2 \cdot \left(p \cdot \color{blue}{\left(p \cdot 0.5\right)}\right)\right)}^{0.5}}{x}
\] |
Simplified64.8%
[Start]64.8 | \[ 0 - \frac{{\left(2 \cdot \left(p \cdot \left(p \cdot 0.5\right)\right)\right)}^{0.5}}{x}
\] |
|---|---|
neg-sub0 [<=]64.8 | \[ \color{blue}{-\frac{{\left(2 \cdot \left(p \cdot \left(p \cdot 0.5\right)\right)\right)}^{0.5}}{x}}
\] |
distribute-neg-frac [=>]64.8 | \[ \color{blue}{\frac{-{\left(2 \cdot \left(p \cdot \left(p \cdot 0.5\right)\right)\right)}^{0.5}}{x}}
\] |
unpow1/2 [=>]64.8 | \[ \frac{-\color{blue}{\sqrt{2 \cdot \left(p \cdot \left(p \cdot 0.5\right)\right)}}}{x}
\] |
*-commutative [=>]64.8 | \[ \frac{-\sqrt{\color{blue}{\left(p \cdot \left(p \cdot 0.5\right)\right) \cdot 2}}}{x}
\] |
associate-*r* [=>]64.8 | \[ \frac{-\sqrt{\color{blue}{\left(\left(p \cdot p\right) \cdot 0.5\right)} \cdot 2}}{x}
\] |
associate-*l* [=>]64.8 | \[ \frac{-\sqrt{\color{blue}{\left(p \cdot p\right) \cdot \left(0.5 \cdot 2\right)}}}{x}
\] |
metadata-eval [=>]64.8 | \[ \frac{-\sqrt{\left(p \cdot p\right) \cdot \color{blue}{1}}}{x}
\] |
*-rgt-identity [=>]64.8 | \[ \frac{-\sqrt{\color{blue}{p \cdot p}}}{x}
\] |
if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 99.7%
Applied egg-rr99.7%
[Start]99.7 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\] |
|---|---|
add-sqr-sqrt [=>]99.7 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\color{blue}{\sqrt{\left(4 \cdot p\right) \cdot p} \cdot \sqrt{\left(4 \cdot p\right) \cdot p}} + x \cdot x}}\right)}
\] |
hypot-def [=>]99.7 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{\mathsf{hypot}\left(\sqrt{\left(4 \cdot p\right) \cdot p}, x\right)}}\right)}
\] |
associate-*l* [=>]99.7 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\sqrt{\color{blue}{4 \cdot \left(p \cdot p\right)}}, x\right)}\right)}
\] |
sqrt-prod [=>]99.7 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{\sqrt{4} \cdot \sqrt{p \cdot p}}, x\right)}\right)}
\] |
metadata-eval [=>]99.7 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{2} \cdot \sqrt{p \cdot p}, x\right)}\right)}
\] |
sqrt-unprod [<=]49.6 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{\left(\sqrt{p} \cdot \sqrt{p}\right)}, x\right)}\right)}
\] |
add-sqr-sqrt [<=]99.7 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{p}, x\right)}\right)}
\] |
Final simplification91.1%
| Alternative 1 | |
|---|---|
| Accuracy | 68.4% |
| Cost | 7124 |
| Alternative 2 | |
|---|---|
| Accuracy | 67.9% |
| Cost | 6860 |
| Alternative 3 | |
|---|---|
| Accuracy | 27.0% |
| Cost | 388 |
| Alternative 4 | |
|---|---|
| Accuracy | 16.9% |
| Cost | 192 |
herbie shell --seed 2023151
(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))))))))