math.abs on complex

Percentage Accurate: 54.2% → 98.9%
Time: 2.6s
Alternatives: 2
Speedup: 11.4×

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}

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.2% 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: 98.9% accurate, 11.4× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ im_m = \left|im\right| \\ [re_m, im_m] = \mathsf{sort}([re_m, im_m])\\ \\ im\_m \end{array} \]
re_m = (fabs.f64 re)
im_m = (fabs.f64 im)
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
(FPCore modulus (re_m im_m) :precision binary64 im_m)
re_m = fabs(re);
im_m = fabs(im);
assert(re_m < im_m);
double modulus(double re_m, double im_m) {
	return im_m;
}
re_m =     private
im_m =     private
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
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_m, im_m)
use fmin_fmax_functions
    real(8), intent (in) :: re_m
    real(8), intent (in) :: im_m
    modulus = im_m
end function
re_m = Math.abs(re);
im_m = Math.abs(im);
assert re_m < im_m;
public static double modulus(double re_m, double im_m) {
	return im_m;
}
re_m = math.fabs(re)
im_m = math.fabs(im)
[re_m, im_m] = sort([re_m, im_m])
def modulus(re_m, im_m):
	return im_m
re_m = abs(re)
im_m = abs(im)
re_m, im_m = sort([re_m, im_m])
function modulus(re_m, im_m)
	return im_m
end
re_m = abs(re);
im_m = abs(im);
re_m, im_m = num2cell(sort([re_m, im_m])){:}
function tmp = modulus(re_m, im_m)
	tmp = im_m;
end
re_m = N[Abs[re], $MachinePrecision]
im_m = N[Abs[im], $MachinePrecision]
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
modulus[re$95$m_, im$95$m_] := im$95$m
\begin{array}{l}
re_m = \left|re\right|
\\
im_m = \left|im\right|
\\
[re_m, im_m] = \mathsf{sort}([re_m, im_m])\\
\\
im\_m
\end{array}
Derivation
  1. Initial program 54.2%

    \[\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. Applied rewrites88.1%

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

    \[\leadsto im \]
  5. Applied rewrites98.9%

    \[\leadsto im \]
  6. Add Preprocessing

Alternative 2: 5.0% accurate, 11.4× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ im_m = \left|im\right| \\ [re_m, im_m] = \mathsf{sort}([re_m, im_m])\\ \\ re\_m \end{array} \]
re_m = (fabs.f64 re)
im_m = (fabs.f64 im)
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
(FPCore modulus (re_m im_m) :precision binary64 re_m)
re_m = fabs(re);
im_m = fabs(im);
assert(re_m < im_m);
double modulus(double re_m, double im_m) {
	return re_m;
}
re_m =     private
im_m =     private
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
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_m, im_m)
use fmin_fmax_functions
    real(8), intent (in) :: re_m
    real(8), intent (in) :: im_m
    modulus = re_m
end function
re_m = Math.abs(re);
im_m = Math.abs(im);
assert re_m < im_m;
public static double modulus(double re_m, double im_m) {
	return re_m;
}
re_m = math.fabs(re)
im_m = math.fabs(im)
[re_m, im_m] = sort([re_m, im_m])
def modulus(re_m, im_m):
	return re_m
re_m = abs(re)
im_m = abs(im)
re_m, im_m = sort([re_m, im_m])
function modulus(re_m, im_m)
	return re_m
end
re_m = abs(re);
im_m = abs(im);
re_m, im_m = num2cell(sort([re_m, im_m])){:}
function tmp = modulus(re_m, im_m)
	tmp = re_m;
end
re_m = N[Abs[re], $MachinePrecision]
im_m = N[Abs[im], $MachinePrecision]
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
modulus[re$95$m_, im$95$m_] := re$95$m
\begin{array}{l}
re_m = \left|re\right|
\\
im_m = \left|im\right|
\\
[re_m, im_m] = \mathsf{sort}([re_m, im_m])\\
\\
re\_m
\end{array}
Derivation
  1. Initial program 54.2%

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

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

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

    \[\leadsto re \]
  5. Add Preprocessing

Reproduce

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