Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.4% → 96.8%
Time: 11.8s
Alternatives: 8
Speedup: 2.6×

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 8 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.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.8% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(0.5, m + n, -M\right)\\ \cos M \cdot e^{\left|n - m\right| - \mathsf{fma}\left(t\_0, t\_0, \ell\right)} \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (fma 0.5 (+ m n) (- M))))
   (* (cos M) (exp (- (fabs (- n m)) (fma t_0 t_0 l))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = fma(0.5, (m + n), -M);
	return cos(M) * exp((fabs((n - m)) - fma(t_0, t_0, l)));
}
function code(K, m, n, M, l)
	t_0 = fma(0.5, Float64(m + n), Float64(-M))
	return Float64(cos(M) * exp(Float64(abs(Float64(n - m)) - fma(t_0, t_0, l))))
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(0.5 * N[(m + n), $MachinePrecision] + (-M)), $MachinePrecision]}, N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - N[(t$95$0 * t$95$0 + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(0.5, m + n, -M\right)\\
\cos M \cdot e^{\left|n - m\right| - \mathsf{fma}\left(t\_0, t\_0, \ell\right)}
\end{array}
\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}{\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. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto \color{blue}{\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)}} \]
    2. cos-negN/A

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

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    4. lower-exp.f64N/A

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{\left|n - m\right|} - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    7. sub-negN/A

      \[\leadsto \cos M \cdot e^{\left|\color{blue}{n + \left(\mathsf{neg}\left(m\right)\right)}\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    8. mul-1-negN/A

      \[\leadsto \cos M \cdot e^{\left|n + \color{blue}{-1 \cdot m}\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    9. lower-fabs.f64N/A

      \[\leadsto \cos M \cdot e^{\color{blue}{\left|n + -1 \cdot m\right|} - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    10. mul-1-negN/A

      \[\leadsto \cos M \cdot e^{\left|n + \color{blue}{\left(\mathsf{neg}\left(m\right)\right)}\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    11. sub-negN/A

      \[\leadsto \cos M \cdot e^{\left|\color{blue}{n - m}\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    12. lower--.f64N/A

      \[\leadsto \cos M \cdot e^{\left|\color{blue}{n - m}\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    13. +-commutativeN/A

      \[\leadsto \cos M \cdot e^{\left|n - m\right| - \color{blue}{\left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)}} \]
    14. unpow2N/A

      \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\color{blue}{\left(\frac{1}{2} \cdot \left(m + n\right) - M\right) \cdot \left(\frac{1}{2} \cdot \left(m + n\right) - M\right)} + \ell\right)} \]
    15. lower-fma.f64N/A

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

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

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

Alternative 2: 96.4% accurate, 2.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(0.5, m + n, -M\right)\\ e^{\left|n - m\right| - \mathsf{fma}\left(t\_0, t\_0, \ell\right)} \cdot 1 \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (fma 0.5 (+ m n) (- M))))
   (* (exp (- (fabs (- n m)) (fma t_0 t_0 l))) 1.0)))
double code(double K, double m, double n, double M, double l) {
	double t_0 = fma(0.5, (m + n), -M);
	return exp((fabs((n - m)) - fma(t_0, t_0, l))) * 1.0;
}
function code(K, m, n, M, l)
	t_0 = fma(0.5, Float64(m + n), Float64(-M))
	return Float64(exp(Float64(abs(Float64(n - m)) - fma(t_0, t_0, l))) * 1.0)
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(0.5 * N[(m + n), $MachinePrecision] + (-M)), $MachinePrecision]}, N[(N[Exp[N[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - N[(t$95$0 * t$95$0 + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 1.0), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(0.5, m + n, -M\right)\\
e^{\left|n - m\right| - \mathsf{fma}\left(t\_0, t\_0, \ell\right)} \cdot 1
\end{array}
\end{array}
Derivation
  1. Initial program 75.4%

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

    \[\leadsto \color{blue}{\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. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto \color{blue}{\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)}} \]
    2. cos-negN/A

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

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    4. lower-exp.f64N/A

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{\left|n - m\right|} - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    7. sub-negN/A

      \[\leadsto \cos M \cdot e^{\left|\color{blue}{n + \left(\mathsf{neg}\left(m\right)\right)}\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    8. mul-1-negN/A

      \[\leadsto \cos M \cdot e^{\left|n + \color{blue}{-1 \cdot m}\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    9. lower-fabs.f64N/A

      \[\leadsto \cos M \cdot e^{\color{blue}{\left|n + -1 \cdot m\right|} - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    10. mul-1-negN/A

      \[\leadsto \cos M \cdot e^{\left|n + \color{blue}{\left(\mathsf{neg}\left(m\right)\right)}\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    11. sub-negN/A

      \[\leadsto \cos M \cdot e^{\left|\color{blue}{n - m}\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    12. lower--.f64N/A

      \[\leadsto \cos M \cdot e^{\left|\color{blue}{n - m}\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    13. +-commutativeN/A

      \[\leadsto \cos M \cdot e^{\left|n - m\right| - \color{blue}{\left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)}} \]
    14. unpow2N/A

      \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\color{blue}{\left(\frac{1}{2} \cdot \left(m + n\right) - M\right) \cdot \left(\frac{1}{2} \cdot \left(m + n\right) - M\right)} + \ell\right)} \]
    15. lower-fma.f64N/A

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

    \[\leadsto \color{blue}{\cos M \cdot e^{\left|n - m\right| - \mathsf{fma}\left(\mathsf{fma}\left(0.5, n + m, -M\right), \mathsf{fma}\left(0.5, n + m, -M\right), \ell\right)}} \]
  6. Taylor expanded in M around 0

    \[\leadsto 1 \cdot e^{\color{blue}{\left|n - m\right| - \mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, n + m, \mathsf{neg}\left(M\right)\right), \mathsf{fma}\left(\frac{1}{2}, n + m, \mathsf{neg}\left(M\right)\right), \ell\right)}} \]
  7. Step-by-step derivation
    1. Applied rewrites96.4%

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

      \[\leadsto e^{\left|n - m\right| - \mathsf{fma}\left(\mathsf{fma}\left(0.5, m + n, -M\right), \mathsf{fma}\left(0.5, m + n, -M\right), \ell\right)} \cdot 1 \]
    3. Add Preprocessing

    Reproduce

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