math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 4.2s
Alternatives: 3
Speedup: 1.0×

Specification

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

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

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

Alternative 1: 100.0% accurate, 1.0× speedup?

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

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Final simplification100.0%

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]

Alternative 2: 65.3% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{-im} + e^{im}\\ \mathbf{if}\;im \leq -5 \cdot 10^{+89} \lor \neg \left(im \leq 2.6 \cdot 10^{+255}\right):\\ \;\;\;\;t_0 \cdot \left(0.5 \cdot \left(1 + -0.5 \cdot \left(re \cdot re\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot t_0\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (+ (exp (- im)) (exp im))))
   (if (or (<= im -5e+89) (not (<= im 2.6e+255)))
     (* t_0 (* 0.5 (+ 1.0 (* -0.5 (* re re)))))
     (* 0.5 t_0))))
double code(double re, double im) {
	double t_0 = exp(-im) + exp(im);
	double tmp;
	if ((im <= -5e+89) || !(im <= 2.6e+255)) {
		tmp = t_0 * (0.5 * (1.0 + (-0.5 * (re * re))));
	} else {
		tmp = 0.5 * t_0;
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = exp(-im) + exp(im)
    if ((im <= (-5d+89)) .or. (.not. (im <= 2.6d+255))) then
        tmp = t_0 * (0.5d0 * (1.0d0 + ((-0.5d0) * (re * re))))
    else
        tmp = 0.5d0 * t_0
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = Math.exp(-im) + Math.exp(im);
	double tmp;
	if ((im <= -5e+89) || !(im <= 2.6e+255)) {
		tmp = t_0 * (0.5 * (1.0 + (-0.5 * (re * re))));
	} else {
		tmp = 0.5 * t_0;
	}
	return tmp;
}
def code(re, im):
	t_0 = math.exp(-im) + math.exp(im)
	tmp = 0
	if (im <= -5e+89) or not (im <= 2.6e+255):
		tmp = t_0 * (0.5 * (1.0 + (-0.5 * (re * re))))
	else:
		tmp = 0.5 * t_0
	return tmp
function code(re, im)
	t_0 = Float64(exp(Float64(-im)) + exp(im))
	tmp = 0.0
	if ((im <= -5e+89) || !(im <= 2.6e+255))
		tmp = Float64(t_0 * Float64(0.5 * Float64(1.0 + Float64(-0.5 * Float64(re * re)))));
	else
		tmp = Float64(0.5 * t_0);
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = exp(-im) + exp(im);
	tmp = 0.0;
	if ((im <= -5e+89) || ~((im <= 2.6e+255)))
		tmp = t_0 * (0.5 * (1.0 + (-0.5 * (re * re))));
	else
		tmp = 0.5 * t_0;
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[im, -5e+89], N[Not[LessEqual[im, 2.6e+255]], $MachinePrecision]], N[(t$95$0 * N[(0.5 * N[(1.0 + N[(-0.5 * N[(re * re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * t$95$0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{-im} + e^{im}\\
\mathbf{if}\;im \leq -5 \cdot 10^{+89} \lor \neg \left(im \leq 2.6 \cdot 10^{+255}\right):\\
\;\;\;\;t_0 \cdot \left(0.5 \cdot \left(1 + -0.5 \cdot \left(re \cdot re\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot t_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < -4.99999999999999983e89 or 2.6000000000000001e255 < im

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Taylor expanded in re around 0 89.7%

      \[\leadsto \left(0.5 \cdot \color{blue}{\left(1 + -0.5 \cdot {re}^{2}\right)}\right) \cdot \left(e^{-im} + e^{im}\right) \]
    3. Step-by-step derivation
      1. unpow289.7%

        \[\leadsto \left(0.5 \cdot \left(1 + -0.5 \cdot \color{blue}{\left(re \cdot re\right)}\right)\right) \cdot \left(e^{-im} + e^{im}\right) \]
    4. Simplified89.7%

      \[\leadsto \left(0.5 \cdot \color{blue}{\left(1 + -0.5 \cdot \left(re \cdot re\right)\right)}\right) \cdot \left(e^{-im} + e^{im}\right) \]

    if -4.99999999999999983e89 < im < 2.6000000000000001e255

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Taylor expanded in re around 0 63.7%

      \[\leadsto \left(0.5 \cdot \color{blue}{1}\right) \cdot \left(e^{-im} + e^{im}\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification69.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -5 \cdot 10^{+89} \lor \neg \left(im \leq 2.6 \cdot 10^{+255}\right):\\ \;\;\;\;\left(e^{-im} + e^{im}\right) \cdot \left(0.5 \cdot \left(1 + -0.5 \cdot \left(re \cdot re\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \end{array} \]

Alternative 3: 65.3% accurate, 1.5× speedup?

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

\\
0.5 \cdot \left(e^{-im} + e^{im}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Taylor expanded in re around 0 63.7%

    \[\leadsto \left(0.5 \cdot \color{blue}{1}\right) \cdot \left(e^{-im} + e^{im}\right) \]
  3. Final simplification63.7%

    \[\leadsto 0.5 \cdot \left(e^{-im} + e^{im}\right) \]

Reproduce

?
herbie shell --seed 2023195 
(FPCore (re im)
  :name "math.cos on complex, real part"
  :precision binary64
  (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))