Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.9% → 96.5%
Time: 21.2s
Alternatives: 10
Speedup: 1.3×

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 10 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.9% 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.0× speedup?

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

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

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

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

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

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

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

Alternative 2: 67.2% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos M \cdot e^{M \cdot \left(m - M\right) + \left(\left|m - n\right| - \ell\right)}\\ \mathbf{if}\;m \leq -18000:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;m \leq -2.3 \cdot 10^{-189}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;m \leq -6 \cdot 10^{-253}:\\ \;\;\;\;\cos \left(0.5 \cdot \left(m \cdot K\right)\right) \cdot e^{\left(m - n\right) - \ell}\\ \mathbf{elif}\;m \leq 1.26 \cdot 10^{-247}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* (cos M) (exp (+ (* M (- m M)) (- (fabs (- m n)) l))))))
   (if (<= m -18000.0)
     (* (cos M) (exp (* (pow m 2.0) -0.25)))
     (if (<= m -2.3e-189)
       t_0
       (if (<= m -6e-253)
         (* (cos (* 0.5 (* m K))) (exp (- (- m n) l)))
         (if (<= m 1.26e-247) t_0 (* (cos M) (exp (* -0.25 (pow n 2.0))))))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = cos(M) * exp(((M * (m - M)) + (fabs((m - n)) - l)));
	double tmp;
	if (m <= -18000.0) {
		tmp = cos(M) * exp((pow(m, 2.0) * -0.25));
	} else if (m <= -2.3e-189) {
		tmp = t_0;
	} else if (m <= -6e-253) {
		tmp = cos((0.5 * (m * K))) * exp(((m - n) - l));
	} else if (m <= 1.26e-247) {
		tmp = t_0;
	} else {
		tmp = cos(M) * exp((-0.25 * pow(n, 2.0)));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos(m_1) * exp(((m_1 * (m - m_1)) + (abs((m - n)) - l)))
    if (m <= (-18000.0d0)) then
        tmp = cos(m_1) * exp(((m ** 2.0d0) * (-0.25d0)))
    else if (m <= (-2.3d-189)) then
        tmp = t_0
    else if (m <= (-6d-253)) then
        tmp = cos((0.5d0 * (m * k))) * exp(((m - n) - l))
    else if (m <= 1.26d-247) then
        tmp = t_0
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n ** 2.0d0)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.cos(M) * Math.exp(((M * (m - M)) + (Math.abs((m - n)) - l)));
	double tmp;
	if (m <= -18000.0) {
		tmp = Math.cos(M) * Math.exp((Math.pow(m, 2.0) * -0.25));
	} else if (m <= -2.3e-189) {
		tmp = t_0;
	} else if (m <= -6e-253) {
		tmp = Math.cos((0.5 * (m * K))) * Math.exp(((m - n) - l));
	} else if (m <= 1.26e-247) {
		tmp = t_0;
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(n, 2.0)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.cos(M) * math.exp(((M * (m - M)) + (math.fabs((m - n)) - l)))
	tmp = 0
	if m <= -18000.0:
		tmp = math.cos(M) * math.exp((math.pow(m, 2.0) * -0.25))
	elif m <= -2.3e-189:
		tmp = t_0
	elif m <= -6e-253:
		tmp = math.cos((0.5 * (m * K))) * math.exp(((m - n) - l))
	elif m <= 1.26e-247:
		tmp = t_0
	else:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(n, 2.0)))
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(cos(M) * exp(Float64(Float64(M * Float64(m - M)) + Float64(abs(Float64(m - n)) - l))))
	tmp = 0.0
	if (m <= -18000.0)
		tmp = Float64(cos(M) * exp(Float64((m ^ 2.0) * -0.25)));
	elseif (m <= -2.3e-189)
		tmp = t_0;
	elseif (m <= -6e-253)
		tmp = Float64(cos(Float64(0.5 * Float64(m * K))) * exp(Float64(Float64(m - n) - l)));
	elseif (m <= 1.26e-247)
		tmp = t_0;
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (n ^ 2.0))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = cos(M) * exp(((M * (m - M)) + (abs((m - n)) - l)));
	tmp = 0.0;
	if (m <= -18000.0)
		tmp = cos(M) * exp(((m ^ 2.0) * -0.25));
	elseif (m <= -2.3e-189)
		tmp = t_0;
	elseif (m <= -6e-253)
		tmp = cos((0.5 * (m * K))) * exp(((m - n) - l));
	elseif (m <= 1.26e-247)
		tmp = t_0;
	else
		tmp = cos(M) * exp((-0.25 * (n ^ 2.0)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(M * N[(m - M), $MachinePrecision]), $MachinePrecision] + N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[m, -18000.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, -2.3e-189], t$95$0, If[LessEqual[m, -6e-253], N[(N[Cos[N[(0.5 * N[(m * K), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(m - n), $MachinePrecision] - l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, 1.26e-247], t$95$0, N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[n, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos M \cdot e^{M \cdot \left(m - M\right) + \left(\left|m - n\right| - \ell\right)}\\
\mathbf{if}\;m \leq -18000:\\
\;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\

\mathbf{elif}\;m \leq -2.3 \cdot 10^{-189}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;m \leq -6 \cdot 10^{-253}:\\
\;\;\;\;\cos \left(0.5 \cdot \left(m \cdot K\right)\right) \cdot e^{\left(m - n\right) - \ell}\\

\mathbf{elif}\;m \leq 1.26 \cdot 10^{-247}:\\
\;\;\;\;t\_0\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\


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

    1. Initial program 73.2%

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
    8. Simplified96.5%

      \[\leadsto \cos M \cdot e^{\color{blue}{{m}^{2} \cdot -0.25}} \]

    if -18000 < m < -2.2999999999999998e-189 or -6.0000000000000004e-253 < m < 1.25999999999999999e-247

    1. Initial program 87.4%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + -1 \cdot \left(M \cdot \left(m - M\right)\right)\right)}} \]
    10. Step-by-step derivation
      1. associate--r+78.4%

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

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

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

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

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

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

    if -2.2999999999999998e-189 < m < -6.0000000000000004e-253

    1. Initial program 90.9%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. associate--r-90.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos \left(0.5 \cdot \left(m \cdot K\right)\right) \cdot e^{\color{blue}{-1 \cdot \ell} + \left(m - n\right)} \]
    9. Step-by-step derivation
      1. neg-mul-191.1%

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

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

    if 1.25999999999999999e-247 < m

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{{n}^{2} \cdot -0.25}} \]
    8. Simplified52.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -18000:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;m \leq -2.3 \cdot 10^{-189}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(m - M\right) + \left(\left|m - n\right| - \ell\right)}\\ \mathbf{elif}\;m \leq -6 \cdot 10^{-253}:\\ \;\;\;\;\cos \left(0.5 \cdot \left(m \cdot K\right)\right) \cdot e^{\left(m - n\right) - \ell}\\ \mathbf{elif}\;m \leq 1.26 \cdot 10^{-247}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(m - M\right) + \left(\left|m - n\right| - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 96.5% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \cos M \cdot e^{\left(\left(m - n\right) - \ell\right) - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (* (cos M) (exp (- (- (- m n) l) (pow (- (* (+ m n) 0.5) M) 2.0)))))
double code(double K, double m, double n, double M, double l) {
	return cos(M) * exp((((m - n) - l) - pow((((m + n) * 0.5) - 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((((m - n) - l) - ((((m + n) * 0.5d0) - 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((((m - n) - l) - Math.pow((((m + n) * 0.5) - M), 2.0)));
}
def code(K, m, n, M, l):
	return math.cos(M) * math.exp((((m - n) - l) - math.pow((((m + n) * 0.5) - M), 2.0)))
function code(K, m, n, M, l)
	return Float64(cos(M) * exp(Float64(Float64(Float64(m - n) - l) - (Float64(Float64(Float64(m + n) * 0.5) - M) ^ 2.0))))
end
function tmp = code(K, m, n, M, l)
	tmp = cos(M) * exp((((m - n) - l) - ((((m + n) * 0.5) - M) ^ 2.0)));
end
code[K_, m_, n_, M_, l_] := N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[(m - n), $MachinePrecision] - l), $MachinePrecision] - N[Power[N[(N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 4: 74.8% accurate, 1.3× speedup?

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

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

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


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

    1. Initial program 78.2%

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
    8. Simplified98.4%

      \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]

    if -16 < M < 27

    1. Initial program 77.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. Add Preprocessing
    3. Step-by-step derivation
      1. associate--r-77.7%

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left({\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2} - \ell\right) + \color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}} \]
      11. add-sqr-sqrt28.9%

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

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

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

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

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

      \[\leadsto \cos \left(0.5 \cdot \left(m \cdot K\right)\right) \cdot e^{\color{blue}{-1 \cdot \ell} + \left(m - n\right)} \]
    9. Step-by-step derivation
      1. neg-mul-156.1%

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

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

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

Alternative 5: 53.7% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq -4.1 \cdot 10^{+102}:\\
\;\;\;\;\cos M \cdot e^{m \cdot \left(M - n \cdot 0.5\right)}\\

\mathbf{elif}\;\ell \leq 200:\\
\;\;\;\;\cos \left(0.5 \cdot \left(m \cdot K\right)\right) \cdot e^{\left(m - n\right) - \ell}\\

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


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

    1. Initial program 63.4%

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

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

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

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

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

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

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

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

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

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

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

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

    if -4.1e102 < l < 200

    1. Initial program 78.6%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. associate--r-78.6%

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left({\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2} - \ell\right) + \color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}} \]
      11. add-sqr-sqrt12.9%

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

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

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

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

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

      \[\leadsto \cos \left(0.5 \cdot \left(m \cdot K\right)\right) \cdot e^{\color{blue}{-1 \cdot \ell} + \left(m - n\right)} \]
    9. Step-by-step derivation
      1. neg-mul-137.8%

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

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

    if 200 < l

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

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

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

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

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

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

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

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

Alternative 6: 54.4% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq 150:\\
\;\;\;\;\cos M \cdot e^{m \cdot \left(M - n \cdot 0.5\right)}\\

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


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

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

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

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

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

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

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

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

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

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

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

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

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

    if 150 < l

    1. Initial program 85.5%

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

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

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

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

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

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

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

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

Alternative 7: 35.9% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \cos M \cdot 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(Float64(-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}

\\
\cos M \cdot e^{-\ell}
\end{array}
Derivation
  1. Initial program 77.9%

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos M \cdot e^{-\ell}} \]
  9. Add Preprocessing

Alternative 8: 6.9% accurate, 4.0× speedup?

\[\begin{array}{l} \\ \cos \left(m \cdot \left(0.5 \cdot K\right) - M\right) \end{array} \]
(FPCore (K m n M l) :precision binary64 (cos (- (* m (* 0.5 K)) M)))
double code(double K, double m, double n, double M, double l) {
	return cos(((m * (0.5 * K)) - 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 * (0.5d0 * k)) - m_1))
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos(((m * (0.5 * K)) - M));
}
def code(K, m, n, M, l):
	return math.cos(((m * (0.5 * K)) - M))
function code(K, m, n, M, l)
	return cos(Float64(Float64(m * Float64(0.5 * K)) - M))
end
function tmp = code(K, m, n, M, l)
	tmp = cos(((m * (0.5 * K)) - M));
end
code[K_, m_, n_, M_, l_] := N[Cos[N[(N[(m * N[(0.5 * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\cos \left(m \cdot \left(0.5 \cdot K\right) - M\right)
\end{array}
Derivation
  1. Initial program 77.9%

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

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

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

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

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

    \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot m\right) - M\right)} \]
  8. Step-by-step derivation
    1. associate-*r*8.0%

      \[\leadsto \cos \left(\color{blue}{\left(0.5 \cdot K\right) \cdot m} - M\right) \]
    2. *-commutative8.0%

      \[\leadsto \cos \left(\color{blue}{\left(K \cdot 0.5\right)} \cdot m - M\right) \]
  9. Simplified8.0%

    \[\leadsto \color{blue}{\cos \left(\left(K \cdot 0.5\right) \cdot m - M\right)} \]
  10. Final simplification8.0%

    \[\leadsto \cos \left(m \cdot \left(0.5 \cdot K\right) - M\right) \]
  11. Add Preprocessing

Alternative 9: 6.9% accurate, 4.0× speedup?

\[\begin{array}{l} \\ \cos \left(m \cdot \left(0.5 \cdot K\right)\right) \end{array} \]
(FPCore (K m n M l) :precision binary64 (cos (* m (* 0.5 K))))
double code(double K, double m, double n, double M, double l) {
	return cos((m * (0.5 * K)));
}
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 * (0.5d0 * k)))
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos((m * (0.5 * K)));
}
def code(K, m, n, M, l):
	return math.cos((m * (0.5 * K)))
function code(K, m, n, M, l)
	return cos(Float64(m * Float64(0.5 * K)))
end
function tmp = code(K, m, n, M, l)
	tmp = cos((m * (0.5 * K)));
end
code[K_, m_, n_, M_, l_] := N[Cos[N[(m * N[(0.5 * K), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\cos \left(m \cdot \left(0.5 \cdot K\right)\right)
\end{array}
Derivation
  1. Initial program 77.9%

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

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

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

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

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

    \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot m\right)\right)} \]
  8. Step-by-step derivation
    1. associate-*r*8.0%

      \[\leadsto \cos \color{blue}{\left(\left(0.5 \cdot K\right) \cdot m\right)} \]
    2. *-commutative8.0%

      \[\leadsto \cos \left(\color{blue}{\left(K \cdot 0.5\right)} \cdot m\right) \]
  9. Simplified8.0%

    \[\leadsto \cos \color{blue}{\left(\left(K \cdot 0.5\right) \cdot m\right)} \]
  10. Final simplification8.0%

    \[\leadsto \cos \left(m \cdot \left(0.5 \cdot K\right)\right) \]
  11. Add Preprocessing

Alternative 10: 7.3% accurate, 4.2× speedup?

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos M} \]
  9. Simplified8.1%

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

Reproduce

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