| Alternative 1 | |
|---|---|
| Accuracy | 76.0% |
| Cost | 7428 |
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im) :precision binary64 (if (<= (- (sqrt (+ (* re re) (* im im))) re) 0.0) (* 0.5 (* im (pow re -0.5))) (* 0.5 (sqrt (* 2.0 (- (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(((re * re) + (im * im))) - re) <= 0.0) {
tmp = 0.5 * (im * pow(re, -0.5));
} else {
tmp = 0.5 * sqrt((2.0 * (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(((re * re) + (im * im))) - re) <= 0.0) {
tmp = 0.5 * (im * Math.pow(re, -0.5));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (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(((re * re) + (im * im))) - re) <= 0.0: tmp = 0.5 * (im * math.pow(re, -0.5)) else: tmp = 0.5 * math.sqrt((2.0 * (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 (Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re) <= 0.0) tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * 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(((re * re) + (im * im))) - re) <= 0.0) tmp = 0.5 * (im * (re ^ -0.5)); else tmp = 0.5 * sqrt((2.0 * (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[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision], 0.0], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $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{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\end{array}
Results
if (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0Initial program 7.0%
Simplified17.8%
[Start]7.0 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
|---|---|
metadata-eval [<=]7.0 | \[ 0.5 \cdot \sqrt{\color{blue}{\left(2 \cdot 1\right)} \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
metadata-eval [<=]7.0 | \[ 0.5 \cdot \sqrt{\left(2 \cdot \color{blue}{\left(--1\right)}\right) \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
associate-*r* [<=]7.0 | \[ 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\left(--1\right) \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)\right)}}
\] |
metadata-eval [=>]7.0 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{1} \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)\right)}
\] |
*-lft-identity [=>]7.0 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right)}}
\] |
hypot-def [=>]17.8 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)}
\] |
Taylor expanded in re around inf 43.6%
Simplified43.6%
[Start]43.6 | \[ 0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{{im}^{2}}{re}\right)}
\] |
|---|---|
associate-*r/ [=>]43.6 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}}
\] |
associate-/l* [=>]43.0 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5}{\frac{re}{{im}^{2}}}}}
\] |
associate-/r/ [=>]43.6 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\frac{0.5}{re} \cdot {im}^{2}\right)}}
\] |
unpow2 [=>]43.6 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{0.5}{re} \cdot \color{blue}{\left(im \cdot im\right)}\right)}
\] |
Taylor expanded in im around 0 90.5%
Applied egg-rr50.6%
[Start]90.5 | \[ 0.5 \cdot \left(\sqrt{\frac{1}{re}} \cdot im\right)
\] |
|---|---|
expm1-log1p-u [=>]88.0 | \[ 0.5 \cdot \left(\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\frac{1}{re}}\right)\right)} \cdot im\right)
\] |
expm1-udef [=>]50.6 | \[ 0.5 \cdot \left(\color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{\frac{1}{re}}\right)} - 1\right)} \cdot im\right)
\] |
inv-pow [=>]50.6 | \[ 0.5 \cdot \left(\left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{{re}^{-1}}}\right)} - 1\right) \cdot im\right)
\] |
metadata-eval [<=]50.6 | \[ 0.5 \cdot \left(\left(e^{\mathsf{log1p}\left(\sqrt{{re}^{\color{blue}{\left(-1\right)}}}\right)} - 1\right) \cdot im\right)
\] |
sqrt-pow1 [=>]50.6 | \[ 0.5 \cdot \left(\left(e^{\mathsf{log1p}\left(\color{blue}{{re}^{\left(\frac{-1}{2}\right)}}\right)} - 1\right) \cdot im\right)
\] |
metadata-eval [=>]50.6 | \[ 0.5 \cdot \left(\left(e^{\mathsf{log1p}\left({re}^{\left(\frac{\color{blue}{-1}}{2}\right)}\right)} - 1\right) \cdot im\right)
\] |
metadata-eval [=>]50.6 | \[ 0.5 \cdot \left(\left(e^{\mathsf{log1p}\left({re}^{\color{blue}{-0.5}}\right)} - 1\right) \cdot im\right)
\] |
Simplified90.5%
[Start]50.6 | \[ 0.5 \cdot \left(\left(e^{\mathsf{log1p}\left({re}^{-0.5}\right)} - 1\right) \cdot im\right)
\] |
|---|---|
expm1-def [=>]88.0 | \[ 0.5 \cdot \left(\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left({re}^{-0.5}\right)\right)} \cdot im\right)
\] |
expm1-log1p [=>]90.5 | \[ 0.5 \cdot \left(\color{blue}{{re}^{-0.5}} \cdot im\right)
\] |
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 46.2%
Simplified90.3%
[Start]46.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
|---|---|
metadata-eval [<=]46.2 | \[ 0.5 \cdot \sqrt{\color{blue}{\left(2 \cdot 1\right)} \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
metadata-eval [<=]46.2 | \[ 0.5 \cdot \sqrt{\left(2 \cdot \color{blue}{\left(--1\right)}\right) \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
associate-*r* [<=]46.2 | \[ 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\left(--1\right) \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)\right)}}
\] |
metadata-eval [=>]46.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{1} \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)\right)}
\] |
*-lft-identity [=>]46.2 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right)}}
\] |
hypot-def [=>]90.3 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)}
\] |
Final simplification90.3%
| Alternative 1 | |
|---|---|
| Accuracy | 76.0% |
| Cost | 7428 |
| Alternative 2 | |
|---|---|
| Accuracy | 76.2% |
| Cost | 7112 |
| Alternative 3 | |
|---|---|
| Accuracy | 75.4% |
| Cost | 7048 |
| Alternative 4 | |
|---|---|
| Accuracy | 75.3% |
| Cost | 6984 |
| Alternative 5 | |
|---|---|
| Accuracy | 64.6% |
| Cost | 6852 |
| Alternative 6 | |
|---|---|
| Accuracy | 52.3% |
| Cost | 6720 |
herbie shell --seed 2023131
(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)))))