math.sqrt on complex, real part

?

Percentage Accurate: 41.7% → 84.0%
Time: 8.2s
Precision: binary64
Cost: 46916

?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} t_0 := {\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}\\ \mathbf{if}\;re \leq -1.26 \cdot 10^{+128}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(-0.125, \frac{im \cdot im}{re} \cdot \frac{t_0}{re}, t_0\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (pow (* (pow (* im im) 0.25) (pow (/ -1.0 re) 0.25)) 2.0)))
   (if (<= re -1.26e+128)
     (* 0.5 (fma -0.125 (* (/ (* im im) re) (/ t_0 re)) t_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 t_0 = pow((pow((im * im), 0.25) * pow((-1.0 / re), 0.25)), 2.0);
	double tmp;
	if (re <= -1.26e+128) {
		tmp = 0.5 * fma(-0.125, (((im * im) / re) * (t_0 / re)), t_0);
	} else {
		tmp = sqrt((0.5 * (re + 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)
	t_0 = Float64((Float64(im * im) ^ 0.25) * (Float64(-1.0 / re) ^ 0.25)) ^ 2.0
	tmp = 0.0
	if (re <= -1.26e+128)
		tmp = Float64(0.5 * fma(-0.125, Float64(Float64(Float64(im * im) / re) * Float64(t_0 / re)), t_0));
	else
		tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im))));
	end
	return 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_] := Block[{t$95$0 = N[Power[N[(N[Power[N[(im * im), $MachinePrecision], 0.25], $MachinePrecision] * N[Power[N[(-1.0 / re), $MachinePrecision], 0.25], $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]}, If[LessEqual[re, -1.26e+128], N[(0.5 * N[(-0.125 * N[(N[(N[(im * im), $MachinePrecision] / re), $MachinePrecision] * N[(t$95$0 / re), $MachinePrecision]), $MachinePrecision] + t$95$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}
t_0 := {\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}\\
\mathbf{if}\;re \leq -1.26 \cdot 10^{+128}:\\
\;\;\;\;0.5 \cdot \mathsf{fma}\left(-0.125, \frac{im \cdot im}{re} \cdot \frac{t_0}{re}, t_0\right)\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\


\end{array}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 9 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Bogosity?

Bogosity

Target

Original41.7%
Target48.8%
Herbie84.0%
\[\begin{array}{l} \mathbf{if}\;re < 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{\sqrt{re \cdot re + im \cdot im} - re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if re < -1.26000000000000009e128

    1. Initial program 3.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Simplified28.5%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
      Step-by-step derivation

      [Start]3.8%

      \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]

      +-commutative [=>]3.8%

      \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \]

      hypot-def [=>]28.5%

      \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Applied egg-rr28.6%

      \[\leadsto 0.5 \cdot \color{blue}{{\left({\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)}^{0.25}\right)}^{2}} \]
      Step-by-step derivation

      [Start]28.5%

      \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} \]

      add-sqr-sqrt [=>]28.6%

      \[ 0.5 \cdot \color{blue}{\left(\sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \cdot \sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}}\right)} \]

      pow2 [=>]28.6%

      \[ 0.5 \cdot \color{blue}{{\left(\sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}}\right)}^{2}} \]

      pow1/2 [=>]28.6%

      \[ 0.5 \cdot {\left(\sqrt{\color{blue}{{\left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}^{0.5}}}\right)}^{2} \]

      sqrt-pow1 [=>]28.6%

      \[ 0.5 \cdot {\color{blue}{\left({\left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}^{\left(\frac{0.5}{2}\right)}\right)}}^{2} \]

      *-commutative [=>]28.6%

      \[ 0.5 \cdot {\left({\color{blue}{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)}}^{\left(\frac{0.5}{2}\right)}\right)}^{2} \]

      metadata-eval [=>]28.6%

      \[ 0.5 \cdot {\left({\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)}^{\color{blue}{0.25}}\right)}^{2} \]
    4. Taylor expanded in re around -inf 76.3%

      \[\leadsto 0.5 \cdot \color{blue}{\left(-0.125 \cdot \frac{{\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2} \cdot {im}^{2}}{{re}^{2}} + {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right)} \]
    5. Simplified86.2%

      \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(-0.125, \frac{im \cdot im}{re} \cdot \frac{{\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}}{re}, {\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}\right)} \]
      Step-by-step derivation

      [Start]76.3%

      \[ 0.5 \cdot \left(-0.125 \cdot \frac{{\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2} \cdot {im}^{2}}{{re}^{2}} + {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right) \]

      fma-def [=>]76.3%

      \[ 0.5 \cdot \color{blue}{\mathsf{fma}\left(-0.125, \frac{{\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2} \cdot {im}^{2}}{{re}^{2}}, {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right)} \]

    if -1.26000000000000009e128 < re

    1. Initial program 50.0%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Simplified90.9%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
      Step-by-step derivation

      [Start]50.0%

      \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]

      +-commutative [=>]50.0%

      \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \]

      hypot-def [=>]90.9%

      \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Applied egg-rr90.9%

      \[\leadsto \color{blue}{\sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot 0.25}} \]
      Step-by-step derivation

      [Start]90.9%

      \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} \]

      add-sqr-sqrt [=>]90.2%

      \[ \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)}}} \]

      sqrt-unprod [=>]90.9%

      \[ \color{blue}{\sqrt{\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)}} \]

      *-commutative [=>]90.9%

      \[ \sqrt{\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)} \]

      *-commutative [=>]90.9%

      \[ \sqrt{\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)}} \]

      swap-sqr [=>]90.9%

      \[ \sqrt{\color{blue}{\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)}} \]

      add-sqr-sqrt [<=]90.9%

      \[ \sqrt{\color{blue}{\left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)} \cdot \left(0.5 \cdot 0.5\right)} \]

      *-commutative [=>]90.9%

      \[ \sqrt{\color{blue}{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)} \cdot \left(0.5 \cdot 0.5\right)} \]

      metadata-eval [=>]90.9%

      \[ \sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot \color{blue}{0.25}} \]
    4. Simplified90.9%

      \[\leadsto \color{blue}{\sqrt{\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 0.5}} \]
      Step-by-step derivation

      [Start]90.9%

      \[ \sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot 0.25} \]

      associate-*l* [=>]90.9%

      \[ \sqrt{\color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot \left(2 \cdot 0.25\right)}} \]

      metadata-eval [=>]90.9%

      \[ \sqrt{\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot \color{blue}{0.5}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification90.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.26 \cdot 10^{+128}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(-0.125, \frac{im \cdot im}{re} \cdot \frac{{\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}}{re}, {\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy84.0%
Cost46916
\[\begin{array}{l} t_0 := {\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}\\ \mathbf{if}\;re \leq -1.26 \cdot 10^{+128}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(-0.125, \frac{im \cdot im}{re} \cdot \frac{t_0}{re}, t_0\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 2
Accuracy84.3%
Cost20100
\[\begin{array}{l} \mathbf{if}\;re \leq -1.95 \cdot 10^{+101}:\\ \;\;\;\;0.5 \cdot {\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 3
Accuracy82.8%
Cost13316
\[\begin{array}{l} \mathbf{if}\;re \leq -5 \cdot 10^{+99}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 4
Accuracy57.9%
Cost7512
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{if}\;im \leq -4.5 \cdot 10^{+16}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1.55 \cdot 10^{-38}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq -1.6 \cdot 10^{-120}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -8.5 \cdot 10^{-287}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{-264}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 5.5 \cdot 10^{-153}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 5
Accuracy59.8%
Cost7376
\[\begin{array}{l} \mathbf{if}\;im \leq -1.16 \cdot 10^{-121}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -8 \cdot 10^{-287}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 1.5 \cdot 10^{-265}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 5.8 \cdot 10^{-153}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 6
Accuracy58.8%
Cost7248
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{if}\;im \leq -5.2 \cdot 10^{+17}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -6 \cdot 10^{-41}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq -5.5 \cdot 10^{-122}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 5.8 \cdot 10^{-153}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 7
Accuracy59.3%
Cost7248
\[\begin{array}{l} \mathbf{if}\;im \leq -2.35 \cdot 10^{-122}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -8 \cdot 10^{-287}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 2.3 \cdot 10^{-263}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 5.8 \cdot 10^{-153}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 8
Accuracy42.6%
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 4.5 \cdot 10^{-96}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \]
Alternative 9
Accuracy26.2%
Cost6464
\[\sqrt{re} \]

Reproduce?

herbie shell --seed 2023178 
(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)))))