math.cos on complex, real part

?

Percentage Accurate: 100.0% → 100.0%
Time: 8.6s
Precision: binary64
Cost: 26048

?

\[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
\[\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right) \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))
(FPCore (re im)
 :precision binary64
 (* (cos re) (fma 0.5 (exp im) (/ 0.5 (exp im)))))
double code(double re, double im) {
	return (0.5 * cos(re)) * (exp(-im) + exp(im));
}
double code(double re, double im) {
	return cos(re) * fma(0.5, exp(im), (0.5 / 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(cos(re) * fma(0.5, exp(im), Float64(0.5 / 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[Cos[re], $MachinePrecision] * N[(0.5 * N[Exp[im], $MachinePrecision] + N[(0.5 / N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{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 14 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

Derivation?

  1. Initial program 100.0%

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

    \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    Step-by-step derivation

    [Start]100.0%

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

    *-commutative [=>]100.0%

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

    associate-*l* [=>]100.0%

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

    +-commutative [=>]100.0%

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

    distribute-lft-in [=>]100.0%

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

    distribute-lft-in [<=]100.0%

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

    distribute-rgt-in [=>]100.0%

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

    *-commutative [=>]100.0%

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

    fma-def [=>]100.0%

    \[ \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]

    exp-neg [=>]100.0%

    \[ \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]

    associate-*l/ [=>]100.0%

    \[ \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]

    metadata-eval [=>]100.0%

    \[ \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{\color{blue}{0.5}}{e^{im}}\right) \]
  3. Final simplification100.0%

    \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right) \]

Alternatives

Alternative 1
Accuracy100.0%
Cost26048
\[\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right) \]
Alternative 2
Accuracy100.0%
Cost19712
\[\left(\cos re \cdot 0.5\right) \cdot \left(e^{im} + e^{-im}\right) \]
Alternative 3
Accuracy92.7%
Cost13712
\[\begin{array}{l} t_0 := im \cdot \left(\cos re \cdot \left(0.5 \cdot im\right)\right)\\ t_1 := 0.5 \cdot \left(e^{im} + e^{-im}\right)\\ \mathbf{if}\;im \leq -1.4 \cdot 10^{+159}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -0.057:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 0.0145:\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{elif}\;im \leq 8 \cdot 10^{+151}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Accuracy95.9%
Cost13712
\[\begin{array}{l} t_0 := 0.5 \cdot \left(e^{im} + e^{-im}\right)\\ t_1 := {im}^{4} \cdot \left(\cos re \cdot 0.041666666666666664\right)\\ \mathbf{if}\;im \leq -5.1 \cdot 10^{+82}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.088:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 0.0145:\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{elif}\;im \leq 1.15 \cdot 10^{+73}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Accuracy97.1%
Cost13512
\[\begin{array}{l} \mathbf{if}\;im \leq -5.1 \cdot 10^{+82}:\\ \;\;\;\;{im}^{4} \cdot \left(\cos re \cdot 0.041666666666666664\right)\\ \mathbf{elif}\;im \leq -9 \cdot 10^{-10}:\\ \;\;\;\;0.5 \cdot \left(e^{im} + e^{-im}\right)\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left(0.5 + 0.5 \cdot e^{im}\right)\\ \end{array} \]
Alternative 6
Accuracy83.9%
Cost7244
\[\begin{array}{l} t_0 := im \cdot \left(\cos re \cdot \left(0.5 \cdot im\right)\right)\\ \mathbf{if}\;im \leq -1.4:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 1.75:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 8 \cdot 10^{+151}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Accuracy84.2%
Cost7112
\[\begin{array}{l} \mathbf{if}\;im \leq 3.5:\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{elif}\;im \leq 8 \cdot 10^{+151}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(\cos re \cdot \left(0.5 \cdot im\right)\right)\\ \end{array} \]
Alternative 8
Accuracy77.4%
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -1.2 \cdot 10^{+47}:\\ \;\;\;\;im \cdot \left(0.5 \cdot im\right)\\ \mathbf{elif}\;im \leq 2:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im}\\ \end{array} \]
Alternative 9
Accuracy69.8%
Cost6728
\[\begin{array}{l} t_0 := 0.5 \cdot \left(im \cdot im\right)\\ t_1 := im \cdot \left(0.5 \cdot im\right)\\ t_2 := im \cdot \left(im \cdot \left(re \cdot \left(re \cdot -0.25\right)\right)\right)\\ \mathbf{if}\;im \leq -4 \cdot 10^{+47}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 5.8 \cdot 10^{+86}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2.3 \cdot 10^{+139}:\\ \;\;\;\;\frac{t_0 \cdot t_0 - t_2 \cdot t_2}{t_0 - t_2}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 10
Accuracy47.9%
Cost3272
\[\begin{array}{l} t_0 := im \cdot \left(im \cdot \left(re \cdot \left(re \cdot -0.25\right)\right)\right)\\ t_1 := 0.5 \cdot \left(im \cdot im\right)\\ \mathbf{if}\;im \leq 5.8 \cdot 10^{+86}:\\ \;\;\;\;t_1 + 1\\ \mathbf{elif}\;im \leq 2.3 \cdot 10^{+139}:\\ \;\;\;\;\frac{t_1 \cdot t_1 - t_0 \cdot t_0}{t_1 - t_0}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(0.5 \cdot im\right)\\ \end{array} \]
Alternative 11
Accuracy46.0%
Cost585
\[\begin{array}{l} \mathbf{if}\;im \leq -0.042 \lor \neg \left(im \leq 1.45\right):\\ \;\;\;\;im \cdot \left(0.5 \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 12
Accuracy46.2%
Cost448
\[0.5 \cdot \left(im \cdot im\right) + 1 \]
Alternative 13
Accuracy4.0%
Cost64
\[-1 \]
Alternative 14
Accuracy28.2%
Cost64
\[1 \]

Reproduce?

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