Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.2% → 96.8%
Time: 14.6s
Alternatives: 5
Speedup: 2.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 5 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.2% accurate, 1.0× speedup?

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

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

Alternative 1: 96.8% accurate, 1.0× speedup?

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

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

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

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

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

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

Alternative 2: 36.6% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{-\ell}\\ \mathbf{if}\;K \leq -5.9 \cdot 10^{-242}:\\ \;\;\;\;\frac{\cos M}{e^{\ell}}\\ \mathbf{elif}\;K \leq 3 \cdot 10^{-152}:\\ \;\;\;\;-0.5 \cdot \left(K \cdot \left(m \cdot \left(t\_0 \cdot \sin \left(0.5 \cdot \left(n \cdot K\right) - M\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (exp (- l))))
   (if (<= K -5.9e-242)
     (/ (cos M) (exp l))
     (if (<= K 3e-152)
       (* -0.5 (* K (* m (* t_0 (sin (- (* 0.5 (* n K)) M))))))
       (* (cos M) t_0)))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = exp(-l);
	double tmp;
	if (K <= -5.9e-242) {
		tmp = cos(M) / exp(l);
	} else if (K <= 3e-152) {
		tmp = -0.5 * (K * (m * (t_0 * sin(((0.5 * (n * K)) - M)))));
	} else {
		tmp = cos(M) * t_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 = exp(-l)
    if (k <= (-5.9d-242)) then
        tmp = cos(m_1) / exp(l)
    else if (k <= 3d-152) then
        tmp = (-0.5d0) * (k * (m * (t_0 * sin(((0.5d0 * (n * k)) - m_1)))))
    else
        tmp = cos(m_1) * t_0
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.exp(-l);
	double tmp;
	if (K <= -5.9e-242) {
		tmp = Math.cos(M) / Math.exp(l);
	} else if (K <= 3e-152) {
		tmp = -0.5 * (K * (m * (t_0 * Math.sin(((0.5 * (n * K)) - M)))));
	} else {
		tmp = Math.cos(M) * t_0;
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.exp(-l)
	tmp = 0
	if K <= -5.9e-242:
		tmp = math.cos(M) / math.exp(l)
	elif K <= 3e-152:
		tmp = -0.5 * (K * (m * (t_0 * math.sin(((0.5 * (n * K)) - M)))))
	else:
		tmp = math.cos(M) * t_0
	return tmp
function code(K, m, n, M, l)
	t_0 = exp(Float64(-l))
	tmp = 0.0
	if (K <= -5.9e-242)
		tmp = Float64(cos(M) / exp(l));
	elseif (K <= 3e-152)
		tmp = Float64(-0.5 * Float64(K * Float64(m * Float64(t_0 * sin(Float64(Float64(0.5 * Float64(n * K)) - M))))));
	else
		tmp = Float64(cos(M) * t_0);
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = exp(-l);
	tmp = 0.0;
	if (K <= -5.9e-242)
		tmp = cos(M) / exp(l);
	elseif (K <= 3e-152)
		tmp = -0.5 * (K * (m * (t_0 * sin(((0.5 * (n * K)) - M)))));
	else
		tmp = cos(M) * t_0;
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Exp[(-l)], $MachinePrecision]}, If[LessEqual[K, -5.9e-242], N[(N[Cos[M], $MachinePrecision] / N[Exp[l], $MachinePrecision]), $MachinePrecision], If[LessEqual[K, 3e-152], N[(-0.5 * N[(K * N[(m * N[(t$95$0 * N[Sin[N[(N[(0.5 * N[(n * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * t$95$0), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{-\ell}\\
\mathbf{if}\;K \leq -5.9 \cdot 10^{-242}:\\
\;\;\;\;\frac{\cos M}{e^{\ell}}\\

\mathbf{elif}\;K \leq 3 \cdot 10^{-152}:\\
\;\;\;\;-0.5 \cdot \left(K \cdot \left(m \cdot \left(t\_0 \cdot \sin \left(0.5 \cdot \left(n \cdot K\right) - M\right)\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if K < -5.89999999999999999e-242

    1. Initial program 72.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 l around inf 26.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-neg26.2%

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

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

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

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

      \[\leadsto \color{blue}{\cos M \cdot e^{-\ell}} \]
    9. Taylor expanded in l around -inf 33.3%

      \[\leadsto \color{blue}{\cos M \cdot e^{-1 \cdot \ell}} \]
    10. Step-by-step derivation
      1. neg-mul-133.3%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
      2. exp-neg33.3%

        \[\leadsto \cos M \cdot \color{blue}{\frac{1}{e^{\ell}}} \]
      3. associate-*r/33.3%

        \[\leadsto \color{blue}{\frac{\cos M \cdot 1}{e^{\ell}}} \]
      4. *-rgt-identity33.3%

        \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
    11. Simplified33.3%

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

    if -5.89999999999999999e-242 < K < 3e-152

    1. Initial program 100.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 m around 0 100.0%

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

        \[\leadsto \left(\cos \left(0.5 \cdot \color{blue}{\left(n \cdot K\right)} - M\right) + -0.5 \cdot \left(K \cdot \left(m \cdot \sin \left(0.5 \cdot \left(K \cdot n\right) - M\right)\right)\right)\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r*100.0%

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

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

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

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

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

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

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

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

    if 3e-152 < K

    1. Initial program 61.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 35.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-neg35.3%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;K \leq -5.9 \cdot 10^{-242}:\\ \;\;\;\;\frac{\cos M}{e^{\ell}}\\ \mathbf{elif}\;K \leq 3 \cdot 10^{-152}:\\ \;\;\;\;-0.5 \cdot \left(K \cdot \left(m \cdot \left(e^{-\ell} \cdot \sin \left(0.5 \cdot \left(n \cdot K\right) - M\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 96.4% accurate, 2.0× speedup?

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

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

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

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

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

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

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{-\left({\left(\mathsf{fma}\left(m + n, \color{blue}{0.5}, -M\right)\right)}^{2} + \left(\ell - \left|m - n\right|\right)\right)} \]
    6. add-sqr-sqrt32.2%

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{-\left({\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(\ell - \color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right)\right)} \]
    8. add-sqr-sqrt72.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\left(-\ell\right) + \left(\left(-\color{blue}{\left(-m\right)}\right) + \left(-n\right)\right)\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \]
    14. remove-double-neg72.5%

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

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

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

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

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

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

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

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

Alternative 4: 35.0% accurate, 2.1× speedup?

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

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

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

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

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

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

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

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

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

Alternative 5: 34.7% accurate, 4.2× speedup?

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos M \cdot e^{-\ell}} \]
  9. Taylor expanded in M around 0 34.1%

    \[\leadsto \color{blue}{e^{-\ell}} \]
  10. Add Preprocessing

Reproduce

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