math.square on complex, real part

Percentage Accurate: 93.9% → 99.0%
Time: 4.2s
Alternatives: 5
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 5 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: 99.0% accurate, 0.1× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ \begin{array}{l} \mathbf{if}\;re\_m \leq 10^{+207}:\\ \;\;\;\;\mathsf{fma}\left(re\_m, re\_m, im \cdot \left(-im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;re\_m \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 1e+207) (fma re_m re_m (* im (- im))) (* re_m (+ re_m im))))
re_m = fabs(re);
double re_sqr(double re_m, double im) {
	double tmp;
	if (re_m <= 1e+207) {
		tmp = fma(re_m, re_m, (im * -im));
	} else {
		tmp = re_m * (re_m + im);
	}
	return tmp;
}
re_m = abs(re)
function re_sqr(re_m, im)
	tmp = 0.0
	if (re_m <= 1e+207)
		tmp = fma(re_m, re_m, Float64(im * Float64(-im)));
	else
		tmp = Float64(re_m * 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, 1e+207], N[(re$95$m * re$95$m + N[(im * (-im)), $MachinePrecision]), $MachinePrecision], N[(re$95$m * N[(re$95$m + im), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
re_m = \left|re\right|

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

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


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

    1. Initial program 96.9%

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

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

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

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

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

    if 1e207 < re

    1. Initial program 71.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-sqrt45.2%

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

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

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

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

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

      \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re + im\right)} \]
    5. Taylor expanded in re around inf 93.5%

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

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

Alternative 2: 96.6% accurate, 0.6× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ \begin{array}{l} \mathbf{if}\;re\_m \leq 3.4 \cdot 10^{+145}:\\ \;\;\;\;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 3.4e+145) (- (* 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 <= 3.4e+145) {
		tmp = (re_m * re_m) - (im * im);
	} else {
		tmp = re_m * re_m;
	}
	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 <= 3.4d+145) 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 <= 3.4e+145) {
		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 <= 3.4e+145:
		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 <= 3.4e+145)
		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 <= 3.4e+145)
		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, 3.4e+145], 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 3.4 \cdot 10^{+145}:\\
\;\;\;\;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 < 3.3999999999999999e145

    1. Initial program 97.2%

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

    if 3.3999999999999999e145 < re

    1. Initial program 75.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-sqrt42.5%

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

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

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

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

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

      \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re + im\right)} \]
    5. Taylor expanded in re around inf 92.5%

      \[\leadsto \left(re + im\right) \cdot \color{blue}{re} \]
    6. Taylor expanded in re around inf 92.5%

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

Alternative 3: 77.2% accurate, 0.6× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ \begin{array}{l} \mathbf{if}\;re\_m \cdot re\_m \leq 2 \cdot 10^{+134}:\\ \;\;\;\;im \cdot \left(-im\right)\\ \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) 2e+134) (* 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) <= 2e+134) {
		tmp = im * -im;
	} else {
		tmp = re_m * re_m;
	}
	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 * re_m) <= 2d+134) 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) <= 2e+134) {
		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) <= 2e+134:
		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(re_m * re_m) <= 2e+134)
		tmp = Float64(im * Float64(-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) <= 2e+134)
		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[(re$95$m * re$95$m), $MachinePrecision], 2e+134], 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 \leq 2 \cdot 10^{+134}:\\
\;\;\;\;im \cdot \left(-im\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 re re) < 1.99999999999999984e134

    1. Initial program 100.0%

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

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

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

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

        \[\leadsto -\color{blue}{im \cdot im} \]
      2. distribute-lft-neg-in82.1%

        \[\leadsto \color{blue}{\left(-im\right) \cdot im} \]
    7. Applied egg-rr82.1%

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

    if 1.99999999999999984e134 < (*.f64 re re)

    1. Initial program 86.2%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re + im\right)} \]
    5. Taylor expanded in re around inf 86.1%

      \[\leadsto \left(re + im\right) \cdot \color{blue}{re} \]
    6. Taylor expanded in re around inf 81.6%

      \[\leadsto \color{blue}{re} \cdot re \]
  3. Recombined 2 regimes into one program.
  4. Final simplification81.9%

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

Alternative 4: 53.7% accurate, 2.3× 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 = 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 * 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 93.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.9%

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

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

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

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

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

    \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re + im\right)} \]
  5. Taylor expanded in re around inf 59.4%

    \[\leadsto \left(re + im\right) \cdot \color{blue}{re} \]
  6. Taylor expanded in re around inf 56.7%

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

Alternative 5: 11.4% accurate, 2.3× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ im \cdot im \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 * 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 im
\end{array}
Derivation
  1. Initial program 93.8%

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{-{im}^{2}} \cdot \sqrt{-{im}^{2}}} \]
    2. sqrt-unprod17.4%

      \[\leadsto \color{blue}{\sqrt{\left(-{im}^{2}\right) \cdot \left(-{im}^{2}\right)}} \]
    3. sqr-neg17.4%

      \[\leadsto \sqrt{\color{blue}{{im}^{2} \cdot {im}^{2}}} \]
    4. sqrt-unprod14.8%

      \[\leadsto \color{blue}{\sqrt{{im}^{2}} \cdot \sqrt{{im}^{2}}} \]
    5. add-sqr-sqrt14.8%

      \[\leadsto \color{blue}{{im}^{2}} \]
    6. unpow214.8%

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

    \[\leadsto \color{blue}{im \cdot im} \]
  8. Add Preprocessing

Reproduce

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