
(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 14 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 (* (cos re) (fma 0.5 (exp im) (/ 0.5 (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(cos(re) * fma(0.5, exp(im), Float64(0.5 / exp(im)))) end
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]
\begin{array}{l}
\\
\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)
\end{array}
Initial program 100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (re im) :precision binary64 (* (* (cos re) 0.5) (+ (exp im) (exp (- im)))))
double code(double re, double im) {
return (cos(re) * 0.5) * (exp(im) + exp(-im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (cos(re) * 0.5d0) * (exp(im) + exp(-im))
end function
public static double code(double re, double im) {
return (Math.cos(re) * 0.5) * (Math.exp(im) + Math.exp(-im));
}
def code(re, im): return (math.cos(re) * 0.5) * (math.exp(im) + math.exp(-im))
function code(re, im) return Float64(Float64(cos(re) * 0.5) * Float64(exp(im) + exp(Float64(-im)))) end
function tmp = code(re, im) tmp = (cos(re) * 0.5) * (exp(im) + exp(-im)); end
code[re_, im_] := N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(N[Exp[im], $MachinePrecision] + N[Exp[(-im)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\cos re \cdot 0.5\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) (* (* (cos re) 0.5) (fma im im 2.0)) (* (cos re) (fma 0.5 (exp im) 0.0))))
double code(double re, double im) {
double tmp;
if (im <= 1.3) {
tmp = (cos(re) * 0.5) * fma(im, im, 2.0);
} else {
tmp = cos(re) * fma(0.5, exp(im), 0.0);
}
return tmp;
}
function code(re, im) tmp = 0.0 if (im <= 1.3) tmp = Float64(Float64(cos(re) * 0.5) * fma(im, im, 2.0)); else tmp = Float64(cos(re) * fma(0.5, exp(im), 0.0)); end return tmp end
code[re_, im_] := If[LessEqual[im, 1.3], N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[(0.5 * N[Exp[im], $MachinePrecision] + 0.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 1.3:\\
\;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\
\mathbf{else}:\\
\;\;\;\;\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, 0\right)\\
\end{array}
\end{array}
if im < 1.30000000000000004Initial program 100.0%
Taylor expanded in im around 0 78.0%
+-commutative78.0%
unpow278.0%
fma-def78.0%
Simplified78.0%
if 1.30000000000000004 < im Initial program 100.0%
Simplified100.0%
Applied egg-rr99.9%
Final simplification84.1%
(FPCore (re im)
:precision binary64
(if (<= im 4000.0)
(cos re)
(if (<= im 1.15e+77)
(* 0.5 (+ (exp im) (exp (- im))))
(* 0.041666666666666664 (* (cos re) (pow im 4.0))))))
double code(double re, double im) {
double tmp;
if (im <= 4000.0) {
tmp = cos(re);
} else if (im <= 1.15e+77) {
tmp = 0.5 * (exp(im) + exp(-im));
} else {
tmp = 0.041666666666666664 * (cos(re) * pow(im, 4.0));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 4000.0d0) then
tmp = cos(re)
else if (im <= 1.15d+77) then
tmp = 0.5d0 * (exp(im) + exp(-im))
else
tmp = 0.041666666666666664d0 * (cos(re) * (im ** 4.0d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 4000.0) {
tmp = Math.cos(re);
} else if (im <= 1.15e+77) {
tmp = 0.5 * (Math.exp(im) + Math.exp(-im));
} else {
tmp = 0.041666666666666664 * (Math.cos(re) * Math.pow(im, 4.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 4000.0: tmp = math.cos(re) elif im <= 1.15e+77: tmp = 0.5 * (math.exp(im) + math.exp(-im)) else: tmp = 0.041666666666666664 * (math.cos(re) * math.pow(im, 4.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= 4000.0) tmp = cos(re); elseif (im <= 1.15e+77) tmp = Float64(0.5 * Float64(exp(im) + exp(Float64(-im)))); else tmp = Float64(0.041666666666666664 * Float64(cos(re) * (im ^ 4.0))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 4000.0) tmp = cos(re); elseif (im <= 1.15e+77) tmp = 0.5 * (exp(im) + exp(-im)); else tmp = 0.041666666666666664 * (cos(re) * (im ^ 4.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 4000.0], N[Cos[re], $MachinePrecision], If[LessEqual[im, 1.15e+77], N[(0.5 * N[(N[Exp[im], $MachinePrecision] + N[Exp[(-im)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.041666666666666664 * N[(N[Cos[re], $MachinePrecision] * N[Power[im, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 4000:\\
\;\;\;\;\cos re\\
\mathbf{elif}\;im \leq 1.15 \cdot 10^{+77}:\\
\;\;\;\;0.5 \cdot \left(e^{im} + e^{-im}\right)\\
\mathbf{else}:\\
\;\;\;\;0.041666666666666664 \cdot \left(\cos re \cdot {im}^{4}\right)\\
\end{array}
\end{array}
if im < 4e3Initial program 100.0%
Simplified100.0%
Taylor expanded in im around 0 62.6%
if 4e3 < im < 1.14999999999999997e77Initial program 100.0%
Taylor expanded in re around 0 75.0%
if 1.14999999999999997e77 < im Initial program 100.0%
Simplified100.0%
Taylor expanded in im around 0 100.0%
+-commutative100.0%
associate-+r+100.0%
associate-+l+100.0%
associate-*r*100.0%
associate-*r*100.0%
distribute-rgt-out100.0%
fma-def100.0%
associate-*r*100.0%
distribute-lft1-in100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in im around 0 100.0%
associate-*r*100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in re around inf 100.0%
Taylor expanded in im around inf 100.0%
Final simplification70.9%
(FPCore (re im)
:precision binary64
(if (<= im 4000.0)
(* (* (cos re) 0.5) (fma im im 2.0))
(if (<= im 1.15e+77)
(* 0.5 (+ (exp im) (exp (- im))))
(* 0.041666666666666664 (* (cos re) (pow im 4.0))))))
double code(double re, double im) {
double tmp;
if (im <= 4000.0) {
tmp = (cos(re) * 0.5) * fma(im, im, 2.0);
} else if (im <= 1.15e+77) {
tmp = 0.5 * (exp(im) + exp(-im));
} else {
tmp = 0.041666666666666664 * (cos(re) * pow(im, 4.0));
}
return tmp;
}
function code(re, im) tmp = 0.0 if (im <= 4000.0) tmp = Float64(Float64(cos(re) * 0.5) * fma(im, im, 2.0)); elseif (im <= 1.15e+77) tmp = Float64(0.5 * Float64(exp(im) + exp(Float64(-im)))); else tmp = Float64(0.041666666666666664 * Float64(cos(re) * (im ^ 4.0))); end return tmp end
code[re_, im_] := If[LessEqual[im, 4000.0], N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.15e+77], N[(0.5 * N[(N[Exp[im], $MachinePrecision] + N[Exp[(-im)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.041666666666666664 * N[(N[Cos[re], $MachinePrecision] * N[Power[im, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 4000:\\
\;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\
\mathbf{elif}\;im \leq 1.15 \cdot 10^{+77}:\\
\;\;\;\;0.5 \cdot \left(e^{im} + e^{-im}\right)\\
\mathbf{else}:\\
\;\;\;\;0.041666666666666664 \cdot \left(\cos re \cdot {im}^{4}\right)\\
\end{array}
\end{array}
if im < 4e3Initial program 100.0%
Taylor expanded in im around 0 76.8%
+-commutative76.8%
unpow276.8%
fma-def76.8%
Simplified76.8%
if 4e3 < im < 1.14999999999999997e77Initial program 100.0%
Taylor expanded in re around 0 75.0%
if 1.14999999999999997e77 < im Initial program 100.0%
Simplified100.0%
Taylor expanded in im around 0 100.0%
+-commutative100.0%
associate-+r+100.0%
associate-+l+100.0%
associate-*r*100.0%
associate-*r*100.0%
distribute-rgt-out100.0%
fma-def100.0%
associate-*r*100.0%
distribute-lft1-in100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in im around 0 100.0%
associate-*r*100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in re around inf 100.0%
Taylor expanded in im around inf 100.0%
Final simplification81.4%
(FPCore (re im) :precision binary64 (if (<= im 2.2) (cos re) (* 0.041666666666666664 (* (cos re) (pow im 4.0)))))
double code(double re, double im) {
double tmp;
if (im <= 2.2) {
tmp = cos(re);
} else {
tmp = 0.041666666666666664 * (cos(re) * pow(im, 4.0));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 2.2d0) then
tmp = cos(re)
else
tmp = 0.041666666666666664d0 * (cos(re) * (im ** 4.0d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 2.2) {
tmp = Math.cos(re);
} else {
tmp = 0.041666666666666664 * (Math.cos(re) * Math.pow(im, 4.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 2.2: tmp = math.cos(re) else: tmp = 0.041666666666666664 * (math.cos(re) * math.pow(im, 4.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= 2.2) tmp = cos(re); else tmp = Float64(0.041666666666666664 * Float64(cos(re) * (im ^ 4.0))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 2.2) tmp = cos(re); else tmp = 0.041666666666666664 * (cos(re) * (im ^ 4.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 2.2], N[Cos[re], $MachinePrecision], N[(0.041666666666666664 * N[(N[Cos[re], $MachinePrecision] * N[Power[im, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 2.2:\\
\;\;\;\;\cos re\\
\mathbf{else}:\\
\;\;\;\;0.041666666666666664 \cdot \left(\cos re \cdot {im}^{4}\right)\\
\end{array}
\end{array}
if im < 2.2000000000000002Initial program 100.0%
Simplified100.0%
Taylor expanded in im around 0 63.5%
if 2.2000000000000002 < im Initial program 100.0%
Simplified100.0%
Taylor expanded in im around 0 77.3%
+-commutative77.3%
associate-+r+77.3%
associate-+l+77.3%
associate-*r*77.3%
associate-*r*77.3%
distribute-rgt-out77.3%
fma-def77.3%
associate-*r*77.3%
distribute-lft1-in77.3%
fma-def77.3%
Simplified77.3%
Taylor expanded in im around 0 74.5%
associate-*r*74.5%
*-commutative74.5%
Simplified74.5%
Taylor expanded in re around inf 74.5%
Taylor expanded in im around inf 74.5%
Final simplification66.5%
(FPCore (re im) :precision binary64 (if (<= im 3.8e+41) (cos re) (* 0.5 (fma im im 2.0))))
double code(double re, double im) {
double tmp;
if (im <= 3.8e+41) {
tmp = cos(re);
} else {
tmp = 0.5 * fma(im, im, 2.0);
}
return tmp;
}
function code(re, im) tmp = 0.0 if (im <= 3.8e+41) tmp = cos(re); else tmp = Float64(0.5 * fma(im, im, 2.0)); end return tmp end
code[re_, im_] := If[LessEqual[im, 3.8e+41], N[Cos[re], $MachinePrecision], N[(0.5 * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 3.8 \cdot 10^{+41}:\\
\;\;\;\;\cos re\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\
\end{array}
\end{array}
if im < 3.8000000000000001e41Initial program 100.0%
Simplified100.0%
Taylor expanded in im around 0 59.0%
if 3.8000000000000001e41 < im Initial program 100.0%
Taylor expanded in re around 0 69.6%
Taylor expanded in im around 0 44.5%
+-commutative44.5%
unpow244.5%
fma-def44.5%
Simplified44.5%
Final simplification55.8%
(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%
Simplified100.0%
Taylor expanded in im around 0 46.8%
Final simplification46.8%
(FPCore (re im) :precision binary64 -0.5)
double code(double re, double im) {
return -0.5;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = -0.5d0
end function
public static double code(double re, double im) {
return -0.5;
}
def code(re, im): return -0.5
function code(re, im) return -0.5 end
function tmp = code(re, im) tmp = -0.5; end
code[re_, im_] := -0.5
\begin{array}{l}
\\
-0.5
\end{array}
Initial program 100.0%
Taylor expanded in re around 0 66.1%
Applied egg-rr3.6%
Final simplification3.6%
(FPCore (re im) :precision binary64 0.125)
double code(double re, double im) {
return 0.125;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.125d0
end function
public static double code(double re, double im) {
return 0.125;
}
def code(re, im): return 0.125
function code(re, im) return 0.125 end
function tmp = code(re, im) tmp = 0.125; end
code[re_, im_] := 0.125
\begin{array}{l}
\\
0.125
\end{array}
Initial program 100.0%
Taylor expanded in re around 0 66.1%
Applied egg-rr7.2%
Final simplification7.2%
(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}
Initial program 100.0%
Taylor expanded in re around 0 66.1%
Applied egg-rr7.5%
Final simplification7.5%
(FPCore (re im) :precision binary64 0.5)
double code(double re, double im) {
return 0.5;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0
end function
public static double code(double re, double im) {
return 0.5;
}
def code(re, im): return 0.5
function code(re, im) return 0.5 end
function tmp = code(re, im) tmp = 0.5; end
code[re_, im_] := 0.5
\begin{array}{l}
\\
0.5
\end{array}
Initial program 100.0%
Taylor expanded in re around 0 66.1%
Applied egg-rr8.0%
Final simplification8.0%
(FPCore (re im) :precision binary64 0.75)
double code(double re, double im) {
return 0.75;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.75d0
end function
public static double code(double re, double im) {
return 0.75;
}
def code(re, im): return 0.75
function code(re, im) return 0.75 end
function tmp = code(re, im) tmp = 0.75; end
code[re_, im_] := 0.75
\begin{array}{l}
\\
0.75
\end{array}
Initial program 100.0%
Taylor expanded in re around 0 66.1%
Applied egg-rr8.4%
Final simplification8.4%
(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%
Taylor expanded in re around 0 66.1%
Taylor expanded in im around 0 28.2%
Final simplification28.2%
herbie shell --seed 2023320
(FPCore (re im)
:name "math.cos on complex, real part"
:precision binary64
(* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))