Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.4% → 96.7%
Time: 30.5s
Alternatives: 6
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 6 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.4% 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.7% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := \left(m + n\right) \cdot 0.5 - M\\
\cos M \cdot e^{\left|m - n\right| - \left(t_0 \cdot t_0 + \ell\right)}
\end{array}
\end{array}
Derivation
  1. Initial program 77.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. Simplified77.6%

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\left(\left(-\left(\left(m + n\right) \cdot 0.5 - M\right) \cdot \left(\left(m + n\right) \cdot \color{blue}{0.5} - M\right)\right) - \ell\right) + \left|n - m\right|} \]
  7. Applied egg-rr97.1%

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

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

Alternative 2: 96.4% accurate, 1.4× speedup?

\[\begin{array}{l} \\ e^{\left(\left|m - n\right| - \ell\right) - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}} \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(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 = exp(((abs((m - n)) - l) - ((((m + n) * 0.5) - M) ^ 2.0)));
end
code[K_, m_, n_, M_, l_] := N[Exp[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|m - n\right| - \ell\right) - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}}
\end{array}
Derivation
  1. Initial program 77.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. Simplified77.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 3: 86.5% accurate, 2.0× speedup?

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

\\
e^{\left|m - n\right| - \left(\ell + 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)\right)}
\end{array}
Derivation
  1. Initial program 77.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. Simplified77.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto e^{\left|m - n\right| - \left(\ell + 0.25 \cdot \left(\left(m + n\right) \cdot \left(m + n\right)\right)\right)} \]

Alternative 4: 35.9% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := m \cdot 0.5 - M\\ t_1 := n \cdot 0.5 - M\\ t_2 := e^{\left(n + t_1 \cdot \left(m + t_1\right)\right) - \left(m + \ell\right)}\\ \mathbf{if}\;n \leq -1.08 \cdot 10^{-184}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;n \leq 4:\\ \;\;\;\;e^{\left(n + 0.25 \cdot {n}^{2}\right) - \left(m + \ell\right)}\\ \mathbf{elif}\;n \leq 2 \cdot 10^{+140}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n + t_0 \cdot \left(n + t_0\right)\right) - \left(m + \ell\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (- (* m 0.5) M))
        (t_1 (- (* n 0.5) M))
        (t_2 (exp (- (+ n (* t_1 (+ m t_1))) (+ m l)))))
   (if (<= n -1.08e-184)
     t_2
     (if (<= n 4.0)
       (exp (- (+ n (* 0.25 (pow n 2.0))) (+ m l)))
       (if (<= n 2e+140) t_2 (exp (- (+ n (* t_0 (+ n t_0))) (+ m l))))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = (m * 0.5) - M;
	double t_1 = (n * 0.5) - M;
	double t_2 = exp(((n + (t_1 * (m + t_1))) - (m + l)));
	double tmp;
	if (n <= -1.08e-184) {
		tmp = t_2;
	} else if (n <= 4.0) {
		tmp = exp(((n + (0.25 * pow(n, 2.0))) - (m + l)));
	} else if (n <= 2e+140) {
		tmp = t_2;
	} else {
		tmp = exp(((n + (t_0 * (n + t_0))) - (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) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = (m * 0.5d0) - m_1
    t_1 = (n * 0.5d0) - m_1
    t_2 = exp(((n + (t_1 * (m + t_1))) - (m + l)))
    if (n <= (-1.08d-184)) then
        tmp = t_2
    else if (n <= 4.0d0) then
        tmp = exp(((n + (0.25d0 * (n ** 2.0d0))) - (m + l)))
    else if (n <= 2d+140) then
        tmp = t_2
    else
        tmp = exp(((n + (t_0 * (n + t_0))) - (m + l)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = (m * 0.5) - M;
	double t_1 = (n * 0.5) - M;
	double t_2 = Math.exp(((n + (t_1 * (m + t_1))) - (m + l)));
	double tmp;
	if (n <= -1.08e-184) {
		tmp = t_2;
	} else if (n <= 4.0) {
		tmp = Math.exp(((n + (0.25 * Math.pow(n, 2.0))) - (m + l)));
	} else if (n <= 2e+140) {
		tmp = t_2;
	} else {
		tmp = Math.exp(((n + (t_0 * (n + t_0))) - (m + l)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = (m * 0.5) - M
	t_1 = (n * 0.5) - M
	t_2 = math.exp(((n + (t_1 * (m + t_1))) - (m + l)))
	tmp = 0
	if n <= -1.08e-184:
		tmp = t_2
	elif n <= 4.0:
		tmp = math.exp(((n + (0.25 * math.pow(n, 2.0))) - (m + l)))
	elif n <= 2e+140:
		tmp = t_2
	else:
		tmp = math.exp(((n + (t_0 * (n + t_0))) - (m + l)))
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(Float64(m * 0.5) - M)
	t_1 = Float64(Float64(n * 0.5) - M)
	t_2 = exp(Float64(Float64(n + Float64(t_1 * Float64(m + t_1))) - Float64(m + l)))
	tmp = 0.0
	if (n <= -1.08e-184)
		tmp = t_2;
	elseif (n <= 4.0)
		tmp = exp(Float64(Float64(n + Float64(0.25 * (n ^ 2.0))) - Float64(m + l)));
	elseif (n <= 2e+140)
		tmp = t_2;
	else
		tmp = exp(Float64(Float64(n + Float64(t_0 * Float64(n + t_0))) - Float64(m + l)));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = (m * 0.5) - M;
	t_1 = (n * 0.5) - M;
	t_2 = exp(((n + (t_1 * (m + t_1))) - (m + l)));
	tmp = 0.0;
	if (n <= -1.08e-184)
		tmp = t_2;
	elseif (n <= 4.0)
		tmp = exp(((n + (0.25 * (n ^ 2.0))) - (m + l)));
	elseif (n <= 2e+140)
		tmp = t_2;
	else
		tmp = exp(((n + (t_0 * (n + t_0))) - (m + l)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[(m * 0.5), $MachinePrecision] - M), $MachinePrecision]}, Block[{t$95$1 = N[(N[(n * 0.5), $MachinePrecision] - M), $MachinePrecision]}, Block[{t$95$2 = N[Exp[N[(N[(n + N[(t$95$1 * N[(m + t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(m + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[n, -1.08e-184], t$95$2, If[LessEqual[n, 4.0], N[Exp[N[(N[(n + N[(0.25 * N[Power[n, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(m + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[n, 2e+140], t$95$2, N[Exp[N[(N[(n + N[(t$95$0 * N[(n + t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(m + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := m \cdot 0.5 - M\\
t_1 := n \cdot 0.5 - M\\
t_2 := e^{\left(n + t_1 \cdot \left(m + t_1\right)\right) - \left(m + \ell\right)}\\
\mathbf{if}\;n \leq -1.08 \cdot 10^{-184}:\\
\;\;\;\;t_2\\

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

\mathbf{elif}\;n \leq 2 \cdot 10^{+140}:\\
\;\;\;\;t_2\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if n < -1.07999999999999995e-184 or 4 < n < 2.00000000000000012e140

    1. Initial program 77.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. Simplified78.6%

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

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

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|n - m\right|}\right)\right)} \]
      2. associate-+l-77.0%

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

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

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

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

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

        \[\leadsto e^{\left(n + \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) - \left(\ell + m\right)} \]
      3. distribute-rgt-out27.4%

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

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

    if -1.07999999999999995e-184 < n < 4

    1. Initial program 81.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. Simplified81.3%

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right)} \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|n - m\right|} \]
    6. Step-by-step derivation
      1. log1p-expm1-u81.3%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|n - m\right|}\right)\right)} \]
      2. associate-+l-81.3%

        \[\leadsto \mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\color{blue}{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)}}\right)\right) \]
    7. Applied egg-rr25.7%

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

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

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

    if 2.00000000000000012e140 < n

    1. Initial program 69.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. Simplified69.4%

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

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

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|n - m\right|}\right)\right)} \]
      2. associate-+l-69.4%

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

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

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

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

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

        \[\leadsto e^{\left(n + \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) - \left(\ell + m\right)} \]
      3. distribute-rgt-out33.7%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq -1.08 \cdot 10^{-184}:\\ \;\;\;\;e^{\left(n + \left(n \cdot 0.5 - M\right) \cdot \left(m + \left(n \cdot 0.5 - M\right)\right)\right) - \left(m + \ell\right)}\\ \mathbf{elif}\;n \leq 4:\\ \;\;\;\;e^{\left(n + 0.25 \cdot {n}^{2}\right) - \left(m + \ell\right)}\\ \mathbf{elif}\;n \leq 2 \cdot 10^{+140}:\\ \;\;\;\;e^{\left(n + \left(n \cdot 0.5 - M\right) \cdot \left(m + \left(n \cdot 0.5 - M\right)\right)\right) - \left(m + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n + \left(m \cdot 0.5 - M\right) \cdot \left(n + \left(m \cdot 0.5 - M\right)\right)\right) - \left(m + \ell\right)}\\ \end{array} \]

Alternative 5: 32.2% accurate, 3.5× speedup?

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

\\
\begin{array}{l}
t_0 := m \cdot 0.5 - M\\
t_1 := n \cdot 0.5 - M\\
\mathbf{if}\;n \leq 10^{+134}:\\
\;\;\;\;e^{\left(n + t_1 \cdot \left(m + t_1\right)\right) - \left(m + \ell\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if n < 9.99999999999999921e133

    1. Initial program 79.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. Simplified79.6%

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

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

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|n - m\right|}\right)\right)} \]
      2. associate-+l-78.6%

        \[\leadsto \mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\color{blue}{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)}}\right)\right) \]
    7. Applied egg-rr15.6%

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

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

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

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

        \[\leadsto e^{\left(n + \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) - \left(\ell + m\right)} \]
      3. distribute-rgt-out32.5%

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

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

    if 9.99999999999999921e133 < n

    1. Initial program 69.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. Simplified69.4%

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

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

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|n - m\right|}\right)\right)} \]
      2. associate-+l-69.4%

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

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

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

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

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

        \[\leadsto e^{\left(n + \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) - \left(\ell + m\right)} \]
      3. distribute-rgt-out33.7%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 10^{+134}:\\ \;\;\;\;e^{\left(n + \left(n \cdot 0.5 - M\right) \cdot \left(m + \left(n \cdot 0.5 - M\right)\right)\right) - \left(m + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n + \left(m \cdot 0.5 - M\right) \cdot \left(n + \left(m \cdot 0.5 - M\right)\right)\right) - \left(m + \ell\right)}\\ \end{array} \]

Alternative 6: 29.1% accurate, 3.6× speedup?

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

\\
\begin{array}{l}
t_0 := m \cdot 0.5 - M\\
e^{\left(n + t_0 \cdot \left(n + t_0\right)\right) - \left(m + \ell\right)}
\end{array}
\end{array}
Derivation
  1. Initial program 77.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. Simplified77.6%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|n - m\right|}\right)\right)} \]
    2. associate-+l-76.9%

      \[\leadsto \mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{\color{blue}{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)}}\right)\right) \]
  7. Applied egg-rr12.8%

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

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

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

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

      \[\leadsto e^{\left(n + \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) - \left(\ell + m\right)} \]
    3. distribute-rgt-out29.7%

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

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

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

Reproduce

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