Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.7% → 96.6%
Time: 14.7s
Alternatives: 12
Speedup: 2.7×

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

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := \left|m - n\right| - \ell\\ t_1 := e^{t\_0 - {\left(\frac{m + n}{2} - M\right)}^{2}}\\ t_2 := t\_1 \cdot \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\ t_3 := e^{t\_0 - M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(m + n\right) \cdot \left(m + n\right)}{M}, m\right)}{-M}, M\right)} \cdot 1\\ \mathbf{if}\;t\_2 \leq -0.2:\\ \;\;\;\;\cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \frac{2}{n}\right)} - M\right) \cdot e^{-\ell}\\ \mathbf{elif}\;t\_2 \leq 0.9705712466018273:\\ \;\;\;\;t\_3\\ \mathbf{elif}\;t\_2 \leq \infty:\\ \;\;\;\;t\_1 \cdot \mathsf{fma}\left(0.5, K \cdot \left(M \cdot \left(m + n\right)\right), 1\right)\\ \mathbf{else}:\\ \;\;\;\;t\_3\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (- (fabs (- m n)) l))
        (t_1 (exp (- t_0 (pow (- (/ (+ m n) 2.0) M) 2.0))))
        (t_2 (* t_1 (cos (- (/ (* K (+ m n)) 2.0) M))))
        (t_3
         (*
          (exp
           (-
            t_0
            (*
             M
             (fma
              M
              (/ (+ n (fma -0.25 (/ (* (+ m n) (+ m n)) M) m)) (- M))
              M))))
          1.0)))
   (if (<= t_2 -0.2)
     (* (cos (- (/ K (fma (/ m (* n n)) -2.0 (/ 2.0 n))) M)) (exp (- l)))
     (if (<= t_2 0.9705712466018273)
       t_3
       (if (<= t_2 INFINITY) (* t_1 (fma 0.5 (* K (* M (+ m n))) 1.0)) t_3)))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = fabs((m - n)) - l;
	double t_1 = exp((t_0 - pow((((m + n) / 2.0) - M), 2.0)));
	double t_2 = t_1 * cos((((K * (m + n)) / 2.0) - M));
	double t_3 = exp((t_0 - (M * fma(M, ((n + fma(-0.25, (((m + n) * (m + n)) / M), m)) / -M), M)))) * 1.0;
	double tmp;
	if (t_2 <= -0.2) {
		tmp = cos(((K / fma((m / (n * n)), -2.0, (2.0 / n))) - M)) * exp(-l);
	} else if (t_2 <= 0.9705712466018273) {
		tmp = t_3;
	} else if (t_2 <= ((double) INFINITY)) {
		tmp = t_1 * fma(0.5, (K * (M * (m + n))), 1.0);
	} else {
		tmp = t_3;
	}
	return tmp;
}
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(abs(Float64(m - n)) - l)
	t_1 = exp(Float64(t_0 - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)))
	t_2 = Float64(t_1 * cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)))
	t_3 = Float64(exp(Float64(t_0 - Float64(M * fma(M, Float64(Float64(n + fma(-0.25, Float64(Float64(Float64(m + n) * Float64(m + n)) / M), m)) / Float64(-M)), M)))) * 1.0)
	tmp = 0.0
	if (t_2 <= -0.2)
		tmp = Float64(cos(Float64(Float64(K / fma(Float64(m / Float64(n * n)), -2.0, Float64(2.0 / n))) - M)) * exp(Float64(-l)));
	elseif (t_2 <= 0.9705712466018273)
		tmp = t_3;
	elseif (t_2 <= Inf)
		tmp = Float64(t_1 * fma(0.5, Float64(K * Float64(M * Float64(m + n))), 1.0));
	else
		tmp = t_3;
	end
	return tmp
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, Block[{t$95$1 = N[Exp[N[(t$95$0 - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 * N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[Exp[N[(t$95$0 - N[(M * N[(M * N[(N[(n + N[(-0.25 * N[(N[(N[(m + n), $MachinePrecision] * N[(m + n), $MachinePrecision]), $MachinePrecision] / M), $MachinePrecision] + m), $MachinePrecision]), $MachinePrecision] / (-M)), $MachinePrecision] + M), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 1.0), $MachinePrecision]}, If[LessEqual[t$95$2, -0.2], N[(N[Cos[N[(N[(K / N[(N[(m / N[(n * n), $MachinePrecision]), $MachinePrecision] * -2.0 + N[(2.0 / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$2, 0.9705712466018273], t$95$3, If[LessEqual[t$95$2, Infinity], N[(t$95$1 * N[(0.5 * N[(K * N[(M * N[(m + n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], t$95$3]]]]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \left|m - n\right| - \ell\\
t_1 := e^{t\_0 - {\left(\frac{m + n}{2} - M\right)}^{2}}\\
t_2 := t\_1 \cdot \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\
t_3 := e^{t\_0 - M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(m + n\right) \cdot \left(m + n\right)}{M}, m\right)}{-M}, M\right)} \cdot 1\\
\mathbf{if}\;t\_2 \leq -0.2:\\
\;\;\;\;\cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \frac{2}{n}\right)} - M\right) \cdot e^{-\ell}\\

\mathbf{elif}\;t\_2 \leq 0.9705712466018273:\\
\;\;\;\;t\_3\\

\mathbf{elif}\;t\_2 \leq \infty:\\
\;\;\;\;t\_1 \cdot \mathsf{fma}\left(0.5, K \cdot \left(M \cdot \left(m + n\right)\right), 1\right)\\

\mathbf{else}:\\
\;\;\;\;t\_3\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 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.20000000000000001

    1. Initial program 39.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

      \[\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-negN/A

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{\mathsf{neg}\left(\ell\right)}} \]
      2. lower-neg.f6439.0

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
    6. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \cos \left(\color{blue}{\frac{K \cdot \left(m + n\right)}{2}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      2. lift-*.f64N/A

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

        \[\leadsto \cos \left(\color{blue}{K \cdot \frac{m + n}{2}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      4. clear-numN/A

        \[\leadsto \cos \left(K \cdot \color{blue}{\frac{1}{\frac{2}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      5. un-div-invN/A

        \[\leadsto \cos \left(\color{blue}{\frac{K}{\frac{2}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      6. lower-/.f64N/A

        \[\leadsto \cos \left(\color{blue}{\frac{K}{\frac{2}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      7. lift-+.f64N/A

        \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      8. +-commutativeN/A

        \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{n + m}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      9. lift-+.f64N/A

        \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{n + m}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      10. lower-/.f6443.4

        \[\leadsto \cos \left(\frac{K}{\color{blue}{\frac{2}{n + m}}} - M\right) \cdot e^{-\ell} \]
      11. lift-+.f64N/A

        \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{n + m}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      12. +-commutativeN/A

        \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      13. lift-+.f6443.4

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

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

      \[\leadsto \cos \left(\frac{K}{\color{blue}{-2 \cdot \frac{m}{{n}^{2}} + 2 \cdot \frac{1}{n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
    9. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos \left(\frac{K}{\color{blue}{\frac{m}{{n}^{2}} \cdot -2} + 2 \cdot \frac{1}{n}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      2. lower-fma.f64N/A

        \[\leadsto \cos \left(\frac{K}{\color{blue}{\mathsf{fma}\left(\frac{m}{{n}^{2}}, -2, 2 \cdot \frac{1}{n}\right)}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      3. lower-/.f64N/A

        \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\color{blue}{\frac{m}{{n}^{2}}}, -2, 2 \cdot \frac{1}{n}\right)} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      4. unpow2N/A

        \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{\color{blue}{n \cdot n}}, -2, 2 \cdot \frac{1}{n}\right)} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      5. lower-*.f64N/A

        \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{\color{blue}{n \cdot n}}, -2, 2 \cdot \frac{1}{n}\right)} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      6. associate-*r/N/A

        \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \color{blue}{\frac{2 \cdot 1}{n}}\right)} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      7. metadata-evalN/A

        \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \frac{\color{blue}{2}}{n}\right)} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      8. lower-/.f6470.2

        \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \color{blue}{\frac{2}{n}}\right)} - M\right) \cdot e^{-\ell} \]
    10. Applied rewrites70.2%

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

    if -0.20000000000000001 < (*.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.97057124660182725 or +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 76.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 M around -inf

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(\color{blue}{M \cdot \left(M \cdot \left(1 + -1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M}\right)\right)}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. lower-*.f64N/A

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(\color{blue}{M \cdot \left(M \cdot \left(1 + -1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M}\right)\right)}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      4. +-commutativeN/A

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(M \cdot \left(M \cdot \color{blue}{\left(-1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M} + 1\right)}\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      5. distribute-lft-inN/A

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(M \cdot \color{blue}{\left(M \cdot \left(-1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M}\right) + M \cdot 1\right)}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      6. *-rgt-identityN/A

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(M \cdot \left(M \cdot \left(-1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M}\right) + \color{blue}{M}\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      7. lower-fma.f64N/A

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(M \cdot \color{blue}{\mathsf{fma}\left(M, -1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M}, M\right)}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
    5. Applied rewrites76.2%

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

      \[\leadsto \color{blue}{\cos \left(\mathsf{neg}\left(M\right)\right)} \cdot e^{\left(\mathsf{neg}\left(M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(\frac{-1}{4}, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{\mathsf{neg}\left(M\right)}, M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\mathsf{neg}\left(M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(\frac{-1}{4}, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{\mathsf{neg}\left(M\right)}, M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. lower-cos.f6498.0

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{-M}, M\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
    8. Applied rewrites98.0%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{-M}, M\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
    9. Taylor expanded in M around 0

      \[\leadsto 1 \cdot e^{\left(\mathsf{neg}\left(M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(\frac{-1}{4}, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{\mathsf{neg}\left(M\right)}, M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
    10. Step-by-step derivation
      1. Applied rewrites98.0%

        \[\leadsto 1 \cdot e^{\left(-M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{-M}, M\right)\right) - \left(\ell - \left|m - n\right|\right)} \]

      if 0.97057124660182725 < (*.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 89.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 K around 0

        \[\leadsto \color{blue}{\left(\cos \left(\mathsf{neg}\left(M\right)\right) + \frac{-1}{2} \cdot \left(K \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right)\right)} \cdot e^{\left(\mathsf{neg}\left({\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      4. Step-by-step derivation
        1. +-commutativeN/A

          \[\leadsto \color{blue}{\left(\frac{-1}{2} \cdot \left(K \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right) + \cos \left(\mathsf{neg}\left(M\right)\right)\right)} \cdot e^{\left(\mathsf{neg}\left({\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
        2. associate-*r*N/A

          \[\leadsto \left(\color{blue}{\left(\frac{-1}{2} \cdot K\right) \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)} + \cos \left(\mathsf{neg}\left(M\right)\right)\right) \cdot e^{\left(\mathsf{neg}\left({\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
        3. *-commutativeN/A

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

          \[\leadsto \left(\color{blue}{K \cdot \left(\frac{-1}{2} \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right)} + \cos \left(\mathsf{neg}\left(M\right)\right)\right) \cdot e^{\left(\mathsf{neg}\left({\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
        5. metadata-evalN/A

          \[\leadsto \left(K \cdot \left(\color{blue}{\left(\mathsf{neg}\left(\frac{1}{2}\right)\right)} \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right) + \cos \left(\mathsf{neg}\left(M\right)\right)\right) \cdot e^{\left(\mathsf{neg}\left({\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
        6. distribute-lft-neg-inN/A

          \[\leadsto \left(K \cdot \color{blue}{\left(\mathsf{neg}\left(\frac{1}{2} \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right)\right)} + \cos \left(\mathsf{neg}\left(M\right)\right)\right) \cdot e^{\left(\mathsf{neg}\left({\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
        7. lower-fma.f64N/A

          \[\leadsto \color{blue}{\mathsf{fma}\left(K, \mathsf{neg}\left(\frac{1}{2} \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right), \cos \left(\mathsf{neg}\left(M\right)\right)\right)} \cdot e^{\left(\mathsf{neg}\left({\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      5. Applied rewrites96.6%

        \[\leadsto \color{blue}{\mathsf{fma}\left(K, \left(n + m\right) \cdot \left(0.5 \cdot \sin M\right), \cos M\right)} \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

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

          \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{\left(M \cdot \left(n + m\right)\right) \cdot K}, 1\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      8. Recombined 3 regimes into one program.
      9. Final simplification96.6%

        \[\leadsto \begin{array}{l} \mathbf{if}\;e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \leq -0.2:\\ \;\;\;\;\cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \frac{2}{n}\right)} - M\right) \cdot e^{-\ell}\\ \mathbf{elif}\;e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \leq 0.9705712466018273:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(m + n\right) \cdot \left(m + n\right)}{M}, m\right)}{-M}, M\right)} \cdot 1\\ \mathbf{elif}\;e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \leq \infty:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \mathsf{fma}\left(0.5, K \cdot \left(M \cdot \left(m + n\right)\right), 1\right)\\ \mathbf{else}:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(m + n\right) \cdot \left(m + n\right)}{M}, m\right)}{-M}, M\right)} \cdot 1\\ \end{array} \]
      10. Add Preprocessing

      Alternative 2: 96.9% accurate, 0.3× speedup?

      \[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := e^{-\ell}\\ t_1 := \left|m - n\right| - \ell\\ t_2 := e^{t\_1 - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\ t_3 := e^{t\_1 - M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(m + n\right) \cdot \left(m + n\right)}{M}, m\right)}{-M}, M\right)} \cdot 1\\ \mathbf{if}\;t\_2 \leq -0.2:\\ \;\;\;\;\cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \frac{2}{n}\right)} - M\right) \cdot t\_0\\ \mathbf{elif}\;t\_2 \leq 0:\\ \;\;\;\;t\_3\\ \mathbf{elif}\;t\_2 \leq \infty:\\ \;\;\;\;t\_0 \cdot \cos \left(\frac{K}{\frac{2}{m}} - M\right)\\ \mathbf{else}:\\ \;\;\;\;t\_3\\ \end{array} \end{array} \]
      NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
      (FPCore (K m n M l)
       :precision binary64
       (let* ((t_0 (exp (- l)))
              (t_1 (- (fabs (- m n)) l))
              (t_2
               (*
                (exp (- t_1 (pow (- (/ (+ m n) 2.0) M) 2.0)))
                (cos (- (/ (* K (+ m n)) 2.0) M))))
              (t_3
               (*
                (exp
                 (-
                  t_1
                  (*
                   M
                   (fma
                    M
                    (/ (+ n (fma -0.25 (/ (* (+ m n) (+ m n)) M) m)) (- M))
                    M))))
                1.0)))
         (if (<= t_2 -0.2)
           (* (cos (- (/ K (fma (/ m (* n n)) -2.0 (/ 2.0 n))) M)) t_0)
           (if (<= t_2 0.0)
             t_3
             (if (<= t_2 INFINITY) (* t_0 (cos (- (/ K (/ 2.0 m)) M))) t_3)))))
      assert(K < m && m < n && n < M && M < l);
      double code(double K, double m, double n, double M, double l) {
      	double t_0 = exp(-l);
      	double t_1 = fabs((m - n)) - l;
      	double t_2 = exp((t_1 - pow((((m + n) / 2.0) - M), 2.0))) * cos((((K * (m + n)) / 2.0) - M));
      	double t_3 = exp((t_1 - (M * fma(M, ((n + fma(-0.25, (((m + n) * (m + n)) / M), m)) / -M), M)))) * 1.0;
      	double tmp;
      	if (t_2 <= -0.2) {
      		tmp = cos(((K / fma((m / (n * n)), -2.0, (2.0 / n))) - M)) * t_0;
      	} else if (t_2 <= 0.0) {
      		tmp = t_3;
      	} else if (t_2 <= ((double) INFINITY)) {
      		tmp = t_0 * cos(((K / (2.0 / m)) - M));
      	} else {
      		tmp = t_3;
      	}
      	return tmp;
      }
      
      K, m, n, M, l = sort([K, m, n, M, l])
      function code(K, m, n, M, l)
      	t_0 = exp(Float64(-l))
      	t_1 = Float64(abs(Float64(m - n)) - l)
      	t_2 = Float64(exp(Float64(t_1 - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0))) * cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)))
      	t_3 = Float64(exp(Float64(t_1 - Float64(M * fma(M, Float64(Float64(n + fma(-0.25, Float64(Float64(Float64(m + n) * Float64(m + n)) / M), m)) / Float64(-M)), M)))) * 1.0)
      	tmp = 0.0
      	if (t_2 <= -0.2)
      		tmp = Float64(cos(Float64(Float64(K / fma(Float64(m / Float64(n * n)), -2.0, Float64(2.0 / n))) - M)) * t_0);
      	elseif (t_2 <= 0.0)
      		tmp = t_3;
      	elseif (t_2 <= Inf)
      		tmp = Float64(t_0 * cos(Float64(Float64(K / Float64(2.0 / m)) - M)));
      	else
      		tmp = t_3;
      	end
      	return tmp
      end
      
      NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
      code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Exp[(-l)], $MachinePrecision]}, Block[{t$95$1 = N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, Block[{t$95$2 = N[(N[Exp[N[(t$95$1 - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[Exp[N[(t$95$1 - N[(M * N[(M * N[(N[(n + N[(-0.25 * N[(N[(N[(m + n), $MachinePrecision] * N[(m + n), $MachinePrecision]), $MachinePrecision] / M), $MachinePrecision] + m), $MachinePrecision]), $MachinePrecision] / (-M)), $MachinePrecision] + M), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 1.0), $MachinePrecision]}, If[LessEqual[t$95$2, -0.2], N[(N[Cos[N[(N[(K / N[(N[(m / N[(n * n), $MachinePrecision]), $MachinePrecision] * -2.0 + N[(2.0 / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * t$95$0), $MachinePrecision], If[LessEqual[t$95$2, 0.0], t$95$3, If[LessEqual[t$95$2, Infinity], N[(t$95$0 * N[Cos[N[(N[(K / N[(2.0 / m), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$3]]]]]]]
      
      \begin{array}{l}
      [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
      \\
      \begin{array}{l}
      t_0 := e^{-\ell}\\
      t_1 := \left|m - n\right| - \ell\\
      t_2 := e^{t\_1 - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\
      t_3 := e^{t\_1 - M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(m + n\right) \cdot \left(m + n\right)}{M}, m\right)}{-M}, M\right)} \cdot 1\\
      \mathbf{if}\;t\_2 \leq -0.2:\\
      \;\;\;\;\cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \frac{2}{n}\right)} - M\right) \cdot t\_0\\
      
      \mathbf{elif}\;t\_2 \leq 0:\\
      \;\;\;\;t\_3\\
      
      \mathbf{elif}\;t\_2 \leq \infty:\\
      \;\;\;\;t\_0 \cdot \cos \left(\frac{K}{\frac{2}{m}} - M\right)\\
      
      \mathbf{else}:\\
      \;\;\;\;t\_3\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 3 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.20000000000000001

        1. Initial program 48.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 l around inf

          \[\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-negN/A

            \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{\mathsf{neg}\left(\ell\right)}} \]
          2. lower-neg.f6448.4

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

          \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
        6. Step-by-step derivation
          1. lift-/.f64N/A

            \[\leadsto \cos \left(\color{blue}{\frac{K \cdot \left(m + n\right)}{2}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          2. lift-*.f64N/A

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

            \[\leadsto \cos \left(\color{blue}{K \cdot \frac{m + n}{2}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          4. clear-numN/A

            \[\leadsto \cos \left(K \cdot \color{blue}{\frac{1}{\frac{2}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          5. un-div-invN/A

            \[\leadsto \cos \left(\color{blue}{\frac{K}{\frac{2}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          6. lower-/.f64N/A

            \[\leadsto \cos \left(\color{blue}{\frac{K}{\frac{2}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          7. lift-+.f64N/A

            \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          8. +-commutativeN/A

            \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{n + m}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          9. lift-+.f64N/A

            \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{n + m}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          10. lower-/.f6448.7

            \[\leadsto \cos \left(\frac{K}{\color{blue}{\frac{2}{n + m}}} - M\right) \cdot e^{-\ell} \]
          11. lift-+.f64N/A

            \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{n + m}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          12. +-commutativeN/A

            \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          13. lift-+.f6448.7

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

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

          \[\leadsto \cos \left(\frac{K}{\color{blue}{-2 \cdot \frac{m}{{n}^{2}} + 2 \cdot \frac{1}{n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
        9. Step-by-step derivation
          1. *-commutativeN/A

            \[\leadsto \cos \left(\frac{K}{\color{blue}{\frac{m}{{n}^{2}} \cdot -2} + 2 \cdot \frac{1}{n}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          2. lower-fma.f64N/A

            \[\leadsto \cos \left(\frac{K}{\color{blue}{\mathsf{fma}\left(\frac{m}{{n}^{2}}, -2, 2 \cdot \frac{1}{n}\right)}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          3. lower-/.f64N/A

            \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\color{blue}{\frac{m}{{n}^{2}}}, -2, 2 \cdot \frac{1}{n}\right)} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          4. unpow2N/A

            \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{\color{blue}{n \cdot n}}, -2, 2 \cdot \frac{1}{n}\right)} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          5. lower-*.f64N/A

            \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{\color{blue}{n \cdot n}}, -2, 2 \cdot \frac{1}{n}\right)} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          6. associate-*r/N/A

            \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \color{blue}{\frac{2 \cdot 1}{n}}\right)} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          7. metadata-evalN/A

            \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \frac{\color{blue}{2}}{n}\right)} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          8. lower-/.f6443.5

            \[\leadsto \cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \color{blue}{\frac{2}{n}}\right)} - M\right) \cdot e^{-\ell} \]
        10. Applied rewrites43.5%

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

        if -0.20000000000000001 < (*.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.0 or +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 75.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 M around -inf

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

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

            \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(\color{blue}{M \cdot \left(M \cdot \left(1 + -1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M}\right)\right)}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
          3. lower-*.f64N/A

            \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(\color{blue}{M \cdot \left(M \cdot \left(1 + -1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M}\right)\right)}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
          4. +-commutativeN/A

            \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(M \cdot \left(M \cdot \color{blue}{\left(-1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M} + 1\right)}\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
          5. distribute-lft-inN/A

            \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(M \cdot \color{blue}{\left(M \cdot \left(-1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M}\right) + M \cdot 1\right)}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
          6. *-rgt-identityN/A

            \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(M \cdot \left(M \cdot \left(-1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M}\right) + \color{blue}{M}\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
          7. lower-fma.f64N/A

            \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\mathsf{neg}\left(M \cdot \color{blue}{\mathsf{fma}\left(M, -1 \cdot \frac{m + \left(n + \frac{-1}{4} \cdot \frac{{\left(m + n\right)}^{2}}{M}\right)}{M}, M\right)}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
        5. Applied rewrites75.5%

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

          \[\leadsto \color{blue}{\cos \left(\mathsf{neg}\left(M\right)\right)} \cdot e^{\left(\mathsf{neg}\left(M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(\frac{-1}{4}, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{\mathsf{neg}\left(M\right)}, M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
        7. Step-by-step derivation
          1. cos-negN/A

            \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\mathsf{neg}\left(M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(\frac{-1}{4}, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{\mathsf{neg}\left(M\right)}, M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
          2. lower-cos.f6499.6

            \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{-M}, M\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
        8. Applied rewrites99.6%

          \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{-M}, M\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
        9. Taylor expanded in M around 0

          \[\leadsto 1 \cdot e^{\left(\mathsf{neg}\left(M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(\frac{-1}{4}, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{\mathsf{neg}\left(M\right)}, M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
        10. Step-by-step derivation
          1. Applied rewrites99.5%

            \[\leadsto 1 \cdot e^{\left(-M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(n + m\right) \cdot \left(n + m\right)}{M}, m\right)}{-M}, M\right)\right) - \left(\ell - \left|m - n\right|\right)} \]

          if -0.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)))))) < +inf.0

          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

            \[\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-negN/A

              \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{\mathsf{neg}\left(\ell\right)}} \]
            2. lower-neg.f6483.8

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

            \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
          6. Step-by-step derivation
            1. lift-/.f64N/A

              \[\leadsto \cos \left(\color{blue}{\frac{K \cdot \left(m + n\right)}{2}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
            2. lift-*.f64N/A

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

              \[\leadsto \cos \left(\color{blue}{K \cdot \frac{m + n}{2}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
            4. clear-numN/A

              \[\leadsto \cos \left(K \cdot \color{blue}{\frac{1}{\frac{2}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
            5. un-div-invN/A

              \[\leadsto \cos \left(\color{blue}{\frac{K}{\frac{2}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
            6. lower-/.f64N/A

              \[\leadsto \cos \left(\color{blue}{\frac{K}{\frac{2}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
            7. lift-+.f64N/A

              \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
            8. +-commutativeN/A

              \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{n + m}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
            9. lift-+.f64N/A

              \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{n + m}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
            10. lower-/.f6483.8

              \[\leadsto \cos \left(\frac{K}{\color{blue}{\frac{2}{n + m}}} - M\right) \cdot e^{-\ell} \]
            11. lift-+.f64N/A

              \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{n + m}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
            12. +-commutativeN/A

              \[\leadsto \cos \left(\frac{K}{\frac{2}{\color{blue}{m + n}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
            13. lift-+.f6483.8

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

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

            \[\leadsto \cos \left(\frac{K}{\color{blue}{\frac{2}{m}}} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          9. Step-by-step derivation
            1. lower-/.f6484.8

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

            \[\leadsto \cos \left(\frac{K}{\color{blue}{\frac{2}{m}}} - M\right) \cdot e^{-\ell} \]
        11. Recombined 3 regimes into one program.
        12. Final simplification96.9%

          \[\leadsto \begin{array}{l} \mathbf{if}\;e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \leq -0.2:\\ \;\;\;\;\cos \left(\frac{K}{\mathsf{fma}\left(\frac{m}{n \cdot n}, -2, \frac{2}{n}\right)} - M\right) \cdot e^{-\ell}\\ \mathbf{elif}\;e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \leq 0:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(m + n\right) \cdot \left(m + n\right)}{M}, m\right)}{-M}, M\right)} \cdot 1\\ \mathbf{elif}\;e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \leq \infty:\\ \;\;\;\;e^{-\ell} \cdot \cos \left(\frac{K}{\frac{2}{m}} - M\right)\\ \mathbf{else}:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - M \cdot \mathsf{fma}\left(M, \frac{n + \mathsf{fma}\left(-0.25, \frac{\left(m + n\right) \cdot \left(m + n\right)}{M}, m\right)}{-M}, M\right)} \cdot 1\\ \end{array} \]
        13. Add Preprocessing

        Reproduce

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