
(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 9 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}
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (* (cos re) (fma 0.5 (exp im) (/ 0.5 (exp im)))))
im = abs(im);
double code(double re, double im) {
return cos(re) * fma(0.5, exp(im), (0.5 / exp(im)));
}
im = abs(im) function code(re, im) return Float64(cos(re) * fma(0.5, exp(im), Float64(0.5 / exp(im)))) end
NOTE: im should be positive before calling this function 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}
im = |im|\\
\\
\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%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (* (* (cos re) 0.5) (+ (exp im) (exp (- im)))))
im = abs(im);
double code(double re, double im) {
return (cos(re) * 0.5) * (exp(im) + exp(-im));
}
NOTE: im should be positive before calling this function
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
im = Math.abs(im);
public static double code(double re, double im) {
return (Math.cos(re) * 0.5) * (Math.exp(im) + Math.exp(-im));
}
im = abs(im) def code(re, im): return (math.cos(re) * 0.5) * (math.exp(im) + math.exp(-im))
im = abs(im) function code(re, im) return Float64(Float64(cos(re) * 0.5) * Float64(exp(im) + exp(Float64(-im)))) end
im = abs(im) function tmp = code(re, im) tmp = (cos(re) * 0.5) * (exp(im) + exp(-im)); end
NOTE: im should be positive before calling this function 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}
im = |im|\\
\\
\left(\cos re \cdot 0.5\right) \cdot \left(e^{im} + e^{-im}\right)
\end{array}
Initial program 100.0%
Final simplification100.0%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= im 1.3) (* (* (cos re) 0.5) (fma im im 2.0)) (* 0.5 (* (cos re) (exp im)))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (im <= 1.3) {
tmp = (cos(re) * 0.5) * fma(im, im, 2.0);
} else {
tmp = 0.5 * (cos(re) * exp(im));
}
return tmp;
}
im = abs(im) 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(0.5 * Float64(cos(re) * exp(im))); end return tmp end
NOTE: im should be positive before calling this function 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[(0.5 * N[(N[Cos[re], $MachinePrecision] * N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\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}:\\
\;\;\;\;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 82.1%
+-commutative82.1%
unpow282.1%
fma-def82.1%
Simplified82.1%
if 1.30000000000000004 < im Initial program 100.0%
Simplified100.0%
Applied egg-rr99.1%
Taylor expanded in re around inf 99.1%
*-commutative99.1%
Simplified99.1%
Final simplification87.1%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= im 0.72) (+ (cos re) (* 0.5 (pow im 2.0))) (* 0.5 (* (cos re) (exp im)))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (im <= 0.72) {
tmp = cos(re) + (0.5 * pow(im, 2.0));
} else {
tmp = 0.5 * (cos(re) * exp(im));
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 0.72d0) then
tmp = cos(re) + (0.5d0 * (im ** 2.0d0))
else
tmp = 0.5d0 * (cos(re) * exp(im))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (im <= 0.72) {
tmp = Math.cos(re) + (0.5 * Math.pow(im, 2.0));
} else {
tmp = 0.5 * (Math.cos(re) * Math.exp(im));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if im <= 0.72: tmp = math.cos(re) + (0.5 * math.pow(im, 2.0)) else: tmp = 0.5 * (math.cos(re) * math.exp(im)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (im <= 0.72) tmp = Float64(cos(re) + Float64(0.5 * (im ^ 2.0))); else tmp = Float64(0.5 * Float64(cos(re) * exp(im))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (im <= 0.72) tmp = cos(re) + (0.5 * (im ^ 2.0)); else tmp = 0.5 * (cos(re) * exp(im)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[im, 0.72], N[(N[Cos[re], $MachinePrecision] + N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Cos[re], $MachinePrecision] * N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.72:\\
\;\;\;\;\cos re + 0.5 \cdot {im}^{2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\cos re \cdot e^{im}\right)\\
\end{array}
\end{array}
if im < 0.71999999999999997Initial program 100.0%
Simplified100.0%
Taylor expanded in im around 0 82.1%
Taylor expanded in re around 0 77.1%
*-commutative77.1%
Simplified77.1%
if 0.71999999999999997 < im Initial program 100.0%
Simplified100.0%
Applied egg-rr99.1%
Taylor expanded in re around inf 99.1%
*-commutative99.1%
Simplified99.1%
Final simplification83.5%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= im 0.7) (cos re) (* 0.5 (* (cos re) (exp im)))))
im = abs(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;
}
NOTE: im should be positive before calling this function
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
im = Math.abs(im);
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;
}
im = abs(im) 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
im = abs(im) 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
im = abs(im) 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
NOTE: im should be positive before calling this function 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}
im = |im|\\
\\
\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%
Simplified100.0%
Taylor expanded in im around 0 63.1%
if 0.69999999999999996 < im Initial program 100.0%
Simplified100.0%
Applied egg-rr99.1%
Taylor expanded in re around inf 99.1%
*-commutative99.1%
Simplified99.1%
Final simplification73.6%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= im 2.6) (cos re) (* 0.5 (exp im))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (im <= 2.6) {
tmp = cos(re);
} else {
tmp = 0.5 * exp(im);
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 2.6d0) then
tmp = cos(re)
else
tmp = 0.5d0 * exp(im)
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (im <= 2.6) {
tmp = Math.cos(re);
} else {
tmp = 0.5 * Math.exp(im);
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if im <= 2.6: tmp = math.cos(re) else: tmp = 0.5 * math.exp(im) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (im <= 2.6) tmp = cos(re); else tmp = Float64(0.5 * exp(im)); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (im <= 2.6) tmp = cos(re); else tmp = 0.5 * exp(im); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[im, 2.6], N[Cos[re], $MachinePrecision], N[(0.5 * N[Exp[im], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;im \leq 2.6:\\
\;\;\;\;\cos re\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot e^{im}\\
\end{array}
\end{array}
if im < 2.60000000000000009Initial program 100.0%
Simplified100.0%
Taylor expanded in im around 0 62.8%
if 2.60000000000000009 < im Initial program 100.0%
Simplified100.0%
Applied egg-rr100.0%
Taylor expanded in re around 0 82.4%
Final simplification68.5%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (cos re))
im = abs(im);
double code(double re, double im) {
return cos(re);
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = cos(re)
end function
im = Math.abs(im);
public static double code(double re, double im) {
return Math.cos(re);
}
im = abs(im) def code(re, im): return math.cos(re)
im = abs(im) function code(re, im) return cos(re) end
im = abs(im) function tmp = code(re, im) tmp = cos(re); end
NOTE: im should be positive before calling this function code[re_, im_] := N[Cos[re], $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
\cos re
\end{array}
Initial program 100.0%
Simplified100.0%
Taylor expanded in im around 0 45.6%
Final simplification45.6%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 1.5)
im = abs(im);
double code(double re, double im) {
return 1.5;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 1.5d0
end function
im = Math.abs(im);
public static double code(double re, double im) {
return 1.5;
}
im = abs(im) def code(re, im): return 1.5
im = abs(im) function code(re, im) return 1.5 end
im = abs(im) function tmp = code(re, im) tmp = 1.5; end
NOTE: im should be positive before calling this function code[re_, im_] := 1.5
\begin{array}{l}
im = |im|\\
\\
1.5
\end{array}
Initial program 100.0%
Simplified100.0%
Taylor expanded in im around 0 71.5%
Applied egg-rr9.2%
Taylor expanded in re around 0 7.4%
Final simplification7.4%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 1.0)
im = abs(im);
double code(double re, double im) {
return 1.0;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 1.0d0
end function
im = Math.abs(im);
public static double code(double re, double im) {
return 1.0;
}
im = abs(im) def code(re, im): return 1.0
im = abs(im) function code(re, im) return 1.0 end
im = abs(im) function tmp = code(re, im) tmp = 1.0; end
NOTE: im should be positive before calling this function code[re_, im_] := 1.0
\begin{array}{l}
im = |im|\\
\\
1
\end{array}
Initial program 100.0%
Taylor expanded in re around 0 65.9%
Taylor expanded in im around 0 23.5%
Final simplification23.5%
herbie shell --seed 2023312
(FPCore (re im)
:name "math.cos on complex, real part"
:precision binary64
(* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))