math.exp on complex, real part

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

    \[\leadsto e^{re} \cdot \cos im \]
  4. Add Preprocessing

Alternative 2: 93.2% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;e^{re} \leq 0 \lor \neg \left(e^{re} \leq 1.000002\right):\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im \cdot \left(re + 1\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= (exp re) 0.0) (not (<= (exp re) 1.000002)))
   (exp re)
   (* (cos im) (+ re 1.0))))
double code(double re, double im) {
	double tmp;
	if ((exp(re) <= 0.0) || !(exp(re) <= 1.000002)) {
		tmp = exp(re);
	} else {
		tmp = cos(im) * (re + 1.0);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if ((exp(re) <= 0.0d0) .or. (.not. (exp(re) <= 1.000002d0))) then
        tmp = exp(re)
    else
        tmp = cos(im) * (re + 1.0d0)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if ((Math.exp(re) <= 0.0) || !(Math.exp(re) <= 1.000002)) {
		tmp = Math.exp(re);
	} else {
		tmp = Math.cos(im) * (re + 1.0);
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (math.exp(re) <= 0.0) or not (math.exp(re) <= 1.000002):
		tmp = math.exp(re)
	else:
		tmp = math.cos(im) * (re + 1.0)
	return tmp
function code(re, im)
	tmp = 0.0
	if ((exp(re) <= 0.0) || !(exp(re) <= 1.000002))
		tmp = exp(re);
	else
		tmp = Float64(cos(im) * Float64(re + 1.0));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((exp(re) <= 0.0) || ~((exp(re) <= 1.000002)))
		tmp = exp(re);
	else
		tmp = cos(im) * (re + 1.0);
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[N[Exp[re], $MachinePrecision], 0.0], N[Not[LessEqual[N[Exp[re], $MachinePrecision], 1.000002]], $MachinePrecision]], N[Exp[re], $MachinePrecision], N[(N[Cos[im], $MachinePrecision] * N[(re + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;e^{re} \leq 0 \lor \neg \left(e^{re} \leq 1.000002\right):\\
\;\;\;\;e^{re}\\

\mathbf{else}:\\
\;\;\;\;\cos im \cdot \left(re + 1\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (exp.f64 re) < 0.0 or 1.00000200000000006 < (exp.f64 re)

    1. Initial program 100.0%

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

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

    if 0.0 < (exp.f64 re) < 1.00000200000000006

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\cos im + re \cdot \cos im} \]
    4. Step-by-step derivation
      1. distribute-rgt1-in99.9%

        \[\leadsto \color{blue}{\left(re + 1\right) \cdot \cos im} \]
    5. Simplified99.9%

      \[\leadsto \color{blue}{\left(re + 1\right) \cdot \cos im} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification91.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{re} \leq 0 \lor \neg \left(e^{re} \leq 1.000002\right):\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im \cdot \left(re + 1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 92.8% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;e^{re} \leq 0 \lor \neg \left(e^{re} \leq 1.000002\right):\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= (exp re) 0.0) (not (<= (exp re) 1.000002))) (exp re) (cos im)))
double code(double re, double im) {
	double tmp;
	if ((exp(re) <= 0.0) || !(exp(re) <= 1.000002)) {
		tmp = exp(re);
	} else {
		tmp = cos(im);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if ((exp(re) <= 0.0d0) .or. (.not. (exp(re) <= 1.000002d0))) then
        tmp = exp(re)
    else
        tmp = cos(im)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if ((Math.exp(re) <= 0.0) || !(Math.exp(re) <= 1.000002)) {
		tmp = Math.exp(re);
	} else {
		tmp = Math.cos(im);
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (math.exp(re) <= 0.0) or not (math.exp(re) <= 1.000002):
		tmp = math.exp(re)
	else:
		tmp = math.cos(im)
	return tmp
function code(re, im)
	tmp = 0.0
	if ((exp(re) <= 0.0) || !(exp(re) <= 1.000002))
		tmp = exp(re);
	else
		tmp = cos(im);
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((exp(re) <= 0.0) || ~((exp(re) <= 1.000002)))
		tmp = exp(re);
	else
		tmp = cos(im);
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[N[Exp[re], $MachinePrecision], 0.0], N[Not[LessEqual[N[Exp[re], $MachinePrecision], 1.000002]], $MachinePrecision]], N[Exp[re], $MachinePrecision], N[Cos[im], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;e^{re} \leq 0 \lor \neg \left(e^{re} \leq 1.000002\right):\\
\;\;\;\;e^{re}\\

\mathbf{else}:\\
\;\;\;\;\cos im\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (exp.f64 re) < 0.0 or 1.00000200000000006 < (exp.f64 re)

    1. Initial program 100.0%

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

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

    if 0.0 < (exp.f64 re) < 1.00000200000000006

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\cos im} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification91.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{re} \leq 0 \lor \neg \left(e^{re} \leq 1.000002\right):\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 51.2% accurate, 2.0× speedup?

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

\\
\cos im
\end{array}
Derivation
  1. Initial program 100.0%

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

    \[\leadsto \color{blue}{\cos im} \]
  4. Final simplification47.0%

    \[\leadsto \cos im \]
  5. Add Preprocessing

Alternative 5: 29.3% accurate, 67.7× speedup?

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

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

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

    \[\leadsto \color{blue}{\cos im + re \cdot \cos im} \]
  4. Step-by-step derivation
    1. distribute-rgt1-in48.0%

      \[\leadsto \color{blue}{\left(re + 1\right) \cdot \cos im} \]
  5. Simplified48.0%

    \[\leadsto \color{blue}{\left(re + 1\right) \cdot \cos im} \]
  6. Taylor expanded in im around 0 28.3%

    \[\leadsto \color{blue}{1 + re} \]
  7. Step-by-step derivation
    1. +-commutative28.3%

      \[\leadsto \color{blue}{re + 1} \]
  8. Simplified28.3%

    \[\leadsto \color{blue}{re + 1} \]
  9. Final simplification28.3%

    \[\leadsto re + 1 \]
  10. Add Preprocessing

Alternative 6: 3.6% accurate, 203.0× speedup?

\[\begin{array}{l} \\ re \end{array} \]
(FPCore (re im) :precision binary64 re)
double code(double re, double im) {
	return re;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = re
end function
public static double code(double re, double im) {
	return re;
}
def code(re, im):
	return re
function code(re, im)
	return re
end
function tmp = code(re, im)
	tmp = re;
end
code[re_, im_] := re
\begin{array}{l}

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

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

    \[\leadsto \color{blue}{\cos im + re \cdot \cos im} \]
  4. Step-by-step derivation
    1. distribute-rgt1-in48.0%

      \[\leadsto \color{blue}{\left(re + 1\right) \cdot \cos im} \]
  5. Simplified48.0%

    \[\leadsto \color{blue}{\left(re + 1\right) \cdot \cos im} \]
  6. Taylor expanded in re around inf 3.8%

    \[\leadsto \color{blue}{re \cdot \cos im} \]
  7. Step-by-step derivation
    1. *-commutative3.8%

      \[\leadsto \color{blue}{\cos im \cdot re} \]
  8. Simplified3.8%

    \[\leadsto \color{blue}{\cos im \cdot re} \]
  9. Taylor expanded in im around 0 3.4%

    \[\leadsto \color{blue}{re} \]
  10. Final simplification3.4%

    \[\leadsto re \]
  11. Add Preprocessing

Reproduce

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