math.square on complex, real part

Percentage Accurate: 93.9% → 98.4%
Time: 3.3s
Alternatives: 4
Speedup: 0.4×

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);
}
real(8) function re_sqr(re, im)
    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}

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 4 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: 93.9% 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);
}
real(8) function re_sqr(re, im)
    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: 98.4% accurate, 0.1× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ \begin{array}{l} \mathbf{if}\;re\_m \leq 1.85 \cdot 10^{+253}:\\ \;\;\;\;\mathsf{fma}\left(re\_m, re\_m, im \cdot \left(-im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(re\_m + im\right) \cdot \left(re\_m + im\right)\\ \end{array} \end{array} \]
re_m = (fabs.f64 re)
(FPCore re_sqr (re_m im)
 :precision binary64
 (if (<= re_m 1.85e+253)
   (fma re_m re_m (* im (- im)))
   (* (+ re_m im) (+ re_m im))))
re_m = fabs(re);
double re_sqr(double re_m, double im) {
	double tmp;
	if (re_m <= 1.85e+253) {
		tmp = fma(re_m, re_m, (im * -im));
	} else {
		tmp = (re_m + im) * (re_m + im);
	}
	return tmp;
}
re_m = abs(re)
function re_sqr(re_m, im)
	tmp = 0.0
	if (re_m <= 1.85e+253)
		tmp = fma(re_m, re_m, Float64(im * Float64(-im)));
	else
		tmp = Float64(Float64(re_m + im) * Float64(re_m + im));
	end
	return tmp
end
re_m = N[Abs[re], $MachinePrecision]
re$95$sqr[re$95$m_, im_] := If[LessEqual[re$95$m, 1.85e+253], N[(re$95$m * re$95$m + N[(im * (-im)), $MachinePrecision]), $MachinePrecision], N[(N[(re$95$m + im), $MachinePrecision] * N[(re$95$m + im), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
re_m = \left|re\right|

\\
\begin{array}{l}
\mathbf{if}\;re\_m \leq 1.85 \cdot 10^{+253}:\\
\;\;\;\;\mathsf{fma}\left(re\_m, re\_m, im \cdot \left(-im\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\left(re\_m + im\right) \cdot \left(re\_m + im\right)\\


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

    1. Initial program 92.2%

      \[re \cdot re - im \cdot im \]
    2. Step-by-step derivation
      1. sqr-neg92.2%

        \[\leadsto re \cdot re - \color{blue}{\left(-im\right) \cdot \left(-im\right)} \]
      2. cancel-sign-sub92.2%

        \[\leadsto \color{blue}{re \cdot re + im \cdot \left(-im\right)} \]
      3. fma-define97.5%

        \[\leadsto \color{blue}{\mathsf{fma}\left(re, re, im \cdot \left(-im\right)\right)} \]
    3. Simplified97.5%

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

    if 1.85000000000000014e253 < re

    1. Initial program 66.7%

      \[re \cdot re - im \cdot im \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. difference-of-squares100.0%

        \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re - im\right)} \]
      2. sub-neg100.0%

        \[\leadsto \left(re + im\right) \cdot \color{blue}{\left(re + \left(-im\right)\right)} \]
      3. add-sqr-sqrt33.3%

        \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{\sqrt{-im} \cdot \sqrt{-im}}\right) \]
      4. sqrt-unprod100.0%

        \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{\sqrt{\left(-im\right) \cdot \left(-im\right)}}\right) \]
      5. sqr-neg100.0%

        \[\leadsto \left(re + im\right) \cdot \left(re + \sqrt{\color{blue}{im \cdot im}}\right) \]
      6. sqrt-prod66.7%

        \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{\sqrt{im} \cdot \sqrt{im}}\right) \]
      7. add-sqr-sqrt100.0%

        \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{im}\right) \]
    4. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re + im\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification97.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 1.85 \cdot 10^{+253}:\\ \;\;\;\;\mathsf{fma}\left(re, re, im \cdot \left(-im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(re + im\right) \cdot \left(re + im\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 96.3% accurate, 0.1× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;-{im}^{2}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 im im) < 9.99999999999999953e270

    1. Initial program 100.0%

      \[re \cdot re - im \cdot im \]
    2. Add Preprocessing

    if 9.99999999999999953e270 < (*.f64 im im)

    1. Initial program 67.6%

      \[re \cdot re - im \cdot im \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0 85.9%

      \[\leadsto \color{blue}{-1 \cdot {im}^{2}} \]
    4. Step-by-step derivation
      1. mul-1-neg85.9%

        \[\leadsto \color{blue}{-{im}^{2}} \]
    5. Simplified85.9%

      \[\leadsto \color{blue}{-{im}^{2}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification96.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \cdot im \leq 10^{+271}:\\ \;\;\;\;re \cdot re - im \cdot im\\ \mathbf{else}:\\ \;\;\;\;-{im}^{2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 96.9% accurate, 0.4× speedup?

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

\\
\begin{array}{l}
t_0 := re\_m \cdot re\_m - im \cdot im\\
\mathbf{if}\;t\_0 \leq 10^{+257}:\\
\;\;\;\;t\_0\\

\mathbf{else}:\\
\;\;\;\;\left(re\_m + im\right) \cdot \left(re\_m + im\right)\\


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

    1. Initial program 100.0%

      \[re \cdot re - im \cdot im \]
    2. Add Preprocessing

    if 1.00000000000000003e257 < (-.f64 (*.f64 re re) (*.f64 im im))

    1. Initial program 70.1%

      \[re \cdot re - im \cdot im \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. difference-of-squares100.0%

        \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re - im\right)} \]
      2. sub-neg100.0%

        \[\leadsto \left(re + im\right) \cdot \color{blue}{\left(re + \left(-im\right)\right)} \]
      3. add-sqr-sqrt48.1%

        \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{\sqrt{-im} \cdot \sqrt{-im}}\right) \]
      4. sqrt-unprod88.3%

        \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{\sqrt{\left(-im\right) \cdot \left(-im\right)}}\right) \]
      5. sqr-neg88.3%

        \[\leadsto \left(re + im\right) \cdot \left(re + \sqrt{\color{blue}{im \cdot im}}\right) \]
      6. sqrt-prod44.2%

        \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{\sqrt{im} \cdot \sqrt{im}}\right) \]
      7. add-sqr-sqrt83.1%

        \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{im}\right) \]
    4. Applied egg-rr83.1%

      \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re + im\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification94.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \cdot re - im \cdot im \leq 10^{+257}:\\ \;\;\;\;re \cdot re - im \cdot im\\ \mathbf{else}:\\ \;\;\;\;\left(re + im\right) \cdot \left(re + im\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 52.0% accurate, 1.0× speedup?

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

\\
\left(re\_m + im\right) \cdot \left(re\_m + im\right)
\end{array}
Derivation
  1. Initial program 91.0%

    \[re \cdot re - im \cdot im \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. difference-of-squares100.0%

      \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re - im\right)} \]
    2. sub-neg100.0%

      \[\leadsto \left(re + im\right) \cdot \color{blue}{\left(re + \left(-im\right)\right)} \]
    3. add-sqr-sqrt49.9%

      \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{\sqrt{-im} \cdot \sqrt{-im}}\right) \]
    4. sqrt-unprod77.5%

      \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{\sqrt{\left(-im\right) \cdot \left(-im\right)}}\right) \]
    5. sqr-neg77.5%

      \[\leadsto \left(re + im\right) \cdot \left(re + \sqrt{\color{blue}{im \cdot im}}\right) \]
    6. sqrt-prod28.7%

      \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{\sqrt{im} \cdot \sqrt{im}}\right) \]
    7. add-sqr-sqrt54.4%

      \[\leadsto \left(re + im\right) \cdot \left(re + \color{blue}{im}\right) \]
  4. Applied egg-rr54.4%

    \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re + im\right)} \]
  5. Final simplification54.4%

    \[\leadsto \left(re + im\right) \cdot \left(re + im\right) \]
  6. Add Preprocessing

Reproduce

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