math.cos on complex, real part

?

Percentage Accurate: 100.0% → 100.0%
Time: 8.8s
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 12 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
Accuracy99.4%
Cost19913
\[\begin{array}{l} \mathbf{if}\;im \leq -360 \lor \neg \left(im \leq 360\right):\\ \;\;\;\;0.5 \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(im \cdot \left(\cos re \cdot im\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + \left(im \cdot im\right) \cdot \left(1 + \left(im \cdot im\right) \cdot 0.08333333333333333\right)\right)\\ \end{array} \]
Alternative 3
Accuracy94.2%
Cost19849
\[\begin{array}{l} \mathbf{if}\;im \leq -2 \cdot 10^{+20} \lor \neg \left(im \leq 1000000\right):\\ \;\;\;\;\left(\cos re \cdot 0.041666666666666664\right) \cdot \sqrt{{im}^{8}}\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + \left(im \cdot im\right) \cdot \left(1 + \left(im \cdot im\right) \cdot 0.08333333333333333\right)\right)\\ \end{array} \]
Alternative 4
Accuracy88.5%
Cost7488
\[\left(0.5 \cdot \cos re\right) \cdot \left(2 + \left(im \cdot im\right) \cdot \left(1 + \left(im \cdot im\right) \cdot 0.08333333333333333\right)\right) \]
Alternative 5
Accuracy86.1%
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \left(\cos re \cdot \left(im \cdot im\right)\right)\\ \mathbf{if}\;im \leq -7.8 \cdot 10^{+155}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1900:\\ \;\;\;\;\left(\left(im \cdot im\right) \cdot \left(im \cdot im\right)\right) \cdot \left(0.041666666666666664 + \left(re \cdot re\right) \cdot -0.020833333333333332\right)\\ \mathbf{elif}\;im \leq 17000000000:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;0.041666666666666664 \cdot {im}^{4}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Accuracy86.3%
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \left(\cos re \cdot \left(im \cdot im\right)\right)\\ \mathbf{if}\;im \leq -7.8 \cdot 10^{+155}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -105000:\\ \;\;\;\;\left(\left(im \cdot im\right) \cdot \left(im \cdot im\right)\right) \cdot \left(0.041666666666666664 + \left(re \cdot re\right) \cdot -0.020833333333333332\right)\\ \mathbf{elif}\;im \leq 660000000000:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;0.041666666666666664 \cdot {im}^{4}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Accuracy88.5%
Cost7369
\[\begin{array}{l} \mathbf{if}\;im \leq -3.7 \lor \neg \left(im \leq 3.7\right):\\ \;\;\;\;\left(\cos re \cdot 0.041666666666666664\right) \cdot \left(\left(im \cdot im\right) \cdot \left(im \cdot im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \end{array} \]
Alternative 8
Accuracy80.2%
Cost6993
\[\begin{array}{l} \mathbf{if}\;im \leq -7 \cdot 10^{+264}:\\ \;\;\;\;0.5 \cdot \left(\left(im \cdot im\right) \cdot \left(1 + \left(re \cdot re\right) \cdot -0.5\right)\right)\\ \mathbf{elif}\;im \leq -4 \cdot 10^{+166}:\\ \;\;\;\;0.041666666666666664 \cdot {im}^{4}\\ \mathbf{elif}\;im \leq -740 \lor \neg \left(im \leq 410\right):\\ \;\;\;\;\left(\left(im \cdot im\right) \cdot \left(im \cdot im\right)\right) \cdot \left(0.041666666666666664 + \left(re \cdot re\right) \cdot -0.020833333333333332\right)\\ \mathbf{else}:\\ \;\;\;\;\cos re\\ \end{array} \]
Alternative 9
Accuracy80.3%
Cost6729
\[\begin{array}{l} \mathbf{if}\;im \leq -490 \lor \neg \left(im \leq 1550\right):\\ \;\;\;\;\left(\left(im \cdot im\right) \cdot \left(im \cdot im\right)\right) \cdot \left(0.041666666666666664 + \left(re \cdot re\right) \cdot -0.020833333333333332\right)\\ \mathbf{else}:\\ \;\;\;\;\cos re\\ \end{array} \]
Alternative 10
Accuracy32.7%
Cost960
\[\left(\left(im \cdot im\right) \cdot \left(im \cdot im\right)\right) \cdot \left(0.041666666666666664 + \left(re \cdot re\right) \cdot -0.020833333333333332\right) \]
Alternative 11
Accuracy26.2%
Cost832
\[0.5 \cdot \left(\left(im \cdot im\right) \cdot \left(1 + \left(re \cdot re\right) \cdot -0.5\right)\right) \]
Alternative 12
Accuracy12.5%
Cost704
\[0.5 \cdot \left(\left(im \cdot im\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right)\right) \]

Reproduce?

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