math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 3.8s
Alternatives: 7
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 7 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: 84.6% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 460 \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\
\;\;\;\;\cos re \cdot \left(0.5 \cdot \left(im \cdot im\right) + 1\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 460 or 1.35000000000000003e154 < im

    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 83.6%

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

        \[\leadsto \cos re + \color{blue}{\left(0.5 \cdot {im}^{2}\right) \cdot \cos re} \]
      2. distribute-rgt1-in83.6%

        \[\leadsto \color{blue}{\left(0.5 \cdot {im}^{2} + 1\right) \cdot \cos re} \]
    5. Simplified83.6%

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

        \[\leadsto 1 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
    7. Applied egg-rr83.6%

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

    if 460 < im < 1.35000000000000003e154

    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 69.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 460 \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\ \;\;\;\;\cos re \cdot \left(0.5 \cdot \left(im \cdot im\right) + 1\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 76.3% accurate, 2.8× speedup?

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

\\
\cos re \cdot \left(0.5 \cdot \left(im \cdot im\right) + 1\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.5%

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

      \[\leadsto \cos re + \color{blue}{\left(0.5 \cdot {im}^{2}\right) \cdot \cos re} \]
    2. distribute-rgt1-in73.5%

      \[\leadsto \color{blue}{\left(0.5 \cdot {im}^{2} + 1\right) \cdot \cos re} \]
  5. Simplified73.5%

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

      \[\leadsto 1 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
  7. Applied egg-rr73.5%

    \[\leadsto \left(0.5 \cdot \color{blue}{\left(im \cdot im\right)} + 1\right) \cdot \cos re \]
  8. Final simplification73.5%

    \[\leadsto \cos re \cdot \left(0.5 \cdot \left(im \cdot im\right) + 1\right) \]
  9. Add Preprocessing

Alternative 4: 60.7% accurate, 2.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 1.05 \cdot 10^{+62}:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot im\right) + 1\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 1.05e+62) (cos re) (+ (* 0.5 (* im im)) 1.0)))
double code(double re, double im) {
	double tmp;
	if (im <= 1.05e+62) {
		tmp = cos(re);
	} else {
		tmp = (0.5 * (im * im)) + 1.0;
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 1.05d+62) then
        tmp = cos(re)
    else
        tmp = (0.5d0 * (im * im)) + 1.0d0
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 1.05e+62) {
		tmp = Math.cos(re);
	} else {
		tmp = (0.5 * (im * im)) + 1.0;
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 1.05e+62:
		tmp = math.cos(re)
	else:
		tmp = (0.5 * (im * im)) + 1.0
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 1.05e+62)
		tmp = cos(re);
	else
		tmp = Float64(Float64(0.5 * Float64(im * im)) + 1.0);
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 1.05e+62)
		tmp = cos(re);
	else
		tmp = (0.5 * (im * im)) + 1.0;
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 1.05e+62], N[Cos[re], $MachinePrecision], N[(N[(0.5 * N[(im * im), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 1.05 \cdot 10^{+62}:\\
\;\;\;\;\cos re\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot im\right) + 1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 1.05e62

    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 63.4%

      \[\leadsto \color{blue}{\cos re} \]

    if 1.05e62 < im

    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 59.6%

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

        \[\leadsto \cos re + \color{blue}{\left(0.5 \cdot {im}^{2}\right) \cdot \cos re} \]
      2. distribute-rgt1-in59.6%

        \[\leadsto \color{blue}{\left(0.5 \cdot {im}^{2} + 1\right) \cdot \cos re} \]
    5. Simplified59.6%

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

      \[\leadsto \color{blue}{1 + 0.5 \cdot {im}^{2}} \]
    7. Step-by-step derivation
      1. unpow245.2%

        \[\leadsto 1 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
    8. Applied egg-rr45.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1.05 \cdot 10^{+62}:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot im\right) + 1\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 47.9% accurate, 44.0× speedup?

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

\\
0.5 \cdot \left(im \cdot im\right) + 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.5%

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

      \[\leadsto \cos re + \color{blue}{\left(0.5 \cdot {im}^{2}\right) \cdot \cos re} \]
    2. distribute-rgt1-in73.5%

      \[\leadsto \color{blue}{\left(0.5 \cdot {im}^{2} + 1\right) \cdot \cos re} \]
  5. Simplified73.5%

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

    \[\leadsto \color{blue}{1 + 0.5 \cdot {im}^{2}} \]
  7. Step-by-step derivation
    1. unpow242.3%

      \[\leadsto 1 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
  8. Applied egg-rr42.3%

    \[\leadsto 1 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
  9. Final simplification42.3%

    \[\leadsto 0.5 \cdot \left(im \cdot im\right) + 1 \]
  10. Add Preprocessing

Alternative 6: 2.4% accurate, 308.0× speedup?

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

\\
0
\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-rr2.4%

    \[\leadsto \color{blue}{\log \left({1}^{\cos re}\right)} \]
  4. Step-by-step derivation
    1. pow-base-12.4%

      \[\leadsto \log \color{blue}{1} \]
    2. metadata-eval2.4%

      \[\leadsto \color{blue}{0} \]
  5. Simplified2.4%

    \[\leadsto \color{blue}{0} \]
  6. Final simplification2.4%

    \[\leadsto 0 \]
  7. Add Preprocessing

Alternative 7: 28.6% 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. Applied egg-rr26.0%

    \[\leadsto \color{blue}{\frac{\cos re \cdot -2}{\cos re \cdot -2 + \left(\cos re \cdot -2 - \cos re \cdot -2\right)}} \]
  4. Step-by-step derivation
    1. +-inverses26.0%

      \[\leadsto \frac{\cos re \cdot -2}{\cos re \cdot -2 + \color{blue}{0}} \]
    2. +-rgt-identity26.0%

      \[\leadsto \frac{\cos re \cdot -2}{\color{blue}{\cos re \cdot -2}} \]
    3. *-inverses26.0%

      \[\leadsto \color{blue}{1} \]
  5. Simplified26.0%

    \[\leadsto \color{blue}{1} \]
  6. Final simplification26.0%

    \[\leadsto 1 \]
  7. Add Preprocessing

Reproduce

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