Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.1% → 79.2%
Time: 2.8s
Alternatives: 6
Speedup: 1.0×

Specification

?
\[\mathsf{TRUE}\left(\right)\]
\[\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.1% 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: 79.2% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)\\ t_1 := e^{t\_0}\\ t_2 := \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t\_1\\ \mathbf{if}\;t\_2 \leq 2:\\ \;\;\;\;t\_2\\ \mathbf{else}:\\ \;\;\;\;t\_0 \cdot t\_1\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (- (- (pow (- (/ (+ m n) 2.0) M) 2.0)) (- l (fabs (- m n)))))
        (t_1 (exp t_0))
        (t_2 (* (cos (- (/ (* K (+ m n)) 2.0) M)) t_1)))
   (if (<= t_2 2.0) t_2 (* t_0 t_1))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = -pow((((m + n) / 2.0) - M), 2.0) - (l - fabs((m - n)));
	double t_1 = exp(t_0);
	double t_2 = cos((((K * (m + n)) / 2.0) - M)) * t_1;
	double tmp;
	if (t_2 <= 2.0) {
		tmp = t_2;
	} else {
		tmp = t_0 * t_1;
	}
	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 + n) / 2.0d0) - m_1) ** 2.0d0) - (l - abs((m - n)))
    t_1 = exp(t_0)
    t_2 = cos((((k * (m + n)) / 2.0d0) - m_1)) * t_1
    if (t_2 <= 2.0d0) then
        tmp = t_2
    else
        tmp = t_0 * t_1
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = -Math.pow((((m + n) / 2.0) - M), 2.0) - (l - Math.abs((m - n)));
	double t_1 = Math.exp(t_0);
	double t_2 = Math.cos((((K * (m + n)) / 2.0) - M)) * t_1;
	double tmp;
	if (t_2 <= 2.0) {
		tmp = t_2;
	} else {
		tmp = t_0 * t_1;
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = -math.pow((((m + n) / 2.0) - M), 2.0) - (l - math.fabs((m - n)))
	t_1 = math.exp(t_0)
	t_2 = math.cos((((K * (m + n)) / 2.0) - M)) * t_1
	tmp = 0
	if t_2 <= 2.0:
		tmp = t_2
	else:
		tmp = t_0 * t_1
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(Float64(-(Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)) - Float64(l - abs(Float64(m - n))))
	t_1 = exp(t_0)
	t_2 = Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * t_1)
	tmp = 0.0
	if (t_2 <= 2.0)
		tmp = t_2;
	else
		tmp = Float64(t_0 * t_1);
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = -((((m + n) / 2.0) - M) ^ 2.0) - (l - abs((m - n)));
	t_1 = exp(t_0);
	t_2 = cos((((K * (m + n)) / 2.0) - M)) * t_1;
	tmp = 0.0;
	if (t_2 <= 2.0)
		tmp = t_2;
	else
		tmp = t_0 * t_1;
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = 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]}, Block[{t$95$1 = N[Exp[t$95$0], $MachinePrecision]}, Block[{t$95$2 = N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * t$95$1), $MachinePrecision]}, If[LessEqual[t$95$2, 2.0], t$95$2, N[(t$95$0 * t$95$1), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)\\
t_1 := e^{t\_0}\\
t_2 := \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t\_1\\
\mathbf{if}\;t\_2 \leq 2:\\
\;\;\;\;t\_2\\

\mathbf{else}:\\
\;\;\;\;t\_0 \cdot t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) #s(literal 2 binary64)) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) #s(literal 2 binary64)) M) #s(literal 2 binary64))) (-.f64 l (fabs.f64 (-.f64 m n)))))) < 2

    1. Initial program 96.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

    if 2 < (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) #s(literal 2 binary64)) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) #s(literal 2 binary64)) M) #s(literal 2 binary64))) (-.f64 l (fabs.f64 (-.f64 m n))))))

    1. Initial program 22.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

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

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

Alternative 2: 8.3% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
t_0 := \frac{m + n}{2} - M\\
t_1 := \frac{K \cdot \left(m + n\right)}{2}\\
\mathbf{if}\;\cos \left(t\_1 - M\right) \cdot e^{\left(-{t\_0}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \leq 0:\\
\;\;\;\;t\_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) #s(literal 2 binary64)) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) #s(literal 2 binary64)) M) #s(literal 2 binary64))) (-.f64 l (fabs.f64 (-.f64 m n)))))) < -0.0

    1. Initial program 96.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

      \[\leadsto \color{blue}{K \cdot \left(\frac{-1}{2} \cdot \left(e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right) + K \cdot \left(\frac{-1}{8} \cdot \left(\cos \left(\mathsf{neg}\left(M\right)\right) \cdot \left(e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \cdot {\left(m + n\right)}^{2}\right)\right) + \frac{1}{48} \cdot \left(K \cdot \left(e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot {\left(m + n\right)}^{3}\right)\right)\right)\right)\right) + \cos \left(\mathsf{neg}\left(M\right)\right) \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
    4. Applied rewrites7.4%

      \[\leadsto \color{blue}{\frac{K \cdot \left(m + n\right)}{2}} \]

    if -0.0 < (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) #s(literal 2 binary64)) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) #s(literal 2 binary64)) M) #s(literal 2 binary64))) (-.f64 l (fabs.f64 (-.f64 m n))))))

    1. Initial program 29.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

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

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \]
    6. Applied rewrites10.3%

      \[\leadsto \cos \left(\frac{-{\left(\frac{m + n}{2} - M\right)}^{2}}{2} - M\right) \]
    7. Taylor expanded in K around 0

      \[\leadsto \cos \left(-1 \cdot M\right) \]
    8. Applied rewrites12.2%

      \[\leadsto \cos \left(\frac{m + n}{2} - M\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 76.1% 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}
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. Add Preprocessing

Alternative 4: 4.8% accurate, 18.0× speedup?

\[\begin{array}{l} \\ \frac{K \cdot \left(m + n\right)}{2} \end{array} \]
(FPCore (K m n M l) :precision binary64 (/ (* K (+ m n)) 2.0))
double code(double K, double m, double n, double M, double l) {
	return (K * (m + n)) / 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 = (k * (m + n)) / 2.0d0
end function
public static double code(double K, double m, double n, double M, double l) {
	return (K * (m + n)) / 2.0;
}
def code(K, m, n, M, l):
	return (K * (m + n)) / 2.0
function code(K, m, n, M, l)
	return Float64(Float64(K * Float64(m + n)) / 2.0)
end
function tmp = code(K, m, n, M, l)
	tmp = (K * (m + n)) / 2.0;
end
code[K_, m_, n_, M_, l_] := N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}

\\
\frac{K \cdot \left(m + n\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

    \[\leadsto \color{blue}{K \cdot \left(\frac{-1}{2} \cdot \left(e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right) + K \cdot \left(\frac{-1}{8} \cdot \left(\cos \left(\mathsf{neg}\left(M\right)\right) \cdot \left(e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \cdot {\left(m + n\right)}^{2}\right)\right) + \frac{1}{48} \cdot \left(K \cdot \left(e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot {\left(m + n\right)}^{3}\right)\right)\right)\right)\right) + \cos \left(\mathsf{neg}\left(M\right)\right) \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
  4. Applied rewrites6.0%

    \[\leadsto \color{blue}{\frac{K \cdot \left(m + n\right)}{2}} \]
  5. Add Preprocessing

Alternative 5: 4.8% accurate, 39.9× speedup?

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

\\
K \cdot \left(m + n\right)
\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

    \[\leadsto \color{blue}{\frac{-1}{2} \cdot \left(K \cdot \left(e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right)\right) + \cos \left(\mathsf{neg}\left(M\right)\right) \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
  4. Applied rewrites5.4%

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

    \[\leadsto \cos \left(\mathsf{neg}\left(M\right)\right) + \color{blue}{\frac{-1}{2} \cdot \left(K \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right)} \]
  6. Applied rewrites6.0%

    \[\leadsto K \cdot \color{blue}{\left(m + n\right)} \]
  7. Add Preprocessing

Alternative 6: 2.6% accurate, 89.8× speedup?

\[\begin{array}{l} \\ m + n \end{array} \]
(FPCore (K m n M l) :precision binary64 (+ m n))
double code(double K, double m, double n, double M, double l) {
	return 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 = m + n
end function
public static double code(double K, double m, double n, double M, double l) {
	return m + n;
}
def code(K, m, n, M, l):
	return m + n
function code(K, m, n, M, l)
	return Float64(m + n)
end
function tmp = code(K, m, n, M, l)
	tmp = m + n;
end
code[K_, m_, n_, M_, l_] := N[(m + n), $MachinePrecision]
\begin{array}{l}

\\
m + n
\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

    \[\leadsto \color{blue}{\frac{-1}{2} \cdot \left(K \cdot \left(e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right)\right) + \cos \left(\mathsf{neg}\left(M\right)\right) \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
  4. Applied rewrites5.4%

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

    \[\leadsto \cos \left(\mathsf{neg}\left(M\right)\right) + \color{blue}{K \cdot \left(K \cdot \left(\frac{-1}{8} \cdot \left(\cos \left(\mathsf{neg}\left(M\right)\right) \cdot {\left(m + n\right)}^{2}\right) + \frac{1}{48} \cdot \left(K \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot {\left(m + n\right)}^{3}\right)\right)\right) - \frac{1}{2} \cdot \left(\sin \left(\mathsf{neg}\left(M\right)\right) \cdot \left(m + n\right)\right)\right)} \]
  6. Applied rewrites2.6%

    \[\leadsto m + \color{blue}{n} \]
  7. Add Preprocessing

Reproduce

?
herbie shell --seed 2024321 
(FPCore (K m n M l)
  :name "Maksimov and Kolovsky, Equation (32)"
  :precision binary64
  :pre (TRUE)
  (* (cos (- (/ (* K (+ m n)) 2.0) M)) (exp (- (- (pow (- (/ (+ m n) 2.0) M) 2.0)) (- l (fabs (- m n)))))))