math.exp on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 3.2s
Alternatives: 5
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 5 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. Final simplification100.0%

    \[\leadsto e^{re} \cdot \cos im \]

Alternative 2: 70.9% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 5 \cdot 10^{+15}:\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;e^{re} \cdot \left(1 + -0.5 \cdot \left(im \cdot im\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re 5e+15) (exp re) (* (exp re) (+ 1.0 (* -0.5 (* im im))))))
double code(double re, double im) {
	double tmp;
	if (re <= 5e+15) {
		tmp = exp(re);
	} else {
		tmp = exp(re) * (1.0 + (-0.5 * (im * im)));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= 5d+15) then
        tmp = exp(re)
    else
        tmp = exp(re) * (1.0d0 + ((-0.5d0) * (im * im)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= 5e+15) {
		tmp = Math.exp(re);
	} else {
		tmp = Math.exp(re) * (1.0 + (-0.5 * (im * im)));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= 5e+15:
		tmp = math.exp(re)
	else:
		tmp = math.exp(re) * (1.0 + (-0.5 * (im * im)))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= 5e+15)
		tmp = exp(re);
	else
		tmp = Float64(exp(re) * Float64(1.0 + Float64(-0.5 * Float64(im * im))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= 5e+15)
		tmp = exp(re);
	else
		tmp = exp(re) * (1.0 + (-0.5 * (im * im)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, 5e+15], N[Exp[re], $MachinePrecision], N[(N[Exp[re], $MachinePrecision] * N[(1.0 + N[(-0.5 * N[(im * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq 5 \cdot 10^{+15}:\\
\;\;\;\;e^{re}\\

\mathbf{else}:\\
\;\;\;\;e^{re} \cdot \left(1 + -0.5 \cdot \left(im \cdot im\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 5e15

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in im around 0 61.0%

      \[\leadsto e^{re} \cdot \color{blue}{\left(1 + -0.5 \cdot {im}^{2}\right)} \]
    3. Step-by-step derivation
      1. unpow261.0%

        \[\leadsto e^{re} \cdot \left(1 + -0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
    4. Simplified61.0%

      \[\leadsto e^{re} \cdot \color{blue}{\left(1 + -0.5 \cdot \left(im \cdot im\right)\right)} \]
    5. Taylor expanded in im around 0 69.3%

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

    if 5e15 < re

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in im around 0 80.6%

      \[\leadsto e^{re} \cdot \color{blue}{\left(1 + -0.5 \cdot {im}^{2}\right)} \]
    3. Step-by-step derivation
      1. unpow280.6%

        \[\leadsto e^{re} \cdot \left(1 + -0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
    4. Simplified80.6%

      \[\leadsto e^{re} \cdot \color{blue}{\left(1 + -0.5 \cdot \left(im \cdot im\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification72.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 5 \cdot 10^{+15}:\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;e^{re} \cdot \left(1 + -0.5 \cdot \left(im \cdot im\right)\right)\\ \end{array} \]

Alternative 3: 71.1% accurate, 2.0× speedup?

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

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

    \[e^{re} \cdot \cos im \]
  2. Taylor expanded in im around 0 65.8%

    \[\leadsto e^{re} \cdot \color{blue}{\left(1 + -0.5 \cdot {im}^{2}\right)} \]
  3. Step-by-step derivation
    1. unpow265.8%

      \[\leadsto e^{re} \cdot \left(1 + -0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
  4. Simplified65.8%

    \[\leadsto e^{re} \cdot \color{blue}{\left(1 + -0.5 \cdot \left(im \cdot im\right)\right)} \]
  5. Taylor expanded in im around 0 69.7%

    \[\leadsto \color{blue}{e^{re}} \]
  6. Final simplification69.7%

    \[\leadsto e^{re} \]

Alternative 4: 12.9% accurate, 29.0× speedup?

\[\begin{array}{l} \\ -0.5 \cdot \left(re \cdot \left(im \cdot im\right)\right) \end{array} \]
(FPCore (re im) :precision binary64 (* -0.5 (* re (* im im))))
double code(double re, double im) {
	return -0.5 * (re * (im * im));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (-0.5d0) * (re * (im * im))
end function
public static double code(double re, double im) {
	return -0.5 * (re * (im * im));
}
def code(re, im):
	return -0.5 * (re * (im * im))
function code(re, im)
	return Float64(-0.5 * Float64(re * Float64(im * im)))
end
function tmp = code(re, im)
	tmp = -0.5 * (re * (im * im));
end
code[re_, im_] := N[(-0.5 * N[(re * N[(im * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
-0.5 \cdot \left(re \cdot \left(im \cdot im\right)\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[e^{re} \cdot \cos im \]
  2. Taylor expanded in im around 0 65.8%

    \[\leadsto e^{re} \cdot \color{blue}{\left(1 + -0.5 \cdot {im}^{2}\right)} \]
  3. Step-by-step derivation
    1. unpow265.8%

      \[\leadsto e^{re} \cdot \left(1 + -0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
  4. Simplified65.8%

    \[\leadsto e^{re} \cdot \color{blue}{\left(1 + -0.5 \cdot \left(im \cdot im\right)\right)} \]
  5. Taylor expanded in im around inf 25.4%

    \[\leadsto \color{blue}{-0.5 \cdot \left(e^{re} \cdot {im}^{2}\right)} \]
  6. Step-by-step derivation
    1. unpow225.4%

      \[\leadsto -0.5 \cdot \left(e^{re} \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
    2. associate-*r*25.4%

      \[\leadsto \color{blue}{\left(-0.5 \cdot e^{re}\right) \cdot \left(im \cdot im\right)} \]
    3. *-commutative25.4%

      \[\leadsto \color{blue}{\left(e^{re} \cdot -0.5\right)} \cdot \left(im \cdot im\right) \]
    4. associate-*l*25.4%

      \[\leadsto \color{blue}{e^{re} \cdot \left(-0.5 \cdot \left(im \cdot im\right)\right)} \]
  7. Simplified25.4%

    \[\leadsto \color{blue}{e^{re} \cdot \left(-0.5 \cdot \left(im \cdot im\right)\right)} \]
  8. Taylor expanded in re around 0 13.3%

    \[\leadsto \color{blue}{-0.5 \cdot \left(re \cdot {im}^{2}\right) + -0.5 \cdot {im}^{2}} \]
  9. Step-by-step derivation
    1. associate-*r*13.3%

      \[\leadsto \color{blue}{\left(-0.5 \cdot re\right) \cdot {im}^{2}} + -0.5 \cdot {im}^{2} \]
    2. distribute-rgt-out13.4%

      \[\leadsto \color{blue}{{im}^{2} \cdot \left(-0.5 \cdot re + -0.5\right)} \]
    3. unpow213.4%

      \[\leadsto \color{blue}{\left(im \cdot im\right)} \cdot \left(-0.5 \cdot re + -0.5\right) \]
  10. Simplified13.4%

    \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot \left(-0.5 \cdot re + -0.5\right)} \]
  11. Taylor expanded in re around inf 13.7%

    \[\leadsto \color{blue}{-0.5 \cdot \left(re \cdot {im}^{2}\right)} \]
  12. Step-by-step derivation
    1. unpow213.7%

      \[\leadsto -0.5 \cdot \left(re \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
  13. Simplified13.7%

    \[\leadsto \color{blue}{-0.5 \cdot \left(re \cdot \left(im \cdot im\right)\right)} \]
  14. Final simplification13.7%

    \[\leadsto -0.5 \cdot \left(re \cdot \left(im \cdot im\right)\right) \]

Alternative 5: 11.1% accurate, 40.6× speedup?

\[\begin{array}{l} \\ -0.5 \cdot \left(im \cdot im\right) \end{array} \]
(FPCore (re im) :precision binary64 (* -0.5 (* im im)))
double code(double re, double im) {
	return -0.5 * (im * im);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (-0.5d0) * (im * im)
end function
public static double code(double re, double im) {
	return -0.5 * (im * im);
}
def code(re, im):
	return -0.5 * (im * im)
function code(re, im)
	return Float64(-0.5 * Float64(im * im))
end
function tmp = code(re, im)
	tmp = -0.5 * (im * im);
end
code[re_, im_] := N[(-0.5 * N[(im * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
-0.5 \cdot \left(im \cdot im\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[e^{re} \cdot \cos im \]
  2. Taylor expanded in im around 0 65.8%

    \[\leadsto e^{re} \cdot \color{blue}{\left(1 + -0.5 \cdot {im}^{2}\right)} \]
  3. Step-by-step derivation
    1. unpow265.8%

      \[\leadsto e^{re} \cdot \left(1 + -0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
  4. Simplified65.8%

    \[\leadsto e^{re} \cdot \color{blue}{\left(1 + -0.5 \cdot \left(im \cdot im\right)\right)} \]
  5. Taylor expanded in im around inf 25.4%

    \[\leadsto \color{blue}{-0.5 \cdot \left(e^{re} \cdot {im}^{2}\right)} \]
  6. Step-by-step derivation
    1. unpow225.4%

      \[\leadsto -0.5 \cdot \left(e^{re} \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
    2. associate-*r*25.4%

      \[\leadsto \color{blue}{\left(-0.5 \cdot e^{re}\right) \cdot \left(im \cdot im\right)} \]
    3. *-commutative25.4%

      \[\leadsto \color{blue}{\left(e^{re} \cdot -0.5\right)} \cdot \left(im \cdot im\right) \]
    4. associate-*l*25.4%

      \[\leadsto \color{blue}{e^{re} \cdot \left(-0.5 \cdot \left(im \cdot im\right)\right)} \]
  7. Simplified25.4%

    \[\leadsto \color{blue}{e^{re} \cdot \left(-0.5 \cdot \left(im \cdot im\right)\right)} \]
  8. Taylor expanded in re around 0 12.2%

    \[\leadsto \color{blue}{-0.5 \cdot {im}^{2}} \]
  9. Step-by-step derivation
    1. unpow212.2%

      \[\leadsto -0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
  10. Simplified12.2%

    \[\leadsto \color{blue}{-0.5 \cdot \left(im \cdot im\right)} \]
  11. Final simplification12.2%

    \[\leadsto -0.5 \cdot \left(im \cdot im\right) \]

Reproduce

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