math.abs on complex

Percentage Accurate: 53.7% → 100.0%
Time: 3.1s
Alternatives: 5
Speedup: 0.2×

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)));
}
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 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]
\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 5 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.7% 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)));
}
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 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]
\begin{array}{l}

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

Alternative 1: 100.0% accurate, 0.2× 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 62.7%

    \[\sqrt{re \cdot re + im \cdot im} \]
  2. Add Preprocessing
  3. 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{\color{blue}{re \cdot re} + im \cdot im} \]
    4. lift-*.f64N/A

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

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

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

Alternative 2: 26.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\frac{0.5}{im} \cdot re, re, im\right) \end{array} \]
(FPCore modulus (re im) :precision binary64 (fma (* (/ 0.5 im) re) re im))
double modulus(double re, double im) {
	return fma(((0.5 / im) * re), re, im);
}
function modulus(re, im)
	return fma(Float64(Float64(0.5 / im) * re), re, im)
end
modulus[re_, im_] := N[(N[(N[(0.5 / im), $MachinePrecision] * re), $MachinePrecision] * re + im), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\frac{0.5}{im} \cdot re, re, im\right)
\end{array}
Derivation
  1. Initial program 62.7%

    \[\sqrt{re \cdot re + im \cdot im} \]
  2. Add Preprocessing
  3. Taylor expanded in re around 0

    \[\leadsto \color{blue}{im + \frac{1}{2} \cdot \frac{{re}^{2}}{im}} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{{re}^{2}}{im} + im} \]
    2. *-lft-identityN/A

      \[\leadsto \frac{1}{2} \cdot \frac{\color{blue}{1 \cdot {re}^{2}}}{im} + im \]
    3. associate-*l/N/A

      \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(\frac{1}{im} \cdot {re}^{2}\right)} + im \]
    4. associate-*l*N/A

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{2} \cdot \frac{1}{im}, {re}^{2}, im\right)} \]
    6. associate-*r/N/A

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

      \[\leadsto \mathsf{fma}\left(\frac{\color{blue}{\frac{1}{2}}}{im}, {re}^{2}, im\right) \]
    8. lower-/.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\frac{1}{2}}{im}}, {re}^{2}, im\right) \]
    9. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\frac{\frac{1}{2}}{im}, \color{blue}{re \cdot re}, im\right) \]
    10. lower-*.f6426.9

      \[\leadsto \mathsf{fma}\left(\frac{0.5}{im}, \color{blue}{re \cdot re}, im\right) \]
  5. Applied rewrites26.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{0.5}{im}, re \cdot re, im\right)} \]
  6. Step-by-step derivation
    1. Applied rewrites27.2%

      \[\leadsto \mathsf{fma}\left(\frac{0.5}{im} \cdot re, \color{blue}{re}, im\right) \]
    2. Add Preprocessing

    Alternative 3: 53.7% accurate, 1.1× speedup?

    \[\begin{array}{l} \\ \sqrt{\mathsf{fma}\left(re, re, im \cdot im\right)} \end{array} \]
    (FPCore modulus (re im) :precision binary64 (sqrt (fma re re (* im im))))
    double modulus(double re, double im) {
    	return sqrt(fma(re, re, (im * im)));
    }
    
    function modulus(re, im)
    	return sqrt(fma(re, re, Float64(im * im)))
    end
    
    modulus[re_, im_] := N[Sqrt[N[(re * re + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \sqrt{\mathsf{fma}\left(re, re, im \cdot im\right)}
    \end{array}
    
    Derivation
    1. Initial program 62.7%

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

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

        \[\leadsto \sqrt{\color{blue}{re \cdot re} + im \cdot im} \]
      3. lower-fma.f6462.7

        \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(re, re, im \cdot im\right)}} \]
    4. Applied rewrites62.7%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(re, re, im \cdot im\right)}} \]
    5. Add Preprocessing

    Alternative 4: 28.4% accurate, 1.5× speedup?

    \[\begin{array}{l} \\ \sqrt{im \cdot im} \end{array} \]
    (FPCore modulus (re im) :precision binary64 (sqrt (* im im)))
    double modulus(double re, double im) {
    	return sqrt((im * im));
    }
    
    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 modulus(re, im)
    use fmin_fmax_functions
        real(8), intent (in) :: re
        real(8), intent (in) :: im
        modulus = sqrt((im * im))
    end function
    
    public static double modulus(double re, double im) {
    	return Math.sqrt((im * im));
    }
    
    def modulus(re, im):
    	return math.sqrt((im * im))
    
    function modulus(re, im)
    	return sqrt(Float64(im * im))
    end
    
    function tmp = modulus(re, im)
    	tmp = sqrt((im * im));
    end
    
    modulus[re_, im_] := N[Sqrt[N[(im * im), $MachinePrecision]], $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \sqrt{im \cdot im}
    \end{array}
    
    Derivation
    1. Initial program 62.7%

      \[\sqrt{re \cdot re + im \cdot im} \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0

      \[\leadsto \sqrt{\color{blue}{{im}^{2}}} \]
    4. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto \sqrt{\color{blue}{im \cdot im}} \]
      2. lower-*.f6434.7

        \[\leadsto \sqrt{\color{blue}{im \cdot im}} \]
    5. Applied rewrites34.7%

      \[\leadsto \sqrt{\color{blue}{im \cdot im}} \]
    6. Add Preprocessing

    Alternative 5: 26.8% accurate, 8.0× speedup?

    \[\begin{array}{l} \\ -re \end{array} \]
    (FPCore modulus (re im) :precision binary64 (- re))
    double modulus(double re, double im) {
    	return -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 modulus(re, im)
    use fmin_fmax_functions
        real(8), intent (in) :: re
        real(8), intent (in) :: im
        modulus = -re
    end function
    
    public static double modulus(double re, double im) {
    	return -re;
    }
    
    def modulus(re, im):
    	return -re
    
    function modulus(re, im)
    	return Float64(-re)
    end
    
    function tmp = modulus(re, im)
    	tmp = -re;
    end
    
    modulus[re_, im_] := (-re)
    
    \begin{array}{l}
    
    \\
    -re
    \end{array}
    
    Derivation
    1. Initial program 62.7%

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

      \[\leadsto \color{blue}{-1 \cdot re} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(re\right)} \]
      2. lower-neg.f6425.9

        \[\leadsto \color{blue}{-re} \]
    5. Applied rewrites25.9%

      \[\leadsto \color{blue}{-re} \]
    6. Add Preprocessing

    Reproduce

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