math.square on complex, real part

Percentage Accurate: 93.8% → 98.4%
Time: 3.1s
Alternatives: 4
Speedup: 0.6×

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.8% 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 5 \cdot 10^{+200}:\\ \;\;\;\;\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 5e+200)
   (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 <= 5e+200) {
		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 <= 5e+200)
		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, 5e+200], 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 5 \cdot 10^{+200}:\\
\;\;\;\;\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 < 5.00000000000000019e200

    1. Initial program 94.5%

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

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

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

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

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

    if 5.00000000000000019e200 < re

    1. Initial program 71.4%

      \[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-sqrt52.4%

        \[\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-prod47.6%

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

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

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

Alternative 2: 76.8% accurate, 0.5× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ \begin{array}{l} \mathbf{if}\;im \cdot im \leq 10^{-85}:\\ \;\;\;\;\left(re\_m + im\right) \cdot \left(re\_m + im\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(-im\right)\\ \end{array} \end{array} \]
re_m = (fabs.f64 re)
(FPCore re_sqr (re_m im)
 :precision binary64
 (if (<= (* im im) 1e-85) (* (+ re_m im) (+ re_m im)) (* im (- im))))
re_m = fabs(re);
double re_sqr(double re_m, double im) {
	double tmp;
	if ((im * im) <= 1e-85) {
		tmp = (re_m + im) * (re_m + im);
	} else {
		tmp = im * -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) :: tmp
    if ((im * im) <= 1d-85) then
        tmp = (re_m + im) * (re_m + im)
    else
        tmp = im * -im
    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-85) {
		tmp = (re_m + im) * (re_m + im);
	} else {
		tmp = im * -im;
	}
	return tmp;
}
re_m = math.fabs(re)
def re_sqr(re_m, im):
	tmp = 0
	if (im * im) <= 1e-85:
		tmp = (re_m + im) * (re_m + im)
	else:
		tmp = im * -im
	return tmp
re_m = abs(re)
function re_sqr(re_m, im)
	tmp = 0.0
	if (Float64(im * im) <= 1e-85)
		tmp = Float64(Float64(re_m + im) * Float64(re_m + im));
	else
		tmp = Float64(im * Float64(-im));
	end
	return tmp
end
re_m = abs(re);
function tmp_2 = re_sqr(re_m, im)
	tmp = 0.0;
	if ((im * im) <= 1e-85)
		tmp = (re_m + im) * (re_m + im);
	else
		tmp = im * -im;
	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-85], N[(N[(re$95$m + im), $MachinePrecision] * N[(re$95$m + im), $MachinePrecision]), $MachinePrecision], N[(im * (-im)), $MachinePrecision]]
\begin{array}{l}
re_m = \left|re\right|

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

\mathbf{else}:\\
\;\;\;\;im \cdot \left(-im\right)\\


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

    1. Initial program 100.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-sqrt43.6%

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

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

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

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

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

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

    if 9.9999999999999998e-86 < (*.f64 im im)

    1. Initial program 86.3%

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

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

        \[\leadsto \color{blue}{-{im}^{2}} \]
    5. Simplified71.3%

      \[\leadsto \color{blue}{-{im}^{2}} \]
    6. Step-by-step derivation
      1. unpow271.3%

        \[\leadsto -\color{blue}{im \cdot im} \]
    7. Applied egg-rr71.3%

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

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

Alternative 3: 97.1% accurate, 0.6× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ \begin{array}{l} \mathbf{if}\;re\_m \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;re\_m \cdot re\_m - im \cdot im\\ \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.35e+154)
   (- (* 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.35e+154) {
		tmp = (re_m * re_m) - (im * im);
	} 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) :: tmp
    if (re_m <= 1.35d+154) then
        tmp = (re_m * re_m) - (im * im)
    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 tmp;
	if (re_m <= 1.35e+154) {
		tmp = (re_m * re_m) - (im * im);
	} else {
		tmp = (re_m + im) * (re_m + im);
	}
	return tmp;
}
re_m = math.fabs(re)
def re_sqr(re_m, im):
	tmp = 0
	if re_m <= 1.35e+154:
		tmp = (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.35e+154)
		tmp = Float64(Float64(re_m * re_m) - Float64(im * im));
	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)
	tmp = 0.0;
	if (re_m <= 1.35e+154)
		tmp = (re_m * re_m) - (im * im);
	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_] := If[LessEqual[re$95$m, 1.35e+154], N[(N[(re$95$m * re$95$m), $MachinePrecision] - 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.35 \cdot 10^{+154}:\\
\;\;\;\;re\_m \cdot re\_m - im \cdot im\\

\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.35000000000000003e154

    1. Initial program 95.5%

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

    if 1.35000000000000003e154 < re

    1. Initial program 73.5%

      \[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-sqrt52.9%

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

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

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

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

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

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

Alternative 4: 53.7% accurate, 1.8× speedup?

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

\\
im \cdot \left(-im\right)
\end{array}
Derivation
  1. Initial program 92.6%

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

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

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

    \[\leadsto \color{blue}{-{im}^{2}} \]
  6. Step-by-step derivation
    1. unpow249.2%

      \[\leadsto -\color{blue}{im \cdot im} \]
  7. Applied egg-rr49.2%

    \[\leadsto -\color{blue}{im \cdot im} \]
  8. Final simplification49.2%

    \[\leadsto im \cdot \left(-im\right) \]
  9. Add Preprocessing

Reproduce

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