math.sqrt on complex, real part

?

Percentage Accurate: 41.5% → 84.5%
Time: 14.5s
Precision: binary64
Cost: 20228

?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} \mathbf{if}\;re + \sqrt{re \cdot re + im \cdot im} \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\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
 (if (<= (+ re (sqrt (+ (* re re) (* im im)))) 0.0)
   (* 0.5 (sqrt (* 2.0 (* (/ im (/ re im)) -0.5))))
   (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 ((re + sqrt(((re * re) + (im * im)))) <= 0.0) {
		tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5)));
	} 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 ((re + Math.sqrt(((re * re) + (im * im)))) <= 0.0) {
		tmp = 0.5 * Math.sqrt((2.0 * ((im / (re / im)) * -0.5)));
	} 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 (re + math.sqrt(((re * re) + (im * im)))) <= 0.0:
		tmp = 0.5 * math.sqrt((2.0 * ((im / (re / im)) * -0.5)))
	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 (Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))) <= 0.0)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(Float64(im / Float64(re / im)) * -0.5))));
	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 ((re + sqrt(((re * re) + (im * im)))) <= 0.0)
		tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5)));
	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[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[(im / N[(re / im), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]], $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}\;re + \sqrt{re \cdot re + im \cdot im} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\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 8 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

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original41.5%
Target48.9%
Herbie84.5%
\[\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 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0

    1. Initial program 9.3%

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

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

      [Start]9.3%

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

      +-commutative [=>]9.3%

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

      hypot-def [=>]15.5%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}} \]
    4. Simplified63.3%

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

      [Start]52.9%

      \[ 0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)} \]

      *-commutative [=>]52.9%

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

      unpow2 [=>]52.9%

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

      associate-/l* [=>]63.3%

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

    if 0.0 < (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)

    1. Initial program 48.7%

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

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

      [Start]48.7%

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

      +-commutative [=>]48.7%

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

      hypot-def [=>]91.3%

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

      \[\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]91.3%

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

      add-sqr-sqrt [=>]90.7%

      \[ \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 [=>]91.3%

      \[ \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 [=>]91.3%

      \[ \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 [=>]91.3%

      \[ \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 [=>]91.3%

      \[ \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 [<=]91.3%

      \[ \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 [=>]91.3%

      \[ \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 [=>]91.3%

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

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

      [Start]91.3%

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

      associate-*l* [=>]91.8%

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

      metadata-eval [=>]91.8%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re + \sqrt{re \cdot re + im \cdot im} \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy84.5%
Cost20228
\[\begin{array}{l} \mathbf{if}\;re + \sqrt{re \cdot re + im \cdot im} \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 2
Accuracy52.5%
Cost7496
\[\begin{array}{l} t_0 := \sqrt{0.5 \cdot \left(re - im\right)}\\ \mathbf{if}\;re \leq -1.8 \cdot 10^{-109}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\ \mathbf{elif}\;re \leq -7.2 \cdot 10^{-296}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{re \cdot re}{im} + 2 \cdot \left(re + im\right)}\\ \mathbf{elif}\;re \leq 8 \cdot 10^{-216}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2.65 \cdot 10^{-148}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\ \mathbf{elif}\;re \leq 2.25 \cdot 10^{-73}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \]
Alternative 3
Accuracy52.5%
Cost7380
\[\begin{array}{l} t_0 := \sqrt{0.5 \cdot \left(re - im\right)}\\ \mathbf{if}\;re \leq -1.95 \cdot 10^{-108}:\\ \;\;\;\;\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot -0.25}\\ \mathbf{elif}\;re \leq -5 \cdot 10^{-297}:\\ \;\;\;\;\sqrt{im \cdot 0.5}\\ \mathbf{elif}\;re \leq 2 \cdot 10^{-218}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 10^{-145}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\ \mathbf{elif}\;re \leq 3 \cdot 10^{-74}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \]
Alternative 4
Accuracy52.5%
Cost7380
\[\begin{array}{l} t_0 := \sqrt{0.5 \cdot \left(re - im\right)}\\ \mathbf{if}\;re \leq -2.05 \cdot 10^{-108}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\ \mathbf{elif}\;re \leq -5 \cdot 10^{-297}:\\ \;\;\;\;\sqrt{im \cdot 0.5}\\ \mathbf{elif}\;re \leq 1.05 \cdot 10^{-215}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2 \cdot 10^{-146}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\ \mathbf{elif}\;re \leq 1.25 \cdot 10^{-74}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \]
Alternative 5
Accuracy59.5%
Cost6856
\[\begin{array}{l} \mathbf{if}\;im \leq -1.7 \cdot 10^{-95}:\\ \;\;\;\;\sqrt{im \cdot -0.5}\\ \mathbf{elif}\;im \leq 1.2 \cdot 10^{-152}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{im \cdot 0.5}\\ \end{array} \]
Alternative 6
Accuracy59.9%
Cost6856
\[\begin{array}{l} \mathbf{if}\;im \leq -1.45 \cdot 10^{-95}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 8 \cdot 10^{-152}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{im \cdot 0.5}\\ \end{array} \]
Alternative 7
Accuracy52.5%
Cost6724
\[\begin{array}{l} \mathbf{if}\;im \leq -5 \cdot 10^{-310}:\\ \;\;\;\;\sqrt{im \cdot -0.5}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{im \cdot 0.5}\\ \end{array} \]
Alternative 8
Accuracy25.9%
Cost6592
\[\sqrt{im \cdot -0.5} \]

Reproduce?

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