math.sqrt on complex, real part

?

Percentage Accurate: 42.9% → 83.0%
Time: 10.2s
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 -7.2 \cdot 10^{+46}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{im}{re} \cdot \left(-im\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 -7.2e+46)
   (* 0.5 (sqrt (* (/ im re) (- im))))
   (* 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 <= -7.2e+46) {
		tmp = 0.5 * sqrt(((im / re) * -im));
	} 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 <= -7.2e+46) {
		tmp = 0.5 * Math.sqrt(((im / re) * -im));
	} 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 <= -7.2e+46:
		tmp = 0.5 * math.sqrt(((im / re) * -im))
	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 <= -7.2e+46)
		tmp = Float64(0.5 * sqrt(Float64(Float64(im / re) * Float64(-im))));
	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 <= -7.2e+46)
		tmp = 0.5 * sqrt(((im / re) * -im));
	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, -7.2e+46], N[(0.5 * N[Sqrt[N[(N[(im / re), $MachinePrecision] * (-im)), $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 -7.2 \cdot 10^{+46}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im}{re} \cdot \left(-im\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 18 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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original42.9%
Target49.8%
Herbie83.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 < -7.1999999999999997e46

    1. Initial program 7.7%

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

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

      [Start]7.7

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

      +-commutative [=>]7.7

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

      hypot-def [=>]26.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 60.0%

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

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

      [Start]60.0

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

      *-commutative [=>]60.0

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

      unpow2 [=>]60.0

      \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)} \]
    5. Applied egg-rr68.3%

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

      [Start]60.0

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

      add-log-exp [=>]16.4

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

      *-un-lft-identity [=>]16.4

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

      log-prod [=>]16.4

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

      metadata-eval [=>]16.4

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

      add-log-exp [<=]60.0

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

      *-commutative [=>]60.0

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

      associate-*l* [=>]60.0

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

      associate-/l* [=>]68.3

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

      associate-/r/ [=>]68.3

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

      metadata-eval [=>]68.3

      \[ 0.5 \cdot \left(0 + \sqrt{\left(\frac{im}{re} \cdot im\right) \cdot \color{blue}{-1}}\right) \]
    6. Simplified68.3%

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

      [Start]68.3

      \[ 0.5 \cdot \left(0 + \sqrt{\left(\frac{im}{re} \cdot im\right) \cdot -1}\right) \]

      +-lft-identity [=>]68.3

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

      associate-*l* [=>]68.3

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

      *-commutative [<=]68.3

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

      neg-mul-1 [<=]68.3

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

    if -7.1999999999999997e46 < re

    1. Initial program 47.3%

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

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

      [Start]47.3

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

      +-commutative [=>]47.3

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

      hypot-def [=>]93.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 simplification89.0%

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

Alternatives

Alternative 1
Accuracy59.3%
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{if}\;im \leq -1.45 \cdot 10^{-138}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 1.1 \cdot 10^{-190}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 2.2 \cdot 10^{-168}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{im}{re} \cdot \left(-im\right)}\\ \mathbf{elif}\;im \leq 2.2 \cdot 10^{-14}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 2
Accuracy59.0%
Cost7112
\[\begin{array}{l} \mathbf{if}\;im \leq -2.55 \cdot 10^{-135}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \left(-2\right)}\\ \mathbf{elif}\;im \leq 3.1 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 3
Accuracy59.7%
Cost7112
\[\begin{array}{l} \mathbf{if}\;im \leq -5.3 \cdot 10^{-139}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 3.1 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Accuracy30.3%
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -2.1 \cdot 10^{-200}:\\ \;\;\;\;265720.5\\ \mathbf{elif}\;im \leq 10^{-297}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 5
Accuracy58.7%
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -5.1 \cdot 10^{-141}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \left(-2\right)}\\ \mathbf{elif}\;im \leq 4.8 \cdot 10^{-14}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 6
Accuracy41.9%
Cost6852
\[\begin{array}{l} \mathbf{if}\;im \leq 5.6 \cdot 10^{-13}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 7
Accuracy8.7%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -2.2 \cdot 10^{+47}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;4.885243819643157 \cdot 10^{-22}\\ \end{array} \]
Alternative 8
Accuracy8.8%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -8 \cdot 10^{+49}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;4.923200210024256 \cdot 10^{-15}\\ \end{array} \]
Alternative 9
Accuracy8.9%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -2.1 \cdot 10^{+48}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;1.071673525377229 \cdot 10^{-5}\\ \end{array} \]
Alternative 10
Accuracy8.9%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -4 \cdot 10^{+50}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;0.0078125\\ \end{array} \]
Alternative 11
Accuracy8.9%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -7.5 \cdot 10^{+48}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;0.28935185185185186\\ \end{array} \]
Alternative 12
Accuracy8.9%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -5.2 \cdot 10^{+49}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;1.1851851851851851\\ \end{array} \]
Alternative 13
Accuracy8.9%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -6.4 \cdot 10^{+47}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;1.6875\\ \end{array} \]
Alternative 14
Accuracy8.9%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -2.5 \cdot 10^{+50}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;19.2216796875\\ \end{array} \]
Alternative 15
Accuracy9.0%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -7 \cdot 10^{+48}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;265720.5\\ \end{array} \]
Alternative 16
Accuracy9.0%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -4.9 \cdot 10^{+48}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;3812798742493.5\\ \end{array} \]
Alternative 17
Accuracy6.0%
Cost64
\[0 \]

Reproduce?

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