| Alternative 1 | |
|---|---|
| Accuracy | 83.4% |
| Cost | 13316 |
(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 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (* (pow (/ -0.25 re) 0.5) (pow (/ -1.0 im) -1.0)) (sqrt (* 0.5 (+ re (hypot re im))))))
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 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
tmp = pow((-0.25 / re), 0.5) * pow((-1.0 / im), -1.0);
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
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 * (re + Math.sqrt(((re * re) + (im * im)))))) <= 0.0) {
tmp = Math.pow((-0.25 / re), 0.5) * Math.pow((-1.0 / im), -1.0);
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
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 * (re + math.sqrt(((re * re) + (im * im)))))) <= 0.0: tmp = math.pow((-0.25 / re), 0.5) * math.pow((-1.0 / im), -1.0) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) 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(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))))) <= 0.0) tmp = Float64((Float64(-0.25 / re) ^ 0.5) * (Float64(-1.0 / im) ^ -1.0)); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); 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 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) tmp = ((-0.25 / re) ^ 0.5) * ((-1.0 / im) ^ -1.0); else tmp = sqrt((0.5 * (re + hypot(re, im)))); 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[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(N[Power[N[(-0.25 / re), $MachinePrecision], 0.5], $MachinePrecision] * N[Power[N[(-1.0 / im), $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $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(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;{\left(\frac{-0.25}{re}\right)}^{0.5} \cdot {\left(\frac{-1}{im}\right)}^{-1}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
Results
| Original | 39.0% |
|---|---|
| Target | 46.9% |
| Herbie | 84.7% |
if (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 13.6%
Simplified13.6%
[Start]13.6 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]13.6 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]13.6 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
Taylor expanded in re around -inf 51.2%
Simplified51.2%
[Start]51.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}
\] |
|---|---|
*-commutative [=>]51.2 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\frac{{im}^{2}}{re} \cdot -0.5\right)}}
\] |
unpow2 [=>]51.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)}
\] |
Applied egg-rr56.9%
[Start]51.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}
\] |
|---|---|
add-sqr-sqrt [=>]51.0 | \[ \color{blue}{\sqrt{0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}} \cdot \sqrt{0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}}}
\] |
pow1/2 [=>]51.0 | \[ \color{blue}{{\left(0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}\right)}^{0.5}} \cdot \sqrt{0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}}
\] |
pow1/2 [=>]51.0 | \[ {\left(0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}\right)}^{0.5} \cdot \color{blue}{{\left(0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}\right)}^{0.5}}
\] |
pow-prod-down [=>]51.2 | \[ \color{blue}{{\left(\left(0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}\right) \cdot \left(0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}\right)\right)}^{0.5}}
\] |
Simplified56.9%
[Start]56.9 | \[ {\left(\left(\left(\frac{im}{re} \cdot im\right) \cdot -1\right) \cdot 0.25\right)}^{0.5}
\] |
|---|---|
unpow1/2 [=>]56.9 | \[ \color{blue}{\sqrt{\left(\left(\frac{im}{re} \cdot im\right) \cdot -1\right) \cdot 0.25}}
\] |
associate-*l* [=>]56.9 | \[ \sqrt{\color{blue}{\left(\frac{im}{re} \cdot im\right) \cdot \left(-1 \cdot 0.25\right)}}
\] |
metadata-eval [=>]56.9 | \[ \sqrt{\left(\frac{im}{re} \cdot im\right) \cdot \color{blue}{-0.25}}
\] |
metadata-eval [<=]56.9 | \[ \sqrt{\left(\frac{im}{re} \cdot im\right) \cdot \color{blue}{\frac{-0.5}{2}}}
\] |
associate-*l* [=>]56.9 | \[ \sqrt{\color{blue}{\frac{im}{re} \cdot \left(im \cdot \frac{-0.5}{2}\right)}}
\] |
metadata-eval [=>]56.9 | \[ \sqrt{\frac{im}{re} \cdot \left(im \cdot \color{blue}{-0.25}\right)}
\] |
Applied egg-rr53.7%
[Start]56.9 | \[ \sqrt{\frac{im}{re} \cdot \left(im \cdot -0.25\right)}
\] |
|---|---|
add-exp-log [=>]54.0 | \[ \color{blue}{e^{\log \left(\sqrt{\frac{im}{re} \cdot \left(im \cdot -0.25\right)}\right)}}
\] |
pow1/2 [=>]54.0 | \[ e^{\log \color{blue}{\left({\left(\frac{im}{re} \cdot \left(im \cdot -0.25\right)\right)}^{0.5}\right)}}
\] |
log-pow [=>]54.0 | \[ e^{\color{blue}{0.5 \cdot \log \left(\frac{im}{re} \cdot \left(im \cdot -0.25\right)\right)}}
\] |
exp-prod [=>]53.7 | \[ \color{blue}{{\left(e^{0.5}\right)}^{\log \left(\frac{im}{re} \cdot \left(im \cdot -0.25\right)\right)}}
\] |
*-commutative [=>]53.7 | \[ {\left(e^{0.5}\right)}^{\log \color{blue}{\left(\left(im \cdot -0.25\right) \cdot \frac{im}{re}\right)}}
\] |
associate-*l* [=>]53.7 | \[ {\left(e^{0.5}\right)}^{\log \color{blue}{\left(im \cdot \left(-0.25 \cdot \frac{im}{re}\right)\right)}}
\] |
Taylor expanded in im around -inf 53.7%
Simplified63.8%
[Start]53.7 | \[ e^{0.5 \cdot \left(-2 \cdot \log \left(\frac{-1}{im}\right) + \log \left(\frac{-0.25}{re}\right)\right)}
\] |
|---|---|
+-commutative [=>]53.7 | \[ e^{0.5 \cdot \color{blue}{\left(\log \left(\frac{-0.25}{re}\right) + -2 \cdot \log \left(\frac{-1}{im}\right)\right)}}
\] |
distribute-rgt-in [=>]53.7 | \[ e^{\color{blue}{\log \left(\frac{-0.25}{re}\right) \cdot 0.5 + \left(-2 \cdot \log \left(\frac{-1}{im}\right)\right) \cdot 0.5}}
\] |
exp-sum [=>]53.5 | \[ \color{blue}{e^{\log \left(\frac{-0.25}{re}\right) \cdot 0.5} \cdot e^{\left(-2 \cdot \log \left(\frac{-1}{im}\right)\right) \cdot 0.5}}
\] |
exp-to-pow [=>]53.6 | \[ \color{blue}{{\left(\frac{-0.25}{re}\right)}^{0.5}} \cdot e^{\left(-2 \cdot \log \left(\frac{-1}{im}\right)\right) \cdot 0.5}
\] |
*-commutative [=>]53.6 | \[ {\left(\frac{-0.25}{re}\right)}^{0.5} \cdot e^{\color{blue}{\left(\log \left(\frac{-1}{im}\right) \cdot -2\right)} \cdot 0.5}
\] |
associate-*l* [=>]53.6 | \[ {\left(\frac{-0.25}{re}\right)}^{0.5} \cdot e^{\color{blue}{\log \left(\frac{-1}{im}\right) \cdot \left(-2 \cdot 0.5\right)}}
\] |
metadata-eval [=>]53.6 | \[ {\left(\frac{-0.25}{re}\right)}^{0.5} \cdot e^{\log \left(\frac{-1}{im}\right) \cdot \color{blue}{-1}}
\] |
exp-to-pow [=>]63.8 | \[ {\left(\frac{-0.25}{re}\right)}^{0.5} \cdot \color{blue}{{\left(\frac{-1}{im}\right)}^{-1}}
\] |
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 44.9%
Simplified86.9%
[Start]44.9 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]44.9 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]86.9 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
Applied egg-rr86.9%
[Start]86.9 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}
\] |
|---|---|
add-sqr-sqrt [=>]86.3 | \[ \color{blue}{\sqrt{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \cdot \sqrt{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}}}
\] |
pow1/2 [=>]86.3 | \[ \color{blue}{{\left(0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\right)}^{0.5}} \cdot \sqrt{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}}
\] |
pow1/2 [=>]86.3 | \[ {\left(0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\right)}^{0.5} \cdot \color{blue}{{\left(0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\right)}^{0.5}}
\] |
pow-prod-down [=>]86.9 | \[ \color{blue}{{\left(\left(0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\right) \cdot \left(0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\right)\right)}^{0.5}}
\] |
*-commutative [=>]86.9 | \[ {\left(\color{blue}{\left(\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} \cdot 0.5\right)} \cdot \left(0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\right)\right)}^{0.5}
\] |
*-commutative [=>]86.9 | \[ {\left(\left(\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} \cdot 0.5\right) \cdot \color{blue}{\left(\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} \cdot 0.5\right)}\right)}^{0.5}
\] |
swap-sqr [=>]86.9 | \[ {\color{blue}{\left(\left(\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\right) \cdot \left(0.5 \cdot 0.5\right)\right)}}^{0.5}
\] |
add-sqr-sqrt [<=]86.9 | \[ {\left(\color{blue}{\left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)} \cdot \left(0.5 \cdot 0.5\right)\right)}^{0.5}
\] |
*-commutative [=>]86.9 | \[ {\left(\color{blue}{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)} \cdot \left(0.5 \cdot 0.5\right)\right)}^{0.5}
\] |
metadata-eval [=>]86.9 | \[ {\left(\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot \color{blue}{0.25}\right)}^{0.5}
\] |
Simplified86.9%
[Start]86.9 | \[ {\left(\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot 0.25\right)}^{0.5}
\] |
|---|---|
unpow1/2 [=>]86.9 | \[ \color{blue}{\sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot 0.25}}
\] |
associate-*l* [=>]86.9 | \[ \sqrt{\color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot \left(2 \cdot 0.25\right)}}
\] |
metadata-eval [=>]86.9 | \[ \sqrt{\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot \color{blue}{0.5}}
\] |
Final simplification83.8%
| Alternative 1 | |
|---|---|
| Accuracy | 83.4% |
| Cost | 13316 |
| Alternative 2 | |
|---|---|
| Accuracy | 58.3% |
| Cost | 6984 |
| Alternative 3 | |
|---|---|
| Accuracy | 57.9% |
| Cost | 6856 |
| Alternative 4 | |
|---|---|
| Accuracy | 42.8% |
| Cost | 6724 |
| Alternative 5 | |
|---|---|
| Accuracy | 26.7% |
| Cost | 6464 |
herbie shell --seed 2023157
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:herbie-target
(if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))