Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.1% → 96.8%
Time: 29.0s
Alternatives: 9
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 9 alternatives:

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

Initial Program: 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.8% accurate, 1.0× speedup?

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

\\
\cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(0.5 \cdot \left(n + m\right) - M\right)}^{2}}
\end{array}
Derivation
  1. Initial program 74.4%

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

      \[\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/74.4%

      \[\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-74.4%

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

      \[\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-74.4%

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

      \[\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+74.4%

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

      \[\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+74.4%

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

    \[\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 97.2%

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

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

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

Alternative 2: 70.2% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;n \leq -3.6 \cdot 10^{-106}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;n \leq 54:\\ \;\;\;\;\cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - M \cdot M}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= n -3.6e-106)
   (* (cos M) (exp (* -0.25 (* m m))))
   (if (<= n 54.0)
     (* (cos M) (exp (- (- (fabs (- n m)) l) (* M M))))
     (* (cos M) (exp (* -0.25 (* n n)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= -3.6e-106) {
		tmp = cos(M) * exp((-0.25 * (m * m)));
	} else if (n <= 54.0) {
		tmp = cos(M) * exp(((fabs((n - m)) - l) - (M * M)));
	} else {
		tmp = cos(M) * exp((-0.25 * (n * n)));
	}
	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) :: tmp
    if (n <= (-3.6d-106)) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m * m)))
    else if (n <= 54.0d0) then
        tmp = cos(m_1) * exp(((abs((n - m)) - l) - (m_1 * m_1)))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n * n)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= -3.6e-106) {
		tmp = Math.cos(M) * Math.exp((-0.25 * (m * m)));
	} else if (n <= 54.0) {
		tmp = Math.cos(M) * Math.exp(((Math.abs((n - m)) - l) - (M * M)));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if n <= -3.6e-106:
		tmp = math.cos(M) * math.exp((-0.25 * (m * m)))
	elif n <= 54.0:
		tmp = math.cos(M) * math.exp(((math.fabs((n - m)) - l) - (M * M)))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * (n * n)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (n <= -3.6e-106)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(m * m))));
	elseif (n <= 54.0)
		tmp = Float64(cos(M) * exp(Float64(Float64(abs(Float64(n - m)) - l) - Float64(M * M))));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(n * n))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (n <= -3.6e-106)
		tmp = cos(M) * exp((-0.25 * (m * m)));
	elseif (n <= 54.0)
		tmp = cos(M) * exp(((abs((n - m)) - l) - (M * M)));
	else
		tmp = cos(M) * exp((-0.25 * (n * n)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[n, -3.6e-106], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(m * m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 54.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] - N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;n \leq -3.6 \cdot 10^{-106}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\

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

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\


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

    1. Initial program 69.4%

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

        \[\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/69.4%

        \[\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-69.4%

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

        \[\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-69.4%

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

        \[\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+69.4%

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

        \[\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+69.4%

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    7. Step-by-step derivation
      1. unpow255.6%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    8. Simplified55.6%

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

    if -3.60000000000000013e-106 < n < 54

    1. Initial program 81.4%

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

        \[\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/81.4%

        \[\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-81.4%

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

        \[\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-81.4%

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

        \[\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+81.4%

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

        \[\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+81.4%

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

      \[\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 61.8%

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

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

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

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

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

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

    if 54 < n

    1. Initial program 73.8%

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

        \[\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/73.8%

        \[\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-73.8%

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

        \[\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-73.8%

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

        \[\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+73.8%

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

        \[\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+73.8%

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

      \[\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 100.0%

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
    7. Step-by-step derivation
      1. *-commutative95.5%

        \[\leadsto \cos M \cdot e^{\color{blue}{{n}^{2} \cdot -0.25}} \]
      2. unpow295.5%

        \[\leadsto \cos M \cdot e^{\color{blue}{\left(n \cdot n\right)} \cdot -0.25} \]
    8. Simplified95.5%

      \[\leadsto \cos M \cdot e^{\color{blue}{\left(n \cdot n\right) \cdot -0.25}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification70.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq -3.6 \cdot 10^{-106}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;n \leq 54:\\ \;\;\;\;\cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - M \cdot M}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \]

Alternative 3: 74.4% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;m \leq -54:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - \left(n \cdot n\right) \cdot 0.25}\\


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

    1. Initial program 65.1%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative65.1%

        \[\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/65.1%

        \[\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-65.1%

        \[\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. +-commutative65.1%

        \[\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-65.1%

        \[\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-neg65.1%

        \[\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+65.1%

        \[\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. +-commutative65.1%

        \[\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+65.1%

        \[\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. Simplified65.1%

      \[\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 100.0%

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    7. Step-by-step derivation
      1. unpow296.9%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    8. Simplified96.9%

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

    if -54 < m

    1. Initial program 77.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. *-commutative77.5%

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

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

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

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

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

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

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

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

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

      \[\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 96.2%

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

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

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

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

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

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

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

Alternative 4: 78.3% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;M \leq -18 \lor \neg \left(M \leq 26.5\right):\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= M -18.0) (not (<= M 26.5)))
   (* (cos M) (exp (* M (- M))))
   (* (cos M) (exp (* -0.25 (* m m))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((M <= -18.0) || !(M <= 26.5)) {
		tmp = cos(M) * exp((M * -M));
	} else {
		tmp = cos(M) * exp((-0.25 * (m * m)));
	}
	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) :: tmp
    if ((m_1 <= (-18.0d0)) .or. (.not. (m_1 <= 26.5d0))) then
        tmp = cos(m_1) * exp((m_1 * -m_1))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (m * m)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((M <= -18.0) || !(M <= 26.5)) {
		tmp = Math.cos(M) * Math.exp((M * -M));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * (m * m)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if (M <= -18.0) or not (M <= 26.5):
		tmp = math.cos(M) * math.exp((M * -M))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * (m * m)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if ((M <= -18.0) || !(M <= 26.5))
		tmp = Float64(cos(M) * exp(Float64(M * Float64(-M))));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(m * m))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if ((M <= -18.0) || ~((M <= 26.5)))
		tmp = cos(M) * exp((M * -M));
	else
		tmp = cos(M) * exp((-0.25 * (m * m)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[Or[LessEqual[M, -18.0], N[Not[LessEqual[M, 26.5]], $MachinePrecision]], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(M * (-M)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(m * m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;M \leq -18 \lor \neg \left(M \leq 26.5\right):\\
\;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -18 or 26.5 < M

    1. Initial program 73.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. *-commutative73.5%

        \[\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/73.5%

        \[\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-73.5%

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

        \[\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-73.5%

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

        \[\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+73.5%

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

        \[\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+73.5%

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

      \[\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 99.9%

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot {M}^{2}}} \]
    7. Step-by-step derivation
      1. mul-1-neg96.0%

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

        \[\leadsto \cos M \cdot e^{-\color{blue}{M \cdot M}} \]
      3. distribute-rgt-neg-in96.0%

        \[\leadsto \cos M \cdot e^{\color{blue}{M \cdot \left(-M\right)}} \]
    8. Simplified96.0%

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

    if -18 < M < 26.5

    1. Initial program 75.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. *-commutative75.3%

        \[\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/75.3%

        \[\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-75.3%

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

        \[\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-75.3%

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

        \[\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+75.3%

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

        \[\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+75.3%

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

      \[\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 94.5%

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    7. Step-by-step derivation
      1. unpow257.7%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    8. Simplified57.7%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(m \cdot m\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification76.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -18 \lor \neg \left(M \leq 26.5\right):\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \end{array} \]

Alternative 5: 65.9% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;n \leq -3.1 \cdot 10^{-88}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;n \leq 54:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= n -3.1e-88)
   (* (cos M) (exp (* -0.25 (* m m))))
   (if (<= n 54.0)
     (* (cos M) (exp (* M (- M))))
     (* (cos M) (exp (* -0.25 (* n n)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= -3.1e-88) {
		tmp = cos(M) * exp((-0.25 * (m * m)));
	} else if (n <= 54.0) {
		tmp = cos(M) * exp((M * -M));
	} else {
		tmp = cos(M) * exp((-0.25 * (n * n)));
	}
	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) :: tmp
    if (n <= (-3.1d-88)) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m * m)))
    else if (n <= 54.0d0) then
        tmp = cos(m_1) * exp((m_1 * -m_1))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n * n)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= -3.1e-88) {
		tmp = Math.cos(M) * Math.exp((-0.25 * (m * m)));
	} else if (n <= 54.0) {
		tmp = Math.cos(M) * Math.exp((M * -M));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if n <= -3.1e-88:
		tmp = math.cos(M) * math.exp((-0.25 * (m * m)))
	elif n <= 54.0:
		tmp = math.cos(M) * math.exp((M * -M))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * (n * n)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (n <= -3.1e-88)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(m * m))));
	elseif (n <= 54.0)
		tmp = Float64(cos(M) * exp(Float64(M * Float64(-M))));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(n * n))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (n <= -3.1e-88)
		tmp = cos(M) * exp((-0.25 * (m * m)));
	elseif (n <= 54.0)
		tmp = cos(M) * exp((M * -M));
	else
		tmp = cos(M) * exp((-0.25 * (n * n)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[n, -3.1e-88], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(m * m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 54.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(M * (-M)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;n \leq -3.1 \cdot 10^{-88}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\

\mathbf{elif}\;n \leq 54:\\
\;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\


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

    1. Initial program 70.7%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative70.7%

        \[\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/70.7%

        \[\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-70.7%

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

        \[\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-70.7%

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

        \[\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+70.7%

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

        \[\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+70.7%

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

      \[\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 99.1%

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    7. Step-by-step derivation
      1. unpow256.6%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    8. Simplified56.6%

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

    if -3.0999999999999998e-88 < n < 54

    1. Initial program 79.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. *-commutative79.5%

        \[\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/79.5%

        \[\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-79.5%

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

        \[\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-79.5%

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

        \[\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+79.5%

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

        \[\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+79.5%

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

      \[\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 92.7%

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
      2. unpow255.7%

        \[\leadsto \cos M \cdot e^{-\color{blue}{M \cdot M}} \]
      3. distribute-rgt-neg-in55.7%

        \[\leadsto \cos M \cdot e^{\color{blue}{M \cdot \left(-M\right)}} \]
    8. Simplified55.7%

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

    if 54 < n

    1. Initial program 73.8%

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

        \[\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/73.8%

        \[\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-73.8%

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

        \[\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-73.8%

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

        \[\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+73.8%

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

        \[\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+73.8%

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

      \[\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 100.0%

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
    7. Step-by-step derivation
      1. *-commutative95.5%

        \[\leadsto \cos M \cdot e^{\color{blue}{{n}^{2} \cdot -0.25}} \]
      2. unpow295.5%

        \[\leadsto \cos M \cdot e^{\color{blue}{\left(n \cdot n\right)} \cdot -0.25} \]
    8. Simplified95.5%

      \[\leadsto \cos M \cdot e^{\color{blue}{\left(n \cdot n\right) \cdot -0.25}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification66.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq -3.1 \cdot 10^{-88}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;n \leq 54:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \]

Alternative 6: 68.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;M \leq -0.012 \lor \neg \left(M \leq 2800000000000\right):\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\cos M}{e^{\ell}}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= M -0.012) (not (<= M 2800000000000.0)))
   (* (cos M) (exp (* M (- M))))
   (/ (cos M) (exp l))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((M <= -0.012) || !(M <= 2800000000000.0)) {
		tmp = cos(M) * exp((M * -M));
	} else {
		tmp = cos(M) / exp(l);
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if ((m_1 <= (-0.012d0)) .or. (.not. (m_1 <= 2800000000000.0d0))) then
        tmp = cos(m_1) * exp((m_1 * -m_1))
    else
        tmp = cos(m_1) / exp(l)
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((M <= -0.012) || !(M <= 2800000000000.0)) {
		tmp = Math.cos(M) * Math.exp((M * -M));
	} else {
		tmp = Math.cos(M) / Math.exp(l);
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if (M <= -0.012) or not (M <= 2800000000000.0):
		tmp = math.cos(M) * math.exp((M * -M))
	else:
		tmp = math.cos(M) / math.exp(l)
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if ((M <= -0.012) || !(M <= 2800000000000.0))
		tmp = Float64(cos(M) * exp(Float64(M * Float64(-M))));
	else
		tmp = Float64(cos(M) / exp(l));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if ((M <= -0.012) || ~((M <= 2800000000000.0)))
		tmp = cos(M) * exp((M * -M));
	else
		tmp = cos(M) / exp(l);
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[Or[LessEqual[M, -0.012], N[Not[LessEqual[M, 2800000000000.0]], $MachinePrecision]], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(M * (-M)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] / N[Exp[l], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;M \leq -0.012 \lor \neg \left(M \leq 2800000000000\right):\\
\;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -0.012 or 2.8e12 < M

    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. *-commutative72.5%

        \[\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/72.5%

        \[\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-72.5%

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

        \[\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-72.5%

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

        \[\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+72.5%

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

        \[\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+72.5%

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

      \[\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 99.9%

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot {M}^{2}}} \]
    7. Step-by-step derivation
      1. mul-1-neg96.0%

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

        \[\leadsto \cos M \cdot e^{-\color{blue}{M \cdot M}} \]
      3. distribute-rgt-neg-in96.0%

        \[\leadsto \cos M \cdot e^{\color{blue}{M \cdot \left(-M\right)}} \]
    8. Simplified96.0%

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

    if -0.012 < M < 2.8e12

    1. Initial program 76.3%

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

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

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

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

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

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

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

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

        \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
    9. Simplified46.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -0.012 \lor \neg \left(M \leq 2800000000000\right):\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\cos M}{e^{\ell}}\\ \end{array} \]

Alternative 7: 35.7% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \frac{\cos M}{e^{\ell}} \end{array} \]
(FPCore (K m n M l) :precision binary64 (/ (cos M) (exp l)))
double code(double K, double m, double n, double M, double l) {
	return cos(M) / exp(l);
}
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(l)
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos(M) / Math.exp(l);
}
def code(K, m, n, M, l):
	return math.cos(M) / math.exp(l)
function code(K, m, n, M, l)
	return Float64(cos(M) / exp(l))
end
function tmp = code(K, m, n, M, l)
	tmp = cos(M) / exp(l);
end
code[K_, m_, n_, M_, l_] := N[(N[Cos[M], $MachinePrecision] / N[Exp[l], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\cos M}{e^{\ell}}
\end{array}
Derivation
  1. Initial program 74.4%

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

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

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

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

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

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

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

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

      \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
  9. Simplified35.6%

    \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
  10. Final simplification35.6%

    \[\leadsto \frac{\cos M}{e^{\ell}} \]

Alternative 8: 35.5% accurate, 4.2× speedup?

\[\begin{array}{l} \\ e^{-\ell} \end{array} \]
(FPCore (K m n M l) :precision binary64 (exp (- l)))
double code(double K, double m, double n, double M, double l) {
	return exp(-l);
}
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
public static double code(double K, double m, double n, double M, double l) {
	return Math.exp(-l);
}
def code(K, m, n, M, l):
	return math.exp(-l)
function code(K, m, n, M, l)
	return exp(Float64(-l))
end
function tmp = code(K, m, n, M, l)
	tmp = exp(-l);
end
code[K_, m_, n_, M_, l_] := N[Exp[(-l)], $MachinePrecision]
\begin{array}{l}

\\
e^{-\ell}
\end{array}
Derivation
  1. Initial program 74.4%

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

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

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

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

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

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

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

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

      \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
  9. Simplified35.6%

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

    \[\leadsto \color{blue}{\frac{1}{e^{\ell}}} \]
  11. Step-by-step derivation
    1. rec-exp34.4%

      \[\leadsto \color{blue}{e^{-\ell}} \]
  12. Simplified34.4%

    \[\leadsto \color{blue}{e^{-\ell}} \]
  13. Final simplification34.4%

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

Alternative 9: 6.8% accurate, 4.2× speedup?

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

\\
\cos M
\end{array}
Derivation
  1. Initial program 74.4%

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

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

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

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

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

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

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

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

      \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
  9. Simplified35.6%

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

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

    \[\leadsto \cos M \]

Reproduce

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