math.sqrt on complex, real part

?

Percentage Accurate: 41.9% → 83.4%
Time: 9.9s
Precision: binary64
Cost: 13444

?

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

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


\end{array}

Local Percentage Accuracy?

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 of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original41.9%
Target49.2%
Herbie83.4%
\[\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 < -2.70000000000000017e63

    1. Initial program 11.5%

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

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

      [Start]11.5

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

      +-commutative [=>]11.5

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

      hypot-def [=>]34.1

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

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

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

      [Start]62.0

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

      *-commutative [=>]62.0

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

      unpow2 [=>]62.0

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

      associate-/l* [=>]70.6

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

    if -2.70000000000000017e63 < re

    1. Initial program 43.5%

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

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

      [Start]43.5

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

      +-commutative [=>]43.5

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

      hypot-def [=>]87.6

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

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

Alternatives

Alternative 1
Accuracy54.9%
Cost14304
\[\begin{array}{l} t_0 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ t_1 := 0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{if}\;im \leq -5.5 \cdot 10^{-223}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{-285}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 1.65 \cdot 10^{-230}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 2.5 \cdot 10^{-113}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 1.1 \cdot 10^{-74}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 3.7 \cdot 10^{-35}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 7.8:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{elif}\;im \leq 1.26 \cdot 10^{+39}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{re + im} \cdot \sqrt{2}\right)\\ \end{array} \]
Alternative 2
Accuracy54.7%
Cost14176
\[\begin{array}{l} t_0 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ t_1 := 0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{if}\;im \leq -2.9 \cdot 10^{-223}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.55 \cdot 10^{-286}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 2.9 \cdot 10^{-230}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 4.3 \cdot 10^{-116}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 6.5 \cdot 10^{-75}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 8.2 \cdot 10^{-35}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 7.8:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{elif}\;im \leq 1.26 \cdot 10^{+39}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{im}\right)\\ \end{array} \]
Alternative 3
Accuracy55.0%
Cost7905
\[\begin{array}{l} t_0 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ t_1 := 0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{if}\;im \leq -5.6 \cdot 10^{-224}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 8.2 \cdot 10^{-285}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 1.6 \cdot 10^{-229}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 3.15 \cdot 10^{-117}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 1.32 \cdot 10^{-74}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 3.7 \cdot 10^{-35} \lor \neg \left(im \leq 7.8\right) \land im \leq 1.26 \cdot 10^{+39}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Accuracy54.8%
Cost7841
\[\begin{array}{l} t_0 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ t_1 := 0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{if}\;im \leq -3.5 \cdot 10^{-223}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 7.5 \cdot 10^{-285}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 1.3 \cdot 10^{-230}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 5.4 \cdot 10^{-114}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 1.9 \cdot 10^{-74}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 8.5 \cdot 10^{-34} \lor \neg \left(im \leq 7.4\right) \land im \leq 1.26 \cdot 10^{+39}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 5
Accuracy57.5%
Cost7249
\[\begin{array}{l} \mathbf{if}\;im \leq -6.2 \cdot 10^{-221}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 2.3 \cdot 10^{-64} \lor \neg \left(im \leq 3800000000\right) \land im \leq 7.2 \cdot 10^{+49}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 6
Accuracy52.8%
Cost6852
\[\begin{array}{l} \mathbf{if}\;im \leq -1 \cdot 10^{-309}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 7
Accuracy25.5%
Cost6720
\[0.5 \cdot \sqrt{im \cdot -2} \]

Reproduce?

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