math.abs on complex

Percentage Accurate: 53.6% → 100.0%
Time: 1.6s
Alternatives: 2
Speedup: 1.9×

Specification

?
\[\sqrt{re \cdot re + im \cdot im} \]
(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)
use fmin_fmax_functions
    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]
\sqrt{re \cdot re + im \cdot im}

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 2 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.6% accurate, 1.0× speedup?

\[\sqrt{re \cdot re + im \cdot im} \]
(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)
use fmin_fmax_functions
    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]
\sqrt{re \cdot re + im \cdot im}

Alternative 1: 100.0% accurate, 0.7× speedup?

\[\mathsf{hypot}\left(re, im\right) \]
(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]
\mathsf{hypot}\left(re, im\right)
Derivation
  1. Initial program 53.6%

    \[\sqrt{re \cdot re + im \cdot im} \]
  2. Step-by-step derivation
    1. lift-sqrt.f64N/A

      \[\leadsto \color{blue}{\sqrt{re \cdot re + im \cdot im}} \]
    2. lift-+.f64N/A

      \[\leadsto \sqrt{\color{blue}{re \cdot re + im \cdot im}} \]
    3. lift-*.f64N/A

      \[\leadsto \sqrt{re \cdot re + \color{blue}{im \cdot im}} \]
    4. sqr-neg-revN/A

      \[\leadsto \sqrt{re \cdot re + \color{blue}{\left(\mathsf{neg}\left(im\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)}} \]
    5. fp-cancel-sign-sub-invN/A

      \[\leadsto \sqrt{\color{blue}{re \cdot re - \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)}} \]
    6. fp-cancel-sub-sign-invN/A

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

      \[\leadsto \sqrt{\color{blue}{re \cdot re} + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)} \]
    8. distribute-lft-neg-inN/A

      \[\leadsto \sqrt{re \cdot re + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)\right)\right)}} \]
    9. distribute-rgt-neg-outN/A

      \[\leadsto \sqrt{re \cdot re + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right)}} \]
    10. sqr-neg-revN/A

      \[\leadsto \sqrt{re \cdot re + \color{blue}{\left(\mathsf{neg}\left(im\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)}} \]
    11. sqr-neg-revN/A

      \[\leadsto \sqrt{re \cdot re + \color{blue}{im \cdot im}} \]
    12. lower-hypot.f64100.0%

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

    \[\leadsto \color{blue}{\mathsf{hypot}\left(re, im\right)} \]
  4. Add Preprocessing

Alternative 2: 98.9% accurate, 1.9× speedup?

\[\mathsf{max}\left(\left|re\right|, \left|im\right|\right) \]
(FPCore modulus (re im)
  :precision binary64
  (fmax (fabs re) (fabs im)))
double modulus(double re, double im) {
	return fmax(fabs(re), fabs(im));
}
real(8) function modulus(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    modulus = fmax(abs(re), abs(im))
end function
public static double modulus(double re, double im) {
	return fmax(Math.abs(re), Math.abs(im));
}
def modulus(re, im):
	return fmax(math.fabs(re), math.fabs(im))
function modulus(re, im)
	return fmax(abs(re), abs(im))
end
function tmp = modulus(re, im)
	tmp = max(abs(re), abs(im));
end
modulus[re_, im_] := N[Max[N[Abs[re], $MachinePrecision], N[Abs[im], $MachinePrecision]], $MachinePrecision]
\mathsf{max}\left(\left|re\right|, \left|im\right|\right)
Derivation
  1. Initial program 53.6%

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

    \[\leadsto \color{blue}{-1 \cdot re} \]
  3. Step-by-step derivation
    1. lower-*.f6426.3%

      \[\leadsto -1 \cdot \color{blue}{re} \]
  4. Applied rewrites26.3%

    \[\leadsto \color{blue}{-1 \cdot re} \]
  5. Taylor expanded in undef-var around zero

    \[\leadsto -1 \cdot 0 \]
  6. Step-by-step derivation
    1. Applied rewrites2.7%

      \[\leadsto -1 \cdot 0 \]
    2. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto -1 \cdot \color{blue}{0} \]
      2. mul-1-negN/A

        \[\leadsto \mathsf{neg}\left(0\right) \]
      3. lower-neg.f642.7%

        \[\leadsto -0 \]
    3. Applied rewrites2.7%

      \[\leadsto -0 \]
    4. Taylor expanded in im around inf

      \[\leadsto \color{blue}{im} \]
    5. Step-by-step derivation
      1. Applied rewrites27.5%

        \[\leadsto \color{blue}{im} \]
      2. Add Preprocessing

      Reproduce

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