math.square on complex, imaginary part

Percentage Accurate: 100.0% → 100.0%
Time: 997.0ms
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, im] = \mathsf{sort}([re, im])\\ \\ \left(re \cdot 2\right) \cdot im \end{array} \]
NOTE: re and im should be sorted in increasing order before calling this function.
(FPCore im_sqr (re im) :precision binary64 (* (* re 2.0) im))
assert(re < im);
double im_sqr(double re, double im) {
	return (re * 2.0) * im;
}
NOTE: re and im should be sorted in increasing order before calling this function.
real(8) function im_sqr(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    im_sqr = (re * 2.0d0) * im
end function
assert re < im;
public static double im_sqr(double re, double im) {
	return (re * 2.0) * im;
}
[re, im] = sort([re, im])
def im_sqr(re, im):
	return (re * 2.0) * im
re, im = sort([re, im])
function im_sqr(re, im)
	return Float64(Float64(re * 2.0) * im)
end
re, im = num2cell(sort([re, im])){:}
function tmp = im_sqr(re, im)
	tmp = (re * 2.0) * im;
end
NOTE: re and im should be sorted in increasing order before calling this function.
im$95$sqr[re_, im_] := N[(N[(re * 2.0), $MachinePrecision] * im), $MachinePrecision]
\begin{array}{l}
[re, im] = \mathsf{sort}([re, im])\\
\\
\left(re \cdot 2\right) \cdot im
\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.7%

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

    \[\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, im] = \mathsf{sort}([re, im])\\ \\ re \cdot \left(2 \cdot im\right) \end{array} \]
NOTE: re and im should be sorted in increasing order before calling this function.
(FPCore im_sqr (re im) :precision binary64 (* re (* 2.0 im)))
assert(re < im);
double im_sqr(double re, double im) {
	return re * (2.0 * im);
}
NOTE: re and im should be sorted in increasing order before calling this function.
real(8) function im_sqr(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    im_sqr = re * (2.0d0 * im)
end function
assert re < im;
public static double im_sqr(double re, double im) {
	return re * (2.0 * im);
}
[re, im] = sort([re, im])
def im_sqr(re, im):
	return re * (2.0 * im)
re, im = sort([re, im])
function im_sqr(re, im)
	return Float64(re * Float64(2.0 * im))
end
re, im = num2cell(sort([re, im])){:}
function tmp = im_sqr(re, im)
	tmp = re * (2.0 * im);
end
NOTE: re and im should be sorted in increasing order before calling this function.
im$95$sqr[re_, im_] := N[(re * N[(2.0 * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[re, im] = \mathsf{sort}([re, im])\\
\\
re \cdot \left(2 \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. 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, im] = \mathsf{sort}([re, im])\\ \\ \frac{0}{0} \end{array} \]
NOTE: re and im should be sorted in increasing order before calling this function.
(FPCore im_sqr (re im) :precision binary64 (/ 0.0 0.0))
assert(re < im);
double im_sqr(double re, double im) {
	return 0.0 / 0.0;
}
NOTE: re and im should be sorted in increasing order before calling this function.
real(8) function im_sqr(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    im_sqr = 0.0d0 / 0.0d0
end function
assert re < im;
public static double im_sqr(double re, double im) {
	return 0.0 / 0.0;
}
[re, im] = sort([re, im])
def im_sqr(re, im):
	return 0.0 / 0.0
re, im = sort([re, im])
function im_sqr(re, im)
	return Float64(0.0 / 0.0)
end
re, im = num2cell(sort([re, im])){:}
function tmp = im_sqr(re, im)
	tmp = 0.0 / 0.0;
end
NOTE: re and im should be sorted in increasing order before calling this function.
im$95$sqr[re_, im_] := N[(0.0 / 0.0), $MachinePrecision]
\begin{array}{l}
[re, im] = \mathsf{sort}([re, im])\\
\\
\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 2024139 
(FPCore im_sqr (re im)
  :name "math.square on complex, imaginary part"
  :precision binary64
  (+ (* re im) (* im re)))