math.square on complex, real part

Percentage Accurate: 93.4% → 98.2%
Time: 2.5s
Alternatives: 3
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 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: 93.4% 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.2% accurate, 0.1× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ \begin{array}{l} \mathbf{if}\;re\_m \leq 1.6 \cdot 10^{+213}:\\ \;\;\;\;\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.6e+213)
   (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.6e+213) {
		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.6e+213)
		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.6e+213], 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.6 \cdot 10^{+213}:\\
\;\;\;\;\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.5999999999999999e213

    1. Initial program 92.0%

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

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

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

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

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

    if 1.5999999999999999e213 < re

    1. Initial program 61.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-sqrt66.7%

        \[\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-prod33.3%

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

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

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

Alternative 2: 96.4% accurate, 0.6× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ \begin{array}{l} \mathbf{if}\;re\_m \leq 7 \cdot 10^{+151}:\\ \;\;\;\;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 7e+151) (- (* 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 <= 7e+151) {
		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 <= 7d+151) 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 <= 7e+151) {
		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 <= 7e+151:
		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 <= 7e+151)
		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 <= 7e+151)
		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, 7e+151], 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 7 \cdot 10^{+151}:\\
\;\;\;\;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 < 7.0000000000000006e151

    1. Initial program 93.7%

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

    if 7.0000000000000006e151 < re

    1. Initial program 64.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-sqrt52.9%

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

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

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

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

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

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

Alternative 3: 52.5% 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 89.8%

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

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

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

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

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

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

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

Reproduce

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