math.exp on complex, imaginary part

Percentage Accurate: 100.0% → 100.0%
Time: 15.2s
Alternatives: 17
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ e^{re} \cdot \sin im \end{array} \]
(FPCore (re im) :precision binary64 (* (exp re) (sin im)))
double code(double re, double im) {
	return exp(re) * sin(im);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = exp(re) * sin(im)
end function
public static double code(double re, double im) {
	return Math.exp(re) * Math.sin(im);
}
def code(re, im):
	return math.exp(re) * math.sin(im)
function code(re, im)
	return Float64(exp(re) * sin(im))
end
function tmp = code(re, im)
	tmp = exp(re) * sin(im);
end
code[re_, im_] := N[(N[Exp[re], $MachinePrecision] * N[Sin[im], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
e^{re} \cdot \sin im
\end{array}

Sampling outcomes in binary64 precision:

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.

Accuracy vs Speed?

Herbie found 17 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ e^{re} \cdot \sin im \end{array} \]
(FPCore (re im) :precision binary64 (* (exp re) (sin im)))
double code(double re, double im) {
	return exp(re) * sin(im);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = exp(re) * sin(im)
end function
public static double code(double re, double im) {
	return Math.exp(re) * Math.sin(im);
}
def code(re, im):
	return math.exp(re) * math.sin(im)
function code(re, im)
	return Float64(exp(re) * sin(im))
end
function tmp = code(re, im)
	tmp = exp(re) * sin(im);
end
code[re_, im_] := N[(N[Exp[re], $MachinePrecision] * N[Sin[im], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
e^{re} \cdot \sin im
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ e^{re} \cdot \sin im \end{array} \]
(FPCore (re im) :precision binary64 (* (exp re) (sin im)))
double code(double re, double im) {
	return exp(re) * sin(im);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = exp(re) * sin(im)
end function
public static double code(double re, double im) {
	return Math.exp(re) * Math.sin(im);
}
def code(re, im):
	return math.exp(re) * math.sin(im)
function code(re, im)
	return Float64(exp(re) * sin(im))
end
function tmp = code(re, im)
	tmp = exp(re) * sin(im);
end
code[re_, im_] := N[(N[Exp[re], $MachinePrecision] * N[Sin[im], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
e^{re} \cdot \sin im
\end{array}
Derivation
  1. Initial program 100.0%

    \[e^{re} \cdot \sin im \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 89.7% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{re} \cdot \sin im\\ t_1 := e^{re} \cdot im\\ \mathbf{if}\;t\_0 \leq -\infty:\\ \;\;\;\;\mathsf{fma}\left(re, \mathsf{fma}\left(re, 0.5, 1\right), 1\right) \cdot \mathsf{fma}\left(im, im \cdot \left(im \cdot -0.16666666666666666\right), im\right)\\ \mathbf{elif}\;t\_0 \leq -0.01:\\ \;\;\;\;\sin im\\ \mathbf{elif}\;t\_0 \leq 10^{-101}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_0 \leq 1:\\ \;\;\;\;\sin im\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* (exp re) (sin im))) (t_1 (* (exp re) im)))
   (if (<= t_0 (- INFINITY))
     (*
      (fma re (fma re 0.5 1.0) 1.0)
      (fma im (* im (* im -0.16666666666666666)) im))
     (if (<= t_0 -0.01)
       (sin im)
       (if (<= t_0 1e-101) t_1 (if (<= t_0 1.0) (sin im) t_1))))))
double code(double re, double im) {
	double t_0 = exp(re) * sin(im);
	double t_1 = exp(re) * im;
	double tmp;
	if (t_0 <= -((double) INFINITY)) {
		tmp = fma(re, fma(re, 0.5, 1.0), 1.0) * fma(im, (im * (im * -0.16666666666666666)), im);
	} else if (t_0 <= -0.01) {
		tmp = sin(im);
	} else if (t_0 <= 1e-101) {
		tmp = t_1;
	} else if (t_0 <= 1.0) {
		tmp = sin(im);
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64(exp(re) * sin(im))
	t_1 = Float64(exp(re) * im)
	tmp = 0.0
	if (t_0 <= Float64(-Inf))
		tmp = Float64(fma(re, fma(re, 0.5, 1.0), 1.0) * fma(im, Float64(im * Float64(im * -0.16666666666666666)), im));
	elseif (t_0 <= -0.01)
		tmp = sin(im);
	elseif (t_0 <= 1e-101)
		tmp = t_1;
	elseif (t_0 <= 1.0)
		tmp = sin(im);
	else
		tmp = t_1;
	end
	return tmp
end
code[re_, im_] := Block[{t$95$0 = N[(N[Exp[re], $MachinePrecision] * N[Sin[im], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Exp[re], $MachinePrecision] * im), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], N[(N[(re * N[(re * 0.5 + 1.0), $MachinePrecision] + 1.0), $MachinePrecision] * N[(im * N[(im * N[(im * -0.16666666666666666), $MachinePrecision]), $MachinePrecision] + im), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, -0.01], N[Sin[im], $MachinePrecision], If[LessEqual[t$95$0, 1e-101], t$95$1, If[LessEqual[t$95$0, 1.0], N[Sin[im], $MachinePrecision], t$95$1]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{re} \cdot \sin im\\
t_1 := e^{re} \cdot im\\
\mathbf{if}\;t\_0 \leq -\infty:\\
\;\;\;\;\mathsf{fma}\left(re, \mathsf{fma}\left(re, 0.5, 1\right), 1\right) \cdot \mathsf{fma}\left(im, im \cdot \left(im \cdot -0.16666666666666666\right), im\right)\\

\mathbf{elif}\;t\_0 \leq -0.01:\\
\;\;\;\;\sin im\\

\mathbf{elif}\;t\_0 \leq 10^{-101}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;t\_0 \leq 1:\\
\;\;\;\;\sin im\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 (exp.f64 re) (sin.f64 im)) < -inf.0

    1. Initial program 100.0%

      \[e^{re} \cdot \sin im \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0

      \[\leadsto \color{blue}{\left(1 + re \cdot \left(1 + \frac{1}{2} \cdot re\right)\right)} \cdot \sin im \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\left(re \cdot \left(1 + \frac{1}{2} \cdot re\right) + 1\right)} \cdot \sin im \]
      2. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(re, 1 + \frac{1}{2} \cdot re, 1\right)} \cdot \sin im \]
      3. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(re, \color{blue}{\frac{1}{2} \cdot re + 1}, 1\right) \cdot \sin im \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(re, \color{blue}{re \cdot \frac{1}{2}} + 1, 1\right) \cdot \sin im \]
      5. lower-fma.f6453.0

        \[\leadsto \mathsf{fma}\left(re, \color{blue}{\mathsf{fma}\left(re, 0.5, 1\right)}, 1\right) \cdot \sin im \]
    5. Applied rewrites53.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(re, \mathsf{fma}\left(re, 0.5, 1\right), 1\right)} \cdot \sin im \]
    6. Taylor expanded in im around 0

      \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \frac{1}{2}, 1\right), 1\right) \cdot \color{blue}{\left(im \cdot \left(1 + \frac{-1}{6} \cdot {im}^{2}\right)\right)} \]
    7. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \frac{1}{2}, 1\right), 1\right) \cdot \left(im \cdot \color{blue}{\left(\frac{-1}{6} \cdot {im}^{2} + 1\right)}\right) \]
      2. distribute-lft-inN/A

        \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \frac{1}{2}, 1\right), 1\right) \cdot \color{blue}{\left(im \cdot \left(\frac{-1}{6} \cdot {im}^{2}\right) + im \cdot 1\right)} \]
      3. *-rgt-identityN/A

        \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \frac{1}{2}, 1\right), 1\right) \cdot \left(im \cdot \left(\frac{-1}{6} \cdot {im}^{2}\right) + \color{blue}{im}\right) \]
      4. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \frac{1}{2}, 1\right), 1\right) \cdot \color{blue}{\mathsf{fma}\left(im, \frac{-1}{6} \cdot {im}^{2}, im\right)} \]
      5. unpow2N/A

        \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \frac{1}{2}, 1\right), 1\right) \cdot \mathsf{fma}\left(im, \frac{-1}{6} \cdot \color{blue}{\left(im \cdot im\right)}, im\right) \]
      6. associate-*r*N/A

        \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \frac{1}{2}, 1\right), 1\right) \cdot \mathsf{fma}\left(im, \color{blue}{\left(\frac{-1}{6} \cdot im\right) \cdot im}, im\right) \]
      7. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \frac{1}{2}, 1\right), 1\right) \cdot \mathsf{fma}\left(im, \color{blue}{im \cdot \left(\frac{-1}{6} \cdot im\right)}, im\right) \]
      8. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \frac{1}{2}, 1\right), 1\right) \cdot \mathsf{fma}\left(im, \color{blue}{im \cdot \left(\frac{-1}{6} \cdot im\right)}, im\right) \]
      9. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \frac{1}{2}, 1\right), 1\right) \cdot \mathsf{fma}\left(im, im \cdot \color{blue}{\left(im \cdot \frac{-1}{6}\right)}, im\right) \]
      10. lower-*.f6449.3

        \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, 0.5, 1\right), 1\right) \cdot \mathsf{fma}\left(im, im \cdot \color{blue}{\left(im \cdot -0.16666666666666666\right)}, im\right) \]
    8. Applied rewrites49.3%

      \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, 0.5, 1\right), 1\right) \cdot \color{blue}{\mathsf{fma}\left(im, im \cdot \left(im \cdot -0.16666666666666666\right), im\right)} \]

    if -inf.0 < (*.f64 (exp.f64 re) (sin.f64 im)) < -0.0100000000000000002 or 1.00000000000000005e-101 < (*.f64 (exp.f64 re) (sin.f64 im)) < 1

    1. Initial program 100.0%

      \[e^{re} \cdot \sin im \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0

      \[\leadsto \color{blue}{\sin im} \]
    4. Step-by-step derivation
      1. lower-sin.f6497.3

        \[\leadsto \color{blue}{\sin im} \]
    5. Applied rewrites97.3%

      \[\leadsto \color{blue}{\sin im} \]

    if -0.0100000000000000002 < (*.f64 (exp.f64 re) (sin.f64 im)) < 1.00000000000000005e-101 or 1 < (*.f64 (exp.f64 re) (sin.f64 im))

    1. Initial program 100.0%

      \[e^{re} \cdot \sin im \]
    2. Add Preprocessing
    3. Taylor expanded in im around 0

      \[\leadsto \color{blue}{im \cdot e^{re}} \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \color{blue}{im \cdot e^{re}} \]
      2. lower-exp.f6494.5

        \[\leadsto im \cdot \color{blue}{e^{re}} \]
    5. Applied rewrites94.5%

      \[\leadsto \color{blue}{im \cdot e^{re}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification89.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{re} \cdot \sin im \leq -\infty:\\ \;\;\;\;\mathsf{fma}\left(re, \mathsf{fma}\left(re, 0.5, 1\right), 1\right) \cdot \mathsf{fma}\left(im, im \cdot \left(im \cdot -0.16666666666666666\right), im\right)\\ \mathbf{elif}\;e^{re} \cdot \sin im \leq -0.01:\\ \;\;\;\;\sin im\\ \mathbf{elif}\;e^{re} \cdot \sin im \leq 10^{-101}:\\ \;\;\;\;e^{re} \cdot im\\ \mathbf{elif}\;e^{re} \cdot \sin im \leq 1:\\ \;\;\;\;\sin im\\ \mathbf{else}:\\ \;\;\;\;e^{re} \cdot im\\ \end{array} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024230 
(FPCore (re im)
  :name "math.exp on complex, imaginary part"
  :precision binary64
  (* (exp re) (sin im)))