Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.1% → 96.4%
Time: 23.5s
Alternatives: 5
Speedup: 2.0×

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 5 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.1% 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.4% accurate, 0.7× speedup?

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

\\
\cos \left(\mathsf{expm1}\left(K \cdot \left(-0.125 \cdot \left(K \cdot {\left(m + n\right)}^{2}\right) + 0.5 \cdot \left(m + n\right)\right)\right) - M\right) \cdot e^{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}
\end{array}
Derivation
  1. Initial program 72.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. Step-by-step derivation
    1. associate-/l*72.8%

      \[\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. +-commutative72.8%

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

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

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

      \[\leadsto \cos \left(K \cdot \frac{\color{blue}{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. exp-diff22.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \cos \left(\mathsf{expm1}\left(K \cdot \left(-0.125 \cdot \left(K \cdot {\left(m + n\right)}^{2}\right) + 0.5 \cdot \left(m + n\right)\right)\right) - M\right) \cdot e^{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)} \]
  11. Add Preprocessing

Alternative 2: 87.2% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := n \cdot 0.5 - M\\ t_1 := \left|m - n\right| - \ell\\ \mathbf{if}\;m \leq -1 \cdot 10^{-27}:\\ \;\;\;\;e^{t\_1 - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{t\_1 - t\_0 \cdot \left(m + t\_0\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (- (* n 0.5) M)) (t_1 (- (fabs (- m n)) l)))
   (if (<= m -1e-27)
     (exp (- t_1 (* 0.25 (* (+ m n) (+ m n)))))
     (* (cos M) (exp (- t_1 (* t_0 (+ m t_0))))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = (n * 0.5) - M;
	double t_1 = fabs((m - n)) - l;
	double tmp;
	if (m <= -1e-27) {
		tmp = exp((t_1 - (0.25 * ((m + n) * (m + n)))));
	} else {
		tmp = cos(M) * exp((t_1 - (t_0 * (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 = (n * 0.5d0) - m_1
    t_1 = abs((m - n)) - l
    if (m <= (-1d-27)) then
        tmp = exp((t_1 - (0.25d0 * ((m + n) * (m + n)))))
    else
        tmp = cos(m_1) * exp((t_1 - (t_0 * (m + 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 = (n * 0.5) - M;
	double t_1 = Math.abs((m - n)) - l;
	double tmp;
	if (m <= -1e-27) {
		tmp = Math.exp((t_1 - (0.25 * ((m + n) * (m + n)))));
	} else {
		tmp = Math.cos(M) * Math.exp((t_1 - (t_0 * (m + t_0))));
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = (n * 0.5) - M
	t_1 = math.fabs((m - n)) - l
	tmp = 0
	if m <= -1e-27:
		tmp = math.exp((t_1 - (0.25 * ((m + n) * (m + n)))))
	else:
		tmp = math.cos(M) * math.exp((t_1 - (t_0 * (m + t_0))))
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(Float64(n * 0.5) - M)
	t_1 = Float64(abs(Float64(m - n)) - l)
	tmp = 0.0
	if (m <= -1e-27)
		tmp = exp(Float64(t_1 - Float64(0.25 * Float64(Float64(m + n) * Float64(m + n)))));
	else
		tmp = Float64(cos(M) * exp(Float64(t_1 - Float64(t_0 * Float64(m + t_0)))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = (n * 0.5) - M;
	t_1 = abs((m - n)) - l;
	tmp = 0.0;
	if (m <= -1e-27)
		tmp = exp((t_1 - (0.25 * ((m + n) * (m + n)))));
	else
		tmp = cos(M) * exp((t_1 - (t_0 * (m + t_0))));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[(n * 0.5), $MachinePrecision] - M), $MachinePrecision]}, Block[{t$95$1 = N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, If[LessEqual[m, -1e-27], N[Exp[N[(t$95$1 - N[(0.25 * N[(N[(m + n), $MachinePrecision] * N[(m + n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$1 - N[(t$95$0 * N[(m + t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := n \cdot 0.5 - M\\
t_1 := \left|m - n\right| - \ell\\
\mathbf{if}\;m \leq -1 \cdot 10^{-27}:\\
\;\;\;\;e^{t\_1 - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)}\\

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


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

    1. Initial program 66.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. associate-/l*67.4%

        \[\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. +-commutative67.4%

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

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

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

        \[\leadsto \cos \left(K \cdot \frac{\color{blue}{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. exp-diff4.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot \color{blue}{\left(\left(n + m\right) \cdot \left(n + m\right)\right)}} \]
    12. Applied egg-rr97.4%

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

    if -1e-27 < m

    1. Initial program 74.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. associate-/l*74.9%

        \[\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. +-commutative74.9%

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

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

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

        \[\leadsto \cos \left(K \cdot \frac{\color{blue}{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. exp-diff28.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - \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)} \]
      3. distribute-rgt-out79.4%

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

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

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

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

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

Alternative 3: 89.3% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := n \cdot 0.5 - M\\ t_1 := \left|m - n\right|\\ \mathbf{if}\;M \leq 3500000000000:\\ \;\;\;\;e^{\left(t\_1 - \ell\right) - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{t\_1 - t\_0 \cdot \left(m + t\_0\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (- (* n 0.5) M)) (t_1 (fabs (- m n))))
   (if (<= M 3500000000000.0)
     (exp (- (- t_1 l) (* 0.25 (* (+ m n) (+ m n)))))
     (* (cos M) (exp (- t_1 (* t_0 (+ m t_0))))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = (n * 0.5) - M;
	double t_1 = fabs((m - n));
	double tmp;
	if (M <= 3500000000000.0) {
		tmp = exp(((t_1 - l) - (0.25 * ((m + n) * (m + n)))));
	} else {
		tmp = cos(M) * exp((t_1 - (t_0 * (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 = (n * 0.5d0) - m_1
    t_1 = abs((m - n))
    if (m_1 <= 3500000000000.0d0) then
        tmp = exp(((t_1 - l) - (0.25d0 * ((m + n) * (m + n)))))
    else
        tmp = cos(m_1) * exp((t_1 - (t_0 * (m + 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 = (n * 0.5) - M;
	double t_1 = Math.abs((m - n));
	double tmp;
	if (M <= 3500000000000.0) {
		tmp = Math.exp(((t_1 - l) - (0.25 * ((m + n) * (m + n)))));
	} else {
		tmp = Math.cos(M) * Math.exp((t_1 - (t_0 * (m + t_0))));
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = (n * 0.5) - M
	t_1 = math.fabs((m - n))
	tmp = 0
	if M <= 3500000000000.0:
		tmp = math.exp(((t_1 - l) - (0.25 * ((m + n) * (m + n)))))
	else:
		tmp = math.cos(M) * math.exp((t_1 - (t_0 * (m + t_0))))
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(Float64(n * 0.5) - M)
	t_1 = abs(Float64(m - n))
	tmp = 0.0
	if (M <= 3500000000000.0)
		tmp = exp(Float64(Float64(t_1 - l) - Float64(0.25 * Float64(Float64(m + n) * Float64(m + n)))));
	else
		tmp = Float64(cos(M) * exp(Float64(t_1 - Float64(t_0 * Float64(m + t_0)))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = (n * 0.5) - M;
	t_1 = abs((m - n));
	tmp = 0.0;
	if (M <= 3500000000000.0)
		tmp = exp(((t_1 - l) - (0.25 * ((m + n) * (m + n)))));
	else
		tmp = cos(M) * exp((t_1 - (t_0 * (m + t_0))));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[(n * 0.5), $MachinePrecision] - M), $MachinePrecision]}, Block[{t$95$1 = N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[M, 3500000000000.0], N[Exp[N[(N[(t$95$1 - l), $MachinePrecision] - N[(0.25 * N[(N[(m + n), $MachinePrecision] * N[(m + n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$1 - N[(t$95$0 * N[(m + t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < 3.5e12

    1. Initial program 72.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. associate-/l*72.2%

        \[\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. +-commutative72.2%

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

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

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

        \[\leadsto \cos \left(K \cdot \frac{\color{blue}{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. exp-diff21.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot \color{blue}{\left(\left(n + m\right) \cdot \left(n + m\right)\right)}} \]
    12. Applied egg-rr87.7%

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

    if 3.5e12 < M

    1. Initial program 73.3%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. associate-/l*75.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. +-commutative75.0%

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

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

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

        \[\leadsto \cos \left(K \cdot \frac{\color{blue}{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. exp-diff23.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \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)} \]
      3. *-commutative81.8%

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

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

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

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

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

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

Alternative 4: 96.7% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \left(m + n\right)\\ \cos M \cdot e^{\left(\left|m - n\right| - \ell\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 (* 0.5 (+ m n))))
   (* (cos M) (exp (+ (- (fabs (- m n)) l) (* (- t_0 M) (- M t_0)))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = 0.5 * (m + n);
	return cos(M) * exp(((fabs((m - n)) - l) + ((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 = 0.5d0 * (m + n)
    code = cos(m_1) * exp(((abs((m - n)) - l) + ((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 = 0.5 * (m + n);
	return Math.cos(M) * Math.exp(((Math.abs((m - n)) - l) + ((t_0 - M) * (M - t_0))));
}
def code(K, m, n, M, l):
	t_0 = 0.5 * (m + n)
	return math.cos(M) * math.exp(((math.fabs((m - n)) - l) + ((t_0 - M) * (M - t_0))))
function code(K, m, n, M, l)
	t_0 = Float64(0.5 * Float64(m + n))
	return Float64(cos(M) * exp(Float64(Float64(abs(Float64(m - n)) - l) + Float64(Float64(t_0 - M) * Float64(M - t_0)))))
end
function tmp = code(K, m, n, M, l)
	t_0 = 0.5 * (m + n);
	tmp = cos(M) * exp(((abs((m - n)) - l) + ((t_0 - M) * (M - t_0))));
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(0.5 * N[(m + n), $MachinePrecision]), $MachinePrecision]}, N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] + N[(N[(t$95$0 - M), $MachinePrecision] * N[(M - t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(m + n\right)\\
\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) + \left(t\_0 - M\right) \cdot \left(M - t\_0\right)}
\end{array}
\end{array}
Derivation
  1. Initial program 72.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. Step-by-step derivation
    1. associate-/l*72.8%

      \[\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. +-commutative72.8%

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

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

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

      \[\leadsto \cos \left(K \cdot \frac{\color{blue}{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. exp-diff22.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 86.9% accurate, 2.0× speedup?

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

\\
e^{\left(\left|m - n\right| - \ell\right) - 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)}
\end{array}
Derivation
  1. Initial program 72.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. Step-by-step derivation
    1. associate-/l*72.8%

      \[\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. +-commutative72.8%

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

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

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

      \[\leadsto \cos \left(K \cdot \frac{\color{blue}{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. exp-diff22.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Reproduce

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