math.sqrt on complex, imaginary part, im greater than 0 branch

?

Percentage Accurate: 41.1% → 89.7%
Time: 4.6s
Precision: binary64
Cost: 33476

?

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\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 6 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

Derivation?

  1. Split input into 2 regimes
  2. if (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 14.3%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in im around 0 99.0%

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    3. Simplified99.1%

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

      [Start]99.0%

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

      associate-*l* [=>]99.1%

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

      *-commutative [=>]99.1%

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

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

    1. Initial program 45.2%

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

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

      [Start]45.2%

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

      hypot-def [=>]89.2%

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

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

Alternatives

Alternative 1
Accuracy89.7%
Cost33476
\[\begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \end{array} \]
Alternative 2
Accuracy89.7%
Cost33476
\[\begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \end{array} \]
Alternative 3
Accuracy83.6%
Cost26884
\[\begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{im \cdot im}{re}\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \end{array} \]
Alternative 4
Accuracy69.2%
Cost7368
\[\begin{array}{l} \mathbf{if}\;re \leq -1.85 \cdot 10^{-19}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 4.4 \cdot 10^{+26}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{im \cdot im}{re}\right)}\\ \end{array} \]
Alternative 5
Accuracy63.3%
Cost6980
\[\begin{array}{l} \mathbf{if}\;re \leq -1.5 \cdot 10^{-19}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \end{array} \]
Alternative 6
Accuracy26.1%
Cost6848
\[0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)} \]

Reproduce?

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