Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.9% → 96.3%
Time: 31.9s
Alternatives: 10
Speedup: 1.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 10 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: 75.9% 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.3% accurate, 0.8× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := 0.5 \cdot \left(n + m\right)\\ \log \left({\left(e^{e^{\left(n - m\right) + \left(\left(t\_0 - M\right) \cdot \left(M - t\_0\right) - \ell\right)}}\right)}^{\cos \left(0.5 \cdot \left(m \cdot K\right) - M\right)}\right) \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* 0.5 (+ n m))))
   (log
    (pow
     (exp (exp (+ (- n m) (- (* (- t_0 M) (- M t_0)) l))))
     (cos (- (* 0.5 (* m K)) M))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = 0.5 * (n + m);
	return log(pow(exp(exp(((n - m) + (((t_0 - M) * (M - t_0)) - l)))), cos(((0.5 * (m * K)) - M))));
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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) :: t_0
    t_0 = 0.5d0 * (n + m)
    code = log((exp(exp(((n - m) + (((t_0 - m_1) * (m_1 - t_0)) - l)))) ** cos(((0.5d0 * (m * k)) - m_1))))
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = 0.5 * (n + m);
	return Math.log(Math.pow(Math.exp(Math.exp(((n - m) + (((t_0 - M) * (M - t_0)) - l)))), Math.cos(((0.5 * (m * K)) - M))));
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = 0.5 * (n + m)
	return math.log(math.pow(math.exp(math.exp(((n - m) + (((t_0 - M) * (M - t_0)) - l)))), math.cos(((0.5 * (m * K)) - M))))
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(0.5 * Float64(n + m))
	return log((exp(exp(Float64(Float64(n - m) + Float64(Float64(Float64(t_0 - M) * Float64(M - t_0)) - l)))) ^ cos(Float64(Float64(0.5 * Float64(m * K)) - M))))
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp = code(K, m, n, M, l)
	t_0 = 0.5 * (n + m);
	tmp = log((exp(exp(((n - m) + (((t_0 - M) * (M - t_0)) - l)))) ^ cos(((0.5 * (m * K)) - M))));
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(0.5 * N[(n + m), $MachinePrecision]), $MachinePrecision]}, N[Log[N[Power[N[Exp[N[Exp[N[(N[(n - m), $MachinePrecision] + N[(N[(N[(t$95$0 - M), $MachinePrecision] * N[(M - t$95$0), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision], N[Cos[N[(N[(0.5 * N[(m * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(n + m\right)\\
\log \left({\left(e^{e^{\left(n - m\right) + \left(\left(t\_0 - M\right) \cdot \left(M - t\_0\right) - \ell\right)}}\right)}^{\cos \left(0.5 \cdot \left(m \cdot K\right) - M\right)}\right)
\end{array}
\end{array}
Derivation
  1. Initial program 76.3%

    \[\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. Simplified76.3%

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

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

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

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

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

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

      \[\leadsto \log \left({\left(e^{e^{\left(n - m\right) - \left(\left(\color{blue}{\left(n + m\right)} \cdot 0.5 - M\right) \cdot \left(\left(m + n\right) \cdot 0.5 - M\right) + \ell\right)}}\right)}^{\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right)}\right) \]
    3. +-commutative96.2%

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

    \[\leadsto \log \left({\left(e^{e^{\left(n - m\right) - \left(\color{blue}{\left(\left(n + m\right) \cdot 0.5 - M\right) \cdot \left(\left(n + m\right) \cdot 0.5 - M\right)} + \ell\right)}}\right)}^{\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right)}\right) \]
  8. Taylor expanded in n around 0 96.2%

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

      \[\leadsto \log \left({\left(e^{e^{\left(n - m\right) - \left(\left(\left(n + m\right) \cdot 0.5 - M\right) \cdot \left(\left(n + m\right) \cdot 0.5 - M\right) + \ell\right)}}\right)}^{\cos \left(0.5 \cdot \color{blue}{\left(m \cdot K\right)} - M\right)}\right) \]
  10. Simplified96.2%

    \[\leadsto \log \left({\left(e^{e^{\left(n - m\right) - \left(\left(\left(n + m\right) \cdot 0.5 - M\right) \cdot \left(\left(n + m\right) \cdot 0.5 - M\right) + \ell\right)}}\right)}^{\color{blue}{\cos \left(0.5 \cdot \left(m \cdot K\right) - M\right)}}\right) \]
  11. Final simplification96.2%

    \[\leadsto \log \left({\left(e^{e^{\left(n - m\right) + \left(\left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(M - 0.5 \cdot \left(n + m\right)\right) - \ell\right)}}\right)}^{\cos \left(0.5 \cdot \left(m \cdot K\right) - M\right)}\right) \]
  12. Add Preprocessing

Alternative 2: 96.6% accurate, 1.0× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{n + m}{2} - M\right)}^{2}} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (* (cos M) (exp (- (- (fabs (- n m)) l) (pow (- (/ (+ n m) 2.0) M) 2.0)))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	return cos(M) * exp(((fabs((n - m)) - l) - pow((((n + m) / 2.0) - M), 2.0)));
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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((n - m)) - l) - ((((n + m) / 2.0d0) - m_1) ** 2.0d0)))
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos(M) * Math.exp(((Math.abs((n - m)) - l) - Math.pow((((n + m) / 2.0) - M), 2.0)));
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	return math.cos(M) * math.exp(((math.fabs((n - m)) - l) - math.pow((((n + m) / 2.0) - M), 2.0)))
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	return Float64(cos(M) * exp(Float64(Float64(abs(Float64(n - m)) - l) - (Float64(Float64(Float64(n + m) / 2.0) - M) ^ 2.0))))
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp = code(K, m, n, M, l)
	tmp = cos(M) * exp(((abs((n - m)) - l) - ((((n + m) / 2.0) - M) ^ 2.0)));
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] - N[Power[N[(N[(N[(n + m), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{n + m}{2} - M\right)}^{2}}
\end{array}
Derivation
  1. Initial program 76.3%

    \[\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. Simplified76.3%

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

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

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

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

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

Alternative 3: 87.6% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;m \leq -5 \cdot 10^{+42}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{elif}\;m \leq 2.8 \cdot 10^{-274}:\\ \;\;\;\;\frac{\cos \left(0.5 \cdot \left(m \cdot K\right) - M\right)}{e^{\left(m + \ell\right) + {\left(m \cdot 0.5 - M\right)}^{2}}}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(m \cdot \left(0.5 \cdot K\right) - M\right) \cdot e^{n - \left(m + {\left(0.5 \cdot \left(n + m\right) - M\right)}^{2}\right)}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (if (<= m -5e+42)
   (* (cos M) (exp (* -0.25 (pow m 2.0))))
   (if (<= m 2.8e-274)
     (/
      (cos (- (* 0.5 (* m K)) M))
      (exp (+ (+ m l) (pow (- (* m 0.5) M) 2.0))))
     (*
      (cos (- (* m (* 0.5 K)) M))
      (exp (- n (+ m (pow (- (* 0.5 (+ n m)) M) 2.0))))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -5e+42) {
		tmp = cos(M) * exp((-0.25 * pow(m, 2.0)));
	} else if (m <= 2.8e-274) {
		tmp = cos(((0.5 * (m * K)) - M)) / exp(((m + l) + pow(((m * 0.5) - M), 2.0)));
	} else {
		tmp = cos(((m * (0.5 * K)) - M)) * exp((n - (m + pow(((0.5 * (n + m)) - M), 2.0))));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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 <= (-5d+42)) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m ** 2.0d0)))
    else if (m <= 2.8d-274) then
        tmp = cos(((0.5d0 * (m * k)) - m_1)) / exp(((m + l) + (((m * 0.5d0) - m_1) ** 2.0d0)))
    else
        tmp = cos(((m * (0.5d0 * k)) - m_1)) * exp((n - (m + (((0.5d0 * (n + m)) - m_1) ** 2.0d0))))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -5e+42) {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(m, 2.0)));
	} else if (m <= 2.8e-274) {
		tmp = Math.cos(((0.5 * (m * K)) - M)) / Math.exp(((m + l) + Math.pow(((m * 0.5) - M), 2.0)));
	} else {
		tmp = Math.cos(((m * (0.5 * K)) - M)) * Math.exp((n - (m + Math.pow(((0.5 * (n + m)) - M), 2.0))));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if m <= -5e+42:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(m, 2.0)))
	elif m <= 2.8e-274:
		tmp = math.cos(((0.5 * (m * K)) - M)) / math.exp(((m + l) + math.pow(((m * 0.5) - M), 2.0)))
	else:
		tmp = math.cos(((m * (0.5 * K)) - M)) * math.exp((n - (m + math.pow(((0.5 * (n + m)) - M), 2.0))))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	tmp = 0.0
	if (m <= -5e+42)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (m ^ 2.0))));
	elseif (m <= 2.8e-274)
		tmp = Float64(cos(Float64(Float64(0.5 * Float64(m * K)) - M)) / exp(Float64(Float64(m + l) + (Float64(Float64(m * 0.5) - M) ^ 2.0))));
	else
		tmp = Float64(cos(Float64(Float64(m * Float64(0.5 * K)) - M)) * exp(Float64(n - Float64(m + (Float64(Float64(0.5 * Float64(n + m)) - M) ^ 2.0)))));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (m <= -5e+42)
		tmp = cos(M) * exp((-0.25 * (m ^ 2.0)));
	elseif (m <= 2.8e-274)
		tmp = cos(((0.5 * (m * K)) - M)) / exp(((m + l) + (((m * 0.5) - M) ^ 2.0)));
	else
		tmp = cos(((m * (0.5 * K)) - M)) * exp((n - (m + (((0.5 * (n + m)) - M) ^ 2.0))));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := If[LessEqual[m, -5e+42], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[m, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, 2.8e-274], N[(N[Cos[N[(N[(0.5 * N[(m * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] / N[Exp[N[(N[(m + l), $MachinePrecision] + N[Power[N[(N[(m * 0.5), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[N[(N[(m * N[(0.5 * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(n - N[(m + N[Power[N[(N[(0.5 * N[(n + m), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;m \leq -5 \cdot 10^{+42}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\

\mathbf{elif}\;m \leq 2.8 \cdot 10^{-274}:\\
\;\;\;\;\frac{\cos \left(0.5 \cdot \left(m \cdot K\right) - M\right)}{e^{\left(m + \ell\right) + {\left(m \cdot 0.5 - M\right)}^{2}}}\\

\mathbf{else}:\\
\;\;\;\;\cos \left(m \cdot \left(0.5 \cdot K\right) - M\right) \cdot e^{n - \left(m + {\left(0.5 \cdot \left(n + m\right) - M\right)}^{2}\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if m < -5.00000000000000007e42

    1. Initial program 71.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. Simplified71.2%

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

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

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

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

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

    if -5.00000000000000007e42 < m < 2.79999999999999975e-274

    1. Initial program 83.6%

      \[\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. Simplified83.6%

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

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

        \[\leadsto \cos \color{blue}{\left(e^{\mathsf{log1p}\left(\left(m + n\right) \cdot \frac{K}{2} - M\right)} - 1\right)} \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      3. div-inv51.0%

        \[\leadsto \cos \left(e^{\mathsf{log1p}\left(\left(m + n\right) \cdot \color{blue}{\left(K \cdot \frac{1}{2}\right)} - M\right)} - 1\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      4. metadata-eval51.0%

        \[\leadsto \cos \left(e^{\mathsf{log1p}\left(\left(m + n\right) \cdot \left(K \cdot \color{blue}{0.5}\right) - M\right)} - 1\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Applied egg-rr51.0%

      \[\leadsto \cos \color{blue}{\left(e^{\mathsf{log1p}\left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right)} - 1\right)} \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Step-by-step derivation
      1. expm1-define51.1%

        \[\leadsto \cos \color{blue}{\left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right)\right)\right)} \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      2. fma-neg51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\mathsf{fma}\left(m + n, K \cdot 0.5, -M\right)}\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      3. *-commutative51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(m + n, \color{blue}{0.5 \cdot K}, -M\right)\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      4. fma-define51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\left(m + n\right) \cdot \left(0.5 \cdot K\right) + \left(-M\right)}\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      5. *-commutative51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\left(0.5 \cdot K\right) \cdot \left(m + n\right)} + \left(-M\right)\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      6. fma-define51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\mathsf{fma}\left(0.5 \cdot K, m + n, -M\right)}\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      7. +-commutative51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(0.5 \cdot K, \color{blue}{n + m}, -M\right)\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      8. remove-double-neg51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(0.5 \cdot K, n + \color{blue}{\left(-\left(-m\right)\right)}, -M\right)\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      9. mul-1-neg51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(0.5 \cdot K, n + \left(-\color{blue}{-1 \cdot m}\right), -M\right)\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      10. sub-neg51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(0.5 \cdot K, \color{blue}{n - -1 \cdot m}, -M\right)\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      11. fma-neg51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\left(0.5 \cdot K\right) \cdot \left(n - -1 \cdot m\right) - M}\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      12. cancel-sign-sub-inv51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(0.5 \cdot K\right) \cdot \color{blue}{\left(n + \left(--1\right) \cdot m\right)} - M\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      13. metadata-eval51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(0.5 \cdot K\right) \cdot \left(n + \color{blue}{1} \cdot m\right) - M\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      14. *-lft-identity51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(0.5 \cdot K\right) \cdot \left(n + \color{blue}{m}\right) - M\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      15. +-commutative51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(0.5 \cdot K\right) \cdot \color{blue}{\left(m + n\right)} - M\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      16. *-commutative51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\left(m + n\right) \cdot \left(0.5 \cdot K\right)} - M\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
      17. +-commutative51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\left(n + m\right)} \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Simplified51.1%

      \[\leadsto \cos \color{blue}{\left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right)} \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    8. Step-by-step derivation
      1. associate--l-51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot e^{\color{blue}{\left|n - m\right| - \left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      2. div-inv51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot e^{\left|n - m\right| - \left(\ell + {\left(\color{blue}{\left(m + n\right) \cdot \frac{1}{2}} - M\right)}^{2}\right)} \]
      3. metadata-eval51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot e^{\left|n - m\right| - \left(\ell + {\left(\left(m + n\right) \cdot \color{blue}{0.5} - M\right)}^{2}\right)} \]
      4. +-commutative51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot e^{\left|n - m\right| - \color{blue}{\left({\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2} + \ell\right)}} \]
      5. exp-diff30.8%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot \color{blue}{\frac{e^{\left|n - m\right|}}{e^{{\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2} + \ell}}} \]
      6. add-sqr-sqrt20.4%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot \frac{e^{\left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right|}}{e^{{\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2} + \ell}} \]
      7. fabs-sqr20.4%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot \frac{e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}}}{e^{{\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2} + \ell}} \]
      8. add-sqr-sqrt41.0%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot \frac{e^{\color{blue}{n - m}}}{e^{{\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2} + \ell}} \]
      9. exp-diff51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot \color{blue}{e^{\left(n - m\right) - \left({\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2} + \ell\right)}} \]
      10. associate--l-51.1%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot e^{\color{blue}{n - \left(m + \left({\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2} + \ell\right)\right)}} \]
      11. exp-diff44.3%

        \[\leadsto \cos \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\right)\right) \cdot \color{blue}{\frac{e^{n}}{e^{m + \left({\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2} + \ell\right)}}} \]
    9. Applied egg-rr44.3%

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

      \[\leadsto \color{blue}{\frac{\cos \left(0.5 \cdot \left(K \cdot m\right) - M\right)}{e^{\ell + \left(m + {\left(0.5 \cdot m - M\right)}^{2}\right)}}} \]
    11. Step-by-step derivation
      1. associate-+r+81.8%

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

        \[\leadsto \frac{\cos \left(0.5 \cdot \left(K \cdot m\right) - M\right)}{e^{\color{blue}{\left(m + \ell\right)} + {\left(0.5 \cdot m - M\right)}^{2}}} \]
    12. Simplified81.8%

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

    if 2.79999999999999975e-274 < m

    1. Initial program 73.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. Simplified73.1%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right) - M\right) \cdot e^{n - \left(m + {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
    7. Taylor expanded in m around inf 74.2%

      \[\leadsto \cos \left(\color{blue}{0.5 \cdot \left(K \cdot m\right)} - M\right) \cdot e^{n - \left(m + {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    8. Step-by-step derivation
      1. associate-*r*74.2%

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

      \[\leadsto \cos \left(\color{blue}{\left(0.5 \cdot K\right) \cdot m} - M\right) \cdot e^{n - \left(m + {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification81.7%

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

Alternative 4: 96.6% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \cos M \cdot {e}^{\left(\left(\left(n - m\right) - \ell\right) - {\left(0.5 \cdot \left(n + m\right) - M\right)}^{2}\right)} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (* (cos M) (pow E (- (- (- n m) l) (pow (- (* 0.5 (+ n m)) M) 2.0)))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	return cos(M) * pow(((double) M_E), (((n - m) - l) - pow(((0.5 * (n + m)) - M), 2.0)));
}
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos(M) * Math.pow(Math.E, (((n - m) - l) - Math.pow(((0.5 * (n + m)) - M), 2.0)));
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	return math.cos(M) * math.pow(math.e, (((n - m) - l) - math.pow(((0.5 * (n + m)) - M), 2.0)))
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	return Float64(cos(M) * (exp(1) ^ Float64(Float64(Float64(n - m) - l) - (Float64(Float64(0.5 * Float64(n + m)) - M) ^ 2.0))))
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp = code(K, m, n, M, l)
	tmp = cos(M) * (2.71828182845904523536 ^ (((n - m) - l) - (((0.5 * (n + m)) - M) ^ 2.0)));
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := N[(N[Cos[M], $MachinePrecision] * N[Power[E, N[(N[(N[(n - m), $MachinePrecision] - l), $MachinePrecision] - N[Power[N[(N[(0.5 * N[(n + m), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\cos M \cdot {e}^{\left(\left(\left(n - m\right) - \ell\right) - {\left(0.5 \cdot \left(n + m\right) - M\right)}^{2}\right)}
\end{array}
Derivation
  1. Initial program 76.3%

    \[\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. Simplified76.3%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot {\left(e^{1}\right)}^{\left(\left(\left(n - m\right) - \ell\right) - {\left(\left(m + n\right) \cdot \color{blue}{0.5} - M\right)}^{2}\right)} \]
    8. +-commutative96.7%

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

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

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

Alternative 5: 88.9% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := 0.5 \cdot \left(n + m\right)\\ \mathbf{if}\;m \leq -5 \cdot 10^{+41} \lor \neg \left(m \leq 54\right):\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n - m\right) + \left(\left(t\_0 - M\right) \cdot \left(M - t\_0\right) - \ell\right)} \cdot \cos \left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* 0.5 (+ n m))))
   (if (or (<= m -5e+41) (not (<= m 54.0)))
     (* (cos M) (exp (* -0.25 (pow m 2.0))))
     (*
      (exp (+ (- n m) (- (* (- t_0 M) (- M t_0)) l)))
      (cos (- (* (+ n m) (* 0.5 K)) M))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = 0.5 * (n + m);
	double tmp;
	if ((m <= -5e+41) || !(m <= 54.0)) {
		tmp = cos(M) * exp((-0.25 * pow(m, 2.0)));
	} else {
		tmp = exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * cos((((n + m) * (0.5 * K)) - M));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * (n + m)
    if ((m <= (-5d+41)) .or. (.not. (m <= 54.0d0))) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m ** 2.0d0)))
    else
        tmp = exp(((n - m) + (((t_0 - m_1) * (m_1 - t_0)) - l))) * cos((((n + m) * (0.5d0 * k)) - m_1))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = 0.5 * (n + m);
	double tmp;
	if ((m <= -5e+41) || !(m <= 54.0)) {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(m, 2.0)));
	} else {
		tmp = Math.exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * Math.cos((((n + m) * (0.5 * K)) - M));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = 0.5 * (n + m)
	tmp = 0
	if (m <= -5e+41) or not (m <= 54.0):
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(m, 2.0)))
	else:
		tmp = math.exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * math.cos((((n + m) * (0.5 * K)) - M))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(0.5 * Float64(n + m))
	tmp = 0.0
	if ((m <= -5e+41) || !(m <= 54.0))
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (m ^ 2.0))));
	else
		tmp = Float64(exp(Float64(Float64(n - m) + Float64(Float64(Float64(t_0 - M) * Float64(M - t_0)) - l))) * cos(Float64(Float64(Float64(n + m) * Float64(0.5 * K)) - M)));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	t_0 = 0.5 * (n + m);
	tmp = 0.0;
	if ((m <= -5e+41) || ~((m <= 54.0)))
		tmp = cos(M) * exp((-0.25 * (m ^ 2.0)));
	else
		tmp = exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * cos((((n + m) * (0.5 * K)) - M));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(0.5 * N[(n + m), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[m, -5e+41], N[Not[LessEqual[m, 54.0]], $MachinePrecision]], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[m, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Exp[N[(N[(n - m), $MachinePrecision] + N[(N[(N[(t$95$0 - M), $MachinePrecision] * N[(M - t$95$0), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(N[(N[(n + m), $MachinePrecision] * N[(0.5 * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(n + m\right)\\
\mathbf{if}\;m \leq -5 \cdot 10^{+41} \lor \neg \left(m \leq 54\right):\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\

\mathbf{else}:\\
\;\;\;\;e^{\left(n - m\right) + \left(\left(t\_0 - M\right) \cdot \left(M - t\_0\right) - \ell\right)} \cdot \cos \left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if m < -5.00000000000000022e41 or 54 < m

    1. Initial program 69.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. Simplified69.2%

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

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

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

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

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

    if -5.00000000000000022e41 < m < 54

    1. Initial program 82.3%

      \[\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. Simplified82.3%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \log \left({\left(e^{e^{\left(n - m\right) - \left(\color{blue}{\left(\left(m + n\right) \cdot 0.5 - M\right) \cdot \left(\left(m + n\right) \cdot 0.5 - M\right)} + \ell\right)}}\right)}^{\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right)}\right) \]
      2. +-commutative96.0%

        \[\leadsto \log \left({\left(e^{e^{\left(n - m\right) - \left(\left(\color{blue}{\left(n + m\right)} \cdot 0.5 - M\right) \cdot \left(\left(m + n\right) \cdot 0.5 - M\right) + \ell\right)}}\right)}^{\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right)}\right) \]
      3. +-commutative96.0%

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

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

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

Alternative 6: 89.7% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := 0.5 \cdot \left(n + m\right)\\ \mathbf{if}\;n \leq -8 \cdot 10^{-94}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{elif}\;n \leq 1200:\\ \;\;\;\;e^{\left(n - m\right) + \left(\left(t\_0 - M\right) \cdot \left(M - t\_0\right) - \ell\right)} \cdot \cos \left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* 0.5 (+ n m))))
   (if (<= n -8e-94)
     (* (cos M) (exp (* -0.25 (pow m 2.0))))
     (if (<= n 1200.0)
       (*
        (exp (+ (- n m) (- (* (- t_0 M) (- M t_0)) l)))
        (cos (- (* (+ n m) (* 0.5 K)) M)))
       (* (cos M) (exp (* -0.25 (pow n 2.0))))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = 0.5 * (n + m);
	double tmp;
	if (n <= -8e-94) {
		tmp = cos(M) * exp((-0.25 * pow(m, 2.0)));
	} else if (n <= 1200.0) {
		tmp = exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * cos((((n + m) * (0.5 * K)) - M));
	} else {
		tmp = cos(M) * exp((-0.25 * pow(n, 2.0)));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * (n + m)
    if (n <= (-8d-94)) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m ** 2.0d0)))
    else if (n <= 1200.0d0) then
        tmp = exp(((n - m) + (((t_0 - m_1) * (m_1 - t_0)) - l))) * cos((((n + m) * (0.5d0 * k)) - m_1))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n ** 2.0d0)))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = 0.5 * (n + m);
	double tmp;
	if (n <= -8e-94) {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(m, 2.0)));
	} else if (n <= 1200.0) {
		tmp = Math.exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * Math.cos((((n + m) * (0.5 * K)) - M));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(n, 2.0)));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = 0.5 * (n + m)
	tmp = 0
	if n <= -8e-94:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(m, 2.0)))
	elif n <= 1200.0:
		tmp = math.exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * math.cos((((n + m) * (0.5 * K)) - M))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(n, 2.0)))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(0.5 * Float64(n + m))
	tmp = 0.0
	if (n <= -8e-94)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (m ^ 2.0))));
	elseif (n <= 1200.0)
		tmp = Float64(exp(Float64(Float64(n - m) + Float64(Float64(Float64(t_0 - M) * Float64(M - t_0)) - l))) * cos(Float64(Float64(Float64(n + m) * Float64(0.5 * K)) - M)));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (n ^ 2.0))));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	t_0 = 0.5 * (n + m);
	tmp = 0.0;
	if (n <= -8e-94)
		tmp = cos(M) * exp((-0.25 * (m ^ 2.0)));
	elseif (n <= 1200.0)
		tmp = exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * cos((((n + m) * (0.5 * K)) - M));
	else
		tmp = cos(M) * exp((-0.25 * (n ^ 2.0)));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(0.5 * N[(n + m), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[n, -8e-94], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[m, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 1200.0], N[(N[Exp[N[(N[(n - m), $MachinePrecision] + N[(N[(N[(t$95$0 - M), $MachinePrecision] * N[(M - t$95$0), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(N[(N[(n + m), $MachinePrecision] * N[(0.5 * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[n, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(n + m\right)\\
\mathbf{if}\;n \leq -8 \cdot 10^{-94}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\

\mathbf{elif}\;n \leq 1200:\\
\;\;\;\;e^{\left(n - m\right) + \left(\left(t\_0 - M\right) \cdot \left(M - t\_0\right) - \ell\right)} \cdot \cos \left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if n < -7.9999999999999996e-94

    1. Initial program 76.6%

      \[\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. Simplified76.6%

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

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

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

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

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

    if -7.9999999999999996e-94 < n < 1200

    1. Initial program 83.3%

      \[\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. Simplified83.3%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \log \left({\left(e^{e^{\left(n - m\right) - \left(\left(\color{blue}{\left(n + m\right)} \cdot 0.5 - M\right) \cdot \left(\left(m + n\right) \cdot 0.5 - M\right) + \ell\right)}}\right)}^{\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right)}\right) \]
      3. +-commutative92.7%

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

      \[\leadsto e^{\left(n - m\right) - \left(\color{blue}{\left(\left(n + m\right) \cdot 0.5 - M\right) \cdot \left(\left(n + m\right) \cdot 0.5 - M\right)} + \ell\right)} \cdot \cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right) \]

    if 1200 < n

    1. Initial program 65.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. Simplified65.7%

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification77.7%

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

Alternative 7: 84.3% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := 0.5 \cdot \left(n + m\right)\\ \mathbf{if}\;M \leq -8.4 \cdot 10^{+18} \lor \neg \left(M \leq 1.4 \cdot 10^{+41}\right):\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n - m\right) + \left(\left(t\_0 - M\right) \cdot \left(M - t\_0\right) - \ell\right)} \cdot \cos \left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* 0.5 (+ n m))))
   (if (or (<= M -8.4e+18) (not (<= M 1.4e+41)))
     (* (cos M) (exp (- (pow M 2.0))))
     (*
      (exp (+ (- n m) (- (* (- t_0 M) (- M t_0)) l)))
      (cos (- (* (+ n m) (* 0.5 K)) M))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = 0.5 * (n + m);
	double tmp;
	if ((M <= -8.4e+18) || !(M <= 1.4e+41)) {
		tmp = cos(M) * exp(-pow(M, 2.0));
	} else {
		tmp = exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * cos((((n + m) * (0.5 * K)) - M));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * (n + m)
    if ((m_1 <= (-8.4d+18)) .or. (.not. (m_1 <= 1.4d+41))) then
        tmp = cos(m_1) * exp(-(m_1 ** 2.0d0))
    else
        tmp = exp(((n - m) + (((t_0 - m_1) * (m_1 - t_0)) - l))) * cos((((n + m) * (0.5d0 * k)) - m_1))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = 0.5 * (n + m);
	double tmp;
	if ((M <= -8.4e+18) || !(M <= 1.4e+41)) {
		tmp = Math.cos(M) * Math.exp(-Math.pow(M, 2.0));
	} else {
		tmp = Math.exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * Math.cos((((n + m) * (0.5 * K)) - M));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = 0.5 * (n + m)
	tmp = 0
	if (M <= -8.4e+18) or not (M <= 1.4e+41):
		tmp = math.cos(M) * math.exp(-math.pow(M, 2.0))
	else:
		tmp = math.exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * math.cos((((n + m) * (0.5 * K)) - M))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(0.5 * Float64(n + m))
	tmp = 0.0
	if ((M <= -8.4e+18) || !(M <= 1.4e+41))
		tmp = Float64(cos(M) * exp(Float64(-(M ^ 2.0))));
	else
		tmp = Float64(exp(Float64(Float64(n - m) + Float64(Float64(Float64(t_0 - M) * Float64(M - t_0)) - l))) * cos(Float64(Float64(Float64(n + m) * Float64(0.5 * K)) - M)));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	t_0 = 0.5 * (n + m);
	tmp = 0.0;
	if ((M <= -8.4e+18) || ~((M <= 1.4e+41)))
		tmp = cos(M) * exp(-(M ^ 2.0));
	else
		tmp = exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * cos((((n + m) * (0.5 * K)) - M));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(0.5 * N[(n + m), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[M, -8.4e+18], N[Not[LessEqual[M, 1.4e+41]], $MachinePrecision]], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-N[Power[M, 2.0], $MachinePrecision])], $MachinePrecision]), $MachinePrecision], N[(N[Exp[N[(N[(n - m), $MachinePrecision] + N[(N[(N[(t$95$0 - M), $MachinePrecision] * N[(M - t$95$0), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(N[(N[(n + m), $MachinePrecision] * N[(0.5 * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(n + m\right)\\
\mathbf{if}\;M \leq -8.4 \cdot 10^{+18} \lor \neg \left(M \leq 1.4 \cdot 10^{+41}\right):\\
\;\;\;\;\cos M \cdot e^{-{M}^{2}}\\

\mathbf{else}:\\
\;\;\;\;e^{\left(n - m\right) + \left(\left(t\_0 - M\right) \cdot \left(M - t\_0\right) - \ell\right)} \cdot \cos \left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -8.4e18 or 1.4e41 < M

    1. Initial program 78.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. Simplified78.8%

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

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

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

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

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

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

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

    if -8.4e18 < M < 1.4e41

    1. Initial program 73.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. Simplified73.7%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \log \left({\left(e^{e^{\left(n - m\right) - \left(\left(\color{blue}{\left(n + m\right)} \cdot 0.5 - M\right) \cdot \left(\left(m + n\right) \cdot 0.5 - M\right) + \ell\right)}}\right)}^{\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right)}\right) \]
      3. +-commutative92.2%

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

      \[\leadsto e^{\left(n - m\right) - \left(\color{blue}{\left(\left(n + m\right) \cdot 0.5 - M\right) \cdot \left(\left(n + m\right) \cdot 0.5 - M\right)} + \ell\right)} \cdot \cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification86.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -8.4 \cdot 10^{+18} \lor \neg \left(M \leq 1.4 \cdot 10^{+41}\right):\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n - m\right) + \left(\left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(M - 0.5 \cdot \left(n + m\right)\right) - \ell\right)} \cdot \cos \left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 80.8% accurate, 1.8× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := 0.5 \cdot \left(n + m\right)\\ \mathbf{if}\;\ell \leq 740:\\ \;\;\;\;e^{\left(n - m\right) + \left(\left(t\_0 - M\right) \cdot \left(M - t\_0\right) - \ell\right)} \cdot \cos \left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* 0.5 (+ n m))))
   (if (<= l 740.0)
     (*
      (exp (+ (- n m) (- (* (- t_0 M) (- M t_0)) l)))
      (cos (- (* (+ n m) (* 0.5 K)) M)))
     (exp (- l)))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = 0.5 * (n + m);
	double tmp;
	if (l <= 740.0) {
		tmp = exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * cos((((n + m) * (0.5 * K)) - M));
	} else {
		tmp = exp(-l);
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * (n + m)
    if (l <= 740.0d0) then
        tmp = exp(((n - m) + (((t_0 - m_1) * (m_1 - t_0)) - l))) * cos((((n + m) * (0.5d0 * k)) - m_1))
    else
        tmp = exp(-l)
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = 0.5 * (n + m);
	double tmp;
	if (l <= 740.0) {
		tmp = Math.exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * Math.cos((((n + m) * (0.5 * K)) - M));
	} else {
		tmp = Math.exp(-l);
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = 0.5 * (n + m)
	tmp = 0
	if l <= 740.0:
		tmp = math.exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * math.cos((((n + m) * (0.5 * K)) - M))
	else:
		tmp = math.exp(-l)
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(0.5 * Float64(n + m))
	tmp = 0.0
	if (l <= 740.0)
		tmp = Float64(exp(Float64(Float64(n - m) + Float64(Float64(Float64(t_0 - M) * Float64(M - t_0)) - l))) * cos(Float64(Float64(Float64(n + m) * Float64(0.5 * K)) - M)));
	else
		tmp = exp(Float64(-l));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	t_0 = 0.5 * (n + m);
	tmp = 0.0;
	if (l <= 740.0)
		tmp = exp(((n - m) + (((t_0 - M) * (M - t_0)) - l))) * cos((((n + m) * (0.5 * K)) - M));
	else
		tmp = exp(-l);
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(0.5 * N[(n + m), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[l, 740.0], N[(N[Exp[N[(N[(n - m), $MachinePrecision] + N[(N[(N[(t$95$0 - M), $MachinePrecision] * N[(M - t$95$0), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(N[(N[(n + m), $MachinePrecision] * N[(0.5 * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Exp[(-l)], $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(n + m\right)\\
\mathbf{if}\;\ell \leq 740:\\
\;\;\;\;e^{\left(n - m\right) + \left(\left(t\_0 - M\right) \cdot \left(M - t\_0\right) - \ell\right)} \cdot \cos \left(\left(n + m\right) \cdot \left(0.5 \cdot K\right) - M\right)\\

\mathbf{else}:\\
\;\;\;\;e^{-\ell}\\


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

    1. Initial program 76.5%

      \[\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. Simplified76.5%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \log \left({\left(e^{e^{\left(n - m\right) - \left(\color{blue}{\left(\left(m + n\right) \cdot 0.5 - M\right) \cdot \left(\left(m + n\right) \cdot 0.5 - M\right)} + \ell\right)}}\right)}^{\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right)}\right) \]
      2. +-commutative95.0%

        \[\leadsto \log \left({\left(e^{e^{\left(n - m\right) - \left(\left(\color{blue}{\left(n + m\right)} \cdot 0.5 - M\right) \cdot \left(\left(m + n\right) \cdot 0.5 - M\right) + \ell\right)}}\right)}^{\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right)}\right) \]
      3. +-commutative95.0%

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

      \[\leadsto e^{\left(n - m\right) - \left(\color{blue}{\left(\left(n + m\right) \cdot 0.5 - M\right) \cdot \left(\left(n + m\right) \cdot 0.5 - M\right)} + \ell\right)} \cdot \cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M\right) \]

    if 740 < l

    1. Initial program 75.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. Simplified75.8%

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    8. Step-by-step derivation
      1. neg-mul-1100.0%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    9. Simplified100.0%

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    10. Taylor expanded in M around 0 100.0%

      \[\leadsto \color{blue}{e^{-\ell}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification82.2%

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

Alternative 9: 34.6% accurate, 4.2× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ e^{-\ell} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l) :precision binary64 (exp (- l)))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	return exp(-l);
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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(-l)
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	return Math.exp(-l);
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	return math.exp(-l)
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	return exp(Float64(-l))
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp = code(K, m, n, M, l)
	tmp = exp(-l);
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := N[Exp[(-l)], $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
e^{-\ell}
\end{array}
Derivation
  1. Initial program 76.3%

    \[\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. Simplified76.3%

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

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

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

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

    \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
  8. Step-by-step derivation
    1. neg-mul-134.9%

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
  9. Simplified34.9%

    \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
  10. Taylor expanded in M around 0 35.3%

    \[\leadsto \color{blue}{e^{-\ell}} \]
  11. Add Preprocessing

Alternative 10: 7.1% accurate, 4.2× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \cos M \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l) :precision binary64 (cos M))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	return cos(M);
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos(M);
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	return math.cos(M)
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	return cos(M)
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp = code(K, m, n, M, l)
	tmp = cos(M);
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := N[Cos[M], $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\cos M
\end{array}
Derivation
  1. Initial program 76.3%

    \[\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. Simplified76.3%

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

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

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

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

    \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
  8. Step-by-step derivation
    1. neg-mul-134.9%

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
  9. Simplified34.9%

    \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
  10. Taylor expanded in l around 0 6.4%

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

Reproduce

?
herbie shell --seed 2024095 
(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)))))))