math.square on complex, real part

Percentage Accurate: 93.5% → 97.8%
Time: 4.3s
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.5% 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: 97.8% accurate, 0.1× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ \begin{array}{l} \mathbf{if}\;re\_m \leq 6.6 \cdot 10^{+190}:\\ \;\;\;\;\mathsf{fma}\left(re\_m, re\_m, im \cdot \left(-im\right)\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 6.6e+190) (fma 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 <= 6.6e+190) {
		tmp = fma(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 <= 6.6e+190)
		tmp = fma(re_m, re_m, Float64(im * Float64(-im)));
	else
		tmp = Float64(re_m * re_m);
	end
	return tmp
end
re_m = N[Abs[re], $MachinePrecision]
re$95$sqr[re$95$m_, im_] := If[LessEqual[re$95$m, 6.6e+190], N[(re$95$m * re$95$m + 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 6.6 \cdot 10^{+190}:\\
\;\;\;\;\mathsf{fma}\left(re\_m, re\_m, im \cdot \left(-im\right)\right)\\

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


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

    1. Initial program 94.7%

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

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

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

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

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

    if 6.6e190 < re

    1. Initial program 76.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-sqrt50.0%

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{re} \cdot re \]
  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.4 \cdot 10^{+148}:\\ \;\;\;\;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 7.4e+148) (- (* 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 <= 7.4e+148) {
		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 <= 7.4d+148) 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 <= 7.4e+148) {
		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 <= 7.4e+148:
		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 <= 7.4e+148)
		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 <= 7.4e+148)
		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, 7.4e+148], 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 7.4 \cdot 10^{+148}:\\
\;\;\;\;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 < 7.4000000000000005e148

    1. Initial program 95.4%

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

    if 7.4000000000000005e148 < re

    1. Initial program 76.9%

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

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

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

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

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

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

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

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

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

Alternative 3: 73.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^{+283}:\\ \;\;\;\;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+283) (* 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+283) {
		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+283) 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+283) {
		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+283:
		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+283)
		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+283)
		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+283], 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^{+283}:\\
\;\;\;\;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.99999999999999991e283

    1. Initial program 100.0%

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

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

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

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

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

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

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

    if 1.99999999999999991e283 < (*.f64 re re)

    1. Initial program 75.3%

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

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

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

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

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

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

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

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

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

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

Alternative 4: 53.5% 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 92.6%

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

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

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

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

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

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

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

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

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

Alternative 5: 11.1% 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 92.6%

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{im}^{2}} \]
    6. unpow212.3%

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

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

Reproduce

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