Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.3% → 95.5%
Time: 13.6s
Alternatives: 11
Speedup: 2.4×

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: 76.3% 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: 95.5% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 1 \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{if}\;M \leq -1 \cdot 10^{+158}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;M \leq 1.52 \cdot 10^{+72}:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \mathsf{fma}\left(M \cdot M, -0.5, 1\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* 1.0 (exp (* M (- M))))))
   (if (<= M -1e+158)
     t_0
     (if (<= M 1.52e+72)
       (*
        (exp (- (- (fabs (- m n)) l) (pow (- (/ (+ m n) 2.0) M) 2.0)))
        (fma (* M M) -0.5 1.0))
       t_0))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = 1.0 * exp((M * -M));
	double tmp;
	if (M <= -1e+158) {
		tmp = t_0;
	} else if (M <= 1.52e+72) {
		tmp = exp(((fabs((m - n)) - l) - pow((((m + n) / 2.0) - M), 2.0))) * fma((M * M), -0.5, 1.0);
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(K, m, n, M, l)
	t_0 = Float64(1.0 * exp(Float64(M * Float64(-M))))
	tmp = 0.0
	if (M <= -1e+158)
		tmp = t_0;
	elseif (M <= 1.52e+72)
		tmp = Float64(exp(Float64(Float64(abs(Float64(m - n)) - l) - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0))) * fma(Float64(M * M), -0.5, 1.0));
	else
		tmp = t_0;
	end
	return tmp
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(1.0 * N[Exp[N[(M * (-M)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[M, -1e+158], t$95$0, If[LessEqual[M, 1.52e+72], N[(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] * N[(N[(M * M), $MachinePrecision] * -0.5 + 1.0), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 1 \cdot e^{M \cdot \left(-M\right)}\\
\mathbf{if}\;M \leq -1 \cdot 10^{+158}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;M \leq 1.52 \cdot 10^{+72}:\\
\;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \mathsf{fma}\left(M \cdot M, -0.5, 1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -9.99999999999999953e157 or 1.52e72 < M

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

      \[\leadsto \color{blue}{\cos \left(\mathsf{neg}\left(M\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. cos-negN/A

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{\mathsf{neg}\left({M}^{2}\right)}} \]
      2. unpow2N/A

        \[\leadsto \cos M \cdot e^{\mathsf{neg}\left(\color{blue}{M \cdot M}\right)} \]
      3. distribute-rgt-neg-inN/A

        \[\leadsto \cos M \cdot e^{\color{blue}{M \cdot \left(\mathsf{neg}\left(M\right)\right)}} \]
      4. lower-*.f64N/A

        \[\leadsto \cos M \cdot e^{\color{blue}{M \cdot \left(\mathsf{neg}\left(M\right)\right)}} \]
      5. lower-neg.f64100.0

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

      \[\leadsto \cos M \cdot e^{\color{blue}{M \cdot \left(-M\right)}} \]
    9. Taylor expanded in M around 0

      \[\leadsto 1 \cdot e^{M \cdot \left(\mathsf{neg}\left(M\right)\right)} \]
    10. Step-by-step derivation
      1. Applied rewrites100.0%

        \[\leadsto 1 \cdot e^{M \cdot \left(-M\right)} \]

      if -9.99999999999999953e157 < M < 1.52e72

      1. Initial program 71.9%

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

        \[\leadsto \color{blue}{\cos \left(\mathsf{neg}\left(M\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. cos-negN/A

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

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

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

        \[\leadsto \left(1 + \color{blue}{\frac{-1}{2} \cdot {M}^{2}}\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 rewrites94.2%

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

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

      Alternative 2: 95.9% accurate, 0.5× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{n \cdot 0.5 - M}{m}\\ t_1 := \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\ t_2 := \left|m - n\right|\\ t_3 := t\_2 - \ell\\ \mathbf{if}\;e^{t\_3 - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot t\_1 \leq 0.9999999999995:\\ \;\;\;\;t\_1 \cdot e^{t\_3 - m \cdot \left(m \cdot \mathsf{fma}\left(t\_0 + 1, t\_0, 0.25\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{e^{\mathsf{fma}\left(0.25, \left(m + n\right) \cdot \left(m + n\right), \ell\right) - t\_2}}\\ \end{array} \end{array} \]
      (FPCore (K m n M l)
       :precision binary64
       (let* ((t_0 (/ (- (* n 0.5) M) m))
              (t_1 (cos (- (/ (* K (+ m n)) 2.0) M)))
              (t_2 (fabs (- m n)))
              (t_3 (- t_2 l)))
         (if (<=
              (* (exp (- t_3 (pow (- (/ (+ m n) 2.0) M) 2.0))) t_1)
              0.9999999999995)
           (* t_1 (exp (- t_3 (* m (* m (fma (+ t_0 1.0) t_0 0.25))))))
           (/ 1.0 (exp (- (fma 0.25 (* (+ m n) (+ m n)) l) t_2))))))
      double code(double K, double m, double n, double M, double l) {
      	double t_0 = ((n * 0.5) - M) / m;
      	double t_1 = cos((((K * (m + n)) / 2.0) - M));
      	double t_2 = fabs((m - n));
      	double t_3 = t_2 - l;
      	double tmp;
      	if ((exp((t_3 - pow((((m + n) / 2.0) - M), 2.0))) * t_1) <= 0.9999999999995) {
      		tmp = t_1 * exp((t_3 - (m * (m * fma((t_0 + 1.0), t_0, 0.25)))));
      	} else {
      		tmp = 1.0 / exp((fma(0.25, ((m + n) * (m + n)), l) - t_2));
      	}
      	return tmp;
      }
      
      function code(K, m, n, M, l)
      	t_0 = Float64(Float64(Float64(n * 0.5) - M) / m)
      	t_1 = cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M))
      	t_2 = abs(Float64(m - n))
      	t_3 = Float64(t_2 - l)
      	tmp = 0.0
      	if (Float64(exp(Float64(t_3 - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0))) * t_1) <= 0.9999999999995)
      		tmp = Float64(t_1 * exp(Float64(t_3 - Float64(m * Float64(m * fma(Float64(t_0 + 1.0), t_0, 0.25))))));
      	else
      		tmp = Float64(1.0 / exp(Float64(fma(0.25, Float64(Float64(m + n) * Float64(m + n)), l) - t_2)));
      	end
      	return tmp
      end
      
      code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[(N[(n * 0.5), $MachinePrecision] - M), $MachinePrecision] / m), $MachinePrecision]}, Block[{t$95$1 = N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$3 = N[(t$95$2 - l), $MachinePrecision]}, If[LessEqual[N[(N[Exp[N[(t$95$3 - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * t$95$1), $MachinePrecision], 0.9999999999995], N[(t$95$1 * N[Exp[N[(t$95$3 - N[(m * N[(m * N[(N[(t$95$0 + 1.0), $MachinePrecision] * t$95$0 + 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(1.0 / N[Exp[N[(N[(0.25 * N[(N[(m + n), $MachinePrecision] * N[(m + n), $MachinePrecision]), $MachinePrecision] + l), $MachinePrecision] - t$95$2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      t_0 := \frac{n \cdot 0.5 - M}{m}\\
      t_1 := \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\
      t_2 := \left|m - n\right|\\
      t_3 := t\_2 - \ell\\
      \mathbf{if}\;e^{t\_3 - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot t\_1 \leq 0.9999999999995:\\
      \;\;\;\;t\_1 \cdot e^{t\_3 - m \cdot \left(m \cdot \mathsf{fma}\left(t\_0 + 1, t\_0, 0.25\right)\right)}\\
      
      \mathbf{else}:\\
      \;\;\;\;\frac{1}{e^{\mathsf{fma}\left(0.25, \left(m + n\right) \cdot \left(m + n\right), \ell\right) - t\_2}}\\
      
      
      \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.99999999999949996

        1. Initial program 96.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 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(\left(\frac{1}{4} + \left(\frac{1}{2} \cdot \frac{n}{m} + \frac{{\left(\frac{1}{2} \cdot n - M\right)}^{2}}{{m}^{2}}\right)\right) - \frac{M}{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(\left(\frac{1}{4} + \left(\frac{1}{2} \cdot \frac{n}{m} + \frac{{\left(\frac{1}{2} \cdot n - M\right)}^{2}}{{m}^{2}}\right)\right) - \frac{M}{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(\left(\frac{1}{4} + \left(\frac{1}{2} \cdot \frac{n}{m} + \frac{{\left(\frac{1}{2} \cdot n - M\right)}^{2}}{{m}^{2}}\right)\right) - \frac{M}{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(\left(\frac{1}{4} + \left(\frac{1}{2} \cdot \frac{n}{m} + \frac{{\left(\frac{1}{2} \cdot n - M\right)}^{2}}{{m}^{2}}\right)\right) - \frac{M}{m}\right)\right)}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
          4. lower-*.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}{\left(m \cdot \left(\left(\frac{1}{4} + \left(\frac{1}{2} \cdot \frac{n}{m} + \frac{{\left(\frac{1}{2} \cdot n - M\right)}^{2}}{{m}^{2}}\right)\right) - \frac{M}{m}\right)\right)}\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
          5. associate--l+N/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(\frac{1}{4} + \left(\left(\frac{1}{2} \cdot \frac{n}{m} + \frac{{\left(\frac{1}{2} \cdot n - M\right)}^{2}}{{m}^{2}}\right) - \frac{M}{m}\right)\right)}\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
          6. +-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(\left(\left(\frac{1}{2} \cdot \frac{n}{m} + \frac{{\left(\frac{1}{2} \cdot n - M\right)}^{2}}{{m}^{2}}\right) - \frac{M}{m}\right) + \frac{1}{4}\right)}\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
        5. Applied rewrites95.5%

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

        if 0.99999999999949996 < (*.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 27.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 M around inf

          \[\leadsto \cos \color{blue}{\left(M \cdot \left(\frac{1}{2} \cdot \frac{K \cdot \left(m + n\right)}{M} - 1\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. lower-*.f64N/A

            \[\leadsto \cos \color{blue}{\left(M \cdot \left(\frac{1}{2} \cdot \frac{K \cdot \left(m + n\right)}{M} - 1\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. sub-negN/A

            \[\leadsto \cos \left(M \cdot \color{blue}{\left(\frac{1}{2} \cdot \frac{K \cdot \left(m + n\right)}{M} + \left(\mathsf{neg}\left(1\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)} \]
          3. associate-*r/N/A

            \[\leadsto \cos \left(M \cdot \left(\color{blue}{\frac{\frac{1}{2} \cdot \left(K \cdot \left(m + n\right)\right)}{M}} + \left(\mathsf{neg}\left(1\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. *-commutativeN/A

            \[\leadsto \cos \left(M \cdot \left(\frac{\color{blue}{\left(K \cdot \left(m + n\right)\right) \cdot \frac{1}{2}}}{M} + \left(\mathsf{neg}\left(1\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)} \]
          5. associate-/l*N/A

            \[\leadsto \cos \left(M \cdot \left(\color{blue}{\left(K \cdot \left(m + n\right)\right) \cdot \frac{\frac{1}{2}}{M}} + \left(\mathsf{neg}\left(1\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)} \]
          6. metadata-evalN/A

            \[\leadsto \cos \left(M \cdot \left(\left(K \cdot \left(m + n\right)\right) \cdot \frac{\frac{1}{2}}{M} + \color{blue}{-1}\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 \cos \left(M \cdot \color{blue}{\mathsf{fma}\left(K \cdot \left(m + n\right), \frac{\frac{1}{2}}{M}, -1\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)} \]
          8. *-commutativeN/A

            \[\leadsto \cos \left(M \cdot \mathsf{fma}\left(\color{blue}{\left(m + n\right) \cdot K}, \frac{\frac{1}{2}}{M}, -1\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)} \]
          9. lower-*.f64N/A

            \[\leadsto \cos \left(M \cdot \mathsf{fma}\left(\color{blue}{\left(m + n\right) \cdot K}, \frac{\frac{1}{2}}{M}, -1\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)} \]
          10. +-commutativeN/A

            \[\leadsto \cos \left(M \cdot \mathsf{fma}\left(\color{blue}{\left(n + m\right)} \cdot K, \frac{\frac{1}{2}}{M}, -1\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)} \]
          11. lower-+.f64N/A

            \[\leadsto \cos \left(M \cdot \mathsf{fma}\left(\color{blue}{\left(n + m\right)} \cdot K, \frac{\frac{1}{2}}{M}, -1\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)} \]
          12. lower-/.f6427.3

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

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

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

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

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

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

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

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

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

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

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

            \[\leadsto \cos \left(M \cdot \mathsf{fma}\left(\left(n + m\right) \cdot K, \frac{\frac{1}{2}}{M}, -1\right)\right) \cdot \frac{1}{e^{\color{blue}{\left(\frac{m + n}{2} - M\right) \cdot \left(\frac{m + n}{2} - M\right)} + \left(\ell - \left|m - n\right|\right)}} \]
          11. lower-fma.f6427.3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            \[\leadsto \frac{\cos \left(\frac{1}{2} \cdot \left(\left(n + m\right) \cdot K\right)\right)}{e^{\mathsf{fma}\left(\frac{1}{4}, \left(n + m\right) \cdot \color{blue}{\left(n + m\right)}, \ell\right) - \left|m - n\right|}} \]
          18. fabs-subN/A

            \[\leadsto \frac{\cos \left(\frac{1}{2} \cdot \left(\left(n + m\right) \cdot K\right)\right)}{e^{\mathsf{fma}\left(\frac{1}{4}, \left(n + m\right) \cdot \left(n + m\right), \ell\right) - \color{blue}{\left|n - m\right|}}} \]
          19. sub-negN/A

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

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

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

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

          \[\leadsto \frac{1}{e^{\color{blue}{\mathsf{fma}\left(\frac{1}{4}, \left(n + m\right) \cdot \left(n + m\right), \ell\right) - \left|n - m\right|}}} \]
        12. Step-by-step derivation
          1. Applied rewrites96.8%

            \[\leadsto \frac{1}{e^{\color{blue}{\mathsf{fma}\left(0.25, \left(n + m\right) \cdot \left(n + m\right), \ell\right) - \left|n - m\right|}}} \]
        13. Recombined 2 regimes into one program.
        14. Final simplification95.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.9999999999995:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - m \cdot \left(m \cdot \mathsf{fma}\left(\frac{n \cdot 0.5 - M}{m} + 1, \frac{n \cdot 0.5 - M}{m}, 0.25\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{e^{\mathsf{fma}\left(0.25, \left(m + n\right) \cdot \left(m + n\right), \ell\right) - \left|m - n\right|}}\\ \end{array} \]
        15. Add Preprocessing

        Reproduce

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