math.abs on complex

Percentage Accurate: 53.5% → 100.0%
Time: 2.2s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \sqrt{re \cdot re + im \cdot im} \end{array} \]
(FPCore modulus (re im) :precision binary64 (sqrt (+ (* re re) (* im im))))
double modulus(double re, double im) {
	return sqrt(((re * re) + (im * im)));
}
real(8) function modulus(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    modulus = sqrt(((re * re) + (im * im)))
end function
public static double modulus(double re, double im) {
	return Math.sqrt(((re * re) + (im * im)));
}
def modulus(re, im):
	return math.sqrt(((re * re) + (im * im)))
function modulus(re, im)
	return sqrt(Float64(Float64(re * re) + Float64(im * im)))
end
function tmp = modulus(re, im)
	tmp = sqrt(((re * re) + (im * im)));
end
modulus[re_, im_] := N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{re \cdot re + im \cdot im}
\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 4 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: 53.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{re \cdot re + im \cdot im} \end{array} \]
(FPCore modulus (re im) :precision binary64 (sqrt (+ (* re re) (* im im))))
double modulus(double re, double im) {
	return sqrt(((re * re) + (im * im)));
}
real(8) function modulus(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    modulus = sqrt(((re * re) + (im * im)))
end function
public static double modulus(double re, double im) {
	return Math.sqrt(((re * re) + (im * im)));
}
def modulus(re, im):
	return math.sqrt(((re * re) + (im * im)))
function modulus(re, im)
	return sqrt(Float64(Float64(re * re) + Float64(im * im)))
end
function tmp = modulus(re, im)
	tmp = sqrt(((re * re) + (im * im)));
end
modulus[re_, im_] := N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{re \cdot re + im \cdot im}
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \mathsf{hypot}\left(re, im\right) \end{array} \]
(FPCore modulus (re im) :precision binary64 (hypot re im))
double modulus(double re, double im) {
	return hypot(re, im);
}
public static double modulus(double re, double im) {
	return Math.hypot(re, im);
}
def modulus(re, im):
	return math.hypot(re, im)
function modulus(re, im)
	return hypot(re, im)
end
function tmp = modulus(re, im)
	tmp = hypot(re, im);
end
modulus[re_, im_] := N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]
\begin{array}{l}

\\
\mathsf{hypot}\left(re, im\right)
\end{array}
Derivation
  1. Initial program 53.7%

    \[\sqrt{re \cdot re + im \cdot im} \]
  2. Step-by-step derivation
    1. expm1-log1p-u51.4%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{re \cdot re + im \cdot im}\right)\right)} \]
    2. expm1-udef31.9%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{re \cdot re + im \cdot im}\right)} - 1} \]
    3. log1p-udef31.9%

      \[\leadsto e^{\color{blue}{\log \left(1 + \sqrt{re \cdot re + im \cdot im}\right)}} - 1 \]
    4. add-exp-log34.2%

      \[\leadsto \color{blue}{\left(1 + \sqrt{re \cdot re + im \cdot im}\right)} - 1 \]
    5. hypot-def74.1%

      \[\leadsto \left(1 + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right) - 1 \]
  3. Applied egg-rr74.1%

    \[\leadsto \color{blue}{\left(1 + \mathsf{hypot}\left(re, im\right)\right) - 1} \]
  4. Step-by-step derivation
    1. +-commutative74.1%

      \[\leadsto \color{blue}{\left(\mathsf{hypot}\left(re, im\right) + 1\right)} - 1 \]
    2. associate--l+100.0%

      \[\leadsto \color{blue}{\mathsf{hypot}\left(re, im\right) + \left(1 - 1\right)} \]
    3. metadata-eval100.0%

      \[\leadsto \mathsf{hypot}\left(re, im\right) + \color{blue}{0} \]
    4. +-rgt-identity100.0%

      \[\leadsto \color{blue}{\mathsf{hypot}\left(re, im\right)} \]
  5. Simplified100.0%

    \[\leadsto \color{blue}{\mathsf{hypot}\left(re, im\right)} \]
  6. Final simplification100.0%

    \[\leadsto \mathsf{hypot}\left(re, im\right) \]

Alternative 2: 43.0% accurate, 9.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -3.5 \cdot 10^{-157}:\\ \;\;\;\;\frac{im}{\frac{re}{im}} \cdot -0.5 - re\\ \mathbf{else}:\\ \;\;\;\;im\\ \end{array} \end{array} \]
(FPCore modulus (re im)
 :precision binary64
 (if (<= re -3.5e-157) (- (* (/ im (/ re im)) -0.5) re) im))
double modulus(double re, double im) {
	double tmp;
	if (re <= -3.5e-157) {
		tmp = ((im / (re / im)) * -0.5) - re;
	} else {
		tmp = im;
	}
	return tmp;
}
real(8) function modulus(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-3.5d-157)) then
        tmp = ((im / (re / im)) * (-0.5d0)) - re
    else
        tmp = im
    end if
    modulus = tmp
end function
public static double modulus(double re, double im) {
	double tmp;
	if (re <= -3.5e-157) {
		tmp = ((im / (re / im)) * -0.5) - re;
	} else {
		tmp = im;
	}
	return tmp;
}
def modulus(re, im):
	tmp = 0
	if re <= -3.5e-157:
		tmp = ((im / (re / im)) * -0.5) - re
	else:
		tmp = im
	return tmp
function modulus(re, im)
	tmp = 0.0
	if (re <= -3.5e-157)
		tmp = Float64(Float64(Float64(im / Float64(re / im)) * -0.5) - re);
	else
		tmp = im;
	end
	return tmp
end
function tmp_2 = modulus(re, im)
	tmp = 0.0;
	if (re <= -3.5e-157)
		tmp = ((im / (re / im)) * -0.5) - re;
	else
		tmp = im;
	end
	tmp_2 = tmp;
end
modulus[re_, im_] := If[LessEqual[re, -3.5e-157], N[(N[(N[(im / N[(re / im), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision] - re), $MachinePrecision], im]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.5 \cdot 10^{-157}:\\
\;\;\;\;\frac{im}{\frac{re}{im}} \cdot -0.5 - re\\

\mathbf{else}:\\
\;\;\;\;im\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -3.5000000000000002e-157

    1. Initial program 56.7%

      \[\sqrt{re \cdot re + im \cdot im} \]
    2. Taylor expanded in re around -inf 62.6%

      \[\leadsto \color{blue}{-1 \cdot re + -0.5 \cdot \frac{{im}^{2}}{re}} \]
    3. Step-by-step derivation
      1. +-commutative62.6%

        \[\leadsto \color{blue}{-0.5 \cdot \frac{{im}^{2}}{re} + -1 \cdot re} \]
      2. mul-1-neg62.6%

        \[\leadsto -0.5 \cdot \frac{{im}^{2}}{re} + \color{blue}{\left(-re\right)} \]
      3. unsub-neg62.6%

        \[\leadsto \color{blue}{-0.5 \cdot \frac{{im}^{2}}{re} - re} \]
      4. *-commutative62.6%

        \[\leadsto \color{blue}{\frac{{im}^{2}}{re} \cdot -0.5} - re \]
      5. unpow262.6%

        \[\leadsto \frac{\color{blue}{im \cdot im}}{re} \cdot -0.5 - re \]
      6. associate-/l*69.3%

        \[\leadsto \color{blue}{\frac{im}{\frac{re}{im}}} \cdot -0.5 - re \]
    4. Simplified69.3%

      \[\leadsto \color{blue}{\frac{im}{\frac{re}{im}} \cdot -0.5 - re} \]

    if -3.5000000000000002e-157 < re

    1. Initial program 51.8%

      \[\sqrt{re \cdot re + im \cdot im} \]
    2. Taylor expanded in re around 0 38.4%

      \[\leadsto \color{blue}{im} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification50.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -3.5 \cdot 10^{-157}:\\ \;\;\;\;\frac{im}{\frac{re}{im}} \cdot -0.5 - re\\ \mathbf{else}:\\ \;\;\;\;im\\ \end{array} \]

Alternative 3: 42.6% accurate, 26.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -2.4 \cdot 10^{-157}:\\ \;\;\;\;-re\\ \mathbf{else}:\\ \;\;\;\;im\\ \end{array} \end{array} \]
(FPCore modulus (re im) :precision binary64 (if (<= re -2.4e-157) (- re) im))
double modulus(double re, double im) {
	double tmp;
	if (re <= -2.4e-157) {
		tmp = -re;
	} else {
		tmp = im;
	}
	return tmp;
}
real(8) function modulus(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-2.4d-157)) then
        tmp = -re
    else
        tmp = im
    end if
    modulus = tmp
end function
public static double modulus(double re, double im) {
	double tmp;
	if (re <= -2.4e-157) {
		tmp = -re;
	} else {
		tmp = im;
	}
	return tmp;
}
def modulus(re, im):
	tmp = 0
	if re <= -2.4e-157:
		tmp = -re
	else:
		tmp = im
	return tmp
function modulus(re, im)
	tmp = 0.0
	if (re <= -2.4e-157)
		tmp = Float64(-re);
	else
		tmp = im;
	end
	return tmp
end
function tmp_2 = modulus(re, im)
	tmp = 0.0;
	if (re <= -2.4e-157)
		tmp = -re;
	else
		tmp = im;
	end
	tmp_2 = tmp;
end
modulus[re_, im_] := If[LessEqual[re, -2.4e-157], (-re), im]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.4 \cdot 10^{-157}:\\
\;\;\;\;-re\\

\mathbf{else}:\\
\;\;\;\;im\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -2.4e-157

    1. Initial program 56.7%

      \[\sqrt{re \cdot re + im \cdot im} \]
    2. Taylor expanded in re around -inf 68.6%

      \[\leadsto \color{blue}{-1 \cdot re} \]
    3. Step-by-step derivation
      1. mul-1-neg68.6%

        \[\leadsto \color{blue}{-re} \]
    4. Simplified68.6%

      \[\leadsto \color{blue}{-re} \]

    if -2.4e-157 < re

    1. Initial program 51.8%

      \[\sqrt{re \cdot re + im \cdot im} \]
    2. Taylor expanded in re around 0 38.4%

      \[\leadsto \color{blue}{im} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification49.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.4 \cdot 10^{-157}:\\ \;\;\;\;-re\\ \mathbf{else}:\\ \;\;\;\;im\\ \end{array} \]

Alternative 4: 26.7% accurate, 107.0× speedup?

\[\begin{array}{l} \\ im \end{array} \]
(FPCore modulus (re im) :precision binary64 im)
double modulus(double re, double im) {
	return im;
}
real(8) function modulus(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    modulus = im
end function
public static double modulus(double re, double im) {
	return im;
}
def modulus(re, im):
	return im
function modulus(re, im)
	return im
end
function tmp = modulus(re, im)
	tmp = im;
end
modulus[re_, im_] := im
\begin{array}{l}

\\
im
\end{array}
Derivation
  1. Initial program 53.7%

    \[\sqrt{re \cdot re + im \cdot im} \]
  2. Taylor expanded in re around 0 30.5%

    \[\leadsto \color{blue}{im} \]
  3. Final simplification30.5%

    \[\leadsto im \]

Reproduce

?
herbie shell --seed 2023174 
(FPCore modulus (re im)
  :name "math.abs on complex"
  :precision binary64
  (sqrt (+ (* re re) (* im im))))