math.square on complex, imaginary part

Percentage Accurate: 100.0% → 100.0%
Time: 1.2s
Alternatives: 3
Speedup: N/A×

Specification

?
\[\begin{array}{l} \\ re \cdot im + im \cdot re \end{array} \]
(FPCore im_sqr (re im) :precision binary64 (+ (* re im) (* im re)))
double im_sqr(double re, double im) {
	return (re * im) + (im * re);
}
real(8) function im_sqr(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    im_sqr = (re * im) + (im * re)
end function
public static double im_sqr(double re, double im) {
	return (re * im) + (im * re);
}
def im_sqr(re, im):
	return (re * im) + (im * re)
function im_sqr(re, im)
	return Float64(Float64(re * im) + Float64(im * re))
end
function tmp = im_sqr(re, im)
	tmp = (re * im) + (im * re);
end
im$95$sqr[re_, im_] := N[(N[(re * im), $MachinePrecision] + N[(im * re), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
re \cdot im + im \cdot re
\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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ re \cdot im + im \cdot re \end{array} \]
(FPCore im_sqr (re im) :precision binary64 (+ (* re im) (* im re)))
double im_sqr(double re, double im) {
	return (re * im) + (im * re);
}
real(8) function im_sqr(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    im_sqr = (re * im) + (im * re)
end function
public static double im_sqr(double re, double im) {
	return (re * im) + (im * re);
}
def im_sqr(re, im):
	return (re * im) + (im * re)
function im_sqr(re, im)
	return Float64(Float64(re * im) + Float64(im * re))
end
function tmp = im_sqr(re, im)
	tmp = (re * im) + (im * re);
end
im$95$sqr[re_, im_] := N[(N[(re * im), $MachinePrecision] + N[(im * re), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
re \cdot im + im \cdot re
\end{array}

Alternative 1: 100.0% accurate, 1.4× speedup?

\[\begin{array}{l} re\_m = \left|re\right| \\ re\_s = \mathsf{copysign}\left(1, re\right) \\ [re_m, im] = \mathsf{sort}([re_m, im])\\ \\ re\_s \cdot \left(\left(re\_m \cdot 2\right) \cdot im\right) \end{array} \]
re\_m = (fabs.f64 re)
re\_s = (copysign.f64 #s(literal 1 binary64) re)
NOTE: re_m and im should be sorted in increasing order before calling this function.
(FPCore im_sqr (re_s re_m im) :precision binary64 (* re_s (* (* re_m 2.0) im)))
re\_m = fabs(re);
re\_s = copysign(1.0, re);
assert(re_m < im);
double im_sqr(double re_s, double re_m, double im) {
	return re_s * ((re_m * 2.0) * im);
}
re\_m = abs(re)
re\_s = copysign(1.0d0, re)
NOTE: re_m and im should be sorted in increasing order before calling this function.
real(8) function im_sqr(re_s, re_m, im)
    real(8), intent (in) :: re_s
    real(8), intent (in) :: re_m
    real(8), intent (in) :: im
    im_sqr = re_s * ((re_m * 2.0d0) * im)
end function
re\_m = Math.abs(re);
re\_s = Math.copySign(1.0, re);
assert re_m < im;
public static double im_sqr(double re_s, double re_m, double im) {
	return re_s * ((re_m * 2.0) * im);
}
re\_m = math.fabs(re)
re\_s = math.copysign(1.0, re)
[re_m, im] = sort([re_m, im])
def im_sqr(re_s, re_m, im):
	return re_s * ((re_m * 2.0) * im)
re\_m = abs(re)
re\_s = copysign(1.0, re)
re_m, im = sort([re_m, im])
function im_sqr(re_s, re_m, im)
	return Float64(re_s * Float64(Float64(re_m * 2.0) * im))
end
re\_m = abs(re);
re\_s = sign(re) * abs(1.0);
re_m, im = num2cell(sort([re_m, im])){:}
function tmp = im_sqr(re_s, re_m, im)
	tmp = re_s * ((re_m * 2.0) * im);
end
re\_m = N[Abs[re], $MachinePrecision]
re\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[re]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: re_m and im should be sorted in increasing order before calling this function.
im$95$sqr[re$95$s_, re$95$m_, im_] := N[(re$95$s * N[(N[(re$95$m * 2.0), $MachinePrecision] * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
re\_m = \left|re\right|
\\
re\_s = \mathsf{copysign}\left(1, re\right)
\\
[re_m, im] = \mathsf{sort}([re_m, im])\\
\\
re\_s \cdot \left(\left(re\_m \cdot 2\right) \cdot im\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[re \cdot im + im \cdot re \]
  2. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto re \cdot im + re \cdot \color{blue}{im} \]
    2. distribute-lft-outN/A

      \[\leadsto re \cdot \color{blue}{\left(im + im\right)} \]
    3. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(re, \color{blue}{\left(im + im\right)}\right) \]
    4. count-2N/A

      \[\leadsto \mathsf{*.f64}\left(re, \left(2 \cdot \color{blue}{im}\right)\right) \]
    5. *-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(re, \left(im \cdot \color{blue}{2}\right)\right) \]
    6. *-lowering-*.f64100.0%

      \[\leadsto \mathsf{*.f64}\left(re, \mathsf{*.f64}\left(im, \color{blue}{2}\right)\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{re \cdot \left(im \cdot 2\right)} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto re \cdot \left(2 \cdot \color{blue}{im}\right) \]
    2. associate-*r*N/A

      \[\leadsto \left(re \cdot 2\right) \cdot \color{blue}{im} \]
    3. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\left(re \cdot 2\right), \color{blue}{im}\right) \]
    4. *-lowering-*.f6499.6%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(re, 2\right), im\right) \]
  6. Applied egg-rr99.6%

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

Alternative 2: 100.0% accurate, 1.4× speedup?

\[\begin{array}{l} re\_m = \left|re\right| \\ re\_s = \mathsf{copysign}\left(1, re\right) \\ [re_m, im] = \mathsf{sort}([re_m, im])\\ \\ re\_s \cdot \left(re\_m \cdot \left(2 \cdot im\right)\right) \end{array} \]
re\_m = (fabs.f64 re)
re\_s = (copysign.f64 #s(literal 1 binary64) re)
NOTE: re_m and im should be sorted in increasing order before calling this function.
(FPCore im_sqr (re_s re_m im) :precision binary64 (* re_s (* re_m (* 2.0 im))))
re\_m = fabs(re);
re\_s = copysign(1.0, re);
assert(re_m < im);
double im_sqr(double re_s, double re_m, double im) {
	return re_s * (re_m * (2.0 * im));
}
re\_m = abs(re)
re\_s = copysign(1.0d0, re)
NOTE: re_m and im should be sorted in increasing order before calling this function.
real(8) function im_sqr(re_s, re_m, im)
    real(8), intent (in) :: re_s
    real(8), intent (in) :: re_m
    real(8), intent (in) :: im
    im_sqr = re_s * (re_m * (2.0d0 * im))
end function
re\_m = Math.abs(re);
re\_s = Math.copySign(1.0, re);
assert re_m < im;
public static double im_sqr(double re_s, double re_m, double im) {
	return re_s * (re_m * (2.0 * im));
}
re\_m = math.fabs(re)
re\_s = math.copysign(1.0, re)
[re_m, im] = sort([re_m, im])
def im_sqr(re_s, re_m, im):
	return re_s * (re_m * (2.0 * im))
re\_m = abs(re)
re\_s = copysign(1.0, re)
re_m, im = sort([re_m, im])
function im_sqr(re_s, re_m, im)
	return Float64(re_s * Float64(re_m * Float64(2.0 * im)))
end
re\_m = abs(re);
re\_s = sign(re) * abs(1.0);
re_m, im = num2cell(sort([re_m, im])){:}
function tmp = im_sqr(re_s, re_m, im)
	tmp = re_s * (re_m * (2.0 * im));
end
re\_m = N[Abs[re], $MachinePrecision]
re\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[re]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: re_m and im should be sorted in increasing order before calling this function.
im$95$sqr[re$95$s_, re$95$m_, im_] := N[(re$95$s * N[(re$95$m * N[(2.0 * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
re\_m = \left|re\right|
\\
re\_s = \mathsf{copysign}\left(1, re\right)
\\
[re_m, im] = \mathsf{sort}([re_m, im])\\
\\
re\_s \cdot \left(re\_m \cdot \left(2 \cdot im\right)\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[re \cdot im + im \cdot re \]
  2. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto re \cdot im + re \cdot \color{blue}{im} \]
    2. distribute-lft-outN/A

      \[\leadsto re \cdot \color{blue}{\left(im + im\right)} \]
    3. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(re, \color{blue}{\left(im + im\right)}\right) \]
    4. count-2N/A

      \[\leadsto \mathsf{*.f64}\left(re, \left(2 \cdot \color{blue}{im}\right)\right) \]
    5. *-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(re, \left(im \cdot \color{blue}{2}\right)\right) \]
    6. *-lowering-*.f64100.0%

      \[\leadsto \mathsf{*.f64}\left(re, \mathsf{*.f64}\left(im, \color{blue}{2}\right)\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{re \cdot \left(im \cdot 2\right)} \]
  4. Add Preprocessing
  5. Final simplification100.0%

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

Alternative 3: 0.0% accurate, 2.3× speedup?

\[\begin{array}{l} re\_m = \left|re\right| \\ re\_s = \mathsf{copysign}\left(1, re\right) \\ [re_m, im] = \mathsf{sort}([re_m, im])\\ \\ re\_s \cdot \frac{0}{0} \end{array} \]
re\_m = (fabs.f64 re)
re\_s = (copysign.f64 #s(literal 1 binary64) re)
NOTE: re_m and im should be sorted in increasing order before calling this function.
(FPCore im_sqr (re_s re_m im) :precision binary64 (* re_s (/ 0.0 0.0)))
re\_m = fabs(re);
re\_s = copysign(1.0, re);
assert(re_m < im);
double im_sqr(double re_s, double re_m, double im) {
	return re_s * (0.0 / 0.0);
}
re\_m = abs(re)
re\_s = copysign(1.0d0, re)
NOTE: re_m and im should be sorted in increasing order before calling this function.
real(8) function im_sqr(re_s, re_m, im)
    real(8), intent (in) :: re_s
    real(8), intent (in) :: re_m
    real(8), intent (in) :: im
    im_sqr = re_s * (0.0d0 / 0.0d0)
end function
re\_m = Math.abs(re);
re\_s = Math.copySign(1.0, re);
assert re_m < im;
public static double im_sqr(double re_s, double re_m, double im) {
	return re_s * (0.0 / 0.0);
}
re\_m = math.fabs(re)
re\_s = math.copysign(1.0, re)
[re_m, im] = sort([re_m, im])
def im_sqr(re_s, re_m, im):
	return re_s * (0.0 / 0.0)
re\_m = abs(re)
re\_s = copysign(1.0, re)
re_m, im = sort([re_m, im])
function im_sqr(re_s, re_m, im)
	return Float64(re_s * Float64(0.0 / 0.0))
end
re\_m = abs(re);
re\_s = sign(re) * abs(1.0);
re_m, im = num2cell(sort([re_m, im])){:}
function tmp = im_sqr(re_s, re_m, im)
	tmp = re_s * (0.0 / 0.0);
end
re\_m = N[Abs[re], $MachinePrecision]
re\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[re]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: re_m and im should be sorted in increasing order before calling this function.
im$95$sqr[re$95$s_, re$95$m_, im_] := N[(re$95$s * N[(0.0 / 0.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
re\_m = \left|re\right|
\\
re\_s = \mathsf{copysign}\left(1, re\right)
\\
[re_m, im] = \mathsf{sort}([re_m, im])\\
\\
re\_s \cdot \frac{0}{0}
\end{array}
Derivation
  1. Initial program 100.0%

    \[re \cdot im + im \cdot re \]
  2. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto re \cdot im + re \cdot \color{blue}{im} \]
    2. distribute-lft-outN/A

      \[\leadsto re \cdot \color{blue}{\left(im + im\right)} \]
    3. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(re, \color{blue}{\left(im + im\right)}\right) \]
    4. count-2N/A

      \[\leadsto \mathsf{*.f64}\left(re, \left(2 \cdot \color{blue}{im}\right)\right) \]
    5. *-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(re, \left(im \cdot \color{blue}{2}\right)\right) \]
    6. *-lowering-*.f64100.0%

      \[\leadsto \mathsf{*.f64}\left(re, \mathsf{*.f64}\left(im, \color{blue}{2}\right)\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{re \cdot \left(im \cdot 2\right)} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. associate-*r*N/A

      \[\leadsto \left(re \cdot im\right) \cdot \color{blue}{2} \]
    2. *-commutativeN/A

      \[\leadsto 2 \cdot \color{blue}{\left(re \cdot im\right)} \]
    3. count-2N/A

      \[\leadsto re \cdot im + \color{blue}{re \cdot im} \]
    4. *-commutativeN/A

      \[\leadsto re \cdot im + im \cdot \color{blue}{re} \]
    5. flip-+N/A

      \[\leadsto \frac{\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(im \cdot re\right) \cdot \left(im \cdot re\right)}{\color{blue}{re \cdot im - im \cdot re}} \]
    6. *-commutativeN/A

      \[\leadsto \frac{\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(im \cdot re\right) \cdot \left(im \cdot re\right)}{re \cdot im - re \cdot \color{blue}{im}} \]
    7. +-inversesN/A

      \[\leadsto \frac{\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(im \cdot re\right) \cdot \left(im \cdot re\right)}{0} \]
    8. +-inversesN/A

      \[\leadsto \frac{\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(im \cdot re\right) \cdot \left(im \cdot re\right)}{\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \color{blue}{\left(re \cdot im\right) \cdot \left(re \cdot im\right)}} \]
    9. *-commutativeN/A

      \[\leadsto \frac{\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(im \cdot re\right) \cdot \left(im \cdot re\right)}{\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(re \cdot im\right) \cdot \left(im \cdot \color{blue}{re}\right)} \]
    10. *-commutativeN/A

      \[\leadsto \frac{\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(im \cdot re\right) \cdot \left(im \cdot re\right)}{\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(im \cdot re\right) \cdot \left(\color{blue}{im} \cdot re\right)} \]
    11. /-lowering-/.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\left(\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(im \cdot re\right) \cdot \left(im \cdot re\right)\right), \color{blue}{\left(\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(im \cdot re\right) \cdot \left(im \cdot re\right)\right)}\right) \]
    12. *-commutativeN/A

      \[\leadsto \mathsf{/.f64}\left(\left(\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(re \cdot im\right) \cdot \left(im \cdot re\right)\right), \left(\left(re \cdot im\right) \cdot \left(\color{blue}{re} \cdot im\right) - \left(im \cdot re\right) \cdot \left(im \cdot re\right)\right)\right) \]
    13. *-commutativeN/A

      \[\leadsto \mathsf{/.f64}\left(\left(\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(re \cdot im\right) \cdot \left(re \cdot im\right)\right), \left(\left(re \cdot im\right) \cdot \left(re \cdot \color{blue}{im}\right) - \left(im \cdot re\right) \cdot \left(im \cdot re\right)\right)\right) \]
    14. +-inversesN/A

      \[\leadsto \mathsf{/.f64}\left(0, \left(\color{blue}{\left(re \cdot im\right) \cdot \left(re \cdot im\right)} - \left(im \cdot re\right) \cdot \left(im \cdot re\right)\right)\right) \]
    15. *-commutativeN/A

      \[\leadsto \mathsf{/.f64}\left(0, \left(\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(re \cdot im\right) \cdot \left(\color{blue}{im} \cdot re\right)\right)\right) \]
    16. *-commutativeN/A

      \[\leadsto \mathsf{/.f64}\left(0, \left(\left(re \cdot im\right) \cdot \left(re \cdot im\right) - \left(re \cdot im\right) \cdot \left(re \cdot \color{blue}{im}\right)\right)\right) \]
    17. +-inverses0.0%

      \[\leadsto \mathsf{/.f64}\left(0, 0\right) \]
  6. Applied egg-rr0.0%

    \[\leadsto \color{blue}{\frac{0}{0}} \]
  7. Add Preprocessing

Reproduce

?
herbie shell --seed 2024138 
(FPCore im_sqr (re im)
  :name "math.square on complex, imaginary part"
  :precision binary64
  (+ (* re im) (* im re)))