math.cos on complex, imaginary part

?

Percentage Accurate: 66.0% → 99.9%
Time: 17.5s
Precision: binary64
Cost: 46537

?

\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ t_1 := 0.5 \cdot \sin re\\ \mathbf{if}\;t_0 \leq -0.01 \lor \neg \left(t_0 \leq 0.02\right):\\ \;\;\;\;t_0 \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot \left(im \cdot -2 + \left(-0.016666666666666666 \cdot {im}^{5} + -0.3333333333333333 \cdot {im}^{3}\right)\right)\\ \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (- (exp (- im)) (exp im))) (t_1 (* 0.5 (sin re))))
   (if (or (<= t_0 -0.01) (not (<= t_0 0.02)))
     (* t_0 t_1)
     (*
      t_1
      (+
       (* im -2.0)
       (+
        (* -0.016666666666666666 (pow im 5.0))
        (* -0.3333333333333333 (pow im 3.0))))))))
double code(double re, double im) {
	return (0.5 * sin(re)) * (exp(-im) - exp(im));
}
double code(double re, double im) {
	double t_0 = exp(-im) - exp(im);
	double t_1 = 0.5 * sin(re);
	double tmp;
	if ((t_0 <= -0.01) || !(t_0 <= 0.02)) {
		tmp = t_0 * t_1;
	} else {
		tmp = t_1 * ((im * -2.0) + ((-0.016666666666666666 * pow(im, 5.0)) + (-0.3333333333333333 * pow(im, 3.0))));
	}
	return tmp;
}
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
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = exp(-im) - exp(im)
    t_1 = 0.5d0 * sin(re)
    if ((t_0 <= (-0.01d0)) .or. (.not. (t_0 <= 0.02d0))) then
        tmp = t_0 * t_1
    else
        tmp = t_1 * ((im * (-2.0d0)) + (((-0.016666666666666666d0) * (im ** 5.0d0)) + ((-0.3333333333333333d0) * (im ** 3.0d0))))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	return (0.5 * Math.sin(re)) * (Math.exp(-im) - Math.exp(im));
}
public static double code(double re, double im) {
	double t_0 = Math.exp(-im) - Math.exp(im);
	double t_1 = 0.5 * Math.sin(re);
	double tmp;
	if ((t_0 <= -0.01) || !(t_0 <= 0.02)) {
		tmp = t_0 * t_1;
	} else {
		tmp = t_1 * ((im * -2.0) + ((-0.016666666666666666 * Math.pow(im, 5.0)) + (-0.3333333333333333 * Math.pow(im, 3.0))));
	}
	return tmp;
}
def code(re, im):
	return (0.5 * math.sin(re)) * (math.exp(-im) - math.exp(im))
def code(re, im):
	t_0 = math.exp(-im) - math.exp(im)
	t_1 = 0.5 * math.sin(re)
	tmp = 0
	if (t_0 <= -0.01) or not (t_0 <= 0.02):
		tmp = t_0 * t_1
	else:
		tmp = t_1 * ((im * -2.0) + ((-0.016666666666666666 * math.pow(im, 5.0)) + (-0.3333333333333333 * math.pow(im, 3.0))))
	return tmp
function code(re, im)
	return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(-im)) - exp(im)))
end
function code(re, im)
	t_0 = Float64(exp(Float64(-im)) - exp(im))
	t_1 = Float64(0.5 * sin(re))
	tmp = 0.0
	if ((t_0 <= -0.01) || !(t_0 <= 0.02))
		tmp = Float64(t_0 * t_1);
	else
		tmp = Float64(t_1 * Float64(Float64(im * -2.0) + Float64(Float64(-0.016666666666666666 * (im ^ 5.0)) + Float64(-0.3333333333333333 * (im ^ 3.0)))));
	end
	return tmp
end
function tmp = code(re, im)
	tmp = (0.5 * sin(re)) * (exp(-im) - exp(im));
end
function tmp_2 = code(re, im)
	t_0 = exp(-im) - exp(im);
	t_1 = 0.5 * sin(re);
	tmp = 0.0;
	if ((t_0 <= -0.01) || ~((t_0 <= 0.02)))
		tmp = t_0 * t_1;
	else
		tmp = t_1 * ((im * -2.0) + ((-0.016666666666666666 * (im ^ 5.0)) + (-0.3333333333333333 * (im ^ 3.0))));
	end
	tmp_2 = tmp;
end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[re_, im_] := Block[{t$95$0 = N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -0.01], N[Not[LessEqual[t$95$0, 0.02]], $MachinePrecision]], N[(t$95$0 * t$95$1), $MachinePrecision], N[(t$95$1 * N[(N[(im * -2.0), $MachinePrecision] + N[(N[(-0.016666666666666666 * N[Power[im, 5.0], $MachinePrecision]), $MachinePrecision] + N[(-0.3333333333333333 * N[Power[im, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\begin{array}{l}
t_0 := e^{-im} - e^{im}\\
t_1 := 0.5 \cdot \sin re\\
\mathbf{if}\;t_0 \leq -0.01 \lor \neg \left(t_0 \leq 0.02\right):\\
\;\;\;\;t_0 \cdot t_1\\

\mathbf{else}:\\
\;\;\;\;t_1 \cdot \left(im \cdot -2 + \left(-0.016666666666666666 \cdot {im}^{5} + -0.3333333333333333 \cdot {im}^{3}\right)\right)\\


\end{array}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 12 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original66.0%
Target99.8%
Herbie99.9%
\[\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} \]

Derivation?

  1. Split input into 2 regimes
  2. if (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)) < -0.0100000000000000002 or 0.0200000000000000004 < (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))

    1. Initial program 100.0%

      \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]

    if -0.0100000000000000002 < (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)) < 0.0200000000000000004

    1. Initial program 24.9%

      \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
    2. Taylor expanded in im around 0 99.8%

      \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \color{blue}{\left(-2 \cdot im + \left(-0.016666666666666666 \cdot {im}^{5} + -0.3333333333333333 \cdot {im}^{3}\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{-im} - e^{im} \leq -0.01 \lor \neg \left(e^{-im} - e^{im} \leq 0.02\right):\\ \;\;\;\;\left(e^{-im} - e^{im}\right) \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \sin re\right) \cdot \left(im \cdot -2 + \left(-0.016666666666666666 \cdot {im}^{5} + -0.3333333333333333 \cdot {im}^{3}\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.9%
Cost46537
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ t_1 := 0.5 \cdot \sin re\\ \mathbf{if}\;t_0 \leq -0.01 \lor \neg \left(t_0 \leq 0.02\right):\\ \;\;\;\;t_0 \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot \left(im \cdot -2 + \left(-0.016666666666666666 \cdot {im}^{5} + -0.3333333333333333 \cdot {im}^{3}\right)\right)\\ \end{array} \]
Alternative 2
Accuracy99.9%
Cost46281
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -0.01 \lor \neg \left(t_0 \leq 0.02\right):\\ \;\;\;\;t_0 \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\sin re \cdot \left(\left({im}^{3} \cdot -0.16666666666666666 - im\right) + {im}^{5} \cdot -0.008333333333333333\right)\\ \end{array} \]
Alternative 3
Accuracy99.7%
Cost45961
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -0.01 \lor \neg \left(t_0 \leq 2 \cdot 10^{-12}\right):\\ \;\;\;\;t_0 \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \end{array} \]
Alternative 4
Accuracy94.9%
Cost19721
\[\begin{array}{l} \mathbf{if}\;re \leq -2 \cdot 10^{-28} \lor \neg \left(re \leq 7 \cdot 10^{-65}\right):\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\left(-im\right) \cdot \sin re\right)\right)\\ \mathbf{else}:\\ \;\;\;\;re \cdot \left(\left({im}^{5} \cdot -0.008333333333333333 + {im}^{3} \cdot -0.16666666666666666\right) - im\right)\\ \end{array} \]
Alternative 5
Accuracy96.6%
Cost13840
\[\begin{array}{l} t_0 := 0.5 \cdot \left(\left(e^{-im} - e^{im}\right) \cdot re\right)\\ t_1 := -0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right)\\ \mathbf{if}\;im \leq -5 \cdot 10^{+76}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.0015:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 0.0062:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{+61}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Accuracy96.8%
Cost13840
\[\begin{array}{l} t_0 := 0.5 \cdot \left(\left(e^{-im} - e^{im}\right) \cdot re\right)\\ t_1 := -0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right)\\ \mathbf{if}\;im \leq -3.3 \cdot 10^{+76}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.0069:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 0.0085:\\ \;\;\;\;\sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{+61}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 7
Accuracy91.1%
Cost13708
\[\begin{array}{l} t_0 := -0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right)\\ \mathbf{if}\;im \leq -3.3:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 6200000:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{+61}:\\ \;\;\;\;\sqrt{{im}^{10} \cdot \left(\left(re \cdot re\right) \cdot 6.944444444444444 \cdot 10^{-5}\right)}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Accuracy90.2%
Cost13449
\[\begin{array}{l} \mathbf{if}\;im \leq -3.3 \lor \neg \left(im \leq 3.3\right):\\ \;\;\;\;-0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \end{array} \]
Alternative 9
Accuracy80.1%
Cost7049
\[\begin{array}{l} \mathbf{if}\;im \leq -32000 \lor \neg \left(im \leq 41000\right):\\ \;\;\;\;-0.008333333333333333 \cdot \left(re \cdot {im}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \end{array} \]
Alternative 10
Accuracy80.1%
Cost7048
\[\begin{array}{l} \mathbf{if}\;im \leq -20000:\\ \;\;\;\;-0.008333333333333333 \cdot \left(re \cdot {im}^{5}\right)\\ \mathbf{elif}\;im \leq 320000:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{else}:\\ \;\;\;\;re \cdot \left({im}^{5} \cdot -0.008333333333333333\right)\\ \end{array} \]
Alternative 11
Accuracy56.0%
Cost6921
\[\begin{array}{l} \mathbf{if}\;im \leq -480 \lor \neg \left(im \leq 15500\right):\\ \;\;\;\;im \cdot \left(-re\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \end{array} \]
Alternative 12
Accuracy32.1%
Cost256
\[im \cdot \left(-re\right) \]

Reproduce?

herbie shell --seed 2023269 
(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))))