Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.6% → 96.5%
Time: 14.4s
Alternatives: 9
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 9 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.6% accurate, 1.0× speedup?

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

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

Alternative 1: 96.5% accurate, 1.0× speedup?

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

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

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

    \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  4. Step-by-step derivation
    1. cos-neg96.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. Simplified96.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. Final simplification96.0%

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

Alternative 2: 95.5% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -27.5 \lor \neg \left(M \leq 31.5\right):\\
\;\;\;\;e^{-{M}^{2}}\\

\mathbf{else}:\\
\;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}\\


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

    1. Initial program 77.1%

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

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

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

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

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

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

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

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

    if -27.5 < M < 31.5

    1. Initial program 67.0%

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

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. cos-neg93.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. Simplified93.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 93.7%

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. fabs-sub93.7%

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+93.7%

        \[\leadsto e^{\color{blue}{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    8. Simplified93.7%

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

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

Alternative 3: 84.5% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;m \leq -7.2 \cdot 10^{-9}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n - m\right) - \left(\ell + 0.25 \cdot \left(n \cdot n\right)\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= m -7.2e-9)
   (* (cos M) (exp (* -0.25 (pow m 2.0))))
   (exp (- (- n m) (+ l (* 0.25 (* n n)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -7.2e-9) {
		tmp = cos(M) * exp((-0.25 * pow(m, 2.0)));
	} else {
		tmp = exp(((n - m) - (l + (0.25 * (n * n)))));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (m <= (-7.2d-9)) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m ** 2.0d0)))
    else
        tmp = exp(((n - m) - (l + (0.25d0 * (n * n)))))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -7.2e-9) {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(m, 2.0)));
	} else {
		tmp = Math.exp(((n - m) - (l + (0.25 * (n * n)))));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if m <= -7.2e-9:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(m, 2.0)))
	else:
		tmp = math.exp(((n - m) - (l + (0.25 * (n * n)))))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (m <= -7.2e-9)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (m ^ 2.0))));
	else
		tmp = exp(Float64(Float64(n - m) - Float64(l + Float64(0.25 * Float64(n * n)))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (m <= -7.2e-9)
		tmp = cos(M) * exp((-0.25 * (m ^ 2.0)));
	else
		tmp = exp(((n - m) - (l + (0.25 * (n * n)))));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[m, -7.2e-9], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[m, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Exp[N[(N[(n - m), $MachinePrecision] - N[(l + N[(0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;m \leq -7.2 \cdot 10^{-9}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\

\mathbf{else}:\\
\;\;\;\;e^{\left(n - m\right) - \left(\ell + 0.25 \cdot \left(n \cdot n\right)\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if m < -7.2e-9

    1. Initial program 68.6%

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

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

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

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

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

    if -7.2e-9 < m

    1. Initial program 72.3%

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

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

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

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

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. fabs-sub81.9%

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+81.9%

        \[\leadsto e^{\color{blue}{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    8. Simplified81.9%

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

      \[\leadsto e^{\color{blue}{\left|n - m\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
    10. Step-by-step derivation
      1. rem-square-sqrt31.5%

        \[\leadsto e^{\left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
      2. fabs-sqr31.5%

        \[\leadsto e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
      3. rem-square-sqrt80.3%

        \[\leadsto e^{\color{blue}{\left(n - m\right)} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
    11. Simplified80.3%

      \[\leadsto e^{\color{blue}{\left(n - m\right) - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
    12. Step-by-step derivation
      1. unpow280.3%

        \[\leadsto e^{\left(n - m\right) - \left(\ell + 0.25 \cdot \color{blue}{\left(n \cdot n\right)}\right)} \]
    13. Applied egg-rr80.3%

      \[\leadsto e^{\left(n - m\right) - \left(\ell + 0.25 \cdot \color{blue}{\left(n \cdot n\right)}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 84.5% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;m \leq -7.2 \cdot 10^{-9}:\\ \;\;\;\;e^{-0.25 \cdot {m}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n - m\right) - \left(\ell + 0.25 \cdot \left(n \cdot n\right)\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= m -7.2e-9)
   (exp (* -0.25 (pow m 2.0)))
   (exp (- (- n m) (+ l (* 0.25 (* n n)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -7.2e-9) {
		tmp = exp((-0.25 * pow(m, 2.0)));
	} else {
		tmp = exp(((n - m) - (l + (0.25 * (n * n)))));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (m <= (-7.2d-9)) then
        tmp = exp(((-0.25d0) * (m ** 2.0d0)))
    else
        tmp = exp(((n - m) - (l + (0.25d0 * (n * n)))))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -7.2e-9) {
		tmp = Math.exp((-0.25 * Math.pow(m, 2.0)));
	} else {
		tmp = Math.exp(((n - m) - (l + (0.25 * (n * n)))));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if m <= -7.2e-9:
		tmp = math.exp((-0.25 * math.pow(m, 2.0)))
	else:
		tmp = math.exp(((n - m) - (l + (0.25 * (n * n)))))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (m <= -7.2e-9)
		tmp = exp(Float64(-0.25 * (m ^ 2.0)));
	else
		tmp = exp(Float64(Float64(n - m) - Float64(l + Float64(0.25 * Float64(n * n)))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (m <= -7.2e-9)
		tmp = exp((-0.25 * (m ^ 2.0)));
	else
		tmp = exp(((n - m) - (l + (0.25 * (n * n)))));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[m, -7.2e-9], N[Exp[N[(-0.25 * N[Power[m, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Exp[N[(N[(n - m), $MachinePrecision] - N[(l + N[(0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;m \leq -7.2 \cdot 10^{-9}:\\
\;\;\;\;e^{-0.25 \cdot {m}^{2}}\\

\mathbf{else}:\\
\;\;\;\;e^{\left(n - m\right) - \left(\ell + 0.25 \cdot \left(n \cdot n\right)\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if m < -7.2e-9

    1. Initial program 68.6%

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

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

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

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

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. fabs-sub100.0%

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+100.0%

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

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

      \[\leadsto e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    10. Step-by-step derivation
      1. *-commutative100.0%

        \[\leadsto e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
    11. Simplified100.0%

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

    if -7.2e-9 < m

    1. Initial program 72.3%

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

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

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

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

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. fabs-sub81.9%

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+81.9%

        \[\leadsto e^{\color{blue}{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    8. Simplified81.9%

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

      \[\leadsto e^{\color{blue}{\left|n - m\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
    10. Step-by-step derivation
      1. rem-square-sqrt31.5%

        \[\leadsto e^{\left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
      2. fabs-sqr31.5%

        \[\leadsto e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
      3. rem-square-sqrt80.3%

        \[\leadsto e^{\color{blue}{\left(n - m\right)} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
    11. Simplified80.3%

      \[\leadsto e^{\color{blue}{\left(n - m\right) - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
    12. Step-by-step derivation
      1. unpow280.3%

        \[\leadsto e^{\left(n - m\right) - \left(\ell + 0.25 \cdot \color{blue}{\left(n \cdot n\right)}\right)} \]
    13. Applied egg-rr80.3%

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

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

Alternative 5: 68.7% accurate, 3.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;n \leq -6.2 \cdot 10^{-22} \lor \neg \left(n \leq 3.4 \cdot 10^{-13}\right):\\ \;\;\;\;e^{-0.25 \cdot \left(n \cdot n\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= n -6.2e-22) (not (<= n 3.4e-13)))
   (exp (* -0.25 (* n n)))
   (exp (- l))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((n <= -6.2e-22) || !(n <= 3.4e-13)) {
		tmp = exp((-0.25 * (n * n)));
	} else {
		tmp = exp(-l);
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if ((n <= (-6.2d-22)) .or. (.not. (n <= 3.4d-13))) then
        tmp = exp(((-0.25d0) * (n * n)))
    else
        tmp = exp(-l)
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((n <= -6.2e-22) || !(n <= 3.4e-13)) {
		tmp = Math.exp((-0.25 * (n * n)));
	} else {
		tmp = Math.exp(-l);
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if (n <= -6.2e-22) or not (n <= 3.4e-13):
		tmp = math.exp((-0.25 * (n * n)))
	else:
		tmp = math.exp(-l)
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if ((n <= -6.2e-22) || !(n <= 3.4e-13))
		tmp = exp(Float64(-0.25 * Float64(n * n)));
	else
		tmp = exp(Float64(-l));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if ((n <= -6.2e-22) || ~((n <= 3.4e-13)))
		tmp = exp((-0.25 * (n * n)));
	else
		tmp = exp(-l);
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[Or[LessEqual[n, -6.2e-22], N[Not[LessEqual[n, 3.4e-13]], $MachinePrecision]], N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Exp[(-l)], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;n \leq -6.2 \cdot 10^{-22} \lor \neg \left(n \leq 3.4 \cdot 10^{-13}\right):\\
\;\;\;\;e^{-0.25 \cdot \left(n \cdot n\right)}\\

\mathbf{else}:\\
\;\;\;\;e^{-\ell}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if n < -6.20000000000000025e-22 or 3.40000000000000015e-13 < n

    1. Initial program 66.2%

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

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

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

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

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. fabs-sub92.2%

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+92.2%

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

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

      \[\leadsto e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
    10. Step-by-step derivation
      1. *-commutative91.5%

        \[\leadsto e^{\color{blue}{{n}^{2} \cdot -0.25}} \]
    11. Simplified91.5%

      \[\leadsto e^{\color{blue}{{n}^{2} \cdot -0.25}} \]
    12. Step-by-step derivation
      1. unpow285.8%

        \[\leadsto e^{\left(n - m\right) - \left(\ell + 0.25 \cdot \color{blue}{\left(n \cdot n\right)}\right)} \]
    13. Applied egg-rr91.5%

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

    if -6.20000000000000025e-22 < n < 3.40000000000000015e-13

    1. Initial program 77.3%

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

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

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

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

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

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+80.4%

        \[\leadsto e^{\color{blue}{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    8. Simplified80.4%

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

      \[\leadsto e^{\color{blue}{\left|n - m\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
    10. Step-by-step derivation
      1. rem-square-sqrt15.5%

        \[\leadsto e^{\left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
      2. fabs-sqr15.5%

        \[\leadsto e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
      3. rem-square-sqrt51.9%

        \[\leadsto e^{\color{blue}{\left(n - m\right)} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
    11. Simplified51.9%

      \[\leadsto e^{\color{blue}{\left(n - m\right) - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
    12. Taylor expanded in l around inf 45.0%

      \[\leadsto e^{\color{blue}{-1 \cdot \ell}} \]
    13. Step-by-step derivation
      1. mul-1-neg45.0%

        \[\leadsto e^{\color{blue}{-\ell}} \]
    14. Simplified45.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq -6.2 \cdot 10^{-22} \lor \neg \left(n \leq 3.4 \cdot 10^{-13}\right):\\ \;\;\;\;e^{-0.25 \cdot \left(n \cdot n\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 49.8% accurate, 4.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;m \leq 6 \cdot 10^{-102}:\\ \;\;\;\;e^{-\ell}\\ \mathbf{else}:\\ \;\;\;\;e^{-m}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= m 6e-102) (exp (- l)) (exp (- m))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= 6e-102) {
		tmp = exp(-l);
	} else {
		tmp = exp(-m);
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (m <= 6d-102) then
        tmp = exp(-l)
    else
        tmp = exp(-m)
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= 6e-102) {
		tmp = Math.exp(-l);
	} else {
		tmp = Math.exp(-m);
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if m <= 6e-102:
		tmp = math.exp(-l)
	else:
		tmp = math.exp(-m)
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (m <= 6e-102)
		tmp = exp(Float64(-l));
	else
		tmp = exp(Float64(-m));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (m <= 6e-102)
		tmp = exp(-l);
	else
		tmp = exp(-m);
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[m, 6e-102], N[Exp[(-l)], $MachinePrecision], N[Exp[(-m)], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;m \leq 6 \cdot 10^{-102}:\\
\;\;\;\;e^{-\ell}\\

\mathbf{else}:\\
\;\;\;\;e^{-m}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if m < 6e-102

    1. Initial program 72.6%

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

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

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

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

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. fabs-sub86.5%

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+86.5%

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

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

      \[\leadsto e^{\color{blue}{\left|n - m\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
    10. Step-by-step derivation
      1. rem-square-sqrt40.6%

        \[\leadsto e^{\left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
      2. fabs-sqr40.6%

        \[\leadsto e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
      3. rem-square-sqrt62.3%

        \[\leadsto e^{\color{blue}{\left(n - m\right)} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
    11. Simplified62.3%

      \[\leadsto e^{\color{blue}{\left(n - m\right) - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
    12. Taylor expanded in l around inf 39.8%

      \[\leadsto e^{\color{blue}{-1 \cdot \ell}} \]
    13. Step-by-step derivation
      1. mul-1-neg39.8%

        \[\leadsto e^{\color{blue}{-\ell}} \]
    14. Simplified39.8%

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

    if 6e-102 < m

    1. Initial program 68.8%

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

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

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

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

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

        \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
      2. associate--r+87.4%

        \[\leadsto e^{\color{blue}{\left(\left|n - m\right| - \ell\right) - 0.25 \cdot {\left(m + n\right)}^{2}}} \]
    8. Simplified87.4%

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

      \[\leadsto e^{\color{blue}{\left|n - m\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
    10. Step-by-step derivation
      1. rem-square-sqrt17.2%

        \[\leadsto e^{\left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
      2. fabs-sqr17.2%

        \[\leadsto e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
      3. rem-square-sqrt84.4%

        \[\leadsto e^{\color{blue}{\left(n - m\right)} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
    11. Simplified84.4%

      \[\leadsto e^{\color{blue}{\left(n - m\right) - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
    12. Taylor expanded in m around inf 72.4%

      \[\leadsto e^{\color{blue}{-1 \cdot m}} \]
    13. Step-by-step derivation
      1. neg-mul-172.4%

        \[\leadsto e^{\color{blue}{-m}} \]
    14. Simplified72.4%

      \[\leadsto e^{\color{blue}{-m}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 7: 35.2% 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 71.3%

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

    \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  4. Step-by-step derivation
    1. cos-neg96.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. Simplified96.0%

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

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

      \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
    2. associate--r+86.8%

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

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

    \[\leadsto e^{\color{blue}{\left|n - m\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
  10. Step-by-step derivation
    1. rem-square-sqrt32.1%

      \[\leadsto e^{\left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right| - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
    2. fabs-sqr32.1%

      \[\leadsto e^{\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
    3. rem-square-sqrt70.3%

      \[\leadsto e^{\color{blue}{\left(n - m\right)} - \left(\ell + 0.25 \cdot {n}^{2}\right)} \]
  11. Simplified70.3%

    \[\leadsto e^{\color{blue}{\left(n - m\right) - \left(\ell + 0.25 \cdot {n}^{2}\right)}} \]
  12. Taylor expanded in l around inf 35.2%

    \[\leadsto e^{\color{blue}{-1 \cdot \ell}} \]
  13. Step-by-step derivation
    1. mul-1-neg35.2%

      \[\leadsto e^{\color{blue}{-\ell}} \]
  14. Simplified35.2%

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

Alternative 8: 7.0% accurate, 4.2× speedup?

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

\\
\cos M
\end{array}
Derivation
  1. Initial program 71.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 28.0%

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

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

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

    \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right) - M\right)} \]
  7. Step-by-step derivation
    1. *-commutative6.0%

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

      \[\leadsto \cos \left(\color{blue}{\left(\left(m + n\right) \cdot K\right) \cdot 0.5} - M\right) \]
    3. associate-*l*6.0%

      \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \left(K \cdot 0.5\right)} - M\right) \]
    4. *-commutative6.0%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \color{blue}{\left(0.5 \cdot K\right)} - M\right) \]
  8. Simplified6.0%

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

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

      \[\leadsto \color{blue}{\cos M} \]
  11. Simplified6.9%

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

Alternative 9: 7.0% accurate, 425.0× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (K m n M l) :precision binary64 1.0)
double code(double K, double m, double n, double M, double l) {
	return 1.0;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = 1.0d0
end function
public static double code(double K, double m, double n, double M, double l) {
	return 1.0;
}
def code(K, m, n, M, l):
	return 1.0
function code(K, m, n, M, l)
	return 1.0
end
function tmp = code(K, m, n, M, l)
	tmp = 1.0;
end
code[K_, m_, n_, M_, l_] := 1.0
\begin{array}{l}

\\
1
\end{array}
Derivation
  1. Initial program 71.3%

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

    \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  4. Step-by-step derivation
    1. cos-neg96.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. Simplified96.0%

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

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

      \[\leadsto e^{\color{blue}{\left|n - m\right|} - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)} \]
    2. associate--r+86.8%

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

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

    \[\leadsto e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
  10. Step-by-step derivation
    1. *-commutative54.6%

      \[\leadsto e^{\color{blue}{{n}^{2} \cdot -0.25}} \]
  11. Simplified54.6%

    \[\leadsto e^{\color{blue}{{n}^{2} \cdot -0.25}} \]
  12. Taylor expanded in n around 0 6.9%

    \[\leadsto \color{blue}{1} \]
  13. Add Preprocessing

Reproduce

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