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

Percentage Accurate: 41.7% → 90.1%
Time: 4.2s
Alternatives: 1

Specification

?
\[im > 0\]
\[\begin{array}{l} \\ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \end{array} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
	return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) - re)))
end function
public static double code(double re, double im) {
	return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) - re)));
}
def code(re, im):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re)))
function code(re, im)
	return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re))))
end
function tmp = code(re, im)
	tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re)));
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]
\begin{array}{l}

\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\end{array}

Sampling outcomes in binary64 precision:

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.

Accuracy vs Speed?

Herbie found 1 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 41.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \end{array} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
	return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) - re)))
end function
public static double code(double re, double im) {
	return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) - re)));
}
def code(re, im):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re)))
function code(re, im)
	return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re))))
end
function tmp = code(re, im)
	tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re)));
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]
\begin{array}{l}

\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\end{array}

Alternative 1: 90.1% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right) \leq 0:\\ \;\;\;\;\left(0.5 \cdot im\right) \cdot {re}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\left(\mathsf{hypot}\left(im, re\right) - re\right) \cdot 2} \cdot 0.5\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)) 0.0)
   (* (* 0.5 im) (pow re -0.5))
   (* (sqrt (* (- (hypot im re) re) 2.0)) 0.5)))
double code(double re, double im) {
	double tmp;
	if ((2.0 * (sqrt(((re * re) + (im * im))) - re)) <= 0.0) {
		tmp = (0.5 * im) * pow(re, -0.5);
	} else {
		tmp = sqrt(((hypot(im, re) - re) * 2.0)) * 0.5;
	}
	return tmp;
}
public static double code(double re, double im) {
	double tmp;
	if ((2.0 * (Math.sqrt(((re * re) + (im * im))) - re)) <= 0.0) {
		tmp = (0.5 * im) * Math.pow(re, -0.5);
	} else {
		tmp = Math.sqrt(((Math.hypot(im, re) - re) * 2.0)) * 0.5;
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (2.0 * (math.sqrt(((re * re) + (im * im))) - re)) <= 0.0:
		tmp = (0.5 * im) * math.pow(re, -0.5)
	else:
		tmp = math.sqrt(((math.hypot(im, re) - re) * 2.0)) * 0.5
	return tmp
function code(re, im)
	tmp = 0.0
	if (Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re)) <= 0.0)
		tmp = Float64(Float64(0.5 * im) * (re ^ -0.5));
	else
		tmp = Float64(sqrt(Float64(Float64(hypot(im, re) - re) * 2.0)) * 0.5);
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((2.0 * (sqrt(((re * re) + (im * im))) - re)) <= 0.0)
		tmp = (0.5 * im) * (re ^ -0.5);
	else
		tmp = sqrt(((hypot(im, re) - re) * 2.0)) * 0.5;
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(0.5 * im), $MachinePrecision] * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision], N[(N[Sqrt[N[(N[(N[Sqrt[im ^ 2 + re ^ 2], $MachinePrecision] - re), $MachinePrecision] * 2.0), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right) \leq 0:\\
\;\;\;\;\left(0.5 \cdot im\right) \cdot {re}^{-0.5}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)) < 0.0

    1. Initial program 10.2%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\left(im \cdot \left(\sqrt{\frac{1}{2}} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    4. Step-by-step derivation
      1. associate-*r*N/A

        \[\leadsto \left(\frac{1}{2} \cdot \left(im \cdot \left(\sqrt{\frac{1}{2}} \cdot \sqrt{2}\right)\right)\right) \cdot \color{blue}{\sqrt{\frac{1}{re}}} \]
      2. lower-*.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \left(im \cdot \left(\sqrt{\frac{1}{2}} \cdot \sqrt{2}\right)\right)\right) \cdot \color{blue}{\sqrt{\frac{1}{re}}} \]
      3. sqrt-unprodN/A

        \[\leadsto \left(\frac{1}{2} \cdot \left(im \cdot \sqrt{\frac{1}{2} \cdot 2}\right)\right) \cdot \sqrt{\frac{1}{re}} \]
      4. metadata-evalN/A

        \[\leadsto \left(\frac{1}{2} \cdot \left(im \cdot \sqrt{1}\right)\right) \cdot \sqrt{\frac{1}{re}} \]
      5. metadata-evalN/A

        \[\leadsto \left(\frac{1}{2} \cdot \left(im \cdot 1\right)\right) \cdot \sqrt{\frac{1}{re}} \]
      6. lower-*.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \left(im \cdot 1\right)\right) \cdot \sqrt{\color{blue}{\frac{1}{re}}} \]
      7. *-commutativeN/A

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \sqrt{\frac{1}{\color{blue}{re}}} \]
      8. lower-*.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \sqrt{\frac{1}{\color{blue}{re}}} \]
      9. inv-powN/A

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \sqrt{{re}^{-1}} \]
      10. sqrt-pow1N/A

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\color{blue}{\left(\frac{-1}{2}\right)}} \]
      11. metadata-evalN/A

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\frac{-1}{2}} \]
      12. lower-pow.f6494.5

        \[\leadsto \left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\color{blue}{-0.5}} \]
    5. Applied rewrites94.5%

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

    if 0.0 < (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))

    1. Initial program 44.6%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
      2. lift-sqrt.f64N/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
      3. lift-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
      4. lift--.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
      5. lift-sqrt.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\color{blue}{\sqrt{re \cdot re + im \cdot im}} - re\right)} \]
      6. lift-+.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{\color{blue}{re \cdot re + im \cdot im}} - re\right)} \]
      7. lift-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{\color{blue}{re \cdot re} + im \cdot im} - re\right)} \]
      8. lift-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \color{blue}{im \cdot im}} - re\right)} \]
      9. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot \frac{1}{2}} \]
      10. lower-*.f64N/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot \frac{1}{2}} \]
    4. Applied rewrites90.3%

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

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

Reproduce

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