Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.7% → 96.9%
Time: 15.4s
Alternatives: 11
Speedup: 1.9×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 11 alternatives:

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

Initial Program: 75.7% 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.9% accurate, 0.4× speedup?

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

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

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


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

    1. Initial program 95.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. Step-by-step derivation
      1. add-cube-cbrt95.6%

        \[\leadsto \cos \left(\color{blue}{\left(\sqrt[3]{\frac{K \cdot \left(m + n\right)}{2}} \cdot \sqrt[3]{\frac{K \cdot \left(m + n\right)}{2}}\right) \cdot \sqrt[3]{\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. pow396.1%

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

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

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

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

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

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

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

    1. Initial program 0.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 96.6% accurate, 0.5× speedup?

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

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

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


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

    1. Initial program 54.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 54.5%

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

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

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

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

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

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

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

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

    1. Initial program 79.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 98.0%

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

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

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

Alternative 3: 89.6% accurate, 1.8× speedup?

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

\\
\begin{array}{l}
t_0 := \left(m + n\right) \cdot 0.5\\
\mathbf{if}\;\ell \leq -4.2 \cdot 10^{+235}:\\
\;\;\;\;\cos \left(\frac{K \cdot m}{2} - M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot 0.5 + -1\right)\right)\\

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

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


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

    1. Initial program 78.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 56.9%

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

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

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

      \[\leadsto \cos \left(\frac{\color{blue}{K \cdot m}}{2} - M\right) \cdot e^{-\ell} \]
    7. Step-by-step derivation
      1. *-commutative43.9%

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

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

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

    if -4.2000000000000001e235 < l < 740

    1. Initial program 73.3%

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

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

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

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

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

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

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

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

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

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

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

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

    if 740 < l

    1. Initial program 90.6%

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

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

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

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

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

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

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

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

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

Alternative 4: 84.4% accurate, 1.9× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if n < 34

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 34 < n

    1. Initial program 75.0%

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

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-0.25 \cdot {n}^{2}} \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\cos M} \cdot e^{-0.25 \cdot {n}^{2}} \]
    7. Taylor expanded in M around 0 100.0%

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

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

Alternative 5: 69.3% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
t_0 := \cos \left(\frac{K \cdot m}{2} - M\right)\\
\mathbf{if}\;\ell \leq -4.2 \cdot 10^{+235}:\\
\;\;\;\;t\_0 \cdot \left(1 + \ell \cdot \left(\ell \cdot 0.5 + -1\right)\right)\\

\mathbf{elif}\;\ell \leq -1.55 \cdot 10^{+32}:\\
\;\;\;\;t\_0 \cdot e^{\ell}\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if l < -4.2000000000000001e235

    1. Initial program 78.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 56.9%

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

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

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

      \[\leadsto \cos \left(\frac{\color{blue}{K \cdot m}}{2} - M\right) \cdot e^{-\ell} \]
    7. Step-by-step derivation
      1. *-commutative43.9%

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

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

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

    if -4.2000000000000001e235 < l < -1.54999999999999997e32

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

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

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

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

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

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

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

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

        \[\leadsto {\left(\cos \left(m \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\sqrt{-\ell} \cdot \sqrt{-\ell}}}\right)}^{1} \]
      4. sqrt-unprod9.4%

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

        \[\leadsto {\left(\cos \left(m \cdot \frac{K}{2} - M\right) \cdot e^{\sqrt{\color{blue}{\ell \cdot \ell}}}\right)}^{1} \]
      6. sqrt-unprod0.0%

        \[\leadsto {\left(\cos \left(m \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\sqrt{\ell} \cdot \sqrt{\ell}}}\right)}^{1} \]
      7. add-sqr-sqrt66.9%

        \[\leadsto {\left(\cos \left(m \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\ell}}\right)}^{1} \]
    10. Applied egg-rr66.9%

      \[\leadsto \color{blue}{{\left(\cos \left(m \cdot \frac{K}{2} - M\right) \cdot e^{\ell}\right)}^{1}} \]
    11. Step-by-step derivation
      1. unpow166.9%

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

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

        \[\leadsto \cos \left(\frac{\color{blue}{K \cdot m}}{2} - M\right) \cdot e^{\ell} \]
    12. Simplified66.9%

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

    if -1.54999999999999997e32 < l < 700

    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 n around inf 42.2%

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

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{-0.25 \cdot {n}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg56.8%

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

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

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

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

    if 700 < l

    1. Initial program 90.6%

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

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

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

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

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

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

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

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

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

Alternative 6: 71.2% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;n \leq -54:\\ \;\;\;\;e^{-0.25 \cdot {n}^{2}}\\ \mathbf{elif}\;n \leq 55:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \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 -54.0)
   (exp (* -0.25 (pow n 2.0)))
   (if (<= n 55.0)
     (* (cos M) (exp (- l)))
     (* (cos M) (exp (* -0.25 (* n n)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= -54.0) {
		tmp = exp((-0.25 * pow(n, 2.0)));
	} else if (n <= 55.0) {
		tmp = cos(M) * exp(-l);
	} 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 <= (-54.0d0)) then
        tmp = exp(((-0.25d0) * (n ** 2.0d0)))
    else if (n <= 55.0d0) then
        tmp = cos(m_1) * exp(-l)
    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 <= -54.0) {
		tmp = Math.exp((-0.25 * Math.pow(n, 2.0)));
	} else if (n <= 55.0) {
		tmp = Math.cos(M) * Math.exp(-l);
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if n <= -54.0:
		tmp = math.exp((-0.25 * math.pow(n, 2.0)))
	elif n <= 55.0:
		tmp = math.cos(M) * math.exp(-l)
	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 <= -54.0)
		tmp = exp(Float64(-0.25 * (n ^ 2.0)));
	elseif (n <= 55.0)
		tmp = Float64(cos(M) * exp(Float64(-l)));
	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 <= -54.0)
		tmp = exp((-0.25 * (n ^ 2.0)));
	elseif (n <= 55.0)
		tmp = cos(M) * exp(-l);
	else
		tmp = cos(M) * exp((-0.25 * (n * n)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[n, -54.0], N[Exp[N[(-0.25 * N[Power[n, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[n, 55.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-l)], $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 -54:\\
\;\;\;\;e^{-0.25 \cdot {n}^{2}}\\

\mathbf{elif}\;n \leq 55:\\
\;\;\;\;\cos M \cdot e^{-\ell}\\

\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 < -54

    1. Initial program 71.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. Taylor expanded in n around inf 68.4%

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-0.25 \cdot {n}^{2}} \]
    6. Simplified96.7%

      \[\leadsto \color{blue}{\cos M} \cdot e^{-0.25 \cdot {n}^{2}} \]
    7. Taylor expanded in M around 0 96.7%

      \[\leadsto \color{blue}{e^{-0.25 \cdot {n}^{2}}} \]

    if -54 < n < 55

    1. Initial program 81.6%

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

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

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

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

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

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

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

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

    if 55 < n

    1. Initial program 75.0%

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

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-0.25 \cdot {n}^{2}} \]
    6. Simplified100.0%

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

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

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

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

Alternative 7: 71.2% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;n \leq -54 \lor \neg \left(n \leq 53\right):\\
\;\;\;\;e^{-0.25 \cdot {n}^{2}}\\

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


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

    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 n around inf 71.3%

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

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

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

      \[\leadsto \color{blue}{\cos M} \cdot e^{-0.25 \cdot {n}^{2}} \]
    7. Taylor expanded in M around 0 98.2%

      \[\leadsto \color{blue}{e^{-0.25 \cdot {n}^{2}}} \]

    if -54 < n < 53

    1. Initial program 81.6%

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

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

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

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

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

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

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

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

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

Alternative 8: 57.1% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;n \leq -4.8 \cdot 10^{-134} \lor \neg \left(n \leq 51\right):\\
\;\;\;\;e^{-0.25 \cdot {n}^{2}}\\

\mathbf{else}:\\
\;\;\;\;\cos \left(\frac{K \cdot m}{2} - M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot \left(0.5 + \ell \cdot -0.16666666666666666\right) + -1\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if n < -4.80000000000000019e-134 or 51 < n

    1. Initial program 77.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 n around inf 55.3%

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

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{-0.25 \cdot {n}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg75.3%

        \[\leadsto \color{blue}{\cos M} \cdot e^{-0.25 \cdot {n}^{2}} \]
    6. Simplified75.3%

      \[\leadsto \color{blue}{\cos M} \cdot e^{-0.25 \cdot {n}^{2}} \]
    7. Taylor expanded in M around 0 75.3%

      \[\leadsto \color{blue}{e^{-0.25 \cdot {n}^{2}}} \]

    if -4.80000000000000019e-134 < n < 51

    1. Initial program 79.0%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in l around inf 43.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-neg43.0%

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

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

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

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

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

      \[\leadsto \cos \left(\frac{m \cdot K}{2} - M\right) \cdot \color{blue}{\left(1 + \ell \cdot \left(\ell \cdot \left(0.5 + -0.16666666666666666 \cdot \ell\right) - 1\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification51.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq -4.8 \cdot 10^{-134} \lor \neg \left(n \leq 51\right):\\ \;\;\;\;e^{-0.25 \cdot {n}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(\frac{K \cdot m}{2} - M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot \left(0.5 + \ell \cdot -0.16666666666666666\right) + -1\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 10.1% accurate, 3.5× speedup?

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

\\
\cos \left(\frac{K \cdot m}{2} - M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot \left(0.5 + \ell \cdot -0.16666666666666666\right) + -1\right)\right)
\end{array}
Derivation
  1. Initial program 78.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 l around inf 36.2%

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

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

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

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

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

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

    \[\leadsto \cos \left(\frac{m \cdot K}{2} - M\right) \cdot \color{blue}{\left(1 + \ell \cdot \left(\ell \cdot \left(0.5 + -0.16666666666666666 \cdot \ell\right) - 1\right)\right)} \]
  10. Final simplification12.5%

    \[\leadsto \cos \left(\frac{K \cdot m}{2} - M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot \left(0.5 + \ell \cdot -0.16666666666666666\right) + -1\right)\right) \]
  11. Add Preprocessing

Alternative 10: 9.3% accurate, 3.6× speedup?

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

\\
\cos \left(\frac{K \cdot m}{2} - M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot 0.5 + -1\right)\right)
\end{array}
Derivation
  1. Initial program 78.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 l around inf 36.2%

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

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

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

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

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

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

    \[\leadsto \cos \left(\frac{m \cdot K}{2} - M\right) \cdot \color{blue}{\left(1 + \ell \cdot \left(0.5 \cdot \ell - 1\right)\right)} \]
  10. Final simplification11.8%

    \[\leadsto \cos \left(\frac{K \cdot m}{2} - M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot 0.5 + -1\right)\right) \]
  11. Add Preprocessing

Alternative 11: 7.0% 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 78.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 l around inf 36.2%

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

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

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

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

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

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

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

Reproduce

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