Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.6% → 96.5%
Time: 14.2s
Alternatives: 6
Speedup: 3.9×

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 6 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.6% 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.5% accurate, 1.3× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 93.7% accurate, 2.0× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 3: 93.4% accurate, 3.9× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{1} \cdot e^{m - \left(\left(n + \ell\right) + M \cdot M\right)} \]
  15. Final simplification72.7%

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

Alternative 4: 35.1% accurate, 4.2× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 6.9% accurate, 4.2× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos \left(-M\right)} \]
  9. Step-by-step derivation
    1. cos-neg6.7%

      \[\leadsto \color{blue}{\cos M} \]
  10. Simplified6.7%

    \[\leadsto \color{blue}{\cos M} \]
  11. Final simplification6.7%

    \[\leadsto \cos M \]

Alternative 6: 6.9% accurate, 425.0× speedup?

\[\begin{array}{l} [m, n] = \mathsf{sort}([m, n])\\ \\ 1 \end{array} \]
NOTE: m and n should be sorted in increasing order before calling this function.
(FPCore (K m n M l) :precision binary64 1.0)
assert(m < n);
double code(double K, double m, double n, double M, double l) {
	return 1.0;
}
NOTE: m and n should be sorted in increasing order before calling this function.
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = 1.0d0
end function
assert m < n;
public static double code(double K, double m, double n, double M, double l) {
	return 1.0;
}
[m, n] = sort([m, n])
def code(K, m, n, M, l):
	return 1.0
m, n = sort([m, n])
function code(K, m, n, M, l)
	return 1.0
end
m, n = num2cell(sort([m, n])){:}
function tmp = code(K, m, n, M, l)
	tmp = 1.0;
end
NOTE: m and n should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := 1.0
\begin{array}{l}
[m, n] = \mathsf{sort}([m, n])\\
\\
1
\end{array}
Derivation
  1. Initial program 77.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. *-commutative77.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos \left(-M\right)} \]
  9. Step-by-step derivation
    1. cos-neg6.7%

      \[\leadsto \color{blue}{\cos M} \]
  10. Simplified6.7%

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

    \[\leadsto \color{blue}{1} \]
  12. Final simplification6.7%

    \[\leadsto 1 \]

Reproduce

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