Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.3% → 95.2%
Time: 27.8s
Alternatives: 8
Speedup: 1.4×

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 8 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.3% 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: 95.2% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := M - n \cdot 0.5\\ t_1 := \left|m - n\right|\\ \mathbf{if}\;m \leq -6.2:\\ \;\;\;\;e^{t_1 - 0.25 \cdot {\left(m + n\right)}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{\left(m - t_0\right) \cdot t_0 + \left(t_1 - \ell\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 (- M (* n 0.5))) (t_1 (fabs (- m n))))
   (if (<= m -6.2)
     (exp (- t_1 (* 0.25 (pow (+ m n) 2.0))))
     (* (cos M) (exp (+ (* (- m t_0) t_0) (- t_1 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 = M - (n * 0.5);
	double t_1 = fabs((m - n));
	double tmp;
	if (m <= -6.2) {
		tmp = exp((t_1 - (0.25 * pow((m + n), 2.0))));
	} else {
		tmp = cos(M) * exp((((m - t_0) * t_0) + (t_1 - 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) :: t_1
    real(8) :: tmp
    t_0 = m_1 - (n * 0.5d0)
    t_1 = abs((m - n))
    if (m <= (-6.2d0)) then
        tmp = exp((t_1 - (0.25d0 * ((m + n) ** 2.0d0))))
    else
        tmp = cos(m_1) * exp((((m - t_0) * t_0) + (t_1 - 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 = M - (n * 0.5);
	double t_1 = Math.abs((m - n));
	double tmp;
	if (m <= -6.2) {
		tmp = Math.exp((t_1 - (0.25 * Math.pow((m + n), 2.0))));
	} else {
		tmp = Math.cos(M) * Math.exp((((m - t_0) * t_0) + (t_1 - l)));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = M - (n * 0.5)
	t_1 = math.fabs((m - n))
	tmp = 0
	if m <= -6.2:
		tmp = math.exp((t_1 - (0.25 * math.pow((m + n), 2.0))))
	else:
		tmp = math.cos(M) * math.exp((((m - t_0) * t_0) + (t_1 - l)))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(M - Float64(n * 0.5))
	t_1 = abs(Float64(m - n))
	tmp = 0.0
	if (m <= -6.2)
		tmp = exp(Float64(t_1 - Float64(0.25 * (Float64(m + n) ^ 2.0))));
	else
		tmp = Float64(cos(M) * exp(Float64(Float64(Float64(m - t_0) * t_0) + Float64(t_1 - 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 = M - (n * 0.5);
	t_1 = abs((m - n));
	tmp = 0.0;
	if (m <= -6.2)
		tmp = exp((t_1 - (0.25 * ((m + n) ^ 2.0))));
	else
		tmp = cos(M) * exp((((m - t_0) * t_0) + (t_1 - 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[(M - N[(n * 0.5), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[m, -6.2], N[Exp[N[(t$95$1 - N[(0.25 * N[Power[N[(m + n), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[(m - t$95$0), $MachinePrecision] * t$95$0), $MachinePrecision] + N[(t$95$1 - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := M - n \cdot 0.5\\
t_1 := \left|m - n\right|\\
\mathbf{if}\;m \leq -6.2:\\
\;\;\;\;e^{t_1 - 0.25 \cdot {\left(m + n\right)}^{2}}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{\left(m - t_0\right) \cdot t_0 + \left(t_1 - \ell\right)}\\


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

    1. Initial program 72.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. Taylor expanded in l around 0 72.6%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left|n - m\right| - {\left(\color{blue}{\left(m + n\right) \cdot 0.5} - M\right)}^{2}} \]
    4. Simplified72.6%

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

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

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

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

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

    if -6.20000000000000018 < m

    1. Initial program 82.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. Taylor expanded in K around 0 97.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)} \]
    3. Step-by-step derivation
      1. cos-neg89.3%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left|n - m\right| - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}} \]
    4. Simplified97.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. Taylor expanded in m around 0 80.8%

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(0.5 \cdot n - M\right)} + m \cdot \left(0.5 \cdot n - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out84.9%

        \[\leadsto \cos M \cdot e^{\left(-\color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(\left(0.5 \cdot n - M\right) + m\right)}\right) - \left(\ell - \left|m - n\right|\right)} \]
      4. *-commutative84.9%

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{n \cdot 0.5} - M\right) \cdot \left(\left(0.5 \cdot n - M\right) + m\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      5. *-commutative84.9%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -6.2:\\ \;\;\;\;e^{\left|m - n\right| - 0.25 \cdot {\left(m + n\right)}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{\left(m - \left(M - n \cdot 0.5\right)\right) \cdot \left(M - n \cdot 0.5\right) + \left(\left|m - n\right| - \ell\right)}\\ \end{array} \]

Alternative 2: 96.3% 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|m - n\right| - \ell\right) - {\left(\frac{m + n}{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 (- m n)) l) (pow (- (/ (+ m n) 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((m - n)) - l) - pow((((m + n) / 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((m - n)) - l) - ((((m + n) / 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((m - n)) - l) - Math.pow((((m + n) / 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((m - n)) - l) - math.pow((((m + n) / 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(m - n)) - l) - (Float64(Float64(Float64(m + n) / 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((m - n)) - l) - ((((m + n) / 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[(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}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, 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 79.9%

    \[\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. Taylor expanded in K around 0 97.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)} \]
  3. Step-by-step derivation
    1. cos-neg91.1%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left|n - m\right| - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}} \]
  4. Simplified97.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. Final simplification97.0%

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

Alternative 3: 95.4% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := \left|m - n\right|\\ \mathbf{if}\;n \leq 290:\\ \;\;\;\;\cos M \cdot e^{\left(m \cdot 0.5 - M\right) \cdot \left(\left(M - m \cdot 0.5\right) - n\right) + \left(t_0 - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{t_0 - 0.25 \cdot {\left(m + n\right)}^{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 (fabs (- m n))))
   (if (<= n 290.0)
     (* (cos M) (exp (+ (* (- (* m 0.5) M) (- (- M (* m 0.5)) n)) (- t_0 l))))
     (exp (- t_0 (* 0.25 (pow (+ m 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 = fabs((m - n));
	double tmp;
	if (n <= 290.0) {
		tmp = cos(M) * exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + (t_0 - l)));
	} else {
		tmp = exp((t_0 - (0.25 * pow((m + 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 = abs((m - n))
    if (n <= 290.0d0) then
        tmp = cos(m_1) * exp(((((m * 0.5d0) - m_1) * ((m_1 - (m * 0.5d0)) - n)) + (t_0 - l)))
    else
        tmp = exp((t_0 - (0.25d0 * ((m + 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 = Math.abs((m - n));
	double tmp;
	if (n <= 290.0) {
		tmp = Math.cos(M) * Math.exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + (t_0 - l)));
	} else {
		tmp = Math.exp((t_0 - (0.25 * Math.pow((m + 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 = math.fabs((m - n))
	tmp = 0
	if n <= 290.0:
		tmp = math.cos(M) * math.exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + (t_0 - l)))
	else:
		tmp = math.exp((t_0 - (0.25 * math.pow((m + 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 = abs(Float64(m - n))
	tmp = 0.0
	if (n <= 290.0)
		tmp = Float64(cos(M) * exp(Float64(Float64(Float64(Float64(m * 0.5) - M) * Float64(Float64(M - Float64(m * 0.5)) - n)) + Float64(t_0 - l))));
	else
		tmp = exp(Float64(t_0 - Float64(0.25 * (Float64(m + 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 = abs((m - n));
	tmp = 0.0;
	if (n <= 290.0)
		tmp = cos(M) * exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + (t_0 - l)));
	else
		tmp = exp((t_0 - (0.25 * ((m + 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[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[n, 290.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[(N[(m * 0.5), $MachinePrecision] - M), $MachinePrecision] * N[(N[(M - N[(m * 0.5), $MachinePrecision]), $MachinePrecision] - n), $MachinePrecision]), $MachinePrecision] + N[(t$95$0 - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Exp[N[(t$95$0 - N[(0.25 * N[Power[N[(m + n), $MachinePrecision], 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 := \left|m - n\right|\\
\mathbf{if}\;n \leq 290:\\
\;\;\;\;\cos M \cdot e^{\left(m \cdot 0.5 - M\right) \cdot \left(\left(M - m \cdot 0.5\right) - n\right) + \left(t_0 - \ell\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if n < 290

    1. Initial program 82.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. Taylor expanded in K around 0 96.2%

      \[\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)} \]
    3. Step-by-step derivation
      1. cos-neg88.3%

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

      \[\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. Taylor expanded in n around 0 76.8%

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out80.2%

        \[\leadsto \cos M \cdot e^{\left(-\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(\left(0.5 \cdot m - M\right) + n\right)}\right) - \left(\ell - \left|m - n\right|\right)} \]
      4. *-commutative80.2%

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

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

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

    if 290 < n

    1. Initial program 74.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. Taylor expanded in l around 0 73.4%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 290:\\ \;\;\;\;\cos M \cdot e^{\left(m \cdot 0.5 - M\right) \cdot \left(\left(M - m \cdot 0.5\right) - n\right) + \left(\left|m - n\right| - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{\left|m - n\right| - 0.25 \cdot {\left(m + n\right)}^{2}}\\ \end{array} \]

Alternative 4: 86.0% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := \left|m - n\right|\\ t_1 := e^{t_0 - 0.25 \cdot {\left(m + n\right)}^{2}}\\ \mathbf{if}\;n \leq -5 \cdot 10^{-19}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;n \leq 9.8 \cdot 10^{-169}:\\ \;\;\;\;e^{m + \left({\left(n \cdot 0.5\right)}^{2} - \left(n + \ell\right)\right)}\\ \mathbf{elif}\;n \leq 0.012:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(m - M\right) + \left(t_0 - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \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 (fabs (- m n))) (t_1 (exp (- t_0 (* 0.25 (pow (+ m n) 2.0))))))
   (if (<= n -5e-19)
     t_1
     (if (<= n 9.8e-169)
       (exp (+ m (- (pow (* n 0.5) 2.0) (+ n l))))
       (if (<= n 0.012) (* (cos M) (exp (+ (* M (- m M)) (- t_0 l)))) t_1)))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = fabs((m - n));
	double t_1 = exp((t_0 - (0.25 * pow((m + n), 2.0))));
	double tmp;
	if (n <= -5e-19) {
		tmp = t_1;
	} else if (n <= 9.8e-169) {
		tmp = exp((m + (pow((n * 0.5), 2.0) - (n + l))));
	} else if (n <= 0.012) {
		tmp = cos(M) * exp(((M * (m - M)) + (t_0 - l)));
	} else {
		tmp = t_1;
	}
	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) :: t_1
    real(8) :: tmp
    t_0 = abs((m - n))
    t_1 = exp((t_0 - (0.25d0 * ((m + n) ** 2.0d0))))
    if (n <= (-5d-19)) then
        tmp = t_1
    else if (n <= 9.8d-169) then
        tmp = exp((m + (((n * 0.5d0) ** 2.0d0) - (n + l))))
    else if (n <= 0.012d0) then
        tmp = cos(m_1) * exp(((m_1 * (m - m_1)) + (t_0 - l)))
    else
        tmp = t_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 = Math.abs((m - n));
	double t_1 = Math.exp((t_0 - (0.25 * Math.pow((m + n), 2.0))));
	double tmp;
	if (n <= -5e-19) {
		tmp = t_1;
	} else if (n <= 9.8e-169) {
		tmp = Math.exp((m + (Math.pow((n * 0.5), 2.0) - (n + l))));
	} else if (n <= 0.012) {
		tmp = Math.cos(M) * Math.exp(((M * (m - M)) + (t_0 - l)));
	} else {
		tmp = t_1;
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = math.fabs((m - n))
	t_1 = math.exp((t_0 - (0.25 * math.pow((m + n), 2.0))))
	tmp = 0
	if n <= -5e-19:
		tmp = t_1
	elif n <= 9.8e-169:
		tmp = math.exp((m + (math.pow((n * 0.5), 2.0) - (n + l))))
	elif n <= 0.012:
		tmp = math.cos(M) * math.exp(((M * (m - M)) + (t_0 - l)))
	else:
		tmp = t_1
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = abs(Float64(m - n))
	t_1 = exp(Float64(t_0 - Float64(0.25 * (Float64(m + n) ^ 2.0))))
	tmp = 0.0
	if (n <= -5e-19)
		tmp = t_1;
	elseif (n <= 9.8e-169)
		tmp = exp(Float64(m + Float64((Float64(n * 0.5) ^ 2.0) - Float64(n + l))));
	elseif (n <= 0.012)
		tmp = Float64(cos(M) * exp(Float64(Float64(M * Float64(m - M)) + Float64(t_0 - l))));
	else
		tmp = t_1;
	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 = abs((m - n));
	t_1 = exp((t_0 - (0.25 * ((m + n) ^ 2.0))));
	tmp = 0.0;
	if (n <= -5e-19)
		tmp = t_1;
	elseif (n <= 9.8e-169)
		tmp = exp((m + (((n * 0.5) ^ 2.0) - (n + l))));
	elseif (n <= 0.012)
		tmp = cos(M) * exp(((M * (m - M)) + (t_0 - l)));
	else
		tmp = t_1;
	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[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Exp[N[(t$95$0 - N[(0.25 * N[Power[N[(m + n), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[n, -5e-19], t$95$1, If[LessEqual[n, 9.8e-169], N[Exp[N[(m + N[(N[Power[N[(n * 0.5), $MachinePrecision], 2.0], $MachinePrecision] - N[(n + l), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[n, 0.012], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(M * N[(m - M), $MachinePrecision]), $MachinePrecision] + N[(t$95$0 - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$1]]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \left|m - n\right|\\
t_1 := e^{t_0 - 0.25 \cdot {\left(m + n\right)}^{2}}\\
\mathbf{if}\;n \leq -5 \cdot 10^{-19}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;n \leq 9.8 \cdot 10^{-169}:\\
\;\;\;\;e^{m + \left({\left(n \cdot 0.5\right)}^{2} - \left(n + \ell\right)\right)}\\

\mathbf{elif}\;n \leq 0.012:\\
\;\;\;\;\cos M \cdot e^{M \cdot \left(m - M\right) + \left(t_0 - \ell\right)}\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if n < -5.0000000000000004e-19 or 0.012 < n

    1. Initial program 76.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. Taylor expanded in l around 0 75.5%

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

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

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

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

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

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

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

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

    if -5.0000000000000004e-19 < n < 9.7999999999999999e-169

    1. Initial program 79.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. Step-by-step derivation
      1. expm1-log1p-u79.8%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(0.5 \cdot \color{blue}{\left(\left(m + n\right) \cdot K\right)}\right) \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      2. *-commutative30.8%

        \[\leadsto \cos \color{blue}{\left(\left(\left(m + n\right) \cdot K\right) \cdot 0.5\right)} \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      3. associate-*l*30.8%

        \[\leadsto \cos \color{blue}{\left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right)} \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      4. +-commutative30.8%

        \[\leadsto \cos \left(\color{blue}{\left(n + m\right)} \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      5. associate--l+30.8%

        \[\leadsto \cos \left(\left(n + m\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\color{blue}{m + \left(0.25 \cdot {\left(m + n\right)}^{2} - \left(\ell + n\right)\right)}} \]
      6. +-commutative30.8%

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

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

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

      \[\leadsto \color{blue}{1} \cdot e^{m + \left(0.25 \cdot {\left(n + m\right)}^{2} - \left(n + \ell\right)\right)} \]
    10. Taylor expanded in n around inf 53.9%

      \[\leadsto 1 \cdot e^{m + \left(\color{blue}{0.25 \cdot {n}^{2}} - \left(n + \ell\right)\right)} \]
    11. Step-by-step derivation
      1. metadata-eval53.9%

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

        \[\leadsto 1 \cdot e^{m + \left(\left(0.5 \cdot 0.5\right) \cdot \color{blue}{\left(n \cdot n\right)} - \left(n + \ell\right)\right)} \]
      3. swap-sqr53.9%

        \[\leadsto 1 \cdot e^{m + \left(\color{blue}{\left(0.5 \cdot n\right) \cdot \left(0.5 \cdot n\right)} - \left(n + \ell\right)\right)} \]
      4. unpow253.9%

        \[\leadsto 1 \cdot e^{m + \left(\color{blue}{{\left(0.5 \cdot n\right)}^{2}} - \left(n + \ell\right)\right)} \]
      5. *-commutative53.9%

        \[\leadsto 1 \cdot e^{m + \left({\color{blue}{\left(n \cdot 0.5\right)}}^{2} - \left(n + \ell\right)\right)} \]
    12. Simplified53.9%

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

    if 9.7999999999999999e-169 < n < 0.012

    1. Initial program 92.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. 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)} \]
    3. Step-by-step derivation
      1. cos-neg87.3%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left|n - m\right| - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}} \]
    4. 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)} \]
    5. Taylor expanded in m around 0 68.5%

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(0.5 \cdot n - M\right)} + m \cdot \left(0.5 \cdot n - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out68.5%

        \[\leadsto \cos M \cdot e^{\left(-\color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(\left(0.5 \cdot n - M\right) + m\right)}\right) - \left(\ell - \left|m - n\right|\right)} \]
      4. *-commutative68.5%

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{n \cdot 0.5} - M\right) \cdot \left(\left(0.5 \cdot n - M\right) + m\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      5. *-commutative68.5%

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{\left(\left|n - m\right| - \ell\right) - -1 \cdot \left(M \cdot \left(m - M\right)\right)}} \]
      3. associate-*r*68.5%

        \[\leadsto \cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - \color{blue}{\left(-1 \cdot M\right) \cdot \left(m - M\right)}} \]
      4. neg-mul-168.5%

        \[\leadsto \cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - \color{blue}{\left(-M\right)} \cdot \left(m - M\right)} \]
      5. cancel-sign-sub68.5%

        \[\leadsto \cos M \cdot e^{\color{blue}{\left(\left|n - m\right| - \ell\right) + M \cdot \left(m - M\right)}} \]
      6. fabs-sub68.5%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq -5 \cdot 10^{-19}:\\ \;\;\;\;e^{\left|m - n\right| - 0.25 \cdot {\left(m + n\right)}^{2}}\\ \mathbf{elif}\;n \leq 9.8 \cdot 10^{-169}:\\ \;\;\;\;e^{m + \left({\left(n \cdot 0.5\right)}^{2} - \left(n + \ell\right)\right)}\\ \mathbf{elif}\;n \leq 0.012:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(m - M\right) + \left(\left|m - n\right| - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{\left|m - n\right| - 0.25 \cdot {\left(m + n\right)}^{2}}\\ \end{array} \]

Alternative 5: 84.1% accurate, 1.4× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;n \leq -2.2 \cdot 10^{-19} \lor \neg \left(n \leq 3 \cdot 10^{-8}\right):\\ \;\;\;\;e^{\left|m - n\right| - 0.25 \cdot {\left(m + n\right)}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{m + \left({\left(n \cdot 0.5\right)}^{2} - \left(n + \ell\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
 (if (or (<= n -2.2e-19) (not (<= n 3e-8)))
   (exp (- (fabs (- m n)) (* 0.25 (pow (+ m n) 2.0))))
   (exp (+ m (- (pow (* n 0.5) 2.0) (+ n l))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((n <= -2.2e-19) || !(n <= 3e-8)) {
		tmp = exp((fabs((m - n)) - (0.25 * pow((m + n), 2.0))));
	} else {
		tmp = exp((m + (pow((n * 0.5), 2.0) - (n + 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) :: tmp
    if ((n <= (-2.2d-19)) .or. (.not. (n <= 3d-8))) then
        tmp = exp((abs((m - n)) - (0.25d0 * ((m + n) ** 2.0d0))))
    else
        tmp = exp((m + (((n * 0.5d0) ** 2.0d0) - (n + 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 tmp;
	if ((n <= -2.2e-19) || !(n <= 3e-8)) {
		tmp = Math.exp((Math.abs((m - n)) - (0.25 * Math.pow((m + n), 2.0))));
	} else {
		tmp = Math.exp((m + (Math.pow((n * 0.5), 2.0) - (n + l))));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if (n <= -2.2e-19) or not (n <= 3e-8):
		tmp = math.exp((math.fabs((m - n)) - (0.25 * math.pow((m + n), 2.0))))
	else:
		tmp = math.exp((m + (math.pow((n * 0.5), 2.0) - (n + l))))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	tmp = 0.0
	if ((n <= -2.2e-19) || !(n <= 3e-8))
		tmp = exp(Float64(abs(Float64(m - n)) - Float64(0.25 * (Float64(m + n) ^ 2.0))));
	else
		tmp = exp(Float64(m + Float64((Float64(n * 0.5) ^ 2.0) - Float64(n + 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)
	tmp = 0.0;
	if ((n <= -2.2e-19) || ~((n <= 3e-8)))
		tmp = exp((abs((m - n)) - (0.25 * ((m + n) ^ 2.0))));
	else
		tmp = exp((m + (((n * 0.5) ^ 2.0) - (n + 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_] := If[Or[LessEqual[n, -2.2e-19], N[Not[LessEqual[n, 3e-8]], $MachinePrecision]], N[Exp[N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - N[(0.25 * N[Power[N[(m + n), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Exp[N[(m + N[(N[Power[N[(n * 0.5), $MachinePrecision], 2.0], $MachinePrecision] - N[(n + l), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;n \leq -2.2 \cdot 10^{-19} \lor \neg \left(n \leq 3 \cdot 10^{-8}\right):\\
\;\;\;\;e^{\left|m - n\right| - 0.25 \cdot {\left(m + n\right)}^{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if n < -2.1999999999999998e-19 or 2.99999999999999973e-8 < n

    1. Initial program 77.0%

      \[\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. Taylor expanded in l around 0 75.6%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left|n - m\right| - {\left(\color{blue}{\left(m + n\right) \cdot 0.5} - M\right)}^{2}} \]
    4. Simplified75.6%

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left|n - m\right| - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}} \]
    7. Simplified98.0%

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

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

    if -2.1999999999999998e-19 < n < 2.99999999999999973e-8

    1. Initial program 83.9%

      \[\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. Step-by-step derivation
      1. expm1-log1p-u83.9%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \color{blue}{\left(\left(\left(m + n\right) \cdot K\right) \cdot 0.5\right)} \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      3. associate-*l*32.2%

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

        \[\leadsto \cos \left(\color{blue}{\left(n + m\right)} \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      5. associate--l+32.2%

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

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

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

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

      \[\leadsto \color{blue}{1} \cdot e^{m + \left(0.25 \cdot {\left(n + m\right)}^{2} - \left(n + \ell\right)\right)} \]
    10. Taylor expanded in n around inf 50.4%

      \[\leadsto 1 \cdot e^{m + \left(\color{blue}{0.25 \cdot {n}^{2}} - \left(n + \ell\right)\right)} \]
    11. Step-by-step derivation
      1. metadata-eval50.4%

        \[\leadsto 1 \cdot e^{m + \left(\color{blue}{\left(0.5 \cdot 0.5\right)} \cdot {n}^{2} - \left(n + \ell\right)\right)} \]
      2. unpow250.4%

        \[\leadsto 1 \cdot e^{m + \left(\left(0.5 \cdot 0.5\right) \cdot \color{blue}{\left(n \cdot n\right)} - \left(n + \ell\right)\right)} \]
      3. swap-sqr50.4%

        \[\leadsto 1 \cdot e^{m + \left(\color{blue}{\left(0.5 \cdot n\right) \cdot \left(0.5 \cdot n\right)} - \left(n + \ell\right)\right)} \]
      4. unpow250.4%

        \[\leadsto 1 \cdot e^{m + \left(\color{blue}{{\left(0.5 \cdot n\right)}^{2}} - \left(n + \ell\right)\right)} \]
      5. *-commutative50.4%

        \[\leadsto 1 \cdot e^{m + \left({\color{blue}{\left(n \cdot 0.5\right)}}^{2} - \left(n + \ell\right)\right)} \]
    12. Simplified50.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq -2.2 \cdot 10^{-19} \lor \neg \left(n \leq 3 \cdot 10^{-8}\right):\\ \;\;\;\;e^{\left|m - n\right| - 0.25 \cdot {\left(m + n\right)}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{m + \left({\left(n \cdot 0.5\right)}^{2} - \left(n + \ell\right)\right)}\\ \end{array} \]

Alternative 6: 52.8% accurate, 2.0× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;m \leq -1.36 \cdot 10^{+82}:\\ \;\;\;\;e^{-\ell}\\ \mathbf{else}:\\ \;\;\;\;e^{m + \left(0.25 \cdot {m}^{2} - \left(n + \ell\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
 (if (<= m -1.36e+82)
   (exp (- l))
   (exp (+ m (- (* 0.25 (pow m 2.0)) (+ n l))))))
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 <= -1.36e+82) {
		tmp = exp(-l);
	} else {
		tmp = exp((m + ((0.25 * pow(m, 2.0)) - (n + 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) :: tmp
    if (m <= (-1.36d+82)) then
        tmp = exp(-l)
    else
        tmp = exp((m + ((0.25d0 * (m ** 2.0d0)) - (n + 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 tmp;
	if (m <= -1.36e+82) {
		tmp = Math.exp(-l);
	} else {
		tmp = Math.exp((m + ((0.25 * Math.pow(m, 2.0)) - (n + l))));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if m <= -1.36e+82:
		tmp = math.exp(-l)
	else:
		tmp = math.exp((m + ((0.25 * math.pow(m, 2.0)) - (n + l))))
	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 <= -1.36e+82)
		tmp = exp(Float64(-l));
	else
		tmp = exp(Float64(m + Float64(Float64(0.25 * (m ^ 2.0)) - Float64(n + 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)
	tmp = 0.0;
	if (m <= -1.36e+82)
		tmp = exp(-l);
	else
		tmp = exp((m + ((0.25 * (m ^ 2.0)) - (n + 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_] := If[LessEqual[m, -1.36e+82], N[Exp[(-l)], $MachinePrecision], N[Exp[N[(m + N[(N[(0.25 * N[Power[m, 2.0], $MachinePrecision]), $MachinePrecision] - N[(n + l), $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 -1.36 \cdot 10^{+82}:\\
\;\;\;\;e^{-\ell}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if m < -1.36000000000000001e82

    1. Initial program 70.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. Step-by-step derivation
      1. expm1-log1p-u70.8%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \color{blue}{\left(\left(\left(m + n\right) \cdot K\right) \cdot 0.5\right)} \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      3. associate-*l*5.2%

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

        \[\leadsto \cos \left(\color{blue}{\left(n + m\right)} \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      5. associate--l+5.2%

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{0.25 \cdot {n}^{2} - \left(\ell + n\right)}} \]
    10. Taylor expanded in n around 0 31.1%

      \[\leadsto \color{blue}{e^{-\ell}} \]

    if -1.36000000000000001e82 < m

    1. Initial program 82.0%

      \[\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. Step-by-step derivation
      1. expm1-log1p-u82.0%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \color{blue}{\left(\left(\left(m + n\right) \cdot K\right) \cdot 0.5\right)} \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      3. associate-*l*19.0%

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

        \[\leadsto \cos \left(\color{blue}{\left(n + m\right)} \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      5. associate--l+19.0%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -1.36 \cdot 10^{+82}:\\ \;\;\;\;e^{-\ell}\\ \mathbf{else}:\\ \;\;\;\;e^{m + \left(0.25 \cdot {m}^{2} - \left(n + \ell\right)\right)}\\ \end{array} \]

Alternative 7: 67.7% accurate, 2.0× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;n \leq 0.00065:\\ \;\;\;\;e^{m + \left({\left(n \cdot 0.5\right)}^{2} - \left(n + \ell\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{m + \left(0.25 \cdot {m}^{2} - \left(n + \ell\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
 (if (<= n 0.00065)
   (exp (+ m (- (pow (* n 0.5) 2.0) (+ n l))))
   (exp (+ m (- (* 0.25 (pow m 2.0)) (+ n l))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 0.00065) {
		tmp = exp((m + (pow((n * 0.5), 2.0) - (n + l))));
	} else {
		tmp = exp((m + ((0.25 * pow(m, 2.0)) - (n + 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) :: tmp
    if (n <= 0.00065d0) then
        tmp = exp((m + (((n * 0.5d0) ** 2.0d0) - (n + l))))
    else
        tmp = exp((m + ((0.25d0 * (m ** 2.0d0)) - (n + 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 tmp;
	if (n <= 0.00065) {
		tmp = Math.exp((m + (Math.pow((n * 0.5), 2.0) - (n + l))));
	} else {
		tmp = Math.exp((m + ((0.25 * Math.pow(m, 2.0)) - (n + l))));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if n <= 0.00065:
		tmp = math.exp((m + (math.pow((n * 0.5), 2.0) - (n + l))))
	else:
		tmp = math.exp((m + ((0.25 * math.pow(m, 2.0)) - (n + l))))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	tmp = 0.0
	if (n <= 0.00065)
		tmp = exp(Float64(m + Float64((Float64(n * 0.5) ^ 2.0) - Float64(n + l))));
	else
		tmp = exp(Float64(m + Float64(Float64(0.25 * (m ^ 2.0)) - Float64(n + 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)
	tmp = 0.0;
	if (n <= 0.00065)
		tmp = exp((m + (((n * 0.5) ^ 2.0) - (n + l))));
	else
		tmp = exp((m + ((0.25 * (m ^ 2.0)) - (n + 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_] := If[LessEqual[n, 0.00065], N[Exp[N[(m + N[(N[Power[N[(n * 0.5), $MachinePrecision], 2.0], $MachinePrecision] - N[(n + l), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Exp[N[(m + N[(N[(0.25 * N[Power[m, 2.0], $MachinePrecision]), $MachinePrecision] - N[(n + l), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;n \leq 0.00065:\\
\;\;\;\;e^{m + \left({\left(n \cdot 0.5\right)}^{2} - \left(n + \ell\right)\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if n < 6.4999999999999997e-4

    1. Initial program 82.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. Step-by-step derivation
      1. expm1-log1p-u82.2%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(0.5 \cdot \color{blue}{\left(\left(m + n\right) \cdot K\right)}\right) \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      2. *-commutative21.6%

        \[\leadsto \cos \color{blue}{\left(\left(\left(m + n\right) \cdot K\right) \cdot 0.5\right)} \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      3. associate-*l*21.6%

        \[\leadsto \cos \color{blue}{\left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right)} \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      4. +-commutative21.6%

        \[\leadsto \cos \left(\color{blue}{\left(n + m\right)} \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      5. associate--l+21.6%

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

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

        \[\leadsto \cos \left(\left(n + m\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{m + \left(0.25 \cdot {\left(n + m\right)}^{2} - \color{blue}{\left(n + \ell\right)}\right)} \]
    8. Simplified21.6%

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

      \[\leadsto \color{blue}{1} \cdot e^{m + \left(0.25 \cdot {\left(n + m\right)}^{2} - \left(n + \ell\right)\right)} \]
    10. Taylor expanded in n around inf 35.0%

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

        \[\leadsto 1 \cdot e^{m + \left(\color{blue}{\left(0.5 \cdot 0.5\right)} \cdot {n}^{2} - \left(n + \ell\right)\right)} \]
      2. unpow235.0%

        \[\leadsto 1 \cdot e^{m + \left(\left(0.5 \cdot 0.5\right) \cdot \color{blue}{\left(n \cdot n\right)} - \left(n + \ell\right)\right)} \]
      3. swap-sqr35.0%

        \[\leadsto 1 \cdot e^{m + \left(\color{blue}{\left(0.5 \cdot n\right) \cdot \left(0.5 \cdot n\right)} - \left(n + \ell\right)\right)} \]
      4. unpow235.0%

        \[\leadsto 1 \cdot e^{m + \left(\color{blue}{{\left(0.5 \cdot n\right)}^{2}} - \left(n + \ell\right)\right)} \]
      5. *-commutative35.0%

        \[\leadsto 1 \cdot e^{m + \left({\color{blue}{\left(n \cdot 0.5\right)}}^{2} - \left(n + \ell\right)\right)} \]
    12. Simplified35.0%

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

    if 6.4999999999999997e-4 < n

    1. Initial program 74.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. Step-by-step derivation
      1. expm1-log1p-u74.7%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \color{blue}{\left(\left(\left(m + n\right) \cdot K\right) \cdot 0.5\right)} \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      3. associate-*l*4.9%

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

        \[\leadsto \cos \left(\color{blue}{\left(n + m\right)} \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
      5. associate--l+4.9%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 0.00065:\\ \;\;\;\;e^{m + \left({\left(n \cdot 0.5\right)}^{2} - \left(n + \ell\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{m + \left(0.25 \cdot {m}^{2} - \left(n + \ell\right)\right)}\\ \end{array} \]

Alternative 8: 35.8% 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 79.9%

    \[\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. Step-by-step derivation
    1. expm1-log1p-u79.9%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos \left(0.5 \cdot \color{blue}{\left(\left(m + n\right) \cdot K\right)}\right) \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
    2. *-commutative16.4%

      \[\leadsto \cos \color{blue}{\left(\left(\left(m + n\right) \cdot K\right) \cdot 0.5\right)} \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
    3. associate-*l*16.4%

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

      \[\leadsto \cos \left(\color{blue}{\left(n + m\right)} \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)} \]
    5. associate--l+16.4%

      \[\leadsto \cos \left(\left(n + m\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\color{blue}{m + \left(0.25 \cdot {\left(m + n\right)}^{2} - \left(\ell + n\right)\right)}} \]
    6. +-commutative16.4%

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

      \[\leadsto \cos \left(\left(n + m\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{m + \left(0.25 \cdot {\left(n + m\right)}^{2} - \color{blue}{\left(n + \ell\right)}\right)} \]
  8. Simplified16.4%

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

    \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{0.25 \cdot {n}^{2} - \left(\ell + n\right)}} \]
  10. Taylor expanded in n around 0 32.7%

    \[\leadsto \color{blue}{e^{-\ell}} \]
  11. Final simplification32.7%

    \[\leadsto e^{-\ell} \]

Reproduce

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