Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.9% → 96.8%
Time: 20.2s
Alternatives: 10
Speedup: 1.9×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 10 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.9% 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} \\ \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 75.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 97.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-neg97.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. Simplified97.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 simplification97.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: 96.3% accurate, 1.3× speedup?

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

\\
{e}^{\left(\left(\left|m - n\right| - \ell\right) - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}\right)}
\end{array}
Derivation
  1. Initial program 75.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 n around inf 84.7%

    \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*84.7%

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

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

    \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
  7. Step-by-step derivation
    1. *-un-lft-identity96.2%

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

      \[\leadsto \color{blue}{{\left(e^{1}\right)}^{\left(\left|m - n\right| - \left(\ell + {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}\right)\right)}} \]
    3. associate--r+96.2%

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

      \[\leadsto {\left(e^{1}\right)}^{\left(\left(\color{blue}{\left|n - m\right|} - \ell\right) - {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    5. fma-neg96.2%

      \[\leadsto {\left(e^{1}\right)}^{\left(\left(\left|n - m\right| - \ell\right) - {\color{blue}{\left(\mathsf{fma}\left(0.5, m + n, -M\right)\right)}}^{2}\right)} \]
  8. Applied egg-rr96.2%

    \[\leadsto \color{blue}{{\left(e^{1}\right)}^{\left(\left(\left|n - m\right| - \ell\right) - {\left(\mathsf{fma}\left(0.5, m + n, -M\right)\right)}^{2}\right)}} \]
  9. Step-by-step derivation
    1. exp-1-e96.2%

      \[\leadsto {\color{blue}{e}}^{\left(\left(\left|n - m\right| - \ell\right) - {\left(\mathsf{fma}\left(0.5, m + n, -M\right)\right)}^{2}\right)} \]
    2. fabs-sub96.2%

      \[\leadsto {e}^{\left(\left(\color{blue}{\left|m - n\right|} - \ell\right) - {\left(\mathsf{fma}\left(0.5, m + n, -M\right)\right)}^{2}\right)} \]
    3. unpow296.2%

      \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) - \color{blue}{\mathsf{fma}\left(0.5, m + n, -M\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)}\right)} \]
    4. cancel-sign-sub-inv96.2%

      \[\leadsto {e}^{\color{blue}{\left(\left(\left|m - n\right| - \ell\right) + \left(-\mathsf{fma}\left(0.5, m + n, -M\right)\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)}} \]
    5. +-commutative96.2%

      \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\mathsf{fma}\left(0.5, \color{blue}{n + m}, -M\right)\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)} \]
    6. *-lft-identity96.2%

      \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\mathsf{fma}\left(0.5, n + \color{blue}{1 \cdot m}, -M\right)\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)} \]
    7. metadata-eval96.2%

      \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\mathsf{fma}\left(0.5, n + \color{blue}{\left(--1\right)} \cdot m, -M\right)\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)} \]
    8. cancel-sign-sub-inv96.2%

      \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\mathsf{fma}\left(0.5, \color{blue}{n - -1 \cdot m}, -M\right)\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)} \]
    9. fma-neg96.2%

      \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\color{blue}{\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)}\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)} \]
    10. +-commutative96.2%

      \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)\right) \cdot \mathsf{fma}\left(0.5, \color{blue}{n + m}, -M\right)\right)} \]
    11. *-lft-identity96.2%

      \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)\right) \cdot \mathsf{fma}\left(0.5, n + \color{blue}{1 \cdot m}, -M\right)\right)} \]
    12. metadata-eval96.2%

      \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)\right) \cdot \mathsf{fma}\left(0.5, n + \color{blue}{\left(--1\right)} \cdot m, -M\right)\right)} \]
    13. cancel-sign-sub-inv96.2%

      \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)\right) \cdot \mathsf{fma}\left(0.5, \color{blue}{n - -1 \cdot m}, -M\right)\right)} \]
    14. fma-neg96.2%

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

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

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

Alternative 3: 96.3% accurate, 1.4× speedup?

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

\\
e^{\left|m - n\right| - \left(\ell + {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}\right)}
\end{array}
Derivation
  1. Initial program 75.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 n around inf 84.7%

    \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*84.7%

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

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

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

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

Alternative 4: 87.2% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;m \leq -1000000000:\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{else}:\\ \;\;\;\;{e}^{\left(\left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right) - \left(\ell - \left|m - n\right|\right)\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= m -1000000000.0)
   (exp (* (pow m 2.0) -0.25))
   (pow E (- (* (- (* n 0.5) M) (- (- M (* n 0.5)) m)) (- l (fabs (- m n)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -1000000000.0) {
		tmp = exp((pow(m, 2.0) * -0.25));
	} else {
		tmp = pow(((double) M_E), ((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) - (l - fabs((m - n)))));
	}
	return tmp;
}
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -1000000000.0) {
		tmp = Math.exp((Math.pow(m, 2.0) * -0.25));
	} else {
		tmp = Math.pow(Math.E, ((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) - (l - Math.abs((m - n)))));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if m <= -1000000000.0:
		tmp = math.exp((math.pow(m, 2.0) * -0.25))
	else:
		tmp = math.pow(math.e, ((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) - (l - math.fabs((m - n)))))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (m <= -1000000000.0)
		tmp = exp(Float64((m ^ 2.0) * -0.25));
	else
		tmp = exp(1) ^ Float64(Float64(Float64(Float64(n * 0.5) - M) * Float64(Float64(M - Float64(n * 0.5)) - m)) - Float64(l - abs(Float64(m - n))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (m <= -1000000000.0)
		tmp = exp(((m ^ 2.0) * -0.25));
	else
		tmp = 2.71828182845904523536 ^ ((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) - (l - abs((m - n))));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[m, -1000000000.0], N[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision], N[Power[E, N[(N[(N[(N[(n * 0.5), $MachinePrecision] - M), $MachinePrecision] * N[(N[(M - N[(n * 0.5), $MachinePrecision]), $MachinePrecision] - m), $MachinePrecision]), $MachinePrecision] - N[(l - N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;{e}^{\left(\left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right) - \left(\ell - \left|m - n\right|\right)\right)}\\


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

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*91.8%

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

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

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

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

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

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right)} \]
      3. distribute-rgt-out95.2%

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

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

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

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

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

    if -1e9 < m

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*82.4%

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

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

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. *-un-lft-identity95.0%

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

        \[\leadsto \color{blue}{{\left(e^{1}\right)}^{\left(\left|m - n\right| - \left(\ell + {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}\right)\right)}} \]
      3. associate--r+95.0%

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

        \[\leadsto {\left(e^{1}\right)}^{\left(\left(\color{blue}{\left|n - m\right|} - \ell\right) - {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      5. fma-neg95.0%

        \[\leadsto {\left(e^{1}\right)}^{\left(\left(\left|n - m\right| - \ell\right) - {\color{blue}{\left(\mathsf{fma}\left(0.5, m + n, -M\right)\right)}}^{2}\right)} \]
    8. Applied egg-rr95.0%

      \[\leadsto \color{blue}{{\left(e^{1}\right)}^{\left(\left(\left|n - m\right| - \ell\right) - {\left(\mathsf{fma}\left(0.5, m + n, -M\right)\right)}^{2}\right)}} \]
    9. Step-by-step derivation
      1. exp-1-e95.0%

        \[\leadsto {\color{blue}{e}}^{\left(\left(\left|n - m\right| - \ell\right) - {\left(\mathsf{fma}\left(0.5, m + n, -M\right)\right)}^{2}\right)} \]
      2. fabs-sub95.0%

        \[\leadsto {e}^{\left(\left(\color{blue}{\left|m - n\right|} - \ell\right) - {\left(\mathsf{fma}\left(0.5, m + n, -M\right)\right)}^{2}\right)} \]
      3. unpow295.0%

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) - \color{blue}{\mathsf{fma}\left(0.5, m + n, -M\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)}\right)} \]
      4. cancel-sign-sub-inv95.0%

        \[\leadsto {e}^{\color{blue}{\left(\left(\left|m - n\right| - \ell\right) + \left(-\mathsf{fma}\left(0.5, m + n, -M\right)\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)}} \]
      5. +-commutative95.0%

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\mathsf{fma}\left(0.5, \color{blue}{n + m}, -M\right)\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)} \]
      6. *-lft-identity95.0%

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\mathsf{fma}\left(0.5, n + \color{blue}{1 \cdot m}, -M\right)\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)} \]
      7. metadata-eval95.0%

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\mathsf{fma}\left(0.5, n + \color{blue}{\left(--1\right)} \cdot m, -M\right)\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)} \]
      8. cancel-sign-sub-inv95.0%

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\mathsf{fma}\left(0.5, \color{blue}{n - -1 \cdot m}, -M\right)\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)} \]
      9. fma-neg95.0%

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\color{blue}{\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)}\right) \cdot \mathsf{fma}\left(0.5, m + n, -M\right)\right)} \]
      10. +-commutative95.0%

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)\right) \cdot \mathsf{fma}\left(0.5, \color{blue}{n + m}, -M\right)\right)} \]
      11. *-lft-identity95.0%

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)\right) \cdot \mathsf{fma}\left(0.5, n + \color{blue}{1 \cdot m}, -M\right)\right)} \]
      12. metadata-eval95.0%

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)\right) \cdot \mathsf{fma}\left(0.5, n + \color{blue}{\left(--1\right)} \cdot m, -M\right)\right)} \]
      13. cancel-sign-sub-inv95.0%

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)\right) \cdot \mathsf{fma}\left(0.5, \color{blue}{n - -1 \cdot m}, -M\right)\right)} \]
      14. fma-neg95.0%

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) + \left(-\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)\right) \cdot \color{blue}{\left(0.5 \cdot \left(n - -1 \cdot m\right) - M\right)}\right)} \]
    10. Simplified95.0%

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

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

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

        \[\leadsto {e}^{\left(\left(\left|m - n\right| - \ell\right) - \left(\color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(0.5 \cdot n - M\right)} + m \cdot \left(0.5 \cdot n - M\right)\right)\right)} \]
      3. distribute-rgt-out81.4%

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

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

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

Alternative 5: 80.4% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq 1.1 \cdot 10^{-13}:\\
\;\;\;\;e^{\left|m - n\right| + \left(\left(n + \left(m \cdot 0.5 - M\right)\right) \cdot \left(M - m \cdot 0.5\right) - \ell\right)}\\

\mathbf{else}:\\
\;\;\;\;e^{-{M}^{2}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < 1.09999999999999998e-13

    1. Initial program 74.7%

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*83.7%

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

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

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

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

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

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right)} \]
      3. distribute-rgt-out75.9%

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

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

    if 1.09999999999999998e-13 < M

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*87.1%

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

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

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

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

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

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right)} \]
      3. distribute-rgt-out87.3%

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(\left(0.5 \cdot m - M\right) + n\right)}\right)} \]
    9. Simplified87.3%

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

      \[\leadsto e^{\color{blue}{-1 \cdot {M}^{2}}} \]
    11. Step-by-step derivation
      1. mul-1-neg95.8%

        \[\leadsto e^{\color{blue}{-{M}^{2}}} \]
    12. Simplified95.8%

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

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

Alternative 6: 87.2% accurate, 1.9× speedup?

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

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

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


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

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*91.8%

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

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

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

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

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

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right)} \]
      3. distribute-rgt-out95.2%

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

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

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

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

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

    if -110 < m

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*82.4%

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

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

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

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

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

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(0.5 \cdot n - M\right)} + m \cdot \left(0.5 \cdot n - M\right)\right)\right)} \]
      3. distribute-rgt-out81.4%

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

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

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

Alternative 7: 76.5% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -21000000000000 \lor \neg \left(M \leq 1.1 \cdot 10^{-13}\right):\\
\;\;\;\;e^{-{M}^{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -2.1e13 or 1.09999999999999998e-13 < M

    1. Initial program 77.4%

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*86.9%

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

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

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

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

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

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right)} \]
      3. distribute-rgt-out89.2%

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

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

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

        \[\leadsto e^{\color{blue}{-{M}^{2}}} \]
    12. Simplified97.1%

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

    if -2.1e13 < M < 1.09999999999999998e-13

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*82.1%

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

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

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

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

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

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right)} \]
      3. distribute-rgt-out67.3%

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(\left(0.5 \cdot m - M\right) + n\right)}\right)} \]
    9. Simplified67.3%

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

      \[\leadsto e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    11. Step-by-step derivation
      1. *-commutative59.5%

        \[\leadsto e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
    12. Simplified59.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -21000000000000 \lor \neg \left(M \leq 1.1 \cdot 10^{-13}\right):\\ \;\;\;\;e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 70.1% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -27 \lor \neg \left(M \leq 1.75 \cdot 10^{-18}\right):\\
\;\;\;\;e^{-{M}^{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -27 or 1.7499999999999999e-18 < M

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*87.2%

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

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

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

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

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

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right)} \]
      3. distribute-rgt-out88.1%

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

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

      \[\leadsto e^{\color{blue}{-1 \cdot {M}^{2}}} \]
    11. Step-by-step derivation
      1. mul-1-neg96.5%

        \[\leadsto e^{\color{blue}{-{M}^{2}}} \]
    12. Simplified96.5%

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

    if -27 < M < 1.7499999999999999e-18

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*81.5%

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

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

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

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

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

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right)} \]
      3. distribute-rgt-out67.9%

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

      \[\leadsto e^{\left|m - n\right| - \left(\ell + \color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(\left(0.5 \cdot m - M\right) + n\right)}\right)} \]
    10. Taylor expanded in l around inf 35.9%

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

        \[\leadsto e^{\color{blue}{-\ell}} \]
    12. Simplified35.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -27 \lor \neg \left(M \leq 1.75 \cdot 10^{-18}\right):\\ \;\;\;\;e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell}\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 54.6% accurate, 3.8× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if l < 720

    1. Initial program 75.0%

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*83.2%

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

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

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

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

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

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right)} \]
      3. distribute-rgt-out76.6%

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

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

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

    if 720 < l

    1. Initial program 78.8%

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*90.4%

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

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

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

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

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

        \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right)} \]
      3. distribute-rgt-out88.6%

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

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

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

        \[\leadsto e^{\color{blue}{-\ell}} \]
    12. Simplified100.0%

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

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

Alternative 10: 35.5% 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 75.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 n around inf 84.7%

    \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot n\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. associate-*r*84.7%

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

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

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

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

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

      \[\leadsto e^{\left|m - n\right| - \left(\ell + \left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right)} \]
    3. distribute-rgt-out79.0%

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

    \[\leadsto e^{\left|m - n\right| - \left(\ell + \color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(\left(0.5 \cdot m - M\right) + n\right)}\right)} \]
  10. Taylor expanded in l around inf 29.4%

    \[\leadsto e^{\color{blue}{-1 \cdot \ell}} \]
  11. Step-by-step derivation
    1. mul-1-neg29.4%

      \[\leadsto e^{\color{blue}{-\ell}} \]
  12. Simplified29.4%

    \[\leadsto e^{\color{blue}{-\ell}} \]
  13. Final simplification29.4%

    \[\leadsto e^{-\ell} \]
  14. Add Preprocessing

Reproduce

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