
(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 19 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) (cosh im)))
double code(double re, double im) {
return cos(re) * cosh(im);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = cos(re) * cosh(im)
end function
public static double code(double re, double im) {
return Math.cos(re) * Math.cosh(im);
}
def code(re, im): return math.cos(re) * math.cosh(im)
function code(re, im) return Float64(cos(re) * cosh(im)) end
function tmp = code(re, im) tmp = cos(re) * cosh(im); end
code[re_, im_] := N[(N[Cos[re], $MachinePrecision] * N[Cosh[im], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos re \cdot \cosh im
\end{array}
Initial program 100.0%
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
associate-*r*N/A
lower-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
lift-exp.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
cosh-undefN/A
associate-*r*N/A
metadata-evalN/A
lower-*.f64N/A
lower-cosh.f64100.0
Applied rewrites100.0%
Final simplification100.0%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* (+ (exp im) (exp (- im))) (* 0.5 (cos re)))))
(if (<= t_0 (- INFINITY))
(*
(* im im)
(fma
(fma
(fma (* -0.0006944444444444445 re) re 0.020833333333333332)
(* re re)
-0.25)
(* re re)
0.5))
(if (<= t_0 0.9999999999999981)
(*
(fma (fma (* 0.041666666666666664 im) im 0.5) (* im im) 1.0)
(cos re))
(* 1.0 (cosh im))))))
double code(double re, double im) {
double t_0 = (exp(im) + exp(-im)) * (0.5 * cos(re));
double tmp;
if (t_0 <= -((double) INFINITY)) {
tmp = (im * im) * fma(fma(fma((-0.0006944444444444445 * re), re, 0.020833333333333332), (re * re), -0.25), (re * re), 0.5);
} else if (t_0 <= 0.9999999999999981) {
tmp = fma(fma((0.041666666666666664 * im), im, 0.5), (im * im), 1.0) * cos(re);
} else {
tmp = 1.0 * cosh(im);
}
return tmp;
}
function code(re, im) t_0 = Float64(Float64(exp(im) + exp(Float64(-im))) * Float64(0.5 * cos(re))) tmp = 0.0 if (t_0 <= Float64(-Inf)) tmp = Float64(Float64(im * im) * fma(fma(fma(Float64(-0.0006944444444444445 * re), re, 0.020833333333333332), Float64(re * re), -0.25), Float64(re * re), 0.5)); elseif (t_0 <= 0.9999999999999981) tmp = Float64(fma(fma(Float64(0.041666666666666664 * im), im, 0.5), Float64(im * im), 1.0) * cos(re)); else tmp = Float64(1.0 * cosh(im)); end return tmp end
code[re_, im_] := Block[{t$95$0 = N[(N[(N[Exp[im], $MachinePrecision] + N[Exp[(-im)], $MachinePrecision]), $MachinePrecision] * N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], N[(N[(im * im), $MachinePrecision] * N[(N[(N[(N[(-0.0006944444444444445 * re), $MachinePrecision] * re + 0.020833333333333332), $MachinePrecision] * N[(re * re), $MachinePrecision] + -0.25), $MachinePrecision] * N[(re * re), $MachinePrecision] + 0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 0.9999999999999981], N[(N[(N[(N[(0.041666666666666664 * im), $MachinePrecision] * im + 0.5), $MachinePrecision] * N[(im * im), $MachinePrecision] + 1.0), $MachinePrecision] * N[Cos[re], $MachinePrecision]), $MachinePrecision], N[(1.0 * N[Cosh[im], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(e^{im} + e^{-im}\right) \cdot \left(0.5 \cdot \cos re\right)\\
\mathbf{if}\;t\_0 \leq -\infty:\\
\;\;\;\;\left(im \cdot im\right) \cdot \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.0006944444444444445 \cdot re, re, 0.020833333333333332\right), re \cdot re, -0.25\right), re \cdot re, 0.5\right)\\
\mathbf{elif}\;t\_0 \leq 0.9999999999999981:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(0.041666666666666664 \cdot im, im, 0.5\right), im \cdot im, 1\right) \cdot \cos re\\
\mathbf{else}:\\
\;\;\;\;1 \cdot \cosh im\\
\end{array}
\end{array}
if (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))) < -inf.0Initial program 100.0%
Taylor expanded in im around 0
+-commutativeN/A
unpow2N/A
lower-fma.f6451.6
Applied rewrites51.6%
Taylor expanded in re around 0
Applied rewrites0.2%
Taylor expanded in im around inf
Applied rewrites0.2%
Taylor expanded in re around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6496.3
Applied rewrites96.3%
if -inf.0 < (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))) < 0.999999999999998113Initial program 100.0%
Taylor expanded in im around 0
+-commutativeN/A
*-commutativeN/A
+-commutativeN/A
associate-*r*N/A
distribute-rgt-outN/A
associate-*l*N/A
*-rgt-identityN/A
distribute-lft-outN/A
lower-*.f64N/A
lower-cos.f64N/A
lower-fma.f64N/A
+-commutativeN/A
unpow2N/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.5
Applied rewrites99.5%
if 0.999999999999998113 < (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))) Initial program 100.0%
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
associate-*r*N/A
lower-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
lift-exp.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
cosh-undefN/A
associate-*r*N/A
metadata-evalN/A
lower-*.f64N/A
lower-cosh.f64100.0
Applied rewrites100.0%
Taylor expanded in re around 0
Applied rewrites99.9%
lift-*.f64N/A
*-lft-identity99.9
Applied rewrites99.9%
Final simplification99.4%
herbie shell --seed 2024230
(FPCore (re im)
:name "math.cos on complex, real part"
:precision binary64
(* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))