
(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:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(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}
Initial program 100.0%
Final simplification100.0%
(FPCore (re im) :precision binary64 (if (<= im 1.3) (* (* 0.5 (cos re)) (fma im im 2.0)) (* 0.5 (* (cos re) (exp im)))))
double code(double re, double im) {
double tmp;
if (im <= 1.3) {
tmp = (0.5 * cos(re)) * fma(im, im, 2.0);
} else {
tmp = 0.5 * (cos(re) * exp(im));
}
return tmp;
}
function code(re, im) tmp = 0.0 if (im <= 1.3) tmp = Float64(Float64(0.5 * cos(re)) * fma(im, im, 2.0)); else tmp = Float64(0.5 * Float64(cos(re) * exp(im))); end return tmp end
code[re_, im_] := If[LessEqual[im, 1.3], N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Cos[re], $MachinePrecision] * N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 1.3:\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\cos re \cdot e^{im}\right)\\
\end{array}
\end{array}
if im < 1.30000000000000004Initial program 100.0%
Taylor expanded in im around 0 84.1%
+-commutative84.1%
unpow284.1%
fma-def84.1%
Simplified84.1%
if 1.30000000000000004 < im Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cos-neg100.0%
distribute-rgt-in100.0%
distribute-lft-in100.0%
*-commutative100.0%
fma-def100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Applied egg-rr100.0%
Taylor expanded in re around inf 100.0%
*-commutative100.0%
Simplified100.0%
Final simplification88.0%
(FPCore (re im) :precision binary64 (if (<= im 0.7) (cos re) (* 0.5 (* (cos re) (exp im)))))
double code(double re, double im) {
double tmp;
if (im <= 0.7) {
tmp = cos(re);
} else {
tmp = 0.5 * (cos(re) * 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 <= 0.7d0) then
tmp = cos(re)
else
tmp = 0.5d0 * (cos(re) * exp(im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 0.7) {
tmp = Math.cos(re);
} else {
tmp = 0.5 * (Math.cos(re) * Math.exp(im));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 0.7: tmp = math.cos(re) else: tmp = 0.5 * (math.cos(re) * math.exp(im)) return tmp
function code(re, im) tmp = 0.0 if (im <= 0.7) tmp = cos(re); else tmp = Float64(0.5 * Float64(cos(re) * exp(im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 0.7) tmp = cos(re); else tmp = 0.5 * (cos(re) * exp(im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 0.7], N[Cos[re], $MachinePrecision], N[(0.5 * N[(N[Cos[re], $MachinePrecision] * N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.7:\\
\;\;\;\;\cos re\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\cos re \cdot e^{im}\right)\\
\end{array}
\end{array}
if im < 0.69999999999999996Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cos-neg100.0%
distribute-rgt-in100.0%
distribute-lft-in100.0%
*-commutative100.0%
fma-def100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in im around 0 67.6%
if 0.69999999999999996 < im Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cos-neg100.0%
distribute-rgt-in100.0%
distribute-lft-in100.0%
*-commutative100.0%
fma-def100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Applied egg-rr100.0%
Taylor expanded in re around inf 100.0%
*-commutative100.0%
Simplified100.0%
Final simplification75.6%
(FPCore (re im) :precision binary64 (if (<= im 1.3) (cos re) (* 0.5 (exp im))))
double code(double re, double im) {
double tmp;
if (im <= 1.3) {
tmp = cos(re);
} else {
tmp = 0.5 * 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 <= 1.3d0) then
tmp = cos(re)
else
tmp = 0.5d0 * exp(im)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 1.3) {
tmp = Math.cos(re);
} else {
tmp = 0.5 * Math.exp(im);
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 1.3: tmp = math.cos(re) else: tmp = 0.5 * math.exp(im) return tmp
function code(re, im) tmp = 0.0 if (im <= 1.3) tmp = cos(re); else tmp = Float64(0.5 * exp(im)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 1.3) tmp = cos(re); else tmp = 0.5 * exp(im); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 1.3], N[Cos[re], $MachinePrecision], N[(0.5 * N[Exp[im], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 1.3:\\
\;\;\;\;\cos re\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot e^{im}\\
\end{array}
\end{array}
if im < 1.30000000000000004Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cos-neg100.0%
distribute-rgt-in100.0%
distribute-lft-in100.0%
*-commutative100.0%
fma-def100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in im around 0 67.6%
if 1.30000000000000004 < im Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cos-neg100.0%
distribute-rgt-in100.0%
distribute-lft-in100.0%
*-commutative100.0%
fma-def100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Applied egg-rr100.0%
Taylor expanded in re around 0 76.2%
Final simplification69.7%
(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}
Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cos-neg100.0%
distribute-rgt-in100.0%
distribute-lft-in100.0%
*-commutative100.0%
fma-def100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in im around 0 51.8%
Final simplification51.8%
(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}
Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cos-neg100.0%
distribute-rgt-in100.0%
distribute-lft-in100.0%
*-commutative100.0%
fma-def100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in im around 0 51.8%
Applied egg-rr33.5%
unpow-133.5%
Simplified33.5%
Taylor expanded in re around 0 31.9%
Final simplification31.9%
herbie shell --seed 2024019
(FPCore (re im)
:name "math.cos on complex, real part"
:precision binary64
(* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))