Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.7% → 96.6%
Time: 15.5s
Alternatives: 9
Speedup: 2.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 9 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 75.7% 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.6% 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.6%

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

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

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

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

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

Alternative 2: 95.5% accurate, 1.3× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -3.5e15 or 27 < M

    1. Initial program 80.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 100.0%

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

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

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

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

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

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

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

    if -3.5e15 < M < 27

    1. Initial program 71.0%

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

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

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

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

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

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

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

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

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

Alternative 3: 85.4% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;n \leq 57:\\
\;\;\;\;e^{\left(n - m\right) - \left(\ell + 0.25 \cdot {m}^{2}\right)}\\

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


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

    1. Initial program 77.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 K around 0 96.1%

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

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

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

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

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

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

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

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

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

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

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

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

    if 57 < n

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\color{blue}{\left(n \cdot n\right)} \cdot -0.25} \]
    13. Applied egg-rr100.0%

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

Alternative 4: 62.6% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;n \leq 2.3 \cdot 10^{-175}:\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;n \leq 1.95 \cdot 10^{-11}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n \cdot n\right) \cdot -0.25}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= n 2.3e-175)
   (exp (* (pow m 2.0) -0.25))
   (if (<= n 1.95e-11) (* (cos M) (exp (- l))) (exp (* (* n n) -0.25)))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 2.3e-175) {
		tmp = exp((pow(m, 2.0) * -0.25));
	} else if (n <= 1.95e-11) {
		tmp = cos(M) * exp(-l);
	} else {
		tmp = exp(((n * n) * -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 (n <= 2.3d-175) then
        tmp = exp(((m ** 2.0d0) * (-0.25d0)))
    else if (n <= 1.95d-11) then
        tmp = cos(m_1) * exp(-l)
    else
        tmp = exp(((n * n) * (-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 (n <= 2.3e-175) {
		tmp = Math.exp((Math.pow(m, 2.0) * -0.25));
	} else if (n <= 1.95e-11) {
		tmp = Math.cos(M) * Math.exp(-l);
	} else {
		tmp = Math.exp(((n * n) * -0.25));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if n <= 2.3e-175:
		tmp = math.exp((math.pow(m, 2.0) * -0.25))
	elif n <= 1.95e-11:
		tmp = math.cos(M) * math.exp(-l)
	else:
		tmp = math.exp(((n * n) * -0.25))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (n <= 2.3e-175)
		tmp = exp(Float64((m ^ 2.0) * -0.25));
	elseif (n <= 1.95e-11)
		tmp = Float64(cos(M) * exp(Float64(-l)));
	else
		tmp = exp(Float64(Float64(n * n) * -0.25));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (n <= 2.3e-175)
		tmp = exp(((m ^ 2.0) * -0.25));
	elseif (n <= 1.95e-11)
		tmp = cos(M) * exp(-l);
	else
		tmp = exp(((n * n) * -0.25));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[n, 2.3e-175], N[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision], If[LessEqual[n, 1.95e-11], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision], N[Exp[N[(N[(n * n), $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;n \leq 2.3 \cdot 10^{-175}:\\
\;\;\;\;e^{{m}^{2} \cdot -0.25}\\

\mathbf{elif}\;n \leq 1.95 \cdot 10^{-11}:\\
\;\;\;\;\cos M \cdot e^{-\ell}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if n < 2.3e-175

    1. Initial program 77.5%

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

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

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

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

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

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

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

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

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

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

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

    if 2.3e-175 < n < 1.95000000000000005e-11

    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 l around inf 47.8%

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

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

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

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

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

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

    if 1.95000000000000005e-11 < n

    1. Initial program 70.6%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\color{blue}{\left(n \cdot n\right)} \cdot -0.25} \]
    13. Applied egg-rr97.1%

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

Alternative 5: 65.2% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;n \leq 1.4 \cdot 10^{-244}:\\
\;\;\;\;e^{{m}^{2} \cdot -0.25}\\

\mathbf{elif}\;n \leq 55:\\
\;\;\;\;e^{-{M}^{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if n < 1.40000000000000007e-244

    1. Initial program 76.2%

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

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

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

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

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

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

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

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

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

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

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

    if 1.40000000000000007e-244 < n < 55

    1. Initial program 82.3%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in K around 0 92.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-neg92.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. Simplified92.0%

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

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

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

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

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

    if 55 < n

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\color{blue}{\left(n \cdot n\right)} \cdot -0.25} \]
    13. Applied egg-rr100.0%

      \[\leadsto e^{\color{blue}{\left(n \cdot n\right)} \cdot -0.25} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification66.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 1.4 \cdot 10^{-244}:\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;n \leq 55:\\ \;\;\;\;e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(n \cdot n\right) \cdot -0.25}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 65.6% accurate, 2.0× speedup?

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

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

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


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

    1. Initial program 72.6%

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

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

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

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

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

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

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

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

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

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

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

    if -0.0071999999999999998 < m

    1. Initial program 76.6%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\color{blue}{\left(n \cdot n\right)} \cdot -0.25} \]
    13. Applied egg-rr56.7%

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

Alternative 7: 54.1% accurate, 4.0× speedup?

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

\\
e^{\left(n \cdot n\right) \cdot -0.25}
\end{array}
Derivation
  1. Initial program 75.6%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto e^{\color{blue}{\left(n \cdot n\right)} \cdot -0.25} \]
  13. Applied egg-rr56.2%

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

Alternative 8: 7.0% accurate, 4.2× speedup?

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

\\
\cos M
\end{array}
Derivation
  1. Initial program 75.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 l around inf 31.3%

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 9: 7.0% accurate, 425.0× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Reproduce

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