| Alternative 1 | |
|---|---|
| Accuracy | 75.4% |
| Cost | 7249 |

(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))) 0.0) (/ 0.5 (/ (sqrt re) im)) (sqrt (* 0.5 (- (hypot re im) re)))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re)));
}
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
tmp = 0.5 / (sqrt(re) / im);
} else {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
}
return tmp;
}
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) - re)));
}
public static double code(double re, double im) {
double tmp;
if (Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
tmp = 0.5 / (Math.sqrt(re) / im);
} else {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
}
return tmp;
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re)))
def code(re, im): tmp = 0 if math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re))) <= 0.0: tmp = 0.5 / (math.sqrt(re) / im) else: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) return tmp
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re)))) end
function code(re, im) tmp = 0.0 if (sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re))) <= 0.0) tmp = Float64(0.5 / Float64(sqrt(re) / im)); else tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re))); end return tmp end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))); end
function tmp_2 = code(re, im) tmp = 0.0; if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) tmp = 0.5 / (sqrt(re) / im); else tmp = sqrt((0.5 * (hypot(re, im) - re))); end tmp_2 = tmp; end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[re_, im_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 / N[(N[Sqrt[re], $MachinePrecision] / im), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \leq 0:\\
\;\;\;\;\frac{0.5}{\frac{\sqrt{re}}{im}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\end{array}
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
if (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 5.4%
Taylor expanded in im around 0 98.5%
Simplified98.7%
[Start]98.5 | \[ 0.5 \cdot \left(\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}}\right)
\] |
|---|---|
associate-*l* [=>]98.7 | \[ 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \left(\left(\sqrt{0.5} \cdot im\right) \cdot \sqrt{\frac{1}{re}}\right)\right)}
\] |
*-commutative [=>]98.7 | \[ 0.5 \cdot \left(\sqrt{2} \cdot \left(\color{blue}{\left(im \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}\right)\right)
\] |
Applied egg-rr99.6%
[Start]98.7 | \[ 0.5 \cdot \left(\sqrt{2} \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)\right)
\] |
|---|---|
add-log-exp [=>]8.2 | \[ 0.5 \cdot \color{blue}{\log \left(e^{\sqrt{2} \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)}
\] |
*-un-lft-identity [=>]8.2 | \[ 0.5 \cdot \log \color{blue}{\left(1 \cdot e^{\sqrt{2} \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)}
\] |
log-prod [=>]8.2 | \[ 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{\sqrt{2} \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)\right)}
\] |
metadata-eval [=>]8.2 | \[ 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{\sqrt{2} \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)\right)
\] |
add-log-exp [<=]98.7 | \[ 0.5 \cdot \left(0 + \color{blue}{\sqrt{2} \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)
\] |
associate-*r* [=>]98.5 | \[ 0.5 \cdot \left(0 + \color{blue}{\left(\sqrt{2} \cdot \left(im \cdot \sqrt{0.5}\right)\right) \cdot \sqrt{\frac{1}{re}}}\right)
\] |
sqrt-div [=>]98.5 | \[ 0.5 \cdot \left(0 + \left(\sqrt{2} \cdot \left(im \cdot \sqrt{0.5}\right)\right) \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{re}}}\right)
\] |
metadata-eval [=>]98.5 | \[ 0.5 \cdot \left(0 + \left(\sqrt{2} \cdot \left(im \cdot \sqrt{0.5}\right)\right) \cdot \frac{\color{blue}{1}}{\sqrt{re}}\right)
\] |
un-div-inv [=>]98.5 | \[ 0.5 \cdot \left(0 + \color{blue}{\frac{\sqrt{2} \cdot \left(im \cdot \sqrt{0.5}\right)}{\sqrt{re}}}\right)
\] |
*-commutative [=>]98.5 | \[ 0.5 \cdot \left(0 + \frac{\sqrt{2} \cdot \color{blue}{\left(\sqrt{0.5} \cdot im\right)}}{\sqrt{re}}\right)
\] |
associate-*r* [=>]97.9 | \[ 0.5 \cdot \left(0 + \frac{\color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot im}}{\sqrt{re}}\right)
\] |
pow1/2 [=>]97.9 | \[ 0.5 \cdot \left(0 + \frac{\left(\color{blue}{{2}^{0.5}} \cdot \sqrt{0.5}\right) \cdot im}{\sqrt{re}}\right)
\] |
pow1/2 [=>]97.9 | \[ 0.5 \cdot \left(0 + \frac{\left({2}^{0.5} \cdot \color{blue}{{0.5}^{0.5}}\right) \cdot im}{\sqrt{re}}\right)
\] |
pow-prod-down [=>]99.6 | \[ 0.5 \cdot \left(0 + \frac{\color{blue}{{\left(2 \cdot 0.5\right)}^{0.5}} \cdot im}{\sqrt{re}}\right)
\] |
metadata-eval [=>]99.6 | \[ 0.5 \cdot \left(0 + \frac{{\color{blue}{1}}^{0.5} \cdot im}{\sqrt{re}}\right)
\] |
pow-to-exp [=>]99.6 | \[ 0.5 \cdot \left(0 + \frac{\color{blue}{e^{\log 1 \cdot 0.5}} \cdot im}{\sqrt{re}}\right)
\] |
metadata-eval [=>]99.6 | \[ 0.5 \cdot \left(0 + \frac{e^{\color{blue}{0} \cdot 0.5} \cdot im}{\sqrt{re}}\right)
\] |
metadata-eval [=>]99.6 | \[ 0.5 \cdot \left(0 + \frac{e^{\color{blue}{0}} \cdot im}{\sqrt{re}}\right)
\] |
1-exp [<=]99.6 | \[ 0.5 \cdot \left(0 + \frac{\color{blue}{1} \cdot im}{\sqrt{re}}\right)
\] |
*-un-lft-identity [<=]99.6 | \[ 0.5 \cdot \left(0 + \frac{\color{blue}{im}}{\sqrt{re}}\right)
\] |
Simplified99.6%
[Start]99.6 | \[ 0.5 \cdot \left(0 + \frac{im}{\sqrt{re}}\right)
\] |
|---|---|
+-lft-identity [=>]99.6 | \[ 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}}
\] |
Applied egg-rr99.7%
[Start]99.6 | \[ 0.5 \cdot \frac{im}{\sqrt{re}}
\] |
|---|---|
clear-num [=>]99.7 | \[ 0.5 \cdot \color{blue}{\frac{1}{\frac{\sqrt{re}}{im}}}
\] |
un-div-inv [=>]99.7 | \[ \color{blue}{\frac{0.5}{\frac{\sqrt{re}}{im}}}
\] |
if 0.0 < (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 42.8%
Simplified86.5%
[Start]42.8 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
|---|---|
hypot-def [=>]86.5 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)}
\] |
Applied egg-rr86.5%
[Start]86.5 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}
\] |
|---|---|
add-log-exp [=>]10.0 | \[ \color{blue}{\log \left(e^{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}}\right)}
\] |
*-un-lft-identity [=>]10.0 | \[ \log \color{blue}{\left(1 \cdot e^{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}}\right)}
\] |
log-prod [=>]10.0 | \[ \color{blue}{\log 1 + \log \left(e^{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}}\right)}
\] |
metadata-eval [=>]10.0 | \[ \color{blue}{0} + \log \left(e^{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}}\right)
\] |
add-log-exp [<=]86.5 | \[ 0 + \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}}
\] |
*-commutative [=>]86.5 | \[ 0 + \color{blue}{\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot 0.5}
\] |
*-commutative [=>]86.5 | \[ 0 + \sqrt{\color{blue}{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2}} \cdot 0.5
\] |
Simplified86.5%
[Start]86.5 | \[ 0 + \sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot 0.5
\] |
|---|---|
+-lft-identity [=>]86.5 | \[ \color{blue}{\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot 0.5}
\] |
rem-cube-cbrt [<=]84.8 | \[ \color{blue}{{\left(\sqrt[3]{\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot 0.5}\right)}^{3}}
\] |
sqr-pow [=>]84.7 | \[ \color{blue}{{\left(\sqrt[3]{\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot 0.5}\right)}^{\left(\frac{3}{2}\right)} \cdot {\left(\sqrt[3]{\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot 0.5}\right)}^{\left(\frac{3}{2}\right)}}
\] |
fabs-sqr [<=]84.7 | \[ \color{blue}{\left|{\left(\sqrt[3]{\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot 0.5}\right)}^{\left(\frac{3}{2}\right)} \cdot {\left(\sqrt[3]{\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot 0.5}\right)}^{\left(\frac{3}{2}\right)}\right|}
\] |
sqr-pow [<=]84.8 | \[ \left|\color{blue}{{\left(\sqrt[3]{\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot 0.5}\right)}^{3}}\right|
\] |
rem-cube-cbrt [=>]86.5 | \[ \left|\color{blue}{\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot 0.5}\right|
\] |
rem-sqrt-square [<=]86.5 | \[ \color{blue}{\sqrt{\left(\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot 0.5\right) \cdot \left(\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot 0.5\right)}}
\] |
swap-sqr [=>]86.5 | \[ \sqrt{\color{blue}{\left(\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2} \cdot \sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2}\right) \cdot \left(0.5 \cdot 0.5\right)}}
\] |
rem-square-sqrt [=>]86.5 | \[ \sqrt{\color{blue}{\left(\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2\right)} \cdot \left(0.5 \cdot 0.5\right)}
\] |
metadata-eval [=>]86.5 | \[ \sqrt{\left(\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2\right) \cdot \color{blue}{0.25}}
\] |
associate-*l* [=>]86.5 | \[ \sqrt{\color{blue}{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot \left(2 \cdot 0.25\right)}}
\] |
metadata-eval [=>]86.5 | \[ \sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot \color{blue}{0.5}}
\] |
*-commutative [=>]86.5 | \[ \sqrt{\color{blue}{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}}
\] |
Final simplification88.2%
| Alternative 1 | |
|---|---|
| Accuracy | 75.4% |
| Cost | 7249 |
| Alternative 2 | |
|---|---|
| Accuracy | 75.3% |
| Cost | 7248 |
| Alternative 3 | |
|---|---|
| Accuracy | 63.8% |
| Cost | 7116 |
| Alternative 4 | |
|---|---|
| Accuracy | 55.1% |
| Cost | 6720 |
| Alternative 5 | |
|---|---|
| Accuracy | 52.8% |
| Cost | 6592 |
herbie shell --seed 2023161
(FPCore (re im)
:name "math.sqrt on complex, imaginary part, im greater than 0 branch"
:precision binary64
:pre (> im 0.0)
(* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))