
(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 8 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}
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (* (cos re) (fma 0.5 (exp im_m) (/ 0.5 (exp im_m)))))
im_m = fabs(im);
double code(double re, double im_m) {
return cos(re) * fma(0.5, exp(im_m), (0.5 / exp(im_m)));
}
im_m = abs(im) function code(re, im_m) return Float64(cos(re) * fma(0.5, exp(im_m), Float64(0.5 / exp(im_m)))) end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := N[(N[Cos[re], $MachinePrecision] * N[(0.5 * N[Exp[im$95$m], $MachinePrecision] + N[(0.5 / N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
\cos re \cdot \mathsf{fma}\left(0.5, e^{im\_m}, \frac{0.5}{e^{im\_m}}\right)
\end{array}
Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-lft-neg-in100.0%
cos-neg100.0%
*-commutative100.0%
fma-neg100.0%
remove-double-neg100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Final simplification100.0%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (* (* (cos re) 0.5) (+ (exp im_m) (exp (- im_m)))))
im_m = fabs(im);
double code(double re, double im_m) {
return (cos(re) * 0.5) * (exp(im_m) + exp(-im_m));
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
code = (cos(re) * 0.5d0) * (exp(im_m) + exp(-im_m))
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
return (Math.cos(re) * 0.5) * (Math.exp(im_m) + Math.exp(-im_m));
}
im_m = math.fabs(im) def code(re, im_m): return (math.cos(re) * 0.5) * (math.exp(im_m) + math.exp(-im_m))
im_m = abs(im) function code(re, im_m) return Float64(Float64(cos(re) * 0.5) * Float64(exp(im_m) + exp(Float64(-im_m)))) end
im_m = abs(im); function tmp = code(re, im_m) tmp = (cos(re) * 0.5) * (exp(im_m) + exp(-im_m)); end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(N[Exp[im$95$m], $MachinePrecision] + N[Exp[(-im$95$m)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
\left(\cos re \cdot 0.5\right) \cdot \left(e^{im\_m} + e^{-im\_m}\right)
\end{array}
Initial program 100.0%
Final simplification100.0%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= im_m 1.3) (* (* (cos re) 0.5) (fma im_m im_m 2.0)) (* (cos re) (* 0.5 (exp im_m)))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (im_m <= 1.3) {
tmp = (cos(re) * 0.5) * fma(im_m, im_m, 2.0);
} else {
tmp = cos(re) * (0.5 * exp(im_m));
}
return tmp;
}
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (im_m <= 1.3) tmp = Float64(Float64(cos(re) * 0.5) * fma(im_m, im_m, 2.0)); else tmp = Float64(cos(re) * Float64(0.5 * exp(im_m))); end return tmp end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[im$95$m, 1.3], N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(im$95$m * im$95$m + 2.0), $MachinePrecision]), $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[(0.5 * N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;im\_m \leq 1.3:\\
\;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im\_m, im\_m, 2\right)\\
\mathbf{else}:\\
\;\;\;\;\cos re \cdot \left(0.5 \cdot e^{im\_m}\right)\\
\end{array}
\end{array}
if im < 1.30000000000000004Initial program 100.0%
Taylor expanded in im around 0 86.2%
+-commutative86.2%
unpow286.2%
fma-define86.2%
Simplified86.2%
if 1.30000000000000004 < im Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-lft-neg-in100.0%
cos-neg100.0%
*-commutative100.0%
fma-neg100.0%
remove-double-neg100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Applied egg-rr97.5%
Taylor expanded in re around inf 97.5%
*-commutative97.5%
associate-*r*97.5%
Simplified97.5%
Final simplification88.7%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= im_m 0.7) (cos re) (* (cos re) (* 0.5 (exp im_m)))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (im_m <= 0.7) {
tmp = cos(re);
} else {
tmp = cos(re) * (0.5 * exp(im_m));
}
return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (im_m <= 0.7d0) then
tmp = cos(re)
else
tmp = cos(re) * (0.5d0 * exp(im_m))
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (im_m <= 0.7) {
tmp = Math.cos(re);
} else {
tmp = Math.cos(re) * (0.5 * Math.exp(im_m));
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if im_m <= 0.7: tmp = math.cos(re) else: tmp = math.cos(re) * (0.5 * math.exp(im_m)) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (im_m <= 0.7) tmp = cos(re); else tmp = Float64(cos(re) * Float64(0.5 * exp(im_m))); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (im_m <= 0.7) tmp = cos(re); else tmp = cos(re) * (0.5 * exp(im_m)); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[im$95$m, 0.7], N[Cos[re], $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[(0.5 * N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;im\_m \leq 0.7:\\
\;\;\;\;\cos re\\
\mathbf{else}:\\
\;\;\;\;\cos re \cdot \left(0.5 \cdot e^{im\_m}\right)\\
\end{array}
\end{array}
if im < 0.69999999999999996Initial program 100.0%
Taylor expanded in im around 0 86.2%
+-commutative86.2%
unpow286.2%
fma-define86.2%
Simplified86.2%
Taylor expanded in im around 0 69.6%
if 0.69999999999999996 < im Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-lft-neg-in100.0%
cos-neg100.0%
*-commutative100.0%
fma-neg100.0%
remove-double-neg100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Applied egg-rr97.5%
Taylor expanded in re around inf 97.5%
*-commutative97.5%
associate-*r*97.5%
Simplified97.5%
Final simplification75.6%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= im_m 1.2) (cos re) (* 0.5 (exp im_m))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (im_m <= 1.2) {
tmp = cos(re);
} else {
tmp = 0.5 * exp(im_m);
}
return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (im_m <= 1.2d0) then
tmp = cos(re)
else
tmp = 0.5d0 * exp(im_m)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (im_m <= 1.2) {
tmp = Math.cos(re);
} else {
tmp = 0.5 * Math.exp(im_m);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if im_m <= 1.2: tmp = math.cos(re) else: tmp = 0.5 * math.exp(im_m) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (im_m <= 1.2) tmp = cos(re); else tmp = Float64(0.5 * exp(im_m)); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (im_m <= 1.2) tmp = cos(re); else tmp = 0.5 * exp(im_m); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[im$95$m, 1.2], N[Cos[re], $MachinePrecision], N[(0.5 * N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;im\_m \leq 1.2:\\
\;\;\;\;\cos re\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot e^{im\_m}\\
\end{array}
\end{array}
if im < 1.19999999999999996Initial program 100.0%
Taylor expanded in im around 0 86.2%
+-commutative86.2%
unpow286.2%
fma-define86.2%
Simplified86.2%
Taylor expanded in im around 0 69.6%
if 1.19999999999999996 < im Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-lft-neg-in100.0%
cos-neg100.0%
*-commutative100.0%
fma-neg100.0%
remove-double-neg100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Applied egg-rr97.5%
Taylor expanded in re around 0 73.9%
Final simplification70.5%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (cos re))
im_m = fabs(im);
double code(double re, double im_m) {
return cos(re);
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
code = cos(re)
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
return Math.cos(re);
}
im_m = math.fabs(im) def code(re, im_m): return math.cos(re)
im_m = abs(im) function code(re, im_m) return cos(re) end
im_m = abs(im); function tmp = code(re, im_m) tmp = cos(re); end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := N[Cos[re], $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
\cos re
\end{array}
Initial program 100.0%
Taylor expanded in im around 0 77.5%
+-commutative77.5%
unpow277.5%
fma-define77.5%
Simplified77.5%
Taylor expanded in im around 0 55.4%
Final simplification55.4%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (* 0.5 (+ im_m 1.0)))
im_m = fabs(im);
double code(double re, double im_m) {
return 0.5 * (im_m + 1.0);
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
code = 0.5d0 * (im_m + 1.0d0)
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
return 0.5 * (im_m + 1.0);
}
im_m = math.fabs(im) def code(re, im_m): return 0.5 * (im_m + 1.0)
im_m = abs(im) function code(re, im_m) return Float64(0.5 * Float64(im_m + 1.0)) end
im_m = abs(im); function tmp = code(re, im_m) tmp = 0.5 * (im_m + 1.0); end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := N[(0.5 * N[(im$95$m + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
0.5 \cdot \left(im\_m + 1\right)
\end{array}
Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-lft-neg-in100.0%
cos-neg100.0%
*-commutative100.0%
fma-neg100.0%
remove-double-neg100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Applied egg-rr31.5%
Taylor expanded in im around 0 11.5%
distribute-lft-out11.5%
distribute-rgt1-in11.5%
Simplified11.5%
Taylor expanded in re around 0 9.3%
+-commutative9.3%
Simplified9.3%
Final simplification9.3%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (* 0.5 im_m))
im_m = fabs(im);
double code(double re, double im_m) {
return 0.5 * im_m;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
code = 0.5d0 * im_m
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
return 0.5 * im_m;
}
im_m = math.fabs(im) def code(re, im_m): return 0.5 * im_m
im_m = abs(im) function code(re, im_m) return Float64(0.5 * im_m) end
im_m = abs(im); function tmp = code(re, im_m) tmp = 0.5 * im_m; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := N[(0.5 * im$95$m), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
0.5 \cdot im\_m
\end{array}
Initial program 100.0%
cos-neg100.0%
*-commutative100.0%
associate-*l*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-lft-neg-in100.0%
cos-neg100.0%
*-commutative100.0%
fma-neg100.0%
remove-double-neg100.0%
exp-neg100.0%
associate-*l/100.0%
metadata-eval100.0%
Simplified100.0%
Applied egg-rr31.5%
Taylor expanded in im around 0 11.5%
distribute-lft-out11.5%
distribute-rgt1-in11.5%
Simplified11.5%
Taylor expanded in im around inf 3.2%
*-commutative3.2%
Simplified3.2%
Taylor expanded in re around 0 3.3%
Final simplification3.3%
herbie shell --seed 2024066
(FPCore (re im)
:name "math.cos on complex, real part"
:precision binary64
(* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))