
(FPCore (re im) :precision binary64 (* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))
double code(double re, double im) {
return (0.5 * cos(re)) * (exp((0.0 - 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((0.0d0 - im)) - exp(im))
end function
public static double code(double re, double im) {
return (0.5 * Math.cos(re)) * (Math.exp((0.0 - im)) - Math.exp(im));
}
def code(re, im): return (0.5 * math.cos(re)) * (math.exp((0.0 - im)) - math.exp(im))
function code(re, im) return Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(0.0 - im)) - exp(im))) end
function tmp = code(re, im) tmp = (0.5 * cos(re)) * (exp((0.0 - im)) - exp(im)); end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[N[(0.0 - im), $MachinePrecision]], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (re im) :precision binary64 (* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))
double code(double re, double im) {
return (0.5 * cos(re)) * (exp((0.0 - 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((0.0d0 - im)) - exp(im))
end function
public static double code(double re, double im) {
return (0.5 * Math.cos(re)) * (Math.exp((0.0 - im)) - Math.exp(im));
}
def code(re, im): return (0.5 * math.cos(re)) * (math.exp((0.0 - im)) - math.exp(im))
function code(re, im) return Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(0.0 - im)) - exp(im))) end
function tmp = code(re, im) tmp = (0.5 * cos(re)) * (exp((0.0 - im)) - exp(im)); end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[N[(0.0 - im), $MachinePrecision]], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right)
\end{array}
(FPCore (re im) :precision binary64 (* 0.5 (log1p (expm1 (* -2.0 (* im (cos re)))))))
double code(double re, double im) {
return 0.5 * log1p(expm1((-2.0 * (im * cos(re)))));
}
public static double code(double re, double im) {
return 0.5 * Math.log1p(Math.expm1((-2.0 * (im * Math.cos(re)))));
}
def code(re, im): return 0.5 * math.log1p(math.expm1((-2.0 * (im * math.cos(re)))))
function code(re, im) return Float64(0.5 * log1p(expm1(Float64(-2.0 * Float64(im * cos(re)))))) end
code[re_, im_] := N[(0.5 * N[Log[1 + N[(Exp[N[(-2.0 * N[(im * N[Cos[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(-2 \cdot \left(im \cdot \cos re\right)\right)\right)
\end{array}
Initial program 57.3%
cos-neg57.3%
sub-neg57.3%
neg-sub057.3%
remove-double-neg57.3%
remove-double-neg57.3%
sub0-neg57.3%
distribute-neg-in57.3%
+-commutative57.3%
sub-neg57.3%
associate-*l*57.3%
sub-neg57.3%
+-commutative57.3%
distribute-neg-in57.3%
Simplified57.3%
Taylor expanded in im around 0 48.7%
log1p-expm1-u99.0%
associate-*l*99.0%
Applied egg-rr99.0%
Final simplification99.0%
(FPCore (re im) :precision binary64 (* 0.5 (* (cos re) (+ (* -2.0 im) (* -0.3333333333333333 (pow im 3.0))))))
double code(double re, double im) {
return 0.5 * (cos(re) * ((-2.0 * im) + (-0.3333333333333333 * pow(im, 3.0))));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * (cos(re) * (((-2.0d0) * im) + ((-0.3333333333333333d0) * (im ** 3.0d0))))
end function
public static double code(double re, double im) {
return 0.5 * (Math.cos(re) * ((-2.0 * im) + (-0.3333333333333333 * Math.pow(im, 3.0))));
}
def code(re, im): return 0.5 * (math.cos(re) * ((-2.0 * im) + (-0.3333333333333333 * math.pow(im, 3.0))))
function code(re, im) return Float64(0.5 * Float64(cos(re) * Float64(Float64(-2.0 * im) + Float64(-0.3333333333333333 * (im ^ 3.0))))) end
function tmp = code(re, im) tmp = 0.5 * (cos(re) * ((-2.0 * im) + (-0.3333333333333333 * (im ^ 3.0)))); end
code[re_, im_] := N[(0.5 * N[(N[Cos[re], $MachinePrecision] * N[(N[(-2.0 * im), $MachinePrecision] + N[(-0.3333333333333333 * N[Power[im, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(\cos re \cdot \left(-2 \cdot im + -0.3333333333333333 \cdot {im}^{3}\right)\right)
\end{array}
Initial program 57.3%
cos-neg57.3%
sub-neg57.3%
neg-sub057.3%
remove-double-neg57.3%
remove-double-neg57.3%
sub0-neg57.3%
distribute-neg-in57.3%
+-commutative57.3%
sub-neg57.3%
associate-*l*57.3%
sub-neg57.3%
+-commutative57.3%
distribute-neg-in57.3%
Simplified57.3%
Taylor expanded in im around 0 82.6%
Final simplification82.6%
(FPCore (re im) :precision binary64 (* 0.5 (log1p (expm1 (* -2.0 im)))))
double code(double re, double im) {
return 0.5 * log1p(expm1((-2.0 * im)));
}
public static double code(double re, double im) {
return 0.5 * Math.log1p(Math.expm1((-2.0 * im)));
}
def code(re, im): return 0.5 * math.log1p(math.expm1((-2.0 * im)))
function code(re, im) return Float64(0.5 * log1p(expm1(Float64(-2.0 * im)))) end
code[re_, im_] := N[(0.5 * N[Log[1 + N[(Exp[N[(-2.0 * im), $MachinePrecision]] - 1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(-2 \cdot im\right)\right)
\end{array}
Initial program 57.3%
cos-neg57.3%
sub-neg57.3%
neg-sub057.3%
remove-double-neg57.3%
remove-double-neg57.3%
sub0-neg57.3%
distribute-neg-in57.3%
+-commutative57.3%
sub-neg57.3%
associate-*l*57.3%
sub-neg57.3%
+-commutative57.3%
distribute-neg-in57.3%
Simplified57.3%
Taylor expanded in im around 0 48.7%
log1p-expm1-u99.0%
associate-*l*99.0%
Applied egg-rr99.0%
Taylor expanded in re around 0 63.5%
Final simplification63.5%
(FPCore (re im) :precision binary64 (* 0.5 (+ (* -2.0 im) (* -0.3333333333333333 (pow im 3.0)))))
double code(double re, double im) {
return 0.5 * ((-2.0 * im) + (-0.3333333333333333 * pow(im, 3.0)));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * (((-2.0d0) * im) + ((-0.3333333333333333d0) * (im ** 3.0d0)))
end function
public static double code(double re, double im) {
return 0.5 * ((-2.0 * im) + (-0.3333333333333333 * Math.pow(im, 3.0)));
}
def code(re, im): return 0.5 * ((-2.0 * im) + (-0.3333333333333333 * math.pow(im, 3.0)))
function code(re, im) return Float64(0.5 * Float64(Float64(-2.0 * im) + Float64(-0.3333333333333333 * (im ^ 3.0)))) end
function tmp = code(re, im) tmp = 0.5 * ((-2.0 * im) + (-0.3333333333333333 * (im ^ 3.0))); end
code[re_, im_] := N[(0.5 * N[(N[(-2.0 * im), $MachinePrecision] + N[(-0.3333333333333333 * N[Power[im, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(-2 \cdot im + -0.3333333333333333 \cdot {im}^{3}\right)
\end{array}
Initial program 57.3%
cos-neg57.3%
sub-neg57.3%
neg-sub057.3%
remove-double-neg57.3%
remove-double-neg57.3%
sub0-neg57.3%
distribute-neg-in57.3%
+-commutative57.3%
sub-neg57.3%
associate-*l*57.3%
sub-neg57.3%
+-commutative57.3%
distribute-neg-in57.3%
Simplified57.3%
Taylor expanded in im around 0 89.1%
Taylor expanded in re around 0 56.7%
Taylor expanded in im around 0 53.0%
Final simplification53.0%
(FPCore (re im) :precision binary64 (* 0.5 (+ (* -2.0 im) (* -0.016666666666666666 (pow im 5.0)))))
double code(double re, double im) {
return 0.5 * ((-2.0 * im) + (-0.016666666666666666 * pow(im, 5.0)));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * (((-2.0d0) * im) + ((-0.016666666666666666d0) * (im ** 5.0d0)))
end function
public static double code(double re, double im) {
return 0.5 * ((-2.0 * im) + (-0.016666666666666666 * Math.pow(im, 5.0)));
}
def code(re, im): return 0.5 * ((-2.0 * im) + (-0.016666666666666666 * math.pow(im, 5.0)))
function code(re, im) return Float64(0.5 * Float64(Float64(-2.0 * im) + Float64(-0.016666666666666666 * (im ^ 5.0)))) end
function tmp = code(re, im) tmp = 0.5 * ((-2.0 * im) + (-0.016666666666666666 * (im ^ 5.0))); end
code[re_, im_] := N[(0.5 * N[(N[(-2.0 * im), $MachinePrecision] + N[(-0.016666666666666666 * N[Power[im, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(-2 \cdot im + -0.016666666666666666 \cdot {im}^{5}\right)
\end{array}
Initial program 57.3%
cos-neg57.3%
sub-neg57.3%
neg-sub057.3%
remove-double-neg57.3%
remove-double-neg57.3%
sub0-neg57.3%
distribute-neg-in57.3%
+-commutative57.3%
sub-neg57.3%
associate-*l*57.3%
sub-neg57.3%
+-commutative57.3%
distribute-neg-in57.3%
Simplified57.3%
Taylor expanded in im around 0 89.1%
Taylor expanded in re around 0 56.7%
Taylor expanded in im around inf 56.5%
Final simplification56.5%
(FPCore (re im) :precision binary64 (* 0.5 (* (cos re) (* -2.0 im))))
double code(double re, double im) {
return 0.5 * (cos(re) * (-2.0 * im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * (cos(re) * ((-2.0d0) * im))
end function
public static double code(double re, double im) {
return 0.5 * (Math.cos(re) * (-2.0 * im));
}
def code(re, im): return 0.5 * (math.cos(re) * (-2.0 * im))
function code(re, im) return Float64(0.5 * Float64(cos(re) * Float64(-2.0 * im))) end
function tmp = code(re, im) tmp = 0.5 * (cos(re) * (-2.0 * im)); end
code[re_, im_] := N[(0.5 * N[(N[Cos[re], $MachinePrecision] * N[(-2.0 * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(\cos re \cdot \left(-2 \cdot im\right)\right)
\end{array}
Initial program 57.3%
cos-neg57.3%
sub-neg57.3%
neg-sub057.3%
remove-double-neg57.3%
remove-double-neg57.3%
sub0-neg57.3%
distribute-neg-in57.3%
+-commutative57.3%
sub-neg57.3%
associate-*l*57.3%
sub-neg57.3%
+-commutative57.3%
distribute-neg-in57.3%
Simplified57.3%
Taylor expanded in im around 0 48.7%
Final simplification48.7%
(FPCore (re im) :precision binary64 (* 0.5 (* -2.0 im)))
double code(double re, double im) {
return 0.5 * (-2.0 * im);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * ((-2.0d0) * im)
end function
public static double code(double re, double im) {
return 0.5 * (-2.0 * im);
}
def code(re, im): return 0.5 * (-2.0 * im)
function code(re, im) return Float64(0.5 * Float64(-2.0 * im)) end
function tmp = code(re, im) tmp = 0.5 * (-2.0 * im); end
code[re_, im_] := N[(0.5 * N[(-2.0 * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(-2 \cdot im\right)
\end{array}
Initial program 57.3%
cos-neg57.3%
sub-neg57.3%
neg-sub057.3%
remove-double-neg57.3%
remove-double-neg57.3%
sub0-neg57.3%
distribute-neg-in57.3%
+-commutative57.3%
sub-neg57.3%
associate-*l*57.3%
sub-neg57.3%
+-commutative57.3%
distribute-neg-in57.3%
Simplified57.3%
Taylor expanded in im around 0 48.7%
Taylor expanded in re around 0 28.4%
Final simplification28.4%
(FPCore (re im)
:precision binary64
(if (< (fabs im) 1.0)
(-
(*
(cos re)
(+
(+ im (* (* (* 0.16666666666666666 im) im) im))
(* (* (* (* (* 0.008333333333333333 im) im) im) im) im))))
(* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im)))))
double code(double re, double im) {
double tmp;
if (fabs(im) < 1.0) {
tmp = -(cos(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)));
} else {
tmp = (0.5 * cos(re)) * (exp((0.0 - im)) - exp(im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (abs(im) < 1.0d0) then
tmp = -(cos(re) * ((im + (((0.16666666666666666d0 * im) * im) * im)) + (((((0.008333333333333333d0 * im) * im) * im) * im) * im)))
else
tmp = (0.5d0 * cos(re)) * (exp((0.0d0 - im)) - exp(im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (Math.abs(im) < 1.0) {
tmp = -(Math.cos(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)));
} else {
tmp = (0.5 * Math.cos(re)) * (Math.exp((0.0 - im)) - Math.exp(im));
}
return tmp;
}
def code(re, im): tmp = 0 if math.fabs(im) < 1.0: tmp = -(math.cos(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im))) else: tmp = (0.5 * math.cos(re)) * (math.exp((0.0 - im)) - math.exp(im)) return tmp
function code(re, im) tmp = 0.0 if (abs(im) < 1.0) tmp = Float64(-Float64(cos(re) * Float64(Float64(im + Float64(Float64(Float64(0.16666666666666666 * im) * im) * im)) + Float64(Float64(Float64(Float64(Float64(0.008333333333333333 * im) * im) * im) * im) * im)))); else tmp = Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(0.0 - im)) - exp(im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (abs(im) < 1.0) tmp = -(cos(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im))); else tmp = (0.5 * cos(re)) * (exp((0.0 - im)) - exp(im)); end tmp_2 = tmp; end
code[re_, im_] := If[Less[N[Abs[im], $MachinePrecision], 1.0], (-N[(N[Cos[re], $MachinePrecision] * N[(N[(im + N[(N[(N[(0.16666666666666666 * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(N[(N[(0.008333333333333333 * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[N[(0.0 - im), $MachinePrecision]], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|im\right| < 1:\\
\;\;\;\;-\cos re \cdot \left(\left(im + \left(\left(0.16666666666666666 \cdot im\right) \cdot im\right) \cdot im\right) + \left(\left(\left(\left(0.008333333333333333 \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right)\\
\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right)\\
\end{array}
\end{array}
herbie shell --seed 2024033
(FPCore (re im)
:name "math.sin on complex, imaginary part"
:precision binary64
:herbie-target
(if (< (fabs im) 1.0) (- (* (cos re) (+ (+ im (* (* (* 0.16666666666666666 im) im) im)) (* (* (* (* (* 0.008333333333333333 im) im) im) im) im)))) (* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))
(* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))