math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 6.3s
Alternatives: 9
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 9 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. Add Preprocessing
  3. Final simplification100.0%

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

Alternative 2: 75.2% accurate, 1.5× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \left(\cos re \cdot \mathsf{fma}\left(im, im, 2\right)\right) \end{array} \]
(FPCore (re im) :precision binary64 (* 0.5 (* (cos re) (fma im im 2.0))))
double code(double re, double im) {
	return 0.5 * (cos(re) * fma(im, im, 2.0));
}
function code(re, im)
	return Float64(0.5 * Float64(cos(re) * fma(im, im, 2.0)))
end
code[re_, im_] := N[(0.5 * N[(N[Cos[re], $MachinePrecision] * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \left(\cos re \cdot \mathsf{fma}\left(im, im, 2\right)\right)
\end{array}
Derivation
  1. Initial program 100.0%

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

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\left(2 + {im}^{2}\right)} \]
  4. Taylor expanded in re around inf 73.8%

    \[\leadsto \color{blue}{0.5 \cdot \left(\cos re \cdot \left(2 + {im}^{2}\right)\right)} \]
  5. Step-by-step derivation
    1. *-commutative73.8%

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(2 + {im}^{2}\right) \cdot \cos re\right)} \]
    2. +-commutative73.8%

      \[\leadsto 0.5 \cdot \left(\color{blue}{\left({im}^{2} + 2\right)} \cdot \cos re\right) \]
    3. unpow273.8%

      \[\leadsto 0.5 \cdot \left(\left(\color{blue}{im \cdot im} + 2\right) \cdot \cos re\right) \]
    4. fma-udef73.8%

      \[\leadsto 0.5 \cdot \left(\color{blue}{\mathsf{fma}\left(im, im, 2\right)} \cdot \cos re\right) \]
  6. Simplified73.8%

    \[\leadsto \color{blue}{0.5 \cdot \left(\mathsf{fma}\left(im, im, 2\right) \cdot \cos re\right)} \]
  7. Final simplification73.8%

    \[\leadsto 0.5 \cdot \left(\cos re \cdot \mathsf{fma}\left(im, im, 2\right)\right) \]
  8. Add Preprocessing

Alternative 3: 65.8% 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. Add Preprocessing
  3. Taylor expanded in re around 0 63.8%

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

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

Alternative 4: 69.0% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(0.5 \cdot im, im, \cos re\right) \end{array} \]
(FPCore (re im) :precision binary64 (fma (* 0.5 im) im (cos re)))
double code(double re, double im) {
	return fma((0.5 * im), im, cos(re));
}
function code(re, im)
	return fma(Float64(0.5 * im), im, cos(re))
end
code[re_, im_] := N[(N[(0.5 * im), $MachinePrecision] * im + N[Cos[re], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(0.5 \cdot im, im, \cos re\right)
\end{array}
Derivation
  1. Initial program 100.0%

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

    \[\leadsto \color{blue}{\cos re + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)} \]
  4. Taylor expanded in re around 0 64.8%

    \[\leadsto \cos re + 0.5 \cdot \color{blue}{{im}^{2}} \]
  5. Step-by-step derivation
    1. +-commutative64.8%

      \[\leadsto \color{blue}{0.5 \cdot {im}^{2} + \cos re} \]
    2. unpow264.8%

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot im\right)} + \cos re \]
    3. associate-*r*64.8%

      \[\leadsto \color{blue}{\left(0.5 \cdot im\right) \cdot im} + \cos re \]
    4. fma-def64.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.5 \cdot im, im, \cos re\right)} \]
  6. Applied egg-rr64.8%

    \[\leadsto \color{blue}{\mathsf{fma}\left(0.5 \cdot im, im, \cos re\right)} \]
  7. Final simplification64.8%

    \[\leadsto \mathsf{fma}\left(0.5 \cdot im, im, \cos re\right) \]
  8. Add Preprocessing

Alternative 5: 47.1% accurate, 2.9× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{fma}\left(im, im, 2\right) \end{array} \]
(FPCore (re im) :precision binary64 (* 0.5 (fma im im 2.0)))
double code(double re, double im) {
	return 0.5 * fma(im, im, 2.0);
}
function code(re, im)
	return Float64(0.5 * fma(im, im, 2.0))
end
code[re_, im_] := N[(0.5 * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \mathsf{fma}\left(im, im, 2\right)
\end{array}
Derivation
  1. Initial program 100.0%

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

    \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
  4. Taylor expanded in im around 0 45.0%

    \[\leadsto 0.5 \cdot \color{blue}{\left(2 + {im}^{2}\right)} \]
  5. Step-by-step derivation
    1. +-commutative45.0%

      \[\leadsto 0.5 \cdot \color{blue}{\left({im}^{2} + 2\right)} \]
    2. unpow245.0%

      \[\leadsto 0.5 \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
    3. fma-def45.0%

      \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
  6. Simplified45.0%

    \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
  7. Final simplification45.0%

    \[\leadsto 0.5 \cdot \mathsf{fma}\left(im, im, 2\right) \]
  8. Add Preprocessing

Alternative 6: 50.7% accurate, 3.0× speedup?

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

\\
\cos re
\end{array}
Derivation
  1. Initial program 100.0%

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

    \[\leadsto \color{blue}{\cos re} \]
  4. Final simplification47.6%

    \[\leadsto \cos re \]
  5. Add Preprocessing

Alternative 7: 3.7% accurate, 308.0× speedup?

\[\begin{array}{l} \\ -1 \end{array} \]
(FPCore (re im) :precision binary64 -1.0)
double code(double re, double im) {
	return -1.0;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = -1.0d0
end function
public static double code(double re, double im) {
	return -1.0;
}
def code(re, im):
	return -1.0
function code(re, im)
	return -1.0
end
function tmp = code(re, im)
	tmp = -1.0;
end
code[re_, im_] := -1.0
\begin{array}{l}

\\
-1
\end{array}
Derivation
  1. Initial program 100.0%

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

    \[\leadsto \color{blue}{\cos re + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)} \]
  4. Applied egg-rr2.9%

    \[\leadsto \cos re + 0.5 \cdot \color{blue}{-4} \]
  5. Taylor expanded in re around 0 3.2%

    \[\leadsto \color{blue}{-1} \]
  6. Final simplification3.2%

    \[\leadsto -1 \]
  7. Add Preprocessing

Alternative 8: 8.1% accurate, 308.0× speedup?

\[\begin{array}{l} \\ 0.25 \end{array} \]
(FPCore (re im) :precision binary64 0.25)
double code(double re, double im) {
	return 0.25;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 0.25d0
end function
public static double code(double re, double im) {
	return 0.25;
}
def code(re, im):
	return 0.25
function code(re, im)
	return 0.25
end
function tmp = code(re, im)
	tmp = 0.25;
end
code[re_, im_] := 0.25
\begin{array}{l}

\\
0.25
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Add Preprocessing
  3. Applied egg-rr8.0%

    \[\leadsto \color{blue}{{\left(\cos re \cdot -2\right)}^{-2}} \]
  4. Taylor expanded in re around 0 8.0%

    \[\leadsto \color{blue}{0.25} \]
  5. Final simplification8.0%

    \[\leadsto 0.25 \]
  6. Add Preprocessing

Alternative 9: 28.5% accurate, 308.0× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (re im) :precision binary64 1.0)
double code(double re, double im) {
	return 1.0;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 1.0d0
end function
public static double code(double re, double im) {
	return 1.0;
}
def code(re, im):
	return 1.0
function code(re, im)
	return 1.0
end
function tmp = code(re, im)
	tmp = 1.0;
end
code[re_, im_] := 1.0
\begin{array}{l}

\\
1
\end{array}
Derivation
  1. Initial program 100.0%

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

    \[\leadsto \color{blue}{\cos re} \]
  4. Taylor expanded in re around 0 27.4%

    \[\leadsto \color{blue}{1} \]
  5. Final simplification27.4%

    \[\leadsto 1 \]
  6. Add Preprocessing

Reproduce

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