Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.4% → 96.7%
Time: 14.4s
Alternatives: 9
Speedup: 3.8×

Specification

?
\[\begin{array}{l} \\ \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (*
  (cos (- (/ (* K (+ m n)) 2.0) M))
  (exp (- (- (pow (- (/ (+ m n) 2.0) M) 2.0)) (- l (fabs (- m n)))))))
double code(double K, double m, double n, double M, double l) {
	return cos((((K * (m + n)) / 2.0) - M)) * exp((-pow((((m + n) / 2.0) - M), 2.0) - (l - fabs((m - n)))));
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = cos((((k * (m + n)) / 2.0d0) - m_1)) * exp((-((((m + n) / 2.0d0) - m_1) ** 2.0d0) - (l - abs((m - n)))))
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos((((K * (m + n)) / 2.0) - M)) * Math.exp((-Math.pow((((m + n) / 2.0) - M), 2.0) - (l - Math.abs((m - n)))));
}
def code(K, m, n, M, l):
	return math.cos((((K * (m + n)) / 2.0) - M)) * math.exp((-math.pow((((m + n) / 2.0) - M), 2.0) - (l - math.fabs((m - n)))))
function code(K, m, n, M, l)
	return Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * exp(Float64(Float64(-(Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)) - Float64(l - abs(Float64(m - n))))))
end
function tmp = code(K, m, n, M, l)
	tmp = cos((((K * (m + n)) / 2.0) - M)) * exp((-((((m + n) / 2.0) - M) ^ 2.0) - (l - abs((m - n)))));
end
code[K_, m_, n_, M_, l_] := N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[((-N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]) - N[(l - N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)}
\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 9 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: 76.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (*
  (cos (- (/ (* K (+ m n)) 2.0) M))
  (exp (- (- (pow (- (/ (+ m n) 2.0) M) 2.0)) (- l (fabs (- m n)))))))
double code(double K, double m, double n, double M, double l) {
	return cos((((K * (m + n)) / 2.0) - M)) * exp((-pow((((m + n) / 2.0) - M), 2.0) - (l - fabs((m - n)))));
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = cos((((k * (m + n)) / 2.0d0) - m_1)) * exp((-((((m + n) / 2.0d0) - m_1) ** 2.0d0) - (l - abs((m - n)))))
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos((((K * (m + n)) / 2.0) - M)) * Math.exp((-Math.pow((((m + n) / 2.0) - M), 2.0) - (l - Math.abs((m - n)))));
}
def code(K, m, n, M, l):
	return math.cos((((K * (m + n)) / 2.0) - M)) * math.exp((-math.pow((((m + n) / 2.0) - M), 2.0) - (l - math.fabs((m - n)))))
function code(K, m, n, M, l)
	return Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * exp(Float64(Float64(-(Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)) - Float64(l - abs(Float64(m - n))))))
end
function tmp = code(K, m, n, M, l)
	tmp = cos((((K * (m + n)) / 2.0) - M)) * exp((-((((m + n) / 2.0) - M) ^ 2.0) - (l - abs((m - n)))));
end
code[K_, m_, n_, M_, l_] := N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[((-N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]) - N[(l - N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)}
\end{array}

Alternative 1: 96.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (* (cos M) (exp (- (- (fabs (- m n)) l) (pow (- (/ (+ m n) 2.0) M) 2.0)))))
double code(double K, double m, double n, double M, double l) {
	return cos(M) * exp(((fabs((m - n)) - l) - pow((((m + n) / 2.0) - M), 2.0)));
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = cos(m_1) * exp(((abs((m - n)) - l) - ((((m + n) / 2.0d0) - m_1) ** 2.0d0)))
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos(M) * Math.exp(((Math.abs((m - n)) - l) - Math.pow((((m + n) / 2.0) - M), 2.0)));
}
def code(K, m, n, M, l):
	return math.cos(M) * math.exp(((math.fabs((m - n)) - l) - math.pow((((m + n) / 2.0) - M), 2.0)))
function code(K, m, n, M, l)
	return Float64(cos(M) * exp(Float64(Float64(abs(Float64(m - n)) - l) - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0))))
end
function tmp = code(K, m, n, M, l)
	tmp = cos(M) * exp(((abs((m - n)) - l) - ((((m + n) / 2.0) - M) ^ 2.0)));
end
code[K_, m_, n_, M_, l_] := N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}
\end{array}
Derivation
  1. Initial program 78.1%

    \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in K around 0 98.1%

    \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  4. Step-by-step derivation
    1. cos-neg98.1%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  5. Simplified98.1%

    \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  6. Final simplification98.1%

    \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
  7. Add Preprocessing

Alternative 2: 95.0% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;M \leq -340000000000 \lor \neg \left(M \leq 4.8 \cdot 10^{+67}\right):\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= M -340000000000.0) (not (<= M 4.8e+67)))
   (* (cos M) (exp (- (pow M 2.0))))
   (exp (- (- (fabs (- m n)) l) (* 0.25 (pow (+ m n) 2.0))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((M <= -340000000000.0) || !(M <= 4.8e+67)) {
		tmp = cos(M) * exp(-pow(M, 2.0));
	} else {
		tmp = exp(((fabs((m - n)) - l) - (0.25 * pow((m + n), 2.0))));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if ((m_1 <= (-340000000000.0d0)) .or. (.not. (m_1 <= 4.8d+67))) then
        tmp = cos(m_1) * exp(-(m_1 ** 2.0d0))
    else
        tmp = exp(((abs((m - n)) - l) - (0.25d0 * ((m + n) ** 2.0d0))))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((M <= -340000000000.0) || !(M <= 4.8e+67)) {
		tmp = Math.cos(M) * Math.exp(-Math.pow(M, 2.0));
	} else {
		tmp = Math.exp(((Math.abs((m - n)) - l) - (0.25 * Math.pow((m + n), 2.0))));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if (M <= -340000000000.0) or not (M <= 4.8e+67):
		tmp = math.cos(M) * math.exp(-math.pow(M, 2.0))
	else:
		tmp = math.exp(((math.fabs((m - n)) - l) - (0.25 * math.pow((m + n), 2.0))))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if ((M <= -340000000000.0) || !(M <= 4.8e+67))
		tmp = Float64(cos(M) * exp(Float64(-(M ^ 2.0))));
	else
		tmp = exp(Float64(Float64(abs(Float64(m - n)) - l) - Float64(0.25 * (Float64(m + n) ^ 2.0))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if ((M <= -340000000000.0) || ~((M <= 4.8e+67)))
		tmp = cos(M) * exp(-(M ^ 2.0));
	else
		tmp = exp(((abs((m - n)) - l) - (0.25 * ((m + n) ^ 2.0))));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[Or[LessEqual[M, -340000000000.0], N[Not[LessEqual[M, 4.8e+67]], $MachinePrecision]], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-N[Power[M, 2.0], $MachinePrecision])], $MachinePrecision]), $MachinePrecision], N[Exp[N[(N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] - N[(0.25 * N[Power[N[(m + n), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;M \leq -340000000000 \lor \neg \left(M \leq 4.8 \cdot 10^{+67}\right):\\
\;\;\;\;\cos M \cdot e^{-{M}^{2}}\\

\mathbf{else}:\\
\;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -3.4e11 or 4.80000000000000004e67 < M

    1. Initial program 84.8%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in K around 0 100.0%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. cos-neg100.0%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    5. Simplified100.0%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. Taylor expanded in M around inf 98.2%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot {M}^{2}}} \]
    7. Step-by-step derivation
      1. mul-1-neg98.2%

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
    8. Simplified98.2%

      \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]

    if -3.4e11 < M < 4.80000000000000004e67

    1. Initial program 72.8%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in K around 0 96.7%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. cos-neg96.7%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    5. Simplified96.7%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. Taylor expanded in M around 0 94.7%

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. fabs-sub94.7%

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+94.7%

        \[\leadsto e^{\color{blue}{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    8. Simplified94.7%

      \[\leadsto \color{blue}{e^{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification96.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -340000000000 \lor \neg \left(M \leq 4.8 \cdot 10^{+67}\right):\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 88.6% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;M \leq -27 \lor \neg \left(M \leq 27\right):\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n - m\right) - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= M -27.0) (not (<= M 27.0)))
   (* (cos M) (exp (- (pow M 2.0))))
   (exp (- (- n m) (* 0.25 (* (+ m n) (+ m n)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((M <= -27.0) || !(M <= 27.0)) {
		tmp = cos(M) * exp(-pow(M, 2.0));
	} else {
		tmp = exp(((n - m) - (0.25 * ((m + n) * (m + n)))));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if ((m_1 <= (-27.0d0)) .or. (.not. (m_1 <= 27.0d0))) then
        tmp = cos(m_1) * exp(-(m_1 ** 2.0d0))
    else
        tmp = exp(((n - m) - (0.25d0 * ((m + n) * (m + n)))))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((M <= -27.0) || !(M <= 27.0)) {
		tmp = Math.cos(M) * Math.exp(-Math.pow(M, 2.0));
	} else {
		tmp = Math.exp(((n - m) - (0.25 * ((m + n) * (m + n)))));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if (M <= -27.0) or not (M <= 27.0):
		tmp = math.cos(M) * math.exp(-math.pow(M, 2.0))
	else:
		tmp = math.exp(((n - m) - (0.25 * ((m + n) * (m + n)))))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if ((M <= -27.0) || !(M <= 27.0))
		tmp = Float64(cos(M) * exp(Float64(-(M ^ 2.0))));
	else
		tmp = exp(Float64(Float64(n - m) - Float64(0.25 * Float64(Float64(m + n) * Float64(m + n)))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if ((M <= -27.0) || ~((M <= 27.0)))
		tmp = cos(M) * exp(-(M ^ 2.0));
	else
		tmp = exp(((n - m) - (0.25 * ((m + n) * (m + n)))));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[Or[LessEqual[M, -27.0], N[Not[LessEqual[M, 27.0]], $MachinePrecision]], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-N[Power[M, 2.0], $MachinePrecision])], $MachinePrecision]), $MachinePrecision], N[Exp[N[(N[(n - m), $MachinePrecision] - N[(0.25 * N[(N[(m + n), $MachinePrecision] * N[(m + n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;M \leq -27 \lor \neg \left(M \leq 27\right):\\
\;\;\;\;\cos M \cdot e^{-{M}^{2}}\\

\mathbf{else}:\\
\;\;\;\;e^{\left(n - m\right) - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -27 or 27 < M

    1. Initial program 81.8%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in K around 0 100.0%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. cos-neg100.0%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    5. Simplified100.0%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. Taylor expanded in M around inf 96.3%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot {M}^{2}}} \]
    7. Step-by-step derivation
      1. mul-1-neg96.3%

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
    8. Simplified96.3%

      \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]

    if -27 < M < 27

    1. Initial program 74.1%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in K around 0 96.1%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. cos-neg96.1%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    5. Simplified96.1%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. Taylor expanded in M around 0 96.1%

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. fabs-sub96.1%

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+96.1%

        \[\leadsto e^{\color{blue}{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    8. Simplified96.1%

      \[\leadsto \color{blue}{e^{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    9. Taylor expanded in l around 0 82.0%

      \[\leadsto e^{\color{blue}{\left|n - m\right|} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
    10. Step-by-step derivation
      1. rem-square-sqrt39.1%

        \[\leadsto e^{\left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right| - 0.25 \cdot {\left(m + n\right)}^{2}} \]
      2. fabs-sqr39.1%

        \[\leadsto e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
      3. rem-square-sqrt81.5%

        \[\leadsto e^{\color{blue}{\left(n - m\right)} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
    11. Simplified81.5%

      \[\leadsto e^{\color{blue}{\left(n - m\right)} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
    12. Step-by-step derivation
      1. unpow281.5%

        \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \color{blue}{\left(\left(m + n\right) \cdot \left(m + n\right)\right)}} \]
      2. +-commutative81.5%

        \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \left(\color{blue}{\left(n + m\right)} \cdot \left(m + n\right)\right)} \]
      3. +-commutative81.5%

        \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \left(\left(n + m\right) \cdot \color{blue}{\left(n + m\right)}\right)} \]
    13. Applied egg-rr81.5%

      \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \color{blue}{\left(\left(n + m\right) \cdot \left(n + m\right)\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification89.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -27 \lor \neg \left(M \leq 27\right):\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n - m\right) - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 83.3% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\ell \leq 700:\\ \;\;\;\;e^{\left(n - m\right) - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\cos M}{e^{\ell}}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= l 700.0)
   (exp (- (- n m) (* 0.25 (* (+ m n) (+ m n)))))
   (/ (cos M) (exp l))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= 700.0) {
		tmp = exp(((n - m) - (0.25 * ((m + n) * (m + n)))));
	} else {
		tmp = cos(M) / exp(l);
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (l <= 700.0d0) then
        tmp = exp(((n - m) - (0.25d0 * ((m + n) * (m + n)))))
    else
        tmp = cos(m_1) / exp(l)
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= 700.0) {
		tmp = Math.exp(((n - m) - (0.25 * ((m + n) * (m + n)))));
	} else {
		tmp = Math.cos(M) / Math.exp(l);
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if l <= 700.0:
		tmp = math.exp(((n - m) - (0.25 * ((m + n) * (m + n)))))
	else:
		tmp = math.cos(M) / math.exp(l)
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (l <= 700.0)
		tmp = exp(Float64(Float64(n - m) - Float64(0.25 * Float64(Float64(m + n) * Float64(m + n)))));
	else
		tmp = Float64(cos(M) / exp(l));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (l <= 700.0)
		tmp = exp(((n - m) - (0.25 * ((m + n) * (m + n)))));
	else
		tmp = cos(M) / exp(l);
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[l, 700.0], N[Exp[N[(N[(n - m), $MachinePrecision] - N[(0.25 * N[(N[(m + n), $MachinePrecision] * N[(m + n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Cos[M], $MachinePrecision] / N[Exp[l], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq 700:\\
\;\;\;\;e^{\left(n - m\right) - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{\cos M}{e^{\ell}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if l < 700

    1. Initial program 78.4%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in K around 0 97.8%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. cos-neg97.8%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    5. Simplified97.8%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. Taylor expanded in M around 0 81.7%

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. fabs-sub81.7%

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+81.7%

        \[\leadsto e^{\color{blue}{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    8. Simplified81.7%

      \[\leadsto \color{blue}{e^{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    9. Taylor expanded in l around 0 75.9%

      \[\leadsto e^{\color{blue}{\left|n - m\right|} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
    10. Step-by-step derivation
      1. rem-square-sqrt37.5%

        \[\leadsto e^{\left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right| - 0.25 \cdot {\left(m + n\right)}^{2}} \]
      2. fabs-sqr37.5%

        \[\leadsto e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
      3. rem-square-sqrt75.6%

        \[\leadsto e^{\color{blue}{\left(n - m\right)} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
    11. Simplified75.6%

      \[\leadsto e^{\color{blue}{\left(n - m\right)} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
    12. Step-by-step derivation
      1. unpow275.6%

        \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \color{blue}{\left(\left(m + n\right) \cdot \left(m + n\right)\right)}} \]
      2. +-commutative75.6%

        \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \left(\color{blue}{\left(n + m\right)} \cdot \left(m + n\right)\right)} \]
      3. +-commutative75.6%

        \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \left(\left(n + m\right) \cdot \color{blue}{\left(n + m\right)}\right)} \]
    13. Applied egg-rr75.6%

      \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \color{blue}{\left(\left(n + m\right) \cdot \left(n + m\right)\right)}} \]

    if 700 < l

    1. Initial program 76.7%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in l around inf 76.7%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    4. Step-by-step derivation
      1. mul-1-neg76.7%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
    5. Simplified76.7%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
    6. Step-by-step derivation
      1. clear-num76.7%

        \[\leadsto \cos \left(\color{blue}{\frac{1}{\frac{2}{K \cdot \left(m + n\right)}}} - M\right) \cdot e^{-\ell} \]
      2. inv-pow76.7%

        \[\leadsto \cos \left(\color{blue}{{\left(\frac{2}{K \cdot \left(m + n\right)}\right)}^{-1}} - M\right) \cdot e^{-\ell} \]
      3. *-commutative76.7%

        \[\leadsto \cos \left({\left(\frac{2}{\color{blue}{\left(m + n\right) \cdot K}}\right)}^{-1} - M\right) \cdot e^{-\ell} \]
    7. Applied egg-rr76.7%

      \[\leadsto \cos \left(\color{blue}{{\left(\frac{2}{\left(m + n\right) \cdot K}\right)}^{-1}} - M\right) \cdot e^{-\ell} \]
    8. Step-by-step derivation
      1. unpow-176.7%

        \[\leadsto \cos \left(\color{blue}{\frac{1}{\frac{2}{\left(m + n\right) \cdot K}}} - M\right) \cdot e^{-\ell} \]
    9. Simplified76.7%

      \[\leadsto \cos \left(\color{blue}{\frac{1}{\frac{2}{\left(m + n\right) \cdot K}}} - M\right) \cdot e^{-\ell} \]
    10. Step-by-step derivation
      1. exp-neg76.7%

        \[\leadsto \cos \left(\frac{1}{\frac{2}{\left(m + n\right) \cdot K}} - M\right) \cdot \color{blue}{\frac{1}{e^{\ell}}} \]
    11. Applied egg-rr76.7%

      \[\leadsto \cos \left(\frac{1}{\frac{2}{\left(m + n\right) \cdot K}} - M\right) \cdot \color{blue}{\frac{1}{e^{\ell}}} \]
    12. Taylor expanded in K around 0 100.0%

      \[\leadsto \color{blue}{\frac{\cos \left(-M\right)}{e^{\ell}}} \]
    13. Step-by-step derivation
      1. cos-neg100.0%

        \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
    14. Simplified100.0%

      \[\leadsto \color{blue}{\frac{\cos M}{e^{\ell}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification79.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq 700:\\ \;\;\;\;e^{\left(n - m\right) - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\cos M}{e^{\ell}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 51.6% accurate, 3.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;n \leq -8 \cdot 10^{-113} \lor \neg \left(n \leq 300000\right):\\ \;\;\;\;e^{\left(n - m\right) - 0.25 \cdot \left(n \cdot \left(n + m \cdot 2\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(-M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot 0.5 + -1\right)\right)\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= n -8e-113) (not (<= n 300000.0)))
   (exp (- (- n m) (* 0.25 (* n (+ n (* m 2.0))))))
   (* (cos (- M)) (+ 1.0 (* l (+ (* l 0.5) -1.0))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((n <= -8e-113) || !(n <= 300000.0)) {
		tmp = exp(((n - m) - (0.25 * (n * (n + (m * 2.0))))));
	} else {
		tmp = cos(-M) * (1.0 + (l * ((l * 0.5) + -1.0)));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if ((n <= (-8d-113)) .or. (.not. (n <= 300000.0d0))) then
        tmp = exp(((n - m) - (0.25d0 * (n * (n + (m * 2.0d0))))))
    else
        tmp = cos(-m_1) * (1.0d0 + (l * ((l * 0.5d0) + (-1.0d0))))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((n <= -8e-113) || !(n <= 300000.0)) {
		tmp = Math.exp(((n - m) - (0.25 * (n * (n + (m * 2.0))))));
	} else {
		tmp = Math.cos(-M) * (1.0 + (l * ((l * 0.5) + -1.0)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if (n <= -8e-113) or not (n <= 300000.0):
		tmp = math.exp(((n - m) - (0.25 * (n * (n + (m * 2.0))))))
	else:
		tmp = math.cos(-M) * (1.0 + (l * ((l * 0.5) + -1.0)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if ((n <= -8e-113) || !(n <= 300000.0))
		tmp = exp(Float64(Float64(n - m) - Float64(0.25 * Float64(n * Float64(n + Float64(m * 2.0))))));
	else
		tmp = Float64(cos(Float64(-M)) * Float64(1.0 + Float64(l * Float64(Float64(l * 0.5) + -1.0))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if ((n <= -8e-113) || ~((n <= 300000.0)))
		tmp = exp(((n - m) - (0.25 * (n * (n + (m * 2.0))))));
	else
		tmp = cos(-M) * (1.0 + (l * ((l * 0.5) + -1.0)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[Or[LessEqual[n, -8e-113], N[Not[LessEqual[n, 300000.0]], $MachinePrecision]], N[Exp[N[(N[(n - m), $MachinePrecision] - N[(0.25 * N[(n * N[(n + N[(m * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Cos[(-M)], $MachinePrecision] * N[(1.0 + N[(l * N[(N[(l * 0.5), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;n \leq -8 \cdot 10^{-113} \lor \neg \left(n \leq 300000\right):\\
\;\;\;\;e^{\left(n - m\right) - 0.25 \cdot \left(n \cdot \left(n + m \cdot 2\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos \left(-M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot 0.5 + -1\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if n < -7.99999999999999983e-113 or 3e5 < n

    1. Initial program 73.2%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in K around 0 99.3%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. cos-neg99.3%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    5. Simplified99.3%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. Taylor expanded in M around 0 94.5%

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. fabs-sub94.5%

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+94.5%

        \[\leadsto e^{\color{blue}{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    8. Simplified94.5%

      \[\leadsto \color{blue}{e^{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    9. Taylor expanded in l around 0 90.4%

      \[\leadsto e^{\color{blue}{\left|n - m\right|} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
    10. Step-by-step derivation
      1. rem-square-sqrt40.2%

        \[\leadsto e^{\left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right| - 0.25 \cdot {\left(m + n\right)}^{2}} \]
      2. fabs-sqr40.2%

        \[\leadsto e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
      3. rem-square-sqrt90.0%

        \[\leadsto e^{\color{blue}{\left(n - m\right)} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
    11. Simplified90.0%

      \[\leadsto e^{\color{blue}{\left(n - m\right)} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
    12. Taylor expanded in m around 0 76.0%

      \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \color{blue}{\left(2 \cdot \left(m \cdot n\right) + {n}^{2}\right)}} \]
    13. Step-by-step derivation
      1. +-commutative76.0%

        \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \color{blue}{\left({n}^{2} + 2 \cdot \left(m \cdot n\right)\right)}} \]
      2. unpow276.0%

        \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \left(\color{blue}{n \cdot n} + 2 \cdot \left(m \cdot n\right)\right)} \]
      3. associate-*r*76.0%

        \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \left(n \cdot n + \color{blue}{\left(2 \cdot m\right) \cdot n}\right)} \]
      4. distribute-rgt-in79.6%

        \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \color{blue}{\left(n \cdot \left(n + 2 \cdot m\right)\right)}} \]
      5. *-commutative79.6%

        \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \left(n \cdot \left(n + \color{blue}{m \cdot 2}\right)\right)} \]
    14. Simplified79.6%

      \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \color{blue}{\left(n \cdot \left(n + m \cdot 2\right)\right)}} \]

    if -7.99999999999999983e-113 < n < 3e5

    1. Initial program 84.1%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in l around inf 35.5%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    4. Step-by-step derivation
      1. mul-1-neg35.5%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
    5. Simplified35.5%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
    6. Step-by-step derivation
      1. clear-num36.4%

        \[\leadsto \cos \left(\color{blue}{\frac{1}{\frac{2}{K \cdot \left(m + n\right)}}} - M\right) \cdot e^{-\ell} \]
      2. inv-pow36.4%

        \[\leadsto \cos \left(\color{blue}{{\left(\frac{2}{K \cdot \left(m + n\right)}\right)}^{-1}} - M\right) \cdot e^{-\ell} \]
      3. *-commutative36.4%

        \[\leadsto \cos \left({\left(\frac{2}{\color{blue}{\left(m + n\right) \cdot K}}\right)}^{-1} - M\right) \cdot e^{-\ell} \]
    7. Applied egg-rr36.4%

      \[\leadsto \cos \left(\color{blue}{{\left(\frac{2}{\left(m + n\right) \cdot K}\right)}^{-1}} - M\right) \cdot e^{-\ell} \]
    8. Step-by-step derivation
      1. unpow-136.4%

        \[\leadsto \cos \left(\color{blue}{\frac{1}{\frac{2}{\left(m + n\right) \cdot K}}} - M\right) \cdot e^{-\ell} \]
    9. Simplified36.4%

      \[\leadsto \cos \left(\color{blue}{\frac{1}{\frac{2}{\left(m + n\right) \cdot K}}} - M\right) \cdot e^{-\ell} \]
    10. Taylor expanded in l around 0 21.0%

      \[\leadsto \cos \left(\frac{1}{\frac{2}{\left(m + n\right) \cdot K}} - M\right) \cdot \color{blue}{\left(1 + \ell \cdot \left(0.5 \cdot \ell - 1\right)\right)} \]
    11. Taylor expanded in K around 0 20.7%

      \[\leadsto \color{blue}{\cos \left(-M\right) \cdot \left(1 + \ell \cdot \left(0.5 \cdot \ell - 1\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification53.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq -8 \cdot 10^{-113} \lor \neg \left(n \leq 300000\right):\\ \;\;\;\;e^{\left(n - m\right) - 0.25 \cdot \left(n \cdot \left(n + m \cdot 2\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(-M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot 0.5 + -1\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 77.4% accurate, 3.8× speedup?

\[\begin{array}{l} \\ e^{\left(n - m\right) - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (exp (- (- n m) (* 0.25 (* (+ m n) (+ m n))))))
double code(double K, double m, double n, double M, double l) {
	return exp(((n - m) - (0.25 * ((m + n) * (m + n)))));
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = exp(((n - m) - (0.25d0 * ((m + n) * (m + n)))))
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.exp(((n - m) - (0.25 * ((m + n) * (m + n)))));
}
def code(K, m, n, M, l):
	return math.exp(((n - m) - (0.25 * ((m + n) * (m + n)))))
function code(K, m, n, M, l)
	return exp(Float64(Float64(n - m) - Float64(0.25 * Float64(Float64(m + n) * Float64(m + n)))))
end
function tmp = code(K, m, n, M, l)
	tmp = exp(((n - m) - (0.25 * ((m + n) * (m + n)))));
end
code[K_, m_, n_, M_, l_] := N[Exp[N[(N[(n - m), $MachinePrecision] - N[(0.25 * N[(N[(m + n), $MachinePrecision] * N[(m + n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
e^{\left(n - m\right) - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)}
\end{array}
Derivation
  1. Initial program 78.1%

    \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in K around 0 98.1%

    \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  4. Step-by-step derivation
    1. cos-neg98.1%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  5. Simplified98.1%

    \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  6. Taylor expanded in M around 0 84.8%

    \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
  7. Step-by-step derivation
    1. fabs-sub84.8%

      \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
    2. associate--r+84.8%

      \[\leadsto e^{\color{blue}{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
  8. Simplified84.8%

    \[\leadsto \color{blue}{e^{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
  9. Taylor expanded in l around 0 76.5%

    \[\leadsto e^{\color{blue}{\left|n - m\right|} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
  10. Step-by-step derivation
    1. rem-square-sqrt37.5%

      \[\leadsto e^{\left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right| - 0.25 \cdot {\left(m + n\right)}^{2}} \]
    2. fabs-sqr37.5%

      \[\leadsto e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
    3. rem-square-sqrt76.3%

      \[\leadsto e^{\color{blue}{\left(n - m\right)} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
  11. Simplified76.3%

    \[\leadsto e^{\color{blue}{\left(n - m\right)} - 0.25 \cdot {\left(m + n\right)}^{2}} \]
  12. Step-by-step derivation
    1. unpow276.3%

      \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \color{blue}{\left(\left(m + n\right) \cdot \left(m + n\right)\right)}} \]
    2. +-commutative76.3%

      \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \left(\color{blue}{\left(n + m\right)} \cdot \left(m + n\right)\right)} \]
    3. +-commutative76.3%

      \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \left(\left(n + m\right) \cdot \color{blue}{\left(n + m\right)}\right)} \]
  13. Applied egg-rr76.3%

    \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \color{blue}{\left(\left(n + m\right) \cdot \left(n + m\right)\right)}} \]
  14. Final simplification76.3%

    \[\leadsto e^{\left(n - m\right) - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)} \]
  15. Add Preprocessing

Alternative 7: 9.6% accurate, 3.8× speedup?

\[\begin{array}{l} \\ \cos \left(-M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot 0.5 + -1\right)\right) \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (* (cos (- M)) (+ 1.0 (* l (+ (* l 0.5) -1.0)))))
double code(double K, double m, double n, double M, double l) {
	return cos(-M) * (1.0 + (l * ((l * 0.5) + -1.0)));
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = cos(-m_1) * (1.0d0 + (l * ((l * 0.5d0) + (-1.0d0))))
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos(-M) * (1.0 + (l * ((l * 0.5) + -1.0)));
}
def code(K, m, n, M, l):
	return math.cos(-M) * (1.0 + (l * ((l * 0.5) + -1.0)))
function code(K, m, n, M, l)
	return Float64(cos(Float64(-M)) * Float64(1.0 + Float64(l * Float64(Float64(l * 0.5) + -1.0))))
end
function tmp = code(K, m, n, M, l)
	tmp = cos(-M) * (1.0 + (l * ((l * 0.5) + -1.0)));
end
code[K_, m_, n_, M_, l_] := N[(N[Cos[(-M)], $MachinePrecision] * N[(1.0 + N[(l * N[(N[(l * 0.5), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos \left(-M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot 0.5 + -1\right)\right)
\end{array}
Derivation
  1. Initial program 78.1%

    \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in l around inf 26.9%

    \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-1 \cdot \ell}} \]
  4. Step-by-step derivation
    1. mul-1-neg26.9%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
  5. Simplified26.9%

    \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
  6. Step-by-step derivation
    1. clear-num27.3%

      \[\leadsto \cos \left(\color{blue}{\frac{1}{\frac{2}{K \cdot \left(m + n\right)}}} - M\right) \cdot e^{-\ell} \]
    2. inv-pow27.3%

      \[\leadsto \cos \left(\color{blue}{{\left(\frac{2}{K \cdot \left(m + n\right)}\right)}^{-1}} - M\right) \cdot e^{-\ell} \]
    3. *-commutative27.3%

      \[\leadsto \cos \left({\left(\frac{2}{\color{blue}{\left(m + n\right) \cdot K}}\right)}^{-1} - M\right) \cdot e^{-\ell} \]
  7. Applied egg-rr27.3%

    \[\leadsto \cos \left(\color{blue}{{\left(\frac{2}{\left(m + n\right) \cdot K}\right)}^{-1}} - M\right) \cdot e^{-\ell} \]
  8. Step-by-step derivation
    1. unpow-127.3%

      \[\leadsto \cos \left(\color{blue}{\frac{1}{\frac{2}{\left(m + n\right) \cdot K}}} - M\right) \cdot e^{-\ell} \]
  9. Simplified27.3%

    \[\leadsto \cos \left(\color{blue}{\frac{1}{\frac{2}{\left(m + n\right) \cdot K}}} - M\right) \cdot e^{-\ell} \]
  10. Taylor expanded in l around 0 13.6%

    \[\leadsto \cos \left(\frac{1}{\frac{2}{\left(m + n\right) \cdot K}} - M\right) \cdot \color{blue}{\left(1 + \ell \cdot \left(0.5 \cdot \ell - 1\right)\right)} \]
  11. Taylor expanded in K around 0 14.2%

    \[\leadsto \color{blue}{\cos \left(-M\right) \cdot \left(1 + \ell \cdot \left(0.5 \cdot \ell - 1\right)\right)} \]
  12. Final simplification14.2%

    \[\leadsto \cos \left(-M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot 0.5 + -1\right)\right) \]
  13. Add Preprocessing

Alternative 8: 7.0% accurate, 4.2× speedup?

\[\begin{array}{l} \\ \cos M \end{array} \]
(FPCore (K m n M l) :precision binary64 (cos M))
double code(double K, double m, double n, double M, double l) {
	return cos(M);
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = cos(m_1)
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos(M);
}
def code(K, m, n, M, l):
	return math.cos(M)
function code(K, m, n, M, l)
	return cos(M)
end
function tmp = code(K, m, n, M, l)
	tmp = cos(M);
end
code[K_, m_, n_, M_, l_] := N[Cos[M], $MachinePrecision]
\begin{array}{l}

\\
\cos M
\end{array}
Derivation
  1. Initial program 78.1%

    \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in l around inf 26.9%

    \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-1 \cdot \ell}} \]
  4. Step-by-step derivation
    1. mul-1-neg26.9%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
  5. Simplified26.9%

    \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
  6. Taylor expanded in l around 0 8.6%

    \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right) - M\right)} \]
  7. Step-by-step derivation
    1. *-commutative8.6%

      \[\leadsto \cos \left(0.5 \cdot \color{blue}{\left(\left(m + n\right) \cdot K\right)} - M\right) \]
    2. *-commutative8.6%

      \[\leadsto \cos \left(\color{blue}{\left(\left(m + n\right) \cdot K\right) \cdot 0.5} - M\right) \]
    3. *-commutative8.6%

      \[\leadsto \cos \left(\color{blue}{\left(K \cdot \left(m + n\right)\right)} \cdot 0.5 - M\right) \]
    4. associate-*r*8.6%

      \[\leadsto \cos \left(\color{blue}{K \cdot \left(\left(m + n\right) \cdot 0.5\right)} - M\right) \]
    5. fmm-undef8.6%

      \[\leadsto \cos \color{blue}{\left(\mathsf{fma}\left(K, \left(m + n\right) \cdot 0.5, -M\right)\right)} \]
    6. fmm-undef8.6%

      \[\leadsto \cos \color{blue}{\left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right)} \]
    7. associate-*r*8.6%

      \[\leadsto \cos \left(\color{blue}{\left(K \cdot \left(m + n\right)\right) \cdot 0.5} - M\right) \]
    8. *-commutative8.6%

      \[\leadsto \cos \left(\color{blue}{\left(\left(m + n\right) \cdot K\right)} \cdot 0.5 - M\right) \]
    9. associate-*l*8.6%

      \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \left(K \cdot 0.5\right)} - M\right) \]
    10. *-commutative8.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \color{blue}{\left(0.5 \cdot K\right)} - M\right) \]
  8. Simplified8.6%

    \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \left(0.5 \cdot K\right) - M\right)} \]
  9. Taylor expanded in K around 0 9.0%

    \[\leadsto \color{blue}{\cos \left(-M\right)} \]
  10. Step-by-step derivation
    1. cos-neg9.0%

      \[\leadsto \color{blue}{\cos M} \]
  11. Simplified9.0%

    \[\leadsto \color{blue}{\cos M} \]
  12. Add Preprocessing

Alternative 9: 7.0% accurate, 425.0× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (K m n M l) :precision binary64 1.0)
double code(double K, double m, double n, double M, double l) {
	return 1.0;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = 1.0d0
end function
public static double code(double K, double m, double n, double M, double l) {
	return 1.0;
}
def code(K, m, n, M, l):
	return 1.0
function code(K, m, n, M, l)
	return 1.0
end
function tmp = code(K, m, n, M, l)
	tmp = 1.0;
end
code[K_, m_, n_, M_, l_] := 1.0
\begin{array}{l}

\\
1
\end{array}
Derivation
  1. Initial program 78.1%

    \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in l around inf 26.9%

    \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-1 \cdot \ell}} \]
  4. Step-by-step derivation
    1. mul-1-neg26.9%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
  5. Simplified26.9%

    \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
  6. Taylor expanded in l around 0 8.6%

    \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right) - M\right)} \]
  7. Step-by-step derivation
    1. *-commutative8.6%

      \[\leadsto \cos \left(0.5 \cdot \color{blue}{\left(\left(m + n\right) \cdot K\right)} - M\right) \]
    2. *-commutative8.6%

      \[\leadsto \cos \left(\color{blue}{\left(\left(m + n\right) \cdot K\right) \cdot 0.5} - M\right) \]
    3. *-commutative8.6%

      \[\leadsto \cos \left(\color{blue}{\left(K \cdot \left(m + n\right)\right)} \cdot 0.5 - M\right) \]
    4. associate-*r*8.6%

      \[\leadsto \cos \left(\color{blue}{K \cdot \left(\left(m + n\right) \cdot 0.5\right)} - M\right) \]
    5. fmm-undef8.6%

      \[\leadsto \cos \color{blue}{\left(\mathsf{fma}\left(K, \left(m + n\right) \cdot 0.5, -M\right)\right)} \]
    6. fmm-undef8.6%

      \[\leadsto \cos \color{blue}{\left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right)} \]
    7. associate-*r*8.6%

      \[\leadsto \cos \left(\color{blue}{\left(K \cdot \left(m + n\right)\right) \cdot 0.5} - M\right) \]
    8. *-commutative8.6%

      \[\leadsto \cos \left(\color{blue}{\left(\left(m + n\right) \cdot K\right)} \cdot 0.5 - M\right) \]
    9. associate-*l*8.6%

      \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \left(K \cdot 0.5\right)} - M\right) \]
    10. *-commutative8.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \color{blue}{\left(0.5 \cdot K\right)} - M\right) \]
  8. Simplified8.6%

    \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \left(0.5 \cdot K\right) - M\right)} \]
  9. Taylor expanded in K around 0 9.0%

    \[\leadsto \color{blue}{\cos \left(-M\right)} \]
  10. Step-by-step derivation
    1. cos-neg9.0%

      \[\leadsto \color{blue}{\cos M} \]
  11. Simplified9.0%

    \[\leadsto \color{blue}{\cos M} \]
  12. Taylor expanded in M around 0 9.0%

    \[\leadsto \color{blue}{1} \]
  13. Add Preprocessing

Reproduce

?
herbie shell --seed 2024170 
(FPCore (K m n M l)
  :name "Maksimov and Kolovsky, Equation (32)"
  :precision binary64
  (* (cos (- (/ (* K (+ m n)) 2.0) M)) (exp (- (- (pow (- (/ (+ m n) 2.0) M) 2.0)) (- l (fabs (- m n)))))))