math.square on complex, real part

Percentage Accurate: 94.1% → 96.6%
Time: 1.0s
Alternatives: 3
Speedup: 0.7×

Specification

?
\[\begin{array}{l} \\ re \cdot re - im \cdot im \end{array} \]
(FPCore re_sqr (re im) :precision binary64 (- (* re re) (* im im)))
double re_sqr(double re, double im) {
	return (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 re_sqr(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    re_sqr = (re * re) - (im * im)
end function
public static double re_sqr(double re, double im) {
	return (re * re) - (im * im);
}
def re_sqr(re, im):
	return (re * re) - (im * im)
function re_sqr(re, im)
	return Float64(Float64(re * re) - Float64(im * im))
end
function tmp = re_sqr(re, im)
	tmp = (re * re) - (im * im);
end
re$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
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 3 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: 94.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ re \cdot re - im \cdot im \end{array} \]
(FPCore re_sqr (re im) :precision binary64 (- (* re re) (* im im)))
double re_sqr(double re, double im) {
	return (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 re_sqr(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    re_sqr = (re * re) - (im * im)
end function
public static double re_sqr(double re, double im) {
	return (re * re) - (im * im);
}
def re_sqr(re, im):
	return (re * re) - (im * im)
function re_sqr(re, im)
	return Float64(Float64(re * re) - Float64(im * im))
end
function tmp = re_sqr(re, im)
	tmp = (re * re) - (im * im);
end
re$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
re \cdot re - im \cdot im
\end{array}

Alternative 1: 96.6% accurate, 0.7× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ \begin{array}{l} \mathbf{if}\;re\_m \leq 8.5 \cdot 10^{+150}:\\ \;\;\;\;re\_m \cdot re\_m - im \cdot im\\ \mathbf{else}:\\ \;\;\;\;re\_m \cdot re\_m\\ \end{array} \end{array} \]
re_m = (fabs.f64 re)
(FPCore re_sqr (re_m im)
 :precision binary64
 (if (<= re_m 8.5e+150) (- (* re_m re_m) (* im im)) (* re_m re_m)))
re_m = fabs(re);
double re_sqr(double re_m, double im) {
	double tmp;
	if (re_m <= 8.5e+150) {
		tmp = (re_m * re_m) - (im * im);
	} else {
		tmp = re_m * re_m;
	}
	return tmp;
}
re_m =     private
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 re_sqr(re_m, im)
use fmin_fmax_functions
    real(8), intent (in) :: re_m
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re_m <= 8.5d+150) then
        tmp = (re_m * re_m) - (im * im)
    else
        tmp = re_m * re_m
    end if
    re_sqr = tmp
end function
re_m = Math.abs(re);
public static double re_sqr(double re_m, double im) {
	double tmp;
	if (re_m <= 8.5e+150) {
		tmp = (re_m * re_m) - (im * im);
	} else {
		tmp = re_m * re_m;
	}
	return tmp;
}
re_m = math.fabs(re)
def re_sqr(re_m, im):
	tmp = 0
	if re_m <= 8.5e+150:
		tmp = (re_m * re_m) - (im * im)
	else:
		tmp = re_m * re_m
	return tmp
re_m = abs(re)
function re_sqr(re_m, im)
	tmp = 0.0
	if (re_m <= 8.5e+150)
		tmp = Float64(Float64(re_m * re_m) - Float64(im * im));
	else
		tmp = Float64(re_m * re_m);
	end
	return tmp
end
re_m = abs(re);
function tmp_2 = re_sqr(re_m, im)
	tmp = 0.0;
	if (re_m <= 8.5e+150)
		tmp = (re_m * re_m) - (im * im);
	else
		tmp = re_m * re_m;
	end
	tmp_2 = tmp;
end
re_m = N[Abs[re], $MachinePrecision]
re$95$sqr[re$95$m_, im_] := If[LessEqual[re$95$m, 8.5e+150], N[(N[(re$95$m * re$95$m), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision], N[(re$95$m * re$95$m), $MachinePrecision]]
\begin{array}{l}
re_m = \left|re\right|

\\
\begin{array}{l}
\mathbf{if}\;re\_m \leq 8.5 \cdot 10^{+150}:\\
\;\;\;\;re\_m \cdot re\_m - im \cdot im\\

\mathbf{else}:\\
\;\;\;\;re\_m \cdot re\_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 8.4999999999999999e150

    1. Initial program 100.0%

      \[re \cdot re - im \cdot im \]

    if 8.4999999999999999e150 < re

    1. Initial program 77.0%

      \[re \cdot re - im \cdot im \]
    2. Taylor expanded in re around inf

      \[\leadsto \color{blue}{{re}^{2}} \]
    3. Step-by-step derivation
      1. pow2N/A

        \[\leadsto re \cdot \color{blue}{re} \]
      2. lift-*.f6486.6

        \[\leadsto re \cdot \color{blue}{re} \]
    4. Applied rewrites86.6%

      \[\leadsto \color{blue}{re \cdot re} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 95.1% accurate, 0.6× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ \begin{array}{l} \mathbf{if}\;re\_m \cdot re\_m - im \cdot im \leq -5 \cdot 10^{-260}:\\ \;\;\;\;\left(-im\right) \cdot im\\ \mathbf{else}:\\ \;\;\;\;re\_m \cdot re\_m\\ \end{array} \end{array} \]
re_m = (fabs.f64 re)
(FPCore re_sqr (re_m im)
 :precision binary64
 (if (<= (- (* re_m re_m) (* im im)) -5e-260) (* (- im) im) (* re_m re_m)))
re_m = fabs(re);
double re_sqr(double re_m, double im) {
	double tmp;
	if (((re_m * re_m) - (im * im)) <= -5e-260) {
		tmp = -im * im;
	} else {
		tmp = re_m * re_m;
	}
	return tmp;
}
re_m =     private
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 re_sqr(re_m, im)
use fmin_fmax_functions
    real(8), intent (in) :: re_m
    real(8), intent (in) :: im
    real(8) :: tmp
    if (((re_m * re_m) - (im * im)) <= (-5d-260)) then
        tmp = -im * im
    else
        tmp = re_m * re_m
    end if
    re_sqr = tmp
end function
re_m = Math.abs(re);
public static double re_sqr(double re_m, double im) {
	double tmp;
	if (((re_m * re_m) - (im * im)) <= -5e-260) {
		tmp = -im * im;
	} else {
		tmp = re_m * re_m;
	}
	return tmp;
}
re_m = math.fabs(re)
def re_sqr(re_m, im):
	tmp = 0
	if ((re_m * re_m) - (im * im)) <= -5e-260:
		tmp = -im * im
	else:
		tmp = re_m * re_m
	return tmp
re_m = abs(re)
function re_sqr(re_m, im)
	tmp = 0.0
	if (Float64(Float64(re_m * re_m) - Float64(im * im)) <= -5e-260)
		tmp = Float64(Float64(-im) * im);
	else
		tmp = Float64(re_m * re_m);
	end
	return tmp
end
re_m = abs(re);
function tmp_2 = re_sqr(re_m, im)
	tmp = 0.0;
	if (((re_m * re_m) - (im * im)) <= -5e-260)
		tmp = -im * im;
	else
		tmp = re_m * re_m;
	end
	tmp_2 = tmp;
end
re_m = N[Abs[re], $MachinePrecision]
re$95$sqr[re$95$m_, im_] := If[LessEqual[N[(N[(re$95$m * re$95$m), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision], -5e-260], N[((-im) * im), $MachinePrecision], N[(re$95$m * re$95$m), $MachinePrecision]]
\begin{array}{l}
re_m = \left|re\right|

\\
\begin{array}{l}
\mathbf{if}\;re\_m \cdot re\_m - im \cdot im \leq -5 \cdot 10^{-260}:\\
\;\;\;\;\left(-im\right) \cdot im\\

\mathbf{else}:\\
\;\;\;\;re\_m \cdot re\_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 re re) (*.f64 im im)) < -5.0000000000000003e-260

    1. Initial program 100.0%

      \[re \cdot re - im \cdot im \]
    2. Taylor expanded in re around 0

      \[\leadsto \color{blue}{-1 \cdot {im}^{2}} \]
    3. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \mathsf{neg}\left({im}^{2}\right) \]
      2. pow2N/A

        \[\leadsto \mathsf{neg}\left(im \cdot im\right) \]
      3. distribute-lft-neg-inN/A

        \[\leadsto \left(\mathsf{neg}\left(im\right)\right) \cdot \color{blue}{im} \]
      4. lower-*.f64N/A

        \[\leadsto \left(\mathsf{neg}\left(im\right)\right) \cdot \color{blue}{im} \]
      5. lower-neg.f6499.5

        \[\leadsto \left(-im\right) \cdot im \]
    4. Applied rewrites99.5%

      \[\leadsto \color{blue}{\left(-im\right) \cdot im} \]

    if -5.0000000000000003e-260 < (-.f64 (*.f64 re re) (*.f64 im im))

    1. Initial program 89.7%

      \[re \cdot re - im \cdot im \]
    2. Taylor expanded in re around inf

      \[\leadsto \color{blue}{{re}^{2}} \]
    3. Step-by-step derivation
      1. pow2N/A

        \[\leadsto re \cdot \color{blue}{re} \]
      2. lift-*.f6491.8

        \[\leadsto re \cdot \color{blue}{re} \]
    4. Applied rewrites91.8%

      \[\leadsto \color{blue}{re \cdot re} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 53.0% accurate, 2.4× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ re\_m \cdot re\_m \end{array} \]
re_m = (fabs.f64 re)
(FPCore re_sqr (re_m im) :precision binary64 (* re_m re_m))
re_m = fabs(re);
double re_sqr(double re_m, double im) {
	return re_m * re_m;
}
re_m =     private
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 re_sqr(re_m, im)
use fmin_fmax_functions
    real(8), intent (in) :: re_m
    real(8), intent (in) :: im
    re_sqr = re_m * re_m
end function
re_m = Math.abs(re);
public static double re_sqr(double re_m, double im) {
	return re_m * re_m;
}
re_m = math.fabs(re)
def re_sqr(re_m, im):
	return re_m * re_m
re_m = abs(re)
function re_sqr(re_m, im)
	return Float64(re_m * re_m)
end
re_m = abs(re);
function tmp = re_sqr(re_m, im)
	tmp = re_m * re_m;
end
re_m = N[Abs[re], $MachinePrecision]
re$95$sqr[re$95$m_, im_] := N[(re$95$m * re$95$m), $MachinePrecision]
\begin{array}{l}
re_m = \left|re\right|

\\
re\_m \cdot re\_m
\end{array}
Derivation
  1. Initial program 94.1%

    \[re \cdot re - im \cdot im \]
  2. Taylor expanded in re around inf

    \[\leadsto \color{blue}{{re}^{2}} \]
  3. Step-by-step derivation
    1. pow2N/A

      \[\leadsto re \cdot \color{blue}{re} \]
    2. lift-*.f6453.0

      \[\leadsto re \cdot \color{blue}{re} \]
  4. Applied rewrites53.0%

    \[\leadsto \color{blue}{re \cdot re} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2025112 
(FPCore re_sqr (re im)
  :name "math.square on complex, real part"
  :precision binary64
  (- (* re re) (* im im)))