Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.5% → 96.7%
Time: 17.0s
Alternatives: 9
Speedup: 3.6×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 9 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 75.5% accurate, 1.0× speedup?

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

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

Alternative 1: 96.7% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(m + n\right) \cdot 0.5\\ t_1 := \sqrt[3]{t\_0}\\ t_2 := e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}\\ \mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t\_2 \leq \infty:\\ \;\;\;\;t\_2 \cdot \cos \left(t\_1 \cdot \left(K \cdot {t\_1}^{2}\right) - M\right)\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n - m\right) + \left(t\_0 - M\right) \cdot \left(M - t\_0\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* (+ m n) 0.5))
        (t_1 (cbrt t_0))
        (t_2 (exp (- (- (fabs (- m n)) l) (pow (- (/ (+ m n) 2.0) M) 2.0)))))
   (if (<= (* (cos (- (/ (* K (+ m n)) 2.0) M)) t_2) INFINITY)
     (* t_2 (cos (- (* t_1 (* K (pow t_1 2.0))) M)))
     (exp (+ (- n m) (* (- t_0 M) (- M t_0)))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = (m + n) * 0.5;
	double t_1 = cbrt(t_0);
	double t_2 = exp(((fabs((m - n)) - l) - pow((((m + n) / 2.0) - M), 2.0)));
	double tmp;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_2) <= ((double) INFINITY)) {
		tmp = t_2 * cos(((t_1 * (K * pow(t_1, 2.0))) - M));
	} else {
		tmp = exp(((n - m) + ((t_0 - M) * (M - t_0))));
	}
	return tmp;
}
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.cbrt(t_0);
	double t_2 = Math.exp(((Math.abs((m - n)) - l) - Math.pow((((m + n) / 2.0) - M), 2.0)));
	double tmp;
	if ((Math.cos((((K * (m + n)) / 2.0) - M)) * t_2) <= Double.POSITIVE_INFINITY) {
		tmp = t_2 * Math.cos(((t_1 * (K * Math.pow(t_1, 2.0))) - M));
	} else {
		tmp = Math.exp(((n - m) + ((t_0 - M) * (M - t_0))));
	}
	return tmp;
}
function code(K, m, n, M, l)
	t_0 = Float64(Float64(m + n) * 0.5)
	t_1 = cbrt(t_0)
	t_2 = exp(Float64(Float64(abs(Float64(m - n)) - l) - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)))
	tmp = 0.0
	if (Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * t_2) <= Inf)
		tmp = Float64(t_2 * cos(Float64(Float64(t_1 * Float64(K * (t_1 ^ 2.0))) - M)));
	else
		tmp = exp(Float64(Float64(n - m) + Float64(Float64(t_0 - M) * Float64(M - t_0))));
	end
	return tmp
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision]}, Block[{t$95$1 = N[Power[t$95$0, 1/3], $MachinePrecision]}, Block[{t$95$2 = 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]}, If[LessEqual[N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * t$95$2), $MachinePrecision], Infinity], N[(t$95$2 * N[Cos[N[(N[(t$95$1 * N[(K * N[Power[t$95$1, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Exp[N[(N[(n - m), $MachinePrecision] + N[(N[(t$95$0 - M), $MachinePrecision] * N[(M - t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(m + n\right) \cdot 0.5\\
t_1 := \sqrt[3]{t\_0}\\
t_2 := e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}\\
\mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t\_2 \leq \infty:\\
\;\;\;\;t\_2 \cdot \cos \left(t\_1 \cdot \left(K \cdot {t\_1}^{2}\right) - M\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) #s(literal 2 binary64)) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) #s(literal 2 binary64)) M) #s(literal 2 binary64))) (-.f64 l (fabs.f64 (-.f64 m n)))))) < +inf.0

    1. Initial program 94.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. Add Preprocessing
    3. Step-by-step derivation
      1. associate-/l*94.0%

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

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

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

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

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

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

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

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

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

    if +inf.0 < (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) #s(literal 2 binary64)) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) #s(literal 2 binary64)) M) #s(literal 2 binary64))) (-.f64 l (fabs.f64 (-.f64 m n))))))

    1. Initial program 0.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. Add Preprocessing
    3. Taylor expanded in K around 0 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(n - m\right) - \left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(0.5 \cdot \color{blue}{\left(n + m\right)} - M\right)} \cdot \cos M \]
    10. Applied egg-rr100.0%

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

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

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

Alternative 2: 96.9% accurate, 1.0× speedup?

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

\\
e^{\left(\left|m - n\right| - \ell\right) - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}} \cdot \cos M
\end{array}
Derivation
  1. Initial program 77.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. Add Preprocessing
  3. Taylor expanded in K around 0 95.3%

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

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

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

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

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

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

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

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

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

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

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

Alternative 3: 91.5% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(m + n\right) \cdot 0.5\\ t_1 := \left(t\_0 - M\right) \cdot \left(M - t\_0\right)\\ \mathbf{if}\;\ell \leq -1.65 \cdot 10^{+24}:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\left(m - n\right) - \ell\right) + t\_1}\\ \mathbf{elif}\;\ell \leq 1000:\\ \;\;\;\;e^{\left(n - m\right) + t\_1} \cdot \cos M\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* (+ m n) 0.5)) (t_1 (* (- t_0 M) (- M t_0))))
   (if (<= l -1.65e+24)
     (* (cos (- (/ (* K (+ m n)) 2.0) M)) (exp (+ (- (- m n) l) t_1)))
     (if (<= l 1000.0)
       (* (exp (+ (- n m) t_1)) (cos M))
       (* (cos M) (exp (- l)))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = (m + n) * 0.5;
	double t_1 = (t_0 - M) * (M - t_0);
	double tmp;
	if (l <= -1.65e+24) {
		tmp = cos((((K * (m + n)) / 2.0) - M)) * exp((((m - n) - l) + t_1));
	} else if (l <= 1000.0) {
		tmp = exp(((n - m) + t_1)) * cos(M);
	} else {
		tmp = cos(M) * exp(-l);
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = (m + n) * 0.5d0
    t_1 = (t_0 - m_1) * (m_1 - t_0)
    if (l <= (-1.65d+24)) then
        tmp = cos((((k * (m + n)) / 2.0d0) - m_1)) * exp((((m - n) - l) + t_1))
    else if (l <= 1000.0d0) then
        tmp = exp(((n - m) + t_1)) * cos(m_1)
    else
        tmp = cos(m_1) * exp(-l)
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = (m + n) * 0.5;
	double t_1 = (t_0 - M) * (M - t_0);
	double tmp;
	if (l <= -1.65e+24) {
		tmp = Math.cos((((K * (m + n)) / 2.0) - M)) * Math.exp((((m - n) - l) + t_1));
	} else if (l <= 1000.0) {
		tmp = Math.exp(((n - m) + t_1)) * Math.cos(M);
	} else {
		tmp = Math.cos(M) * Math.exp(-l);
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = (m + n) * 0.5
	t_1 = (t_0 - M) * (M - t_0)
	tmp = 0
	if l <= -1.65e+24:
		tmp = math.cos((((K * (m + n)) / 2.0) - M)) * math.exp((((m - n) - l) + t_1))
	elif l <= 1000.0:
		tmp = math.exp(((n - m) + t_1)) * math.cos(M)
	else:
		tmp = math.cos(M) * math.exp(-l)
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(Float64(m + n) * 0.5)
	t_1 = Float64(Float64(t_0 - M) * Float64(M - t_0))
	tmp = 0.0
	if (l <= -1.65e+24)
		tmp = Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * exp(Float64(Float64(Float64(m - n) - l) + t_1)));
	elseif (l <= 1000.0)
		tmp = Float64(exp(Float64(Float64(n - m) + t_1)) * cos(M));
	else
		tmp = Float64(cos(M) * exp(Float64(-l)));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = (m + n) * 0.5;
	t_1 = (t_0 - M) * (M - t_0);
	tmp = 0.0;
	if (l <= -1.65e+24)
		tmp = cos((((K * (m + n)) / 2.0) - M)) * exp((((m - n) - l) + t_1));
	elseif (l <= 1000.0)
		tmp = exp(((n - m) + t_1)) * cos(M);
	else
		tmp = cos(M) * exp(-l);
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision]}, Block[{t$95$1 = N[(N[(t$95$0 - M), $MachinePrecision] * N[(M - t$95$0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[l, -1.65e+24], N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(N[(m - n), $MachinePrecision] - l), $MachinePrecision] + t$95$1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[l, 1000.0], N[(N[Exp[N[(N[(n - m), $MachinePrecision] + t$95$1), $MachinePrecision]], $MachinePrecision] * N[Cos[M], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(m + n\right) \cdot 0.5\\
t_1 := \left(t\_0 - M\right) \cdot \left(M - t\_0\right)\\
\mathbf{if}\;\ell \leq -1.65 \cdot 10^{+24}:\\
\;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\left(m - n\right) - \ell\right) + t\_1}\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if l < -1.6499999999999999e24

    1. Initial program 74.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. Add Preprocessing
    3. Step-by-step derivation
      1. sub-neg74.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\left(-\ell\right) + \left(-\left(-\left(m + \color{blue}{-1 \cdot n}\right)\right)\right)\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \]
      8. distribute-neg-in74.5%

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

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\left(-\ell\right) + \left(-\left(-1 \cdot m + \color{blue}{n}\right)\right)\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \]
      12. distribute-neg-in74.5%

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\left(-\ell\right) + \left(\left(-\color{blue}{\left(-m\right)}\right) + \left(-n\right)\right)\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \]
      14. remove-double-neg74.5%

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

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

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

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

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

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

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

        \[\leadsto e^{\left(n - m\right) - \left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(0.5 \cdot \color{blue}{\left(n + m\right)} - M\right)} \cdot \cos M \]
    8. Applied egg-rr74.5%

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

    if -1.6499999999999999e24 < l < 1e3

    1. Initial program 75.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. Add Preprocessing
    3. Taylor expanded in K around 0 97.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(n - m\right) - \left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(0.5 \cdot \color{blue}{\left(n + m\right)} - M\right)} \cdot \cos M \]
    10. Applied egg-rr97.2%

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

    if 1e3 < l

    1. Initial program 85.2%

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

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

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

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-\ell} \]
      2. *-commutative100.0%

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

      \[\leadsto \color{blue}{e^{-\ell} \cdot \cos M} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification93.3%

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

Alternative 4: 89.4% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{-\ell}\\ t_1 := \left(m + n\right) \cdot 0.5\\ \mathbf{if}\;\ell \leq -1.9 \cdot 10^{+236}:\\ \;\;\;\;t\_0 \cdot \cos \left(0.5 \cdot \left(K \cdot n\right) - M\right)\\ \mathbf{elif}\;\ell \leq 200:\\ \;\;\;\;e^{\left(n - m\right) + \left(t\_1 - M\right) \cdot \left(M - t\_1\right)} \cdot \cos M\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (exp (- l))) (t_1 (* (+ m n) 0.5)))
   (if (<= l -1.9e+236)
     (* t_0 (cos (- (* 0.5 (* K n)) M)))
     (if (<= l 200.0)
       (* (exp (+ (- n m) (* (- t_1 M) (- M t_1)))) (cos M))
       (* (cos M) t_0)))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = exp(-l);
	double t_1 = (m + n) * 0.5;
	double tmp;
	if (l <= -1.9e+236) {
		tmp = t_0 * cos(((0.5 * (K * n)) - M));
	} else if (l <= 200.0) {
		tmp = exp(((n - m) + ((t_1 - M) * (M - t_1)))) * cos(M);
	} else {
		tmp = cos(M) * t_0;
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = exp(-l)
    t_1 = (m + n) * 0.5d0
    if (l <= (-1.9d+236)) then
        tmp = t_0 * cos(((0.5d0 * (k * n)) - m_1))
    else if (l <= 200.0d0) then
        tmp = exp(((n - m) + ((t_1 - m_1) * (m_1 - t_1)))) * cos(m_1)
    else
        tmp = cos(m_1) * t_0
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.exp(-l);
	double t_1 = (m + n) * 0.5;
	double tmp;
	if (l <= -1.9e+236) {
		tmp = t_0 * Math.cos(((0.5 * (K * n)) - M));
	} else if (l <= 200.0) {
		tmp = Math.exp(((n - m) + ((t_1 - M) * (M - t_1)))) * Math.cos(M);
	} else {
		tmp = Math.cos(M) * t_0;
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.exp(-l)
	t_1 = (m + n) * 0.5
	tmp = 0
	if l <= -1.9e+236:
		tmp = t_0 * math.cos(((0.5 * (K * n)) - M))
	elif l <= 200.0:
		tmp = math.exp(((n - m) + ((t_1 - M) * (M - t_1)))) * math.cos(M)
	else:
		tmp = math.cos(M) * t_0
	return tmp
function code(K, m, n, M, l)
	t_0 = exp(Float64(-l))
	t_1 = Float64(Float64(m + n) * 0.5)
	tmp = 0.0
	if (l <= -1.9e+236)
		tmp = Float64(t_0 * cos(Float64(Float64(0.5 * Float64(K * n)) - M)));
	elseif (l <= 200.0)
		tmp = Float64(exp(Float64(Float64(n - m) + Float64(Float64(t_1 - M) * Float64(M - t_1)))) * cos(M));
	else
		tmp = Float64(cos(M) * t_0);
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = exp(-l);
	t_1 = (m + n) * 0.5;
	tmp = 0.0;
	if (l <= -1.9e+236)
		tmp = t_0 * cos(((0.5 * (K * n)) - M));
	elseif (l <= 200.0)
		tmp = exp(((n - m) + ((t_1 - M) * (M - t_1)))) * cos(M);
	else
		tmp = cos(M) * t_0;
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Exp[(-l)], $MachinePrecision]}, Block[{t$95$1 = N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision]}, If[LessEqual[l, -1.9e+236], N[(t$95$0 * N[Cos[N[(N[(0.5 * N[(K * n), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[l, 200.0], N[(N[Exp[N[(N[(n - m), $MachinePrecision] + N[(N[(t$95$1 - M), $MachinePrecision] * N[(M - t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[M], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * t$95$0), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{-\ell}\\
t_1 := \left(m + n\right) \cdot 0.5\\
\mathbf{if}\;\ell \leq -1.9 \cdot 10^{+236}:\\
\;\;\;\;t\_0 \cdot \cos \left(0.5 \cdot \left(K \cdot n\right) - M\right)\\

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

\mathbf{else}:\\
\;\;\;\;\cos M \cdot t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if l < -1.89999999999999993e236

    1. Initial program 60.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. Add Preprocessing
    3. Taylor expanded in l around inf 40.3%

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

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

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

      \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot n\right) - M\right) \cdot e^{-\ell}} \]
    7. Step-by-step derivation
      1. *-commutative50.3%

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

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

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

    if -1.89999999999999993e236 < l < 200

    1. Initial program 76.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(n - m\right) - \left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(0.5 \cdot \color{blue}{\left(n + m\right)} - M\right)} \cdot \cos M \]
    10. Applied egg-rr88.3%

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

    if 200 < l

    1. Initial program 85.2%

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

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

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

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-\ell} \]
      2. *-commutative100.0%

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

      \[\leadsto \color{blue}{e^{-\ell} \cdot \cos M} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification89.6%

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

Alternative 5: 89.5% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{-\ell}\\ t_1 := \left(m + n\right) \cdot 0.5\\ \mathbf{if}\;\ell \leq -2.3 \cdot 10^{+236}:\\ \;\;\;\;t\_0 \cdot \cos \left(0.5 \cdot \left(K \cdot n\right) - M\right)\\ \mathbf{elif}\;\ell \leq 740:\\ \;\;\;\;e^{\left(n - m\right) + \left(t\_1 - M\right) \cdot \left(M - t\_1\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (exp (- l))) (t_1 (* (+ m n) 0.5)))
   (if (<= l -2.3e+236)
     (* t_0 (cos (- (* 0.5 (* K n)) M)))
     (if (<= l 740.0)
       (exp (+ (- n m) (* (- t_1 M) (- M t_1))))
       (* (cos M) t_0)))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = exp(-l);
	double t_1 = (m + n) * 0.5;
	double tmp;
	if (l <= -2.3e+236) {
		tmp = t_0 * cos(((0.5 * (K * n)) - M));
	} else if (l <= 740.0) {
		tmp = exp(((n - m) + ((t_1 - M) * (M - t_1))));
	} else {
		tmp = cos(M) * t_0;
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = exp(-l)
    t_1 = (m + n) * 0.5d0
    if (l <= (-2.3d+236)) then
        tmp = t_0 * cos(((0.5d0 * (k * n)) - m_1))
    else if (l <= 740.0d0) then
        tmp = exp(((n - m) + ((t_1 - m_1) * (m_1 - t_1))))
    else
        tmp = cos(m_1) * t_0
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.exp(-l);
	double t_1 = (m + n) * 0.5;
	double tmp;
	if (l <= -2.3e+236) {
		tmp = t_0 * Math.cos(((0.5 * (K * n)) - M));
	} else if (l <= 740.0) {
		tmp = Math.exp(((n - m) + ((t_1 - M) * (M - t_1))));
	} else {
		tmp = Math.cos(M) * t_0;
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.exp(-l)
	t_1 = (m + n) * 0.5
	tmp = 0
	if l <= -2.3e+236:
		tmp = t_0 * math.cos(((0.5 * (K * n)) - M))
	elif l <= 740.0:
		tmp = math.exp(((n - m) + ((t_1 - M) * (M - t_1))))
	else:
		tmp = math.cos(M) * t_0
	return tmp
function code(K, m, n, M, l)
	t_0 = exp(Float64(-l))
	t_1 = Float64(Float64(m + n) * 0.5)
	tmp = 0.0
	if (l <= -2.3e+236)
		tmp = Float64(t_0 * cos(Float64(Float64(0.5 * Float64(K * n)) - M)));
	elseif (l <= 740.0)
		tmp = exp(Float64(Float64(n - m) + Float64(Float64(t_1 - M) * Float64(M - t_1))));
	else
		tmp = Float64(cos(M) * t_0);
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = exp(-l);
	t_1 = (m + n) * 0.5;
	tmp = 0.0;
	if (l <= -2.3e+236)
		tmp = t_0 * cos(((0.5 * (K * n)) - M));
	elseif (l <= 740.0)
		tmp = exp(((n - m) + ((t_1 - M) * (M - t_1))));
	else
		tmp = cos(M) * t_0;
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Exp[(-l)], $MachinePrecision]}, Block[{t$95$1 = N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision]}, If[LessEqual[l, -2.3e+236], N[(t$95$0 * N[Cos[N[(N[(0.5 * N[(K * n), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[l, 740.0], N[Exp[N[(N[(n - m), $MachinePrecision] + N[(N[(t$95$1 - M), $MachinePrecision] * N[(M - t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * t$95$0), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{-\ell}\\
t_1 := \left(m + n\right) \cdot 0.5\\
\mathbf{if}\;\ell \leq -2.3 \cdot 10^{+236}:\\
\;\;\;\;t\_0 \cdot \cos \left(0.5 \cdot \left(K \cdot n\right) - M\right)\\

\mathbf{elif}\;\ell \leq 740:\\
\;\;\;\;e^{\left(n - m\right) + \left(t\_1 - M\right) \cdot \left(M - t\_1\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if l < -2.3e236

    1. Initial program 60.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. Add Preprocessing
    3. Taylor expanded in l around inf 40.3%

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

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

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

      \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot n\right) - M\right) \cdot e^{-\ell}} \]
    7. Step-by-step derivation
      1. *-commutative50.3%

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

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

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

    if -2.3e236 < l < 740

    1. Initial program 76.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(n - m\right) - \left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(0.5 \cdot \color{blue}{\left(n + m\right)} - M\right)} \cdot \cos M \]
    10. Applied egg-rr88.3%

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

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

    if 740 < l

    1. Initial program 85.2%

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

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

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

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-\ell} \]
      2. *-commutative100.0%

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

      \[\leadsto \color{blue}{e^{-\ell} \cdot \cos M} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification89.4%

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

Alternative 6: 91.3% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
t_0 := \left(m + n\right) \cdot 0.5\\
\mathbf{if}\;\ell \leq 200:\\
\;\;\;\;e^{\left(n - m\right) + \left(t\_0 - M\right) \cdot \left(M - t\_0\right)}\\

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


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

    1. Initial program 75.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. Add Preprocessing
    3. Taylor expanded in K around 0 93.8%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}} - {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}} \cdot \cos M \]
      3. rem-square-sqrt84.8%

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

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

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

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

        \[\leadsto e^{\left(n - m\right) - \left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(0.5 \cdot \color{blue}{\left(n + m\right)} - M\right)} \cdot \cos M \]
    10. Applied egg-rr84.8%

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

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

    if 200 < l

    1. Initial program 85.2%

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

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

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

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-\ell} \]
      2. *-commutative100.0%

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

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

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

Alternative 7: 88.3% accurate, 3.6× speedup?

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

\\
\begin{array}{l}
t_0 := \left(m + n\right) \cdot 0.5\\
e^{\left(n - m\right) + \left(t\_0 - M\right) \cdot \left(M - t\_0\right)}
\end{array}
\end{array}
Derivation
  1. Initial program 77.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. Add Preprocessing
  3. Taylor expanded in K around 0 95.3%

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

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

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

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

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

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

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

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

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

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

    \[\leadsto e^{\color{blue}{\left|n - m\right|} - {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}} \cdot \cos M \]
  7. Step-by-step derivation
    1. rem-square-sqrt44.3%

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

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

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

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

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

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

      \[\leadsto e^{\left(n - m\right) - \left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(0.5 \cdot \color{blue}{\left(n + m\right)} - M\right)} \cdot \cos M \]
  10. Applied egg-rr86.2%

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

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

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

Alternative 8: 6.9% accurate, 4.2× speedup?

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

\\
\cos M
\end{array}
Derivation
  1. Initial program 77.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. Add Preprocessing
  3. Taylor expanded in l around inf 33.0%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos M} \]
  11. Simplified7.6%

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

Alternative 9: 6.9% accurate, 425.0× speedup?

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

\\
1
\end{array}
Derivation
  1. Initial program 77.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. Add Preprocessing
  3. Taylor expanded in l around inf 33.0%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos M} \]
  11. Simplified7.6%

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

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

Reproduce

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