| Alternative 1 | |
|---|---|
| Accuracy | 98.7% |
| Cost | 13632 |
(FPCore (re im) :precision binary64 (* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))
(FPCore (re im)
:precision binary64
(let* ((t_0 (- (exp (- im)) (exp im))))
(if (<= t_0 -2.0)
(* (cos re) (* 0.5 t_0))
(-
(* (cos re) (* im (* -0.16666666666666666 (* im im))))
(* im (cos re))))))double code(double re, double im) {
return (0.5 * cos(re)) * (exp((0.0 - im)) - exp(im));
}
double code(double re, double im) {
double t_0 = exp(-im) - exp(im);
double tmp;
if (t_0 <= -2.0) {
tmp = cos(re) * (0.5 * t_0);
} else {
tmp = (cos(re) * (im * (-0.16666666666666666 * (im * im)))) - (im * cos(re));
}
return tmp;
}
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
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: t_0
real(8) :: tmp
t_0 = exp(-im) - exp(im)
if (t_0 <= (-2.0d0)) then
tmp = cos(re) * (0.5d0 * t_0)
else
tmp = (cos(re) * (im * ((-0.16666666666666666d0) * (im * im)))) - (im * cos(re))
end if
code = tmp
end function
public static double code(double re, double im) {
return (0.5 * Math.cos(re)) * (Math.exp((0.0 - im)) - Math.exp(im));
}
public static double code(double re, double im) {
double t_0 = Math.exp(-im) - Math.exp(im);
double tmp;
if (t_0 <= -2.0) {
tmp = Math.cos(re) * (0.5 * t_0);
} else {
tmp = (Math.cos(re) * (im * (-0.16666666666666666 * (im * im)))) - (im * Math.cos(re));
}
return tmp;
}
def code(re, im): return (0.5 * math.cos(re)) * (math.exp((0.0 - im)) - math.exp(im))
def code(re, im): t_0 = math.exp(-im) - math.exp(im) tmp = 0 if t_0 <= -2.0: tmp = math.cos(re) * (0.5 * t_0) else: tmp = (math.cos(re) * (im * (-0.16666666666666666 * (im * im)))) - (im * math.cos(re)) return tmp
function code(re, im) return Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(0.0 - im)) - exp(im))) end
function code(re, im) t_0 = Float64(exp(Float64(-im)) - exp(im)) tmp = 0.0 if (t_0 <= -2.0) tmp = Float64(cos(re) * Float64(0.5 * t_0)); else tmp = Float64(Float64(cos(re) * Float64(im * Float64(-0.16666666666666666 * Float64(im * im)))) - Float64(im * cos(re))); end return tmp end
function tmp = code(re, im) tmp = (0.5 * cos(re)) * (exp((0.0 - im)) - exp(im)); end
function tmp_2 = code(re, im) t_0 = exp(-im) - exp(im); tmp = 0.0; if (t_0 <= -2.0) tmp = cos(re) * (0.5 * t_0); else tmp = (cos(re) * (im * (-0.16666666666666666 * (im * im)))) - (im * cos(re)); end tmp_2 = tmp; 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]
code[re_, im_] := Block[{t$95$0 = N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -2.0], N[(N[Cos[re], $MachinePrecision] * N[(0.5 * t$95$0), $MachinePrecision]), $MachinePrecision], N[(N[(N[Cos[re], $MachinePrecision] * N[(im * N[(-0.16666666666666666 * N[(im * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(im * N[Cos[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right)
\begin{array}{l}
t_0 := e^{-im} - e^{im}\\
\mathbf{if}\;t_0 \leq -2:\\
\;\;\;\;\cos re \cdot \left(0.5 \cdot t_0\right)\\
\mathbf{else}:\\
\;\;\;\;\cos re \cdot \left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right)\right) - im \cdot \cos re\\
\end{array}
Results
| Original | 9.1% |
|---|---|
| Target | 99.7% |
| Herbie | 99.0% |
if (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im)) < -2Initial program 99.6%
Simplified99.6%
[Start]99.6 | \[ \left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right)
\] |
|---|---|
*-commutative [=>]99.6 | \[ \color{blue}{\left(\cos re \cdot 0.5\right)} \cdot \left(e^{0 - im} - e^{im}\right)
\] |
associate-*l* [=>]99.6 | \[ \color{blue}{\cos re \cdot \left(0.5 \cdot \left(e^{0 - im} - e^{im}\right)\right)}
\] |
sub-neg [=>]99.6 | \[ \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{0 - im} + \left(-e^{im}\right)\right)}\right)
\] |
sub-neg [<=]99.6 | \[ \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{0 - im} - e^{im}\right)}\right)
\] |
sub0-neg [=>]99.6 | \[ \cos re \cdot \left(0.5 \cdot \left(e^{\color{blue}{-im}} - e^{im}\right)\right)
\] |
if -2 < (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im)) Initial program 8.8%
Simplified8.8%
[Start]8.8 | \[ \left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right)
\] |
|---|---|
*-commutative [=>]8.8 | \[ \color{blue}{\left(\cos re \cdot 0.5\right)} \cdot \left(e^{0 - im} - e^{im}\right)
\] |
associate-*l* [=>]8.8 | \[ \color{blue}{\cos re \cdot \left(0.5 \cdot \left(e^{0 - im} - e^{im}\right)\right)}
\] |
sub-neg [=>]8.8 | \[ \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{0 - im} + \left(-e^{im}\right)\right)}\right)
\] |
sub-neg [<=]8.8 | \[ \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{0 - im} - e^{im}\right)}\right)
\] |
sub0-neg [=>]8.8 | \[ \cos re \cdot \left(0.5 \cdot \left(e^{\color{blue}{-im}} - e^{im}\right)\right)
\] |
Taylor expanded in im around 0 99.0%
Simplified99.0%
[Start]99.0 | \[ -0.16666666666666666 \cdot \left(\cos re \cdot {im}^{3}\right) + -1 \cdot \left(\cos re \cdot im\right)
\] |
|---|---|
mul-1-neg [=>]99.0 | \[ -0.16666666666666666 \cdot \left(\cos re \cdot {im}^{3}\right) + \color{blue}{\left(-\cos re \cdot im\right)}
\] |
unsub-neg [=>]99.0 | \[ \color{blue}{-0.16666666666666666 \cdot \left(\cos re \cdot {im}^{3}\right) - \cos re \cdot im}
\] |
*-commutative [=>]99.0 | \[ \color{blue}{\left(\cos re \cdot {im}^{3}\right) \cdot -0.16666666666666666} - \cos re \cdot im
\] |
associate-*l* [=>]99.0 | \[ \color{blue}{\cos re \cdot \left({im}^{3} \cdot -0.16666666666666666\right)} - \cos re \cdot im
\] |
distribute-lft-out-- [=>]99.0 | \[ \color{blue}{\cos re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)}
\] |
Applied egg-rr99.0%
[Start]99.0 | \[ \cos re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)
\] |
|---|---|
*-commutative [=>]99.0 | \[ \cos re \cdot \left(\color{blue}{-0.16666666666666666 \cdot {im}^{3}} - im\right)
\] |
unpow3 [=>]99.0 | \[ \cos re \cdot \left(-0.16666666666666666 \cdot \color{blue}{\left(\left(im \cdot im\right) \cdot im\right)} - im\right)
\] |
associate-*r* [=>]99.0 | \[ \cos re \cdot \left(\color{blue}{\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im} - im\right)
\] |
*-un-lft-identity [=>]99.0 | \[ \cos re \cdot \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im - \color{blue}{1 \cdot im}\right)
\] |
distribute-rgt-out-- [=>]99.0 | \[ \cos re \cdot \color{blue}{\left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right) - 1\right)\right)}
\] |
Applied egg-rr99.0%
[Start]99.0 | \[ \cos re \cdot \left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right) - 1\right)\right)
\] |
|---|---|
sub-neg [=>]99.0 | \[ \cos re \cdot \left(im \cdot \color{blue}{\left(-0.16666666666666666 \cdot \left(im \cdot im\right) + \left(-1\right)\right)}\right)
\] |
distribute-rgt-in [=>]99.0 | \[ \cos re \cdot \color{blue}{\left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im + \left(-1\right) \cdot im\right)}
\] |
distribute-rgt-in [=>]99.0 | \[ \color{blue}{\left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \left(\left(-1\right) \cdot im\right) \cdot \cos re}
\] |
metadata-eval [=>]99.0 | \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \left(\color{blue}{-1} \cdot im\right) \cdot \cos re
\] |
Taylor expanded in im around 0 99.0%
Simplified99.0%
[Start]99.0 | \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + -1 \cdot \left(\cos re \cdot im\right)
\] |
|---|---|
*-commutative [=>]99.0 | \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + -1 \cdot \color{blue}{\left(im \cdot \cos re\right)}
\] |
mul-1-neg [=>]99.0 | \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \color{blue}{\left(-im \cdot \cos re\right)}
\] |
*-commutative [<=]99.0 | \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \left(-\color{blue}{\cos re \cdot im}\right)
\] |
distribute-rgt-neg-in [=>]99.0 | \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \color{blue}{\cos re \cdot \left(-im\right)}
\] |
Final simplification99.0%
| Alternative 1 | |
|---|---|
| Accuracy | 98.7% |
| Cost | 13632 |
| Alternative 2 | |
|---|---|
| Accuracy | 98.7% |
| Cost | 7104 |
| Alternative 3 | |
|---|---|
| Accuracy | 98.7% |
| Cost | 7104 |
| Alternative 4 | |
|---|---|
| Accuracy | 98.2% |
| Cost | 6656 |
| Alternative 5 | |
|---|---|
| Accuracy | 54.7% |
| Cost | 128 |
| Alternative 6 | |
|---|---|
| Accuracy | 3.8% |
| Cost | 64 |
| Alternative 7 | |
|---|---|
| Accuracy | 3.8% |
| Cost | 64 |
| Alternative 8 | |
|---|---|
| Accuracy | 3.9% |
| Cost | 64 |
| Alternative 9 | |
|---|---|
| Accuracy | 5.4% |
| Cost | 64 |
herbie shell --seed 2023143
(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))))