
(FPCore (re im) :precision binary64 (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))
double code(double re, double im) {
return (0.5 * sin(re)) * (exp(-im) - exp(im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (0.5d0 * sin(re)) * (exp(-im) - exp(im))
end function
public static double code(double re, double im) {
return (0.5 * Math.sin(re)) * (Math.exp(-im) - Math.exp(im));
}
def code(re, im): return (0.5 * math.sin(re)) * (math.exp(-im) - math.exp(im))
function code(re, im) return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(-im)) - exp(im))) end
function tmp = code(re, im) tmp = (0.5 * sin(re)) * (exp(-im) - exp(im)); end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (re im) :precision binary64 (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))
double code(double re, double im) {
return (0.5 * sin(re)) * (exp(-im) - exp(im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (0.5d0 * sin(re)) * (exp(-im) - exp(im))
end function
public static double code(double re, double im) {
return (0.5 * Math.sin(re)) * (Math.exp(-im) - Math.exp(im));
}
def code(re, im): return (0.5 * math.sin(re)) * (math.exp(-im) - math.exp(im))
function code(re, im) return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(-im)) - exp(im))) end
function tmp = code(re, im) tmp = (0.5 * sin(re)) * (exp(-im) - exp(im)); end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\end{array}
(FPCore (re im)
:precision binary64
(*
(* 0.5 (sin re))
(+
(* -2.0 im)
(+
(* -0.3333333333333333 (pow im 3.0))
(* -0.016666666666666666 (pow im 5.0))))))
double code(double re, double im) {
return (0.5 * sin(re)) * ((-2.0 * im) + ((-0.3333333333333333 * pow(im, 3.0)) + (-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 * sin(re)) * (((-2.0d0) * im) + (((-0.3333333333333333d0) * (im ** 3.0d0)) + ((-0.016666666666666666d0) * (im ** 5.0d0))))
end function
public static double code(double re, double im) {
return (0.5 * Math.sin(re)) * ((-2.0 * im) + ((-0.3333333333333333 * Math.pow(im, 3.0)) + (-0.016666666666666666 * Math.pow(im, 5.0))));
}
def code(re, im): return (0.5 * math.sin(re)) * ((-2.0 * im) + ((-0.3333333333333333 * math.pow(im, 3.0)) + (-0.016666666666666666 * math.pow(im, 5.0))))
function code(re, im) return Float64(Float64(0.5 * sin(re)) * Float64(Float64(-2.0 * im) + Float64(Float64(-0.3333333333333333 * (im ^ 3.0)) + Float64(-0.016666666666666666 * (im ^ 5.0))))) end
function tmp = code(re, im) tmp = (0.5 * sin(re)) * ((-2.0 * im) + ((-0.3333333333333333 * (im ^ 3.0)) + (-0.016666666666666666 * (im ^ 5.0)))); end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[(-2.0 * im), $MachinePrecision] + N[(N[(-0.3333333333333333 * N[Power[im, 3.0], $MachinePrecision]), $MachinePrecision] + N[(-0.016666666666666666 * N[Power[im, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot \sin re\right) \cdot \left(-2 \cdot im + \left(-0.3333333333333333 \cdot {im}^{3} + -0.016666666666666666 \cdot {im}^{5}\right)\right)
\end{array}
Initial program 69.5%
Taylor expanded in im around 0 89.0%
Final simplification89.0%
(FPCore (re im) :precision binary64 (* (sin re) (- (* (pow im 3.0) -0.16666666666666666) im)))
double code(double re, double im) {
return sin(re) * ((pow(im, 3.0) * -0.16666666666666666) - im);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sin(re) * (((im ** 3.0d0) * (-0.16666666666666666d0)) - im)
end function
public static double code(double re, double im) {
return Math.sin(re) * ((Math.pow(im, 3.0) * -0.16666666666666666) - im);
}
def code(re, im): return math.sin(re) * ((math.pow(im, 3.0) * -0.16666666666666666) - im)
function code(re, im) return Float64(sin(re) * Float64(Float64((im ^ 3.0) * -0.16666666666666666) - im)) end
function tmp = code(re, im) tmp = sin(re) * (((im ^ 3.0) * -0.16666666666666666) - im); end
code[re_, im_] := N[(N[Sin[re], $MachinePrecision] * N[(N[(N[Power[im, 3.0], $MachinePrecision] * -0.16666666666666666), $MachinePrecision] - im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)
\end{array}
Initial program 69.5%
Taylor expanded in im around 0 82.5%
+-commutative82.5%
mul-1-neg82.5%
unsub-neg82.5%
associate-*r*82.5%
distribute-rgt-out--82.5%
*-commutative82.5%
Simplified82.5%
Final simplification82.5%
(FPCore (re im) :precision binary64 (* -0.008333333333333333 (* (sin re) (pow im 5.0))))
double code(double re, double im) {
return -0.008333333333333333 * (sin(re) * pow(im, 5.0));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (-0.008333333333333333d0) * (sin(re) * (im ** 5.0d0))
end function
public static double code(double re, double im) {
return -0.008333333333333333 * (Math.sin(re) * Math.pow(im, 5.0));
}
def code(re, im): return -0.008333333333333333 * (math.sin(re) * math.pow(im, 5.0))
function code(re, im) return Float64(-0.008333333333333333 * Float64(sin(re) * (im ^ 5.0))) end
function tmp = code(re, im) tmp = -0.008333333333333333 * (sin(re) * (im ^ 5.0)); end
code[re_, im_] := N[(-0.008333333333333333 * N[(N[Sin[re], $MachinePrecision] * N[Power[im, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right)
\end{array}
Initial program 69.5%
Taylor expanded in im around 0 89.0%
Taylor expanded in im around inf 57.7%
Final simplification57.7%
(FPCore (re im) :precision binary64 (* (sin re) (- im)))
double code(double re, double im) {
return sin(re) * -im;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sin(re) * -im
end function
public static double code(double re, double im) {
return Math.sin(re) * -im;
}
def code(re, im): return math.sin(re) * -im
function code(re, im) return Float64(sin(re) * Float64(-im)) end
function tmp = code(re, im) tmp = sin(re) * -im; end
code[re_, im_] := N[(N[Sin[re], $MachinePrecision] * (-im)), $MachinePrecision]
\begin{array}{l}
\\
\sin re \cdot \left(-im\right)
\end{array}
Initial program 69.5%
Taylor expanded in im around 0 48.3%
associate-*r*48.3%
neg-mul-148.3%
Simplified48.3%
Final simplification48.3%
(FPCore (re im) :precision binary64 (- (* re im)))
double code(double re, double im) {
return -(re * im);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = -(re * im)
end function
public static double code(double re, double im) {
return -(re * im);
}
def code(re, im): return -(re * im)
function code(re, im) return Float64(-Float64(re * im)) end
function tmp = code(re, im) tmp = -(re * im); end
code[re_, im_] := (-N[(re * im), $MachinePrecision])
\begin{array}{l}
\\
-re \cdot im
\end{array}
Initial program 69.5%
Taylor expanded in im around 0 48.3%
associate-*r*48.3%
neg-mul-148.3%
Simplified48.3%
Taylor expanded in re around 0 33.4%
mul-1-neg33.4%
distribute-rgt-neg-in33.4%
Simplified33.4%
Final simplification33.4%
(FPCore (re im) :precision binary64 (* re 4.0))
double code(double re, double im) {
return re * 4.0;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = re * 4.0d0
end function
public static double code(double re, double im) {
return re * 4.0;
}
def code(re, im): return re * 4.0
function code(re, im) return Float64(re * 4.0) end
function tmp = code(re, im) tmp = re * 4.0; end
code[re_, im_] := N[(re * 4.0), $MachinePrecision]
\begin{array}{l}
\\
re \cdot 4
\end{array}
Initial program 69.5%
Taylor expanded in re around 0 54.0%
associate-*r*54.0%
*-commutative54.0%
Simplified54.0%
Applied egg-rr3.5%
Taylor expanded in re around 0 3.5%
*-commutative3.5%
Simplified3.5%
Final simplification3.5%
(FPCore (re im)
:precision binary64
(if (< (fabs im) 1.0)
(-
(*
(sin re)
(+
(+ im (* (* (* 0.16666666666666666 im) im) im))
(* (* (* (* (* 0.008333333333333333 im) im) im) im) im))))
(* (* 0.5 (sin re)) (- (exp (- im)) (exp im)))))
double code(double re, double im) {
double tmp;
if (fabs(im) < 1.0) {
tmp = -(sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)));
} else {
tmp = (0.5 * sin(re)) * (exp(-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 = -(sin(re) * ((im + (((0.16666666666666666d0 * im) * im) * im)) + (((((0.008333333333333333d0 * im) * im) * im) * im) * im)))
else
tmp = (0.5d0 * sin(re)) * (exp(-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.sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)));
} else {
tmp = (0.5 * Math.sin(re)) * (Math.exp(-im) - Math.exp(im));
}
return tmp;
}
def code(re, im): tmp = 0 if math.fabs(im) < 1.0: tmp = -(math.sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im))) else: tmp = (0.5 * math.sin(re)) * (math.exp(-im) - math.exp(im)) return tmp
function code(re, im) tmp = 0.0 if (abs(im) < 1.0) tmp = Float64(-Float64(sin(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 * sin(re)) * Float64(exp(Float64(-im)) - exp(im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (abs(im) < 1.0) tmp = -(sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im))); else tmp = (0.5 * sin(re)) * (exp(-im) - exp(im)); end tmp_2 = tmp; end
code[re_, im_] := If[Less[N[Abs[im], $MachinePrecision], 1.0], (-N[(N[Sin[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[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|im\right| < 1:\\
\;\;\;\;-\sin 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 \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\\
\end{array}
\end{array}
herbie shell --seed 2024033
(FPCore (re im)
:name "math.cos on complex, imaginary part"
:precision binary64
:herbie-target
(if (< (fabs im) 1.0) (- (* (sin re) (+ (+ im (* (* (* 0.16666666666666666 im) im) im)) (* (* (* (* (* 0.008333333333333333 im) im) im) im) im)))) (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))
(* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))