math.exp on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 3.3s
Alternatives: 4
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 4 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: 72.3% 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 63.4%

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

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

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

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

    \[\leadsto e^{re} \]

Alternative 3: 12.7% 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 63.4%

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

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

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

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

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

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

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

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

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

    \[\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*11.2%

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

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

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

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

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

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

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

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

Alternative 4: 11.5% 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 63.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

Reproduce

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