math.abs on complex

Percentage Accurate: 54.0% → 99.5%
Time: 1.3s
Alternatives: 2
Speedup: 0.2×

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)));
}
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]
\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: 54.0% 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)));
}
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]
\sqrt{re \cdot re + im \cdot im}

Alternative 1: 99.5% accurate, 0.1× speedup?

\[\begin{array}{l} t_0 := \mathsf{max}\left(\left|re\right|, \left|im\right|\right)\\ t_1 := \mathsf{min}\left(\left|re\right|, \left|im\right|\right)\\ t\_0 - \frac{-1}{2} \cdot \left(\frac{t\_1}{t\_0} \cdot t\_1\right) \end{array} \]
(FPCore modulus (re im)
  :precision binary64
  (let* ((t_0 (fmax (fabs re) (fabs im)))
       (t_1 (fmin (fabs re) (fabs im))))
  (- t_0 (* -1/2 (* (/ t_1 t_0) t_1)))))
double modulus(double re, double im) {
	double t_0 = fmax(fabs(re), fabs(im));
	double t_1 = fmin(fabs(re), fabs(im));
	return t_0 - (-0.5 * ((t_1 / t_0) * t_1));
}
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
    real(8) :: t_0
    real(8) :: t_1
    t_0 = fmax(abs(re), abs(im))
    t_1 = fmin(abs(re), abs(im))
    modulus = t_0 - ((-0.5d0) * ((t_1 / t_0) * t_1))
end function
public static double modulus(double re, double im) {
	double t_0 = fmax(Math.abs(re), Math.abs(im));
	double t_1 = fmin(Math.abs(re), Math.abs(im));
	return t_0 - (-0.5 * ((t_1 / t_0) * t_1));
}
def modulus(re, im):
	t_0 = fmax(math.fabs(re), math.fabs(im))
	t_1 = fmin(math.fabs(re), math.fabs(im))
	return t_0 - (-0.5 * ((t_1 / t_0) * t_1))
function modulus(re, im)
	t_0 = fmax(abs(re), abs(im))
	t_1 = fmin(abs(re), abs(im))
	return Float64(t_0 - Float64(-0.5 * Float64(Float64(t_1 / t_0) * t_1)))
end
function tmp = modulus(re, im)
	t_0 = max(abs(re), abs(im));
	t_1 = min(abs(re), abs(im));
	tmp = t_0 - (-0.5 * ((t_1 / t_0) * t_1));
end
modulus[re_, im_] := Block[{t$95$0 = N[Max[N[Abs[re], $MachinePrecision], N[Abs[im], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Min[N[Abs[re], $MachinePrecision], N[Abs[im], $MachinePrecision]], $MachinePrecision]}, N[(t$95$0 - N[(-1/2 * N[(N[(t$95$1 / t$95$0), $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
t_0 := \mathsf{max}\left(\left|re\right|, \left|im\right|\right)\\
t_1 := \mathsf{min}\left(\left|re\right|, \left|im\right|\right)\\
t\_0 - \frac{-1}{2} \cdot \left(\frac{t\_1}{t\_0} \cdot t\_1\right)
\end{array}
Derivation
  1. Initial program 54.0%

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

    \[\leadsto \color{blue}{im \cdot \left(1 + \frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{2}}\right)} \]
  3. Step-by-step derivation
    1. lower-*.f64N/A

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

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

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

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

      \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \frac{{re}^{2}}{{\color{blue}{im}}^{2}}\right) \]
    6. lower-pow.f6423.8%

      \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{\color{blue}{2}}}\right) \]
  4. Applied rewrites23.8%

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

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

      \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \frac{{re}^{2}}{{\color{blue}{im}}^{2}}\right) \]
    3. pow2N/A

      \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \frac{re \cdot re}{{\color{blue}{im}}^{2}}\right) \]
    4. sqr-neg-revN/A

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

      \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \frac{\left(\mathsf{neg}\left(re\right)\right) \cdot \left(\mathsf{neg}\left(re\right)\right)}{{im}^{\color{blue}{2}}}\right) \]
    6. pow2N/A

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

      \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \frac{\left(\mathsf{neg}\left(re\right)\right) \cdot \left(\mathsf{neg}\left(re\right)\right)}{\left(\mathsf{neg}\left(im\right)\right) \cdot \color{blue}{\left(\mathsf{neg}\left(im\right)\right)}}\right) \]
    8. times-fracN/A

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

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

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

      \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \left(\frac{re}{im} \cdot \frac{\color{blue}{\mathsf{neg}\left(re\right)}}{\mathsf{neg}\left(im\right)}\right)\right) \]
    12. frac-2neg-revN/A

      \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \left(\frac{re}{im} \cdot \frac{re}{\color{blue}{im}}\right)\right) \]
    13. lower-/.f6427.0%

      \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \left(\frac{re}{im} \cdot \frac{re}{\color{blue}{im}}\right)\right) \]
  6. Applied rewrites27.0%

    \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \left(\frac{re}{im} \cdot \color{blue}{\frac{re}{im}}\right)\right) \]
  7. Applied rewrites27.1%

    \[\leadsto im - \color{blue}{\frac{-1}{2} \cdot \left(\frac{re}{im} \cdot re\right)} \]
  8. Add Preprocessing

Alternative 2: 99.0% accurate, 0.2× 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));
}
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 = 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 54.0%

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

    \[\leadsto \color{blue}{im \cdot \left(1 + \frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{2}}\right)} \]
  3. Step-by-step derivation
    1. lower-*.f64N/A

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

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

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

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

      \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \frac{{re}^{2}}{{\color{blue}{im}}^{2}}\right) \]
    6. lower-pow.f6423.8%

      \[\leadsto im \cdot \left(1 + \frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{\color{blue}{2}}}\right) \]
  4. Applied rewrites23.8%

    \[\leadsto \color{blue}{im \cdot \left(1 + \frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{2}}\right)} \]
  5. Taylor expanded in re around 0

    \[\leadsto im \]
  6. Step-by-step derivation
    1. Applied rewrites26.8%

      \[\leadsto im \]
    2. Add Preprocessing

    Reproduce

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