| Alternative 1 | |
|---|---|
| Accuracy | 68.2% |
| Cost | 7892 |
(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) (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.5) {
tmp = -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.5) {
tmp = -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.5: tmp = -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.5) tmp = Float64(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.5) tmp = -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.5], N[((-p) / 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 -0.5:\\
\;\;\;\;\frac{-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 | 80.0% |
|---|---|
| Target | 80.0% |
| Herbie | 89.5% |
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -0.5Initial program 17.1%
Taylor expanded in x around -inf 51.3%
Simplified62.4%
[Start]51.3 | \[ \sqrt{0.5 \cdot \left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}
\] |
|---|---|
unpow2 [=>]51.3 | \[ \sqrt{0.5 \cdot \left(2 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}}\right)}
\] |
unpow2 [=>]51.3 | \[ \sqrt{0.5 \cdot \left(2 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}}\right)}
\] |
times-frac [=>]62.4 | \[ \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)}
\] |
Applied egg-rr30.0%
[Start]62.4 | \[ \sqrt{0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}
\] |
|---|---|
*-un-lft-identity [=>]62.4 | \[ \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(1 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}\right)}
\] |
metadata-eval [<=]62.4 | \[ \sqrt{0.5 \cdot \left(2 \cdot \left(\color{blue}{\left(0.5 \cdot 2\right)} \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)\right)}
\] |
associate-*r* [<=]62.4 | \[ \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)\right)}\right)}
\] |
add-cbrt-cube [=>]30.0 | \[ \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\sqrt[3]{\left(\left(0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)\right) \cdot \left(0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)\right)\right) \cdot \left(0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)\right)}}\right)}
\] |
pow3 [=>]30.0 | \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{\color{blue}{{\left(0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)\right)}^{3}}}\right)}
\] |
associate-*r* [=>]30.0 | \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\color{blue}{\left(\left(0.5 \cdot 2\right) \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}}^{3}}\right)}
\] |
metadata-eval [=>]30.0 | \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\left(\color{blue}{1} \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}^{3}}\right)}
\] |
*-un-lft-identity [<=]30.0 | \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}}^{3}}\right)}
\] |
pow2 [=>]30.0 | \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\color{blue}{\left({\left(\frac{p}{x}\right)}^{2}\right)}}^{3}}\right)}
\] |
metadata-eval [<=]30.0 | \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\left({\left(\frac{p}{x}\right)}^{2}\right)}^{\color{blue}{\left(1 + 2\right)}}}\right)}
\] |
pow-pow [=>]30.0 | \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{\color{blue}{{\left(\frac{p}{x}\right)}^{\left(2 \cdot \left(1 + 2\right)\right)}}}\right)}
\] |
metadata-eval [=>]30.0 | \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\left(\frac{p}{x}\right)}^{\left(2 \cdot \color{blue}{3}\right)}}\right)}
\] |
metadata-eval [=>]30.0 | \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\left(\frac{p}{x}\right)}^{\color{blue}{6}}}\right)}
\] |
Taylor expanded in p around -inf 56.4%
Simplified56.4%
[Start]56.4 | \[ -1 \cdot \frac{p}{x}
\] |
|---|---|
associate-*r/ [=>]56.4 | \[ \color{blue}{\frac{-1 \cdot p}{x}}
\] |
neg-mul-1 [<=]56.4 | \[ \frac{\color{blue}{-p}}{x}
\] |
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)}
\] |
|---|---|
add-sqr-sqrt [=>]100.0 | \[ \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 [=>]100.0 | \[ \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* [=>]100.0 | \[ \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 [=>]100.0 | \[ \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 [=>]100.0 | \[ \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.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 [<=]100.0 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{p}, x\right)}\right)}
\] |
Final simplification89.5%
| Alternative 1 | |
|---|---|
| Accuracy | 68.2% |
| Cost | 7892 |
| Alternative 2 | |
|---|---|
| Accuracy | 68.1% |
| Cost | 7256 |
| Alternative 3 | |
|---|---|
| Accuracy | 68.7% |
| Cost | 6860 |
| Alternative 4 | |
|---|---|
| Accuracy | 25.9% |
| Cost | 388 |
| Alternative 5 | |
|---|---|
| Accuracy | 16.1% |
| Cost | 192 |
herbie shell --seed 2023136
(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))))))))