math.exp on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 9.1s
Alternatives: 20
Speedup: 1.0×

Specification

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

\\
e^{re} \cdot \cos 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 20 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 \cos im \end{array} \]
(FPCore (re im) :precision binary64 (* (exp re) (cos im)))
double code(double re, double im) {
	return exp(re) * cos(im);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = exp(re) * cos(im)
end function
public static double code(double re, double im) {
	return Math.exp(re) * Math.cos(im);
}
def code(re, im):
	return math.exp(re) * math.cos(im)
function code(re, im)
	return Float64(exp(re) * cos(im))
end
function tmp = code(re, im)
	tmp = exp(re) * cos(im);
end
code[re_, im_] := N[(N[Exp[re], $MachinePrecision] * N[Cos[im], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 1: 100.0% accurate, 1.0× speedup?

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

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

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

Alternative 2: 99.0% accurate, 0.2× speedup?

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

\\
\begin{array}{l}
t_0 := e^{re} \cdot \cos im\\
\mathbf{if}\;t\_0 \leq -\infty:\\
\;\;\;\;e^{re} \cdot \left(im \cdot \left(im \cdot -0.5\right)\right)\\

\mathbf{elif}\;t\_0 \leq -0.05:\\
\;\;\;\;\cos im \cdot \mathsf{fma}\left(re, \mathsf{fma}\left(re, \mathsf{fma}\left(re, 0.16666666666666666, 0.5\right), 1\right), 1\right)\\

\mathbf{elif}\;t\_0 \leq 0:\\
\;\;\;\;e^{re}\\

\mathbf{elif}\;t\_0 \leq 0.9999999999999974:\\
\;\;\;\;\cos im \cdot \mathsf{fma}\left(\mathsf{fma}\left(re, 0.16666666666666666, 0.5\right), re \cdot re, re + 1\right)\\

\mathbf{else}:\\
\;\;\;\;e^{re}\\


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

    1. Initial program 100.0%

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

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

        \[\leadsto e^{re} \cdot \color{blue}{\left(\frac{-1}{2} \cdot {im}^{2} + 1\right)} \]
      2. unpow2N/A

        \[\leadsto e^{re} \cdot \left(\frac{-1}{2} \cdot \color{blue}{\left(im \cdot im\right)} + 1\right) \]
      3. associate-*r*N/A

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

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

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

        \[\leadsto e^{re} \cdot \mathsf{fma}\left(im, \color{blue}{im \cdot \frac{-1}{2}}, 1\right) \]
      7. lower-*.f64100.0

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

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

      \[\leadsto e^{re} \cdot \left(\frac{-1}{2} \cdot \color{blue}{{im}^{2}}\right) \]
    7. Step-by-step derivation
      1. Applied rewrites100.0%

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

      if -inf.0 < (*.f64 (exp.f64 re) (cos.f64 im)) < -0.050000000000000003

      1. Initial program 100.0%

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

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

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

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

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

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

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

          \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \color{blue}{re \cdot \frac{1}{6}} + \frac{1}{2}, 1\right), 1\right) \cdot \cos im \]
        7. lower-fma.f6499.1

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

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

      if -0.050000000000000003 < (*.f64 (exp.f64 re) (cos.f64 im)) < -0.0 or 0.999999999999997446 < (*.f64 (exp.f64 re) (cos.f64 im))

      1. Initial program 100.0%

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

        \[\leadsto \color{blue}{e^{re}} \]
      4. Step-by-step derivation
        1. lower-exp.f6499.2

          \[\leadsto \color{blue}{e^{re}} \]
      5. Applied rewrites99.2%

        \[\leadsto \color{blue}{e^{re}} \]

      if -0.0 < (*.f64 (exp.f64 re) (cos.f64 im)) < 0.999999999999997446

      1. Initial program 100.0%

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

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

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

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

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

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

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

          \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \color{blue}{re \cdot \frac{1}{6}} + \frac{1}{2}, 1\right), 1\right) \cdot \cos im \]
        7. lower-fma.f6497.4

          \[\leadsto \mathsf{fma}\left(re, \mathsf{fma}\left(re, \color{blue}{\mathsf{fma}\left(re, 0.16666666666666666, 0.5\right)}, 1\right), 1\right) \cdot \cos im \]
      5. Applied rewrites97.4%

        \[\leadsto \color{blue}{\mathsf{fma}\left(re, \mathsf{fma}\left(re, \mathsf{fma}\left(re, 0.16666666666666666, 0.5\right), 1\right), 1\right)} \cdot \cos im \]
      6. Step-by-step derivation
        1. Applied rewrites97.4%

          \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(re, 0.16666666666666666, 0.5\right), \color{blue}{re \cdot re}, re + 1\right) \cdot \cos im \]
      7. Recombined 4 regimes into one program.
      8. Final simplification99.0%

        \[\leadsto \begin{array}{l} \mathbf{if}\;e^{re} \cdot \cos im \leq -\infty:\\ \;\;\;\;e^{re} \cdot \left(im \cdot \left(im \cdot -0.5\right)\right)\\ \mathbf{elif}\;e^{re} \cdot \cos im \leq -0.05:\\ \;\;\;\;\cos im \cdot \mathsf{fma}\left(re, \mathsf{fma}\left(re, \mathsf{fma}\left(re, 0.16666666666666666, 0.5\right), 1\right), 1\right)\\ \mathbf{elif}\;e^{re} \cdot \cos im \leq 0:\\ \;\;\;\;e^{re}\\ \mathbf{elif}\;e^{re} \cdot \cos im \leq 0.9999999999999974:\\ \;\;\;\;\cos im \cdot \mathsf{fma}\left(\mathsf{fma}\left(re, 0.16666666666666666, 0.5\right), re \cdot re, re + 1\right)\\ \mathbf{else}:\\ \;\;\;\;e^{re}\\ \end{array} \]
      9. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024225 
      (FPCore (re im)
        :name "math.exp on complex, real part"
        :precision binary64
        (* (exp re) (cos im)))