| Alternative 1 | |
|---|---|
| Accuracy | 90.8% |
| 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.9999999998) (fabs (- (/ 1.5 (/ (pow x 3.0) (pow p 3.0))) (/ 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)))) <= -0.9999999998) {
tmp = fabs(((1.5 / (pow(x, 3.0) / pow(p, 3.0))) - (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)))) <= -0.9999999998) {
tmp = Math.abs(((1.5 / (Math.pow(x, 3.0) / Math.pow(p, 3.0))) - (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)))) <= -0.9999999998: tmp = math.fabs(((1.5 / (math.pow(x, 3.0) / math.pow(p, 3.0))) - (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)))) <= -0.9999999998) tmp = abs(Float64(Float64(1.5 / Float64((x ^ 3.0) / (p ^ 3.0))) - Float64(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)))) <= -0.9999999998) tmp = abs(((1.5 / ((x ^ 3.0) / (p ^ 3.0))) - (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], -0.9999999998], N[Abs[N[(N[(1.5 / N[(N[Power[x, 3.0], $MachinePrecision] / N[Power[p, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(p / x), $MachinePrecision]), $MachinePrecision]], $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 -0.9999999998:\\
\;\;\;\;\left|\frac{1.5}{\frac{{x}^{3}}{{p}^{3}}} - \frac{p}{x}\right|\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\
\end{array}
Results
| Original | 78.6% |
|---|---|
| Target | 78.6% |
| Herbie | 98.0% |
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -0.9999999998Initial program 15.7%
Applied egg-rr15.7%
[Start]15.7 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\] |
|---|---|
add-sqr-sqrt [=>]15.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 [=>]15.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* [=>]15.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 [=>]15.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 [=>]15.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 [<=]8.3 | \[ \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 [<=]15.7 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{p}, x\right)}\right)}
\] |
Applied egg-rr13.8%
[Start]15.7 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}
\] |
|---|---|
add-cbrt-cube [=>]15.7 | \[ \color{blue}{\sqrt[3]{\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}}}
\] |
pow1/3 [=>]15.7 | \[ \color{blue}{{\left(\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}\right)}^{0.3333333333333333}}
\] |
Simplified13.8%
[Start]13.8 | \[ {\left({\left(0.5 + x \cdot \frac{0.5}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}^{1.5}\right)}^{0.3333333333333333}
\] |
|---|---|
unpow1/3 [=>]13.8 | \[ \color{blue}{\sqrt[3]{{\left(0.5 + x \cdot \frac{0.5}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}^{1.5}}}
\] |
*-commutative [=>]13.8 | \[ \sqrt[3]{{\left(0.5 + x \cdot \frac{0.5}{\mathsf{hypot}\left(\color{blue}{p \cdot 2}, x\right)}\right)}^{1.5}}
\] |
Taylor expanded in x around -inf 42.5%
Simplified52.0%
[Start]42.5 | \[ -1 \cdot \frac{p}{x} + -0.25 \cdot \frac{-2 \cdot {p}^{4} + -4 \cdot {p}^{4}}{p \cdot {x}^{3}}
\] |
|---|---|
fma-def [=>]42.5 | \[ \color{blue}{\mathsf{fma}\left(-1, \frac{p}{x}, -0.25 \cdot \frac{-2 \cdot {p}^{4} + -4 \cdot {p}^{4}}{p \cdot {x}^{3}}\right)}
\] |
associate-/r* [=>]52.0 | \[ \mathsf{fma}\left(-1, \frac{p}{x}, -0.25 \cdot \color{blue}{\frac{\frac{-2 \cdot {p}^{4} + -4 \cdot {p}^{4}}{p}}{{x}^{3}}}\right)
\] |
distribute-rgt-out [=>]52.0 | \[ \mathsf{fma}\left(-1, \frac{p}{x}, -0.25 \cdot \frac{\frac{\color{blue}{{p}^{4} \cdot \left(-2 + -4\right)}}{p}}{{x}^{3}}\right)
\] |
metadata-eval [=>]52.0 | \[ \mathsf{fma}\left(-1, \frac{p}{x}, -0.25 \cdot \frac{\frac{{p}^{4} \cdot \color{blue}{-6}}{p}}{{x}^{3}}\right)
\] |
Applied egg-rr45.1%
[Start]52.0 | \[ \mathsf{fma}\left(-1, \frac{p}{x}, -0.25 \cdot \frac{\frac{{p}^{4} \cdot -6}{p}}{{x}^{3}}\right)
\] |
|---|---|
add-sqr-sqrt [=>]49.6 | \[ \color{blue}{\sqrt{\mathsf{fma}\left(-1, \frac{p}{x}, -0.25 \cdot \frac{\frac{{p}^{4} \cdot -6}{p}}{{x}^{3}}\right)} \cdot \sqrt{\mathsf{fma}\left(-1, \frac{p}{x}, -0.25 \cdot \frac{\frac{{p}^{4} \cdot -6}{p}}{{x}^{3}}\right)}}
\] |
sqrt-unprod [=>]55.5 | \[ \color{blue}{\sqrt{\mathsf{fma}\left(-1, \frac{p}{x}, -0.25 \cdot \frac{\frac{{p}^{4} \cdot -6}{p}}{{x}^{3}}\right) \cdot \mathsf{fma}\left(-1, \frac{p}{x}, -0.25 \cdot \frac{\frac{{p}^{4} \cdot -6}{p}}{{x}^{3}}\right)}}
\] |
pow2 [=>]55.5 | \[ \sqrt{\color{blue}{{\left(\mathsf{fma}\left(-1, \frac{p}{x}, -0.25 \cdot \frac{\frac{{p}^{4} \cdot -6}{p}}{{x}^{3}}\right)\right)}^{2}}}
\] |
*-commutative [=>]55.5 | \[ \sqrt{{\left(\mathsf{fma}\left(-1, \frac{p}{x}, \color{blue}{\frac{\frac{{p}^{4} \cdot -6}{p}}{{x}^{3}} \cdot -0.25}\right)\right)}^{2}}
\] |
associate-/l/ [=>]45.9 | \[ \sqrt{{\left(\mathsf{fma}\left(-1, \frac{p}{x}, \color{blue}{\frac{{p}^{4} \cdot -6}{{x}^{3} \cdot p}} \cdot -0.25\right)\right)}^{2}}
\] |
*-commutative [=>]45.9 | \[ \sqrt{{\left(\mathsf{fma}\left(-1, \frac{p}{x}, \frac{\color{blue}{-6 \cdot {p}^{4}}}{{x}^{3} \cdot p} \cdot -0.25\right)\right)}^{2}}
\] |
associate-/l* [=>]46.0 | \[ \sqrt{{\left(\mathsf{fma}\left(-1, \frac{p}{x}, \color{blue}{\frac{-6}{\frac{{x}^{3} \cdot p}{{p}^{4}}}} \cdot -0.25\right)\right)}^{2}}
\] |
associate-/r/ [=>]45.1 | \[ \sqrt{{\left(\mathsf{fma}\left(-1, \frac{p}{x}, \color{blue}{\left(\frac{-6}{{x}^{3} \cdot p} \cdot {p}^{4}\right)} \cdot -0.25\right)\right)}^{2}}
\] |
*-commutative [=>]45.1 | \[ \sqrt{{\left(\mathsf{fma}\left(-1, \frac{p}{x}, \left(\frac{-6}{\color{blue}{p \cdot {x}^{3}}} \cdot {p}^{4}\right) \cdot -0.25\right)\right)}^{2}}
\] |
Simplified92.2%
[Start]45.1 | \[ \sqrt{{\left(\mathsf{fma}\left(-1, \frac{p}{x}, \left(\frac{-6}{p \cdot {x}^{3}} \cdot {p}^{4}\right) \cdot -0.25\right)\right)}^{2}}
\] |
|---|---|
unpow2 [=>]45.1 | \[ \sqrt{\color{blue}{\mathsf{fma}\left(-1, \frac{p}{x}, \left(\frac{-6}{p \cdot {x}^{3}} \cdot {p}^{4}\right) \cdot -0.25\right) \cdot \mathsf{fma}\left(-1, \frac{p}{x}, \left(\frac{-6}{p \cdot {x}^{3}} \cdot {p}^{4}\right) \cdot -0.25\right)}}
\] |
rem-sqrt-square [=>]71.6 | \[ \color{blue}{\left|\mathsf{fma}\left(-1, \frac{p}{x}, \left(\frac{-6}{p \cdot {x}^{3}} \cdot {p}^{4}\right) \cdot -0.25\right)\right|}
\] |
fma-udef [=>]71.6 | \[ \left|\color{blue}{-1 \cdot \frac{p}{x} + \left(\frac{-6}{p \cdot {x}^{3}} \cdot {p}^{4}\right) \cdot -0.25}\right|
\] |
+-commutative [=>]71.6 | \[ \left|\color{blue}{\left(\frac{-6}{p \cdot {x}^{3}} \cdot {p}^{4}\right) \cdot -0.25 + -1 \cdot \frac{p}{x}}\right|
\] |
if -0.9999999998 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 99.9%
Applied egg-rr99.9%
[Start]99.9 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\] |
|---|---|
add-sqr-sqrt [=>]99.9 | \[ \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.9 | \[ \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.9 | \[ \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.9 | \[ \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.9 | \[ \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.1 | \[ \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.9 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{p}, x\right)}\right)}
\] |
Final simplification98.0%
| Alternative 1 | |
|---|---|
| Accuracy | 90.8% |
| Cost | 20612 |
| Alternative 2 | |
|---|---|
| Accuracy | 67.7% |
| Cost | 7636 |
| Alternative 3 | |
|---|---|
| Accuracy | 67.8% |
| Cost | 7256 |
| Alternative 4 | |
|---|---|
| Accuracy | 67.6% |
| Cost | 6860 |
| Alternative 5 | |
|---|---|
| Accuracy | 26.3% |
| Cost | 388 |
| Alternative 6 | |
|---|---|
| Accuracy | 16.9% |
| Cost | 192 |
herbie shell --seed 2023135
(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))))))))