math.cos on complex, real part

?

Percentage Accurate: 100.0% → 100.0%
Time: 12.1s
Precision: binary64
Cost: 19712

?

\[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
\[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))
(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));
}
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
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));
}
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))
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 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
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]
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)

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.

Herbie found 15 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

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) \]

Alternatives

Alternative 1
Accuracy100.0%
Cost19712
\[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
Alternative 2
Accuracy93.0%
Cost14357
\[\begin{array}{l} t_0 := e^{-im} + e^{im}\\ t_1 := \left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{if}\;im \leq -9.6 \cdot 10^{+158}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -2 \cdot 10^{+71}:\\ \;\;\;\;0.5 \cdot t_0\\ \mathbf{elif}\;im \leq -0.00043 \lor \neg \left(im \leq 33.5\right) \land im \leq 1.34 \cdot 10^{+154}:\\ \;\;\;\;t_0 \cdot \left(0.5 \cdot \left(1 + -0.5 \cdot \left(re \cdot re\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Accuracy93.0%
Cost13714
\[\begin{array}{l} \mathbf{if}\;im \leq -9.6 \cdot 10^{+158} \lor \neg \left(im \leq -0.00043 \lor \neg \left(im \leq 0.031\right) \land im \leq 1.34 \cdot 10^{+154}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \end{array} \]
Alternative 4
Accuracy86.2%
Cost8341
\[\begin{array}{l} t_0 := 2 + \left(im \cdot im + 0.08333333333333333 \cdot {im}^{4}\right)\\ t_1 := \left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{if}\;im \leq -9.6 \cdot 10^{+158}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -7.5 \cdot 10^{+78}:\\ \;\;\;\;0.5 \cdot t_0\\ \mathbf{elif}\;im \leq -0.00043 \lor \neg \left(im \leq 33.5\right) \land im \leq 1.5 \cdot 10^{+154}:\\ \;\;\;\;\left(0.5 \cdot \left(1 + -0.5 \cdot \left(re \cdot re\right)\right)\right) \cdot t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Accuracy86.3%
Cost8085
\[\begin{array}{l} t_0 := 0.08333333333333333 \cdot {im}^{4}\\ t_1 := \left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{if}\;im \leq -9.6 \cdot 10^{+158}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -2 \cdot 10^{+78}:\\ \;\;\;\;0.5 \cdot \left(2 + \left(im \cdot im + t_0\right)\right)\\ \mathbf{elif}\;im \leq -410 \lor \neg \left(im \leq 33.5\right) \land im \leq 1.34 \cdot 10^{+154}:\\ \;\;\;\;\left(0.5 \cdot \left(1 + -0.5 \cdot \left(re \cdot re\right)\right)\right) \cdot \left(2 + t_0\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Accuracy85.5%
Cost7960
\[\begin{array}{l} t_0 := 0.5 \cdot \left(2 + \left(im \cdot im + 0.08333333333333333 \cdot {im}^{4}\right)\right)\\ t_1 := 2 + im \cdot im\\ t_2 := \left(0.5 \cdot \cos re\right) \cdot t_1\\ t_3 := t_1 \cdot \left(0.5 \cdot \left(1 + -0.5 \cdot \left(re \cdot re\right)\right)\right)\\ \mathbf{if}\;im \leq -9.6 \cdot 10^{+158}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq -1.15 \cdot 10^{+77}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -380:\\ \;\;\;\;t_3\\ \mathbf{elif}\;im \leq 33.5:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 1.5 \cdot 10^{+58}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;im \leq 1.34 \cdot 10^{+154}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 7
Accuracy73.1%
Cost7057
\[\begin{array}{l} t_0 := \left(2 + im \cdot im\right) \cdot \left(0.5 \cdot \left(1 + -0.5 \cdot \left(re \cdot re\right)\right)\right)\\ \mathbf{if}\;im \leq -0.000215:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 33.5:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.6 \cdot 10^{+57} \lor \neg \left(im \leq 4.2 \cdot 10^{+116}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;{re}^{-2}\\ \end{array} \]
Alternative 8
Accuracy75.8%
Cost6976
\[\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right) \]
Alternative 9
Accuracy72.9%
Cost6729
\[\begin{array}{l} \mathbf{if}\;im \leq -0.00036 \lor \neg \left(im \leq 33.5\right):\\ \;\;\;\;\left(2 + im \cdot im\right) \cdot \left(0.5 \cdot \left(1 + -0.5 \cdot \left(re \cdot re\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos re\\ \end{array} \]
Alternative 10
Accuracy49.1%
Cost960
\[\left(2 + im \cdot im\right) \cdot \left(0.5 \cdot \left(1 + -0.5 \cdot \left(re \cdot re\right)\right)\right) \]
Alternative 11
Accuracy34.2%
Cost585
\[\begin{array}{l} \mathbf{if}\;im \leq -520 \lor \neg \left(im \leq 600\right):\\ \;\;\;\;-2 - re \cdot re\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 12
Accuracy34.4%
Cost585
\[\begin{array}{l} \mathbf{if}\;im \leq -320 \lor \neg \left(im \leq 550\right):\\ \;\;\;\;re - re \cdot re\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 13
Accuracy47.8%
Cost580
\[\begin{array}{l} \mathbf{if}\;re \leq -4.3 \cdot 10^{+158}:\\ \;\;\;\;-2 - re \cdot re\\ \mathbf{else}:\\ \;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\ \end{array} \]
Alternative 14
Accuracy32.7%
Cost448
\[1 + -0.5 \cdot \left(re \cdot re\right) \]
Alternative 15
Accuracy28.6%
Cost64
\[1 \]

Reproduce?

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