math.cos on complex, imaginary part

Percentage Accurate: 66.0% → 90.4%
Time: 9.1s
Alternatives: 6
Speedup: 1.5×

Specification

?
\[\begin{array}{l} \\ \left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))
double code(double re, double im) {
	return (0.5 * sin(re)) * (exp(-im) - exp(im));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (0.5d0 * sin(re)) * (exp(-im) - exp(im))
end function
public static double code(double re, double im) {
	return (0.5 * Math.sin(re)) * (Math.exp(-im) - Math.exp(im));
}
def code(re, im):
	return (0.5 * math.sin(re)) * (math.exp(-im) - math.exp(im))
function code(re, im)
	return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(-im)) - exp(im)))
end
function tmp = code(re, im)
	tmp = (0.5 * sin(re)) * (exp(-im) - exp(im));
end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\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 6 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: 66.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))
double code(double re, double im) {
	return (0.5 * sin(re)) * (exp(-im) - exp(im));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (0.5d0 * sin(re)) * (exp(-im) - exp(im))
end function
public static double code(double re, double im) {
	return (0.5 * Math.sin(re)) * (Math.exp(-im) - Math.exp(im));
}
def code(re, im):
	return (0.5 * math.sin(re)) * (math.exp(-im) - math.exp(im))
function code(re, im)
	return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(-im)) - exp(im)))
end
function tmp = code(re, im)
	tmp = (0.5 * sin(re)) * (exp(-im) - exp(im));
end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\end{array}

Alternative 1: 90.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(0.5 \cdot \sin re\right) \cdot \left(-2 \cdot im + \left(-0.3333333333333333 \cdot {im}^{3} + -0.016666666666666666 \cdot {im}^{5}\right)\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (*
  (* 0.5 (sin re))
  (+
   (* -2.0 im)
   (+
    (* -0.3333333333333333 (pow im 3.0))
    (* -0.016666666666666666 (pow im 5.0))))))
double code(double re, double im) {
	return (0.5 * sin(re)) * ((-2.0 * im) + ((-0.3333333333333333 * pow(im, 3.0)) + (-0.016666666666666666 * pow(im, 5.0))));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (0.5d0 * sin(re)) * (((-2.0d0) * im) + (((-0.3333333333333333d0) * (im ** 3.0d0)) + ((-0.016666666666666666d0) * (im ** 5.0d0))))
end function
public static double code(double re, double im) {
	return (0.5 * Math.sin(re)) * ((-2.0 * im) + ((-0.3333333333333333 * Math.pow(im, 3.0)) + (-0.016666666666666666 * Math.pow(im, 5.0))));
}
def code(re, im):
	return (0.5 * math.sin(re)) * ((-2.0 * im) + ((-0.3333333333333333 * math.pow(im, 3.0)) + (-0.016666666666666666 * math.pow(im, 5.0))))
function code(re, im)
	return Float64(Float64(0.5 * sin(re)) * Float64(Float64(-2.0 * im) + Float64(Float64(-0.3333333333333333 * (im ^ 3.0)) + Float64(-0.016666666666666666 * (im ^ 5.0)))))
end
function tmp = code(re, im)
	tmp = (0.5 * sin(re)) * ((-2.0 * im) + ((-0.3333333333333333 * (im ^ 3.0)) + (-0.016666666666666666 * (im ^ 5.0))));
end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[(-2.0 * im), $MachinePrecision] + N[(N[(-0.3333333333333333 * N[Power[im, 3.0], $MachinePrecision]), $MachinePrecision] + N[(-0.016666666666666666 * N[Power[im, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \sin re\right) \cdot \left(-2 \cdot im + \left(-0.3333333333333333 \cdot {im}^{3} + -0.016666666666666666 \cdot {im}^{5}\right)\right)
\end{array}
Derivation
  1. Initial program 69.5%

    \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in im around 0 89.0%

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \color{blue}{\left(-2 \cdot im + \left(-0.3333333333333333 \cdot {im}^{3} + -0.016666666666666666 \cdot {im}^{5}\right)\right)} \]
  4. Final simplification89.0%

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \left(-2 \cdot im + \left(-0.3333333333333333 \cdot {im}^{3} + -0.016666666666666666 \cdot {im}^{5}\right)\right) \]
  5. Add Preprocessing

Alternative 2: 83.5% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (sin re) (- (* (pow im 3.0) -0.16666666666666666) im)))
double code(double re, double im) {
	return sin(re) * ((pow(im, 3.0) * -0.16666666666666666) - im);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = sin(re) * (((im ** 3.0d0) * (-0.16666666666666666d0)) - im)
end function
public static double code(double re, double im) {
	return Math.sin(re) * ((Math.pow(im, 3.0) * -0.16666666666666666) - im);
}
def code(re, im):
	return math.sin(re) * ((math.pow(im, 3.0) * -0.16666666666666666) - im)
function code(re, im)
	return Float64(sin(re) * Float64(Float64((im ^ 3.0) * -0.16666666666666666) - im))
end
function tmp = code(re, im)
	tmp = sin(re) * (((im ^ 3.0) * -0.16666666666666666) - im);
end
code[re_, im_] := N[(N[Sin[re], $MachinePrecision] * N[(N[(N[Power[im, 3.0], $MachinePrecision] * -0.16666666666666666), $MachinePrecision] - im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)
\end{array}
Derivation
  1. Initial program 69.5%

    \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in im around 0 82.5%

    \[\leadsto \color{blue}{-1 \cdot \left(im \cdot \sin re\right) + -0.16666666666666666 \cdot \left({im}^{3} \cdot \sin re\right)} \]
  4. Step-by-step derivation
    1. +-commutative82.5%

      \[\leadsto \color{blue}{-0.16666666666666666 \cdot \left({im}^{3} \cdot \sin re\right) + -1 \cdot \left(im \cdot \sin re\right)} \]
    2. mul-1-neg82.5%

      \[\leadsto -0.16666666666666666 \cdot \left({im}^{3} \cdot \sin re\right) + \color{blue}{\left(-im \cdot \sin re\right)} \]
    3. unsub-neg82.5%

      \[\leadsto \color{blue}{-0.16666666666666666 \cdot \left({im}^{3} \cdot \sin re\right) - im \cdot \sin re} \]
    4. associate-*r*82.5%

      \[\leadsto \color{blue}{\left(-0.16666666666666666 \cdot {im}^{3}\right) \cdot \sin re} - im \cdot \sin re \]
    5. distribute-rgt-out--82.5%

      \[\leadsto \color{blue}{\sin re \cdot \left(-0.16666666666666666 \cdot {im}^{3} - im\right)} \]
    6. *-commutative82.5%

      \[\leadsto \sin re \cdot \left(\color{blue}{{im}^{3} \cdot -0.16666666666666666} - im\right) \]
  5. Simplified82.5%

    \[\leadsto \color{blue}{\sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)} \]
  6. Final simplification82.5%

    \[\leadsto \sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right) \]
  7. Add Preprocessing

Alternative 3: 55.1% accurate, 1.5× speedup?

\[\begin{array}{l} \\ -0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* -0.008333333333333333 (* (sin re) (pow im 5.0))))
double code(double re, double im) {
	return -0.008333333333333333 * (sin(re) * pow(im, 5.0));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (-0.008333333333333333d0) * (sin(re) * (im ** 5.0d0))
end function
public static double code(double re, double im) {
	return -0.008333333333333333 * (Math.sin(re) * Math.pow(im, 5.0));
}
def code(re, im):
	return -0.008333333333333333 * (math.sin(re) * math.pow(im, 5.0))
function code(re, im)
	return Float64(-0.008333333333333333 * Float64(sin(re) * (im ^ 5.0)))
end
function tmp = code(re, im)
	tmp = -0.008333333333333333 * (sin(re) * (im ^ 5.0));
end
code[re_, im_] := N[(-0.008333333333333333 * N[(N[Sin[re], $MachinePrecision] * N[Power[im, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
-0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right)
\end{array}
Derivation
  1. Initial program 69.5%

    \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in im around 0 89.0%

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \color{blue}{\left(-2 \cdot im + \left(-0.3333333333333333 \cdot {im}^{3} + -0.016666666666666666 \cdot {im}^{5}\right)\right)} \]
  4. Taylor expanded in im around inf 57.7%

    \[\leadsto \color{blue}{-0.008333333333333333 \cdot \left({im}^{5} \cdot \sin re\right)} \]
  5. Final simplification57.7%

    \[\leadsto -0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right) \]
  6. Add Preprocessing

Alternative 4: 51.3% accurate, 3.0× speedup?

\[\begin{array}{l} \\ \sin re \cdot \left(-im\right) \end{array} \]
(FPCore (re im) :precision binary64 (* (sin re) (- im)))
double code(double re, double im) {
	return sin(re) * -im;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = sin(re) * -im
end function
public static double code(double re, double im) {
	return Math.sin(re) * -im;
}
def code(re, im):
	return math.sin(re) * -im
function code(re, im)
	return Float64(sin(re) * Float64(-im))
end
function tmp = code(re, im)
	tmp = sin(re) * -im;
end
code[re_, im_] := N[(N[Sin[re], $MachinePrecision] * (-im)), $MachinePrecision]
\begin{array}{l}

\\
\sin re \cdot \left(-im\right)
\end{array}
Derivation
  1. Initial program 69.5%

    \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in im around 0 48.3%

    \[\leadsto \color{blue}{-1 \cdot \left(im \cdot \sin re\right)} \]
  4. Step-by-step derivation
    1. associate-*r*48.3%

      \[\leadsto \color{blue}{\left(-1 \cdot im\right) \cdot \sin re} \]
    2. neg-mul-148.3%

      \[\leadsto \color{blue}{\left(-im\right)} \cdot \sin re \]
  5. Simplified48.3%

    \[\leadsto \color{blue}{\left(-im\right) \cdot \sin re} \]
  6. Final simplification48.3%

    \[\leadsto \sin re \cdot \left(-im\right) \]
  7. Add Preprocessing

Alternative 5: 32.7% accurate, 77.0× speedup?

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

\\
-re \cdot im
\end{array}
Derivation
  1. Initial program 69.5%

    \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in im around 0 48.3%

    \[\leadsto \color{blue}{-1 \cdot \left(im \cdot \sin re\right)} \]
  4. Step-by-step derivation
    1. associate-*r*48.3%

      \[\leadsto \color{blue}{\left(-1 \cdot im\right) \cdot \sin re} \]
    2. neg-mul-148.3%

      \[\leadsto \color{blue}{\left(-im\right)} \cdot \sin re \]
  5. Simplified48.3%

    \[\leadsto \color{blue}{\left(-im\right) \cdot \sin re} \]
  6. Taylor expanded in re around 0 33.4%

    \[\leadsto \color{blue}{-1 \cdot \left(im \cdot re\right)} \]
  7. Step-by-step derivation
    1. mul-1-neg33.4%

      \[\leadsto \color{blue}{-im \cdot re} \]
    2. distribute-rgt-neg-in33.4%

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

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

    \[\leadsto -re \cdot im \]
  10. Add Preprocessing

Alternative 6: 3.2% accurate, 102.7× speedup?

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

\\
re \cdot 4
\end{array}
Derivation
  1. Initial program 69.5%

    \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in re around 0 54.0%

    \[\leadsto \color{blue}{0.5 \cdot \left(re \cdot \left(e^{-im} - e^{im}\right)\right)} \]
  4. Step-by-step derivation
    1. associate-*r*54.0%

      \[\leadsto \color{blue}{\left(0.5 \cdot re\right) \cdot \left(e^{-im} - e^{im}\right)} \]
    2. *-commutative54.0%

      \[\leadsto \color{blue}{\left(e^{-im} - e^{im}\right) \cdot \left(0.5 \cdot re\right)} \]
  5. Simplified54.0%

    \[\leadsto \color{blue}{\left(e^{-im} - e^{im}\right) \cdot \left(0.5 \cdot re\right)} \]
  6. Applied egg-rr3.5%

    \[\leadsto \color{blue}{8} \cdot \left(0.5 \cdot re\right) \]
  7. Taylor expanded in re around 0 3.5%

    \[\leadsto \color{blue}{4 \cdot re} \]
  8. Step-by-step derivation
    1. *-commutative3.5%

      \[\leadsto \color{blue}{re \cdot 4} \]
  9. Simplified3.5%

    \[\leadsto \color{blue}{re \cdot 4} \]
  10. Final simplification3.5%

    \[\leadsto re \cdot 4 \]
  11. Add Preprocessing

Developer target: 99.8% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left|im\right| < 1:\\ \;\;\;\;-\sin re \cdot \left(\left(im + \left(\left(0.16666666666666666 \cdot im\right) \cdot im\right) \cdot im\right) + \left(\left(\left(\left(0.008333333333333333 \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (< (fabs im) 1.0)
   (-
    (*
     (sin re)
     (+
      (+ im (* (* (* 0.16666666666666666 im) im) im))
      (* (* (* (* (* 0.008333333333333333 im) im) im) im) im))))
   (* (* 0.5 (sin re)) (- (exp (- im)) (exp im)))))
double code(double re, double im) {
	double tmp;
	if (fabs(im) < 1.0) {
		tmp = -(sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)));
	} else {
		tmp = (0.5 * sin(re)) * (exp(-im) - exp(im));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (abs(im) < 1.0d0) then
        tmp = -(sin(re) * ((im + (((0.16666666666666666d0 * im) * im) * im)) + (((((0.008333333333333333d0 * im) * im) * im) * im) * im)))
    else
        tmp = (0.5d0 * sin(re)) * (exp(-im) - exp(im))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (Math.abs(im) < 1.0) {
		tmp = -(Math.sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)));
	} else {
		tmp = (0.5 * Math.sin(re)) * (Math.exp(-im) - Math.exp(im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if math.fabs(im) < 1.0:
		tmp = -(math.sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)))
	else:
		tmp = (0.5 * math.sin(re)) * (math.exp(-im) - math.exp(im))
	return tmp
function code(re, im)
	tmp = 0.0
	if (abs(im) < 1.0)
		tmp = Float64(-Float64(sin(re) * Float64(Float64(im + Float64(Float64(Float64(0.16666666666666666 * im) * im) * im)) + Float64(Float64(Float64(Float64(Float64(0.008333333333333333 * im) * im) * im) * im) * im))));
	else
		tmp = Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(-im)) - exp(im)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (abs(im) < 1.0)
		tmp = -(sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)));
	else
		tmp = (0.5 * sin(re)) * (exp(-im) - exp(im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Less[N[Abs[im], $MachinePrecision], 1.0], (-N[(N[Sin[re], $MachinePrecision] * N[(N[(im + N[(N[(N[(0.16666666666666666 * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(N[(N[(0.008333333333333333 * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\left|im\right| < 1:\\
\;\;\;\;-\sin re \cdot \left(\left(im + \left(\left(0.16666666666666666 \cdot im\right) \cdot im\right) \cdot im\right) + \left(\left(\left(\left(0.008333333333333333 \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right)\\

\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\\


\end{array}
\end{array}

Reproduce

?
herbie shell --seed 2024033 
(FPCore (re im)
  :name "math.cos on complex, imaginary part"
  :precision binary64

  :herbie-target
  (if (< (fabs im) 1.0) (- (* (sin re) (+ (+ im (* (* (* 0.16666666666666666 im) im) im)) (* (* (* (* (* 0.008333333333333333 im) im) im) im) im)))) (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))

  (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))