Maksimov and Kolovsky, Equation (4)

?

Percentage Accurate: 86.3% → 99.5%
Time: 11.9s
Precision: binary64
Cost: 46217

?

\[\left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U \]
\[\begin{array}{l} t_0 := e^{\ell} - e^{-\ell}\\ \mathbf{if}\;t_0 \leq -\infty \lor \neg \left(t_0 \leq 0\right):\\ \;\;\;\;\cos \left(\frac{K}{2}\right) \cdot \left(t_0 \cdot J\right) + U\\ \mathbf{else}:\\ \;\;\;\;U + \left({\ell}^{3} \cdot \left(J \cdot 0.3333333333333333\right) + \left(J \cdot \cos \left(K \cdot 0.5\right)\right) \cdot \left(\ell \cdot 2\right)\right)\\ \end{array} \]
(FPCore (J l K U)
 :precision binary64
 (+ (* (* J (- (exp l) (exp (- l)))) (cos (/ K 2.0))) U))
(FPCore (J l K U)
 :precision binary64
 (let* ((t_0 (- (exp l) (exp (- l)))))
   (if (or (<= t_0 (- INFINITY)) (not (<= t_0 0.0)))
     (+ (* (cos (/ K 2.0)) (* t_0 J)) U)
     (+
      U
      (+
       (* (pow l 3.0) (* J 0.3333333333333333))
       (* (* J (cos (* K 0.5))) (* l 2.0)))))))
double code(double J, double l, double K, double U) {
	return ((J * (exp(l) - exp(-l))) * cos((K / 2.0))) + U;
}
double code(double J, double l, double K, double U) {
	double t_0 = exp(l) - exp(-l);
	double tmp;
	if ((t_0 <= -((double) INFINITY)) || !(t_0 <= 0.0)) {
		tmp = (cos((K / 2.0)) * (t_0 * J)) + U;
	} else {
		tmp = U + ((pow(l, 3.0) * (J * 0.3333333333333333)) + ((J * cos((K * 0.5))) * (l * 2.0)));
	}
	return tmp;
}
public static double code(double J, double l, double K, double U) {
	return ((J * (Math.exp(l) - Math.exp(-l))) * Math.cos((K / 2.0))) + U;
}
public static double code(double J, double l, double K, double U) {
	double t_0 = Math.exp(l) - Math.exp(-l);
	double tmp;
	if ((t_0 <= -Double.POSITIVE_INFINITY) || !(t_0 <= 0.0)) {
		tmp = (Math.cos((K / 2.0)) * (t_0 * J)) + U;
	} else {
		tmp = U + ((Math.pow(l, 3.0) * (J * 0.3333333333333333)) + ((J * Math.cos((K * 0.5))) * (l * 2.0)));
	}
	return tmp;
}
def code(J, l, K, U):
	return ((J * (math.exp(l) - math.exp(-l))) * math.cos((K / 2.0))) + U
def code(J, l, K, U):
	t_0 = math.exp(l) - math.exp(-l)
	tmp = 0
	if (t_0 <= -math.inf) or not (t_0 <= 0.0):
		tmp = (math.cos((K / 2.0)) * (t_0 * J)) + U
	else:
		tmp = U + ((math.pow(l, 3.0) * (J * 0.3333333333333333)) + ((J * math.cos((K * 0.5))) * (l * 2.0)))
	return tmp
function code(J, l, K, U)
	return Float64(Float64(Float64(J * Float64(exp(l) - exp(Float64(-l)))) * cos(Float64(K / 2.0))) + U)
end
function code(J, l, K, U)
	t_0 = Float64(exp(l) - exp(Float64(-l)))
	tmp = 0.0
	if ((t_0 <= Float64(-Inf)) || !(t_0 <= 0.0))
		tmp = Float64(Float64(cos(Float64(K / 2.0)) * Float64(t_0 * J)) + U);
	else
		tmp = Float64(U + Float64(Float64((l ^ 3.0) * Float64(J * 0.3333333333333333)) + Float64(Float64(J * cos(Float64(K * 0.5))) * Float64(l * 2.0))));
	end
	return tmp
end
function tmp = code(J, l, K, U)
	tmp = ((J * (exp(l) - exp(-l))) * cos((K / 2.0))) + U;
end
function tmp_2 = code(J, l, K, U)
	t_0 = exp(l) - exp(-l);
	tmp = 0.0;
	if ((t_0 <= -Inf) || ~((t_0 <= 0.0)))
		tmp = (cos((K / 2.0)) * (t_0 * J)) + U;
	else
		tmp = U + (((l ^ 3.0) * (J * 0.3333333333333333)) + ((J * cos((K * 0.5))) * (l * 2.0)));
	end
	tmp_2 = tmp;
end
code[J_, l_, K_, U_] := N[(N[(N[(J * N[(N[Exp[l], $MachinePrecision] - N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + U), $MachinePrecision]
code[J_, l_, K_, U_] := Block[{t$95$0 = N[(N[Exp[l], $MachinePrecision] - N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, (-Infinity)], N[Not[LessEqual[t$95$0, 0.0]], $MachinePrecision]], N[(N[(N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision] * N[(t$95$0 * J), $MachinePrecision]), $MachinePrecision] + U), $MachinePrecision], N[(U + N[(N[(N[Power[l, 3.0], $MachinePrecision] * N[(J * 0.3333333333333333), $MachinePrecision]), $MachinePrecision] + N[(N[(J * N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[(l * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U
\begin{array}{l}
t_0 := e^{\ell} - e^{-\ell}\\
\mathbf{if}\;t_0 \leq -\infty \lor \neg \left(t_0 \leq 0\right):\\
\;\;\;\;\cos \left(\frac{K}{2}\right) \cdot \left(t_0 \cdot J\right) + U\\

\mathbf{else}:\\
\;\;\;\;U + \left({\ell}^{3} \cdot \left(J \cdot 0.3333333333333333\right) + \left(J \cdot \cos \left(K \cdot 0.5\right)\right) \cdot \left(\ell \cdot 2\right)\right)\\


\end{array}

Local Percentage Accuracy?

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.

Herbie found 13 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Split input into 2 regimes
  2. if (-.f64 (exp.f64 l) (exp.f64 (neg.f64 l))) < -inf.0 or 0.0 < (-.f64 (exp.f64 l) (exp.f64 (neg.f64 l)))

    1. Initial program 100.0%

      \[\left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U \]

    if -inf.0 < (-.f64 (exp.f64 l) (exp.f64 (neg.f64 l))) < 0.0

    1. Initial program 74.8%

      \[\left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U \]
    2. Taylor expanded in l around 0 99.9%

      \[\leadsto \color{blue}{\left(2 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \left(\ell \cdot J\right)\right) + 0.3333333333333333 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \left({\ell}^{3} \cdot J\right)\right)\right)} + U \]
    3. Simplified99.9%

      \[\leadsto \color{blue}{J \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \mathsf{fma}\left(0.3333333333333333, {\ell}^{3}, 2 \cdot \ell\right)\right)} + U \]
      Step-by-step derivation

      [Start]99.9

      \[ \left(2 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \left(\ell \cdot J\right)\right) + 0.3333333333333333 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \left({\ell}^{3} \cdot J\right)\right)\right) + U \]

      associate-*r* [=>]99.9

      \[ \left(2 \cdot \color{blue}{\left(\left(\cos \left(0.5 \cdot K\right) \cdot \ell\right) \cdot J\right)} + 0.3333333333333333 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \left({\ell}^{3} \cdot J\right)\right)\right) + U \]

      associate-*r* [=>]99.9

      \[ \left(\color{blue}{\left(2 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \ell\right)\right) \cdot J} + 0.3333333333333333 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \left({\ell}^{3} \cdot J\right)\right)\right) + U \]

      associate-*r* [=>]99.9

      \[ \left(\left(2 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \ell\right)\right) \cdot J + 0.3333333333333333 \cdot \color{blue}{\left(\left(\cos \left(0.5 \cdot K\right) \cdot {\ell}^{3}\right) \cdot J\right)}\right) + U \]

      associate-*r* [=>]99.9

      \[ \left(\left(2 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \ell\right)\right) \cdot J + \color{blue}{\left(0.3333333333333333 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot {\ell}^{3}\right)\right) \cdot J}\right) + U \]

      distribute-rgt-out [=>]99.9

      \[ \color{blue}{J \cdot \left(2 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \ell\right) + 0.3333333333333333 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot {\ell}^{3}\right)\right)} + U \]

      *-commutative [=>]99.9

      \[ J \cdot \left(2 \cdot \color{blue}{\left(\ell \cdot \cos \left(0.5 \cdot K\right)\right)} + 0.3333333333333333 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot {\ell}^{3}\right)\right) + U \]

      associate-*r* [=>]99.9

      \[ J \cdot \left(\color{blue}{\left(2 \cdot \ell\right) \cdot \cos \left(0.5 \cdot K\right)} + 0.3333333333333333 \cdot \left(\cos \left(0.5 \cdot K\right) \cdot {\ell}^{3}\right)\right) + U \]

      *-commutative [=>]99.9

      \[ J \cdot \left(\left(2 \cdot \ell\right) \cdot \cos \left(0.5 \cdot K\right) + 0.3333333333333333 \cdot \color{blue}{\left({\ell}^{3} \cdot \cos \left(0.5 \cdot K\right)\right)}\right) + U \]

      associate-*r* [=>]99.9

      \[ J \cdot \left(\left(2 \cdot \ell\right) \cdot \cos \left(0.5 \cdot K\right) + \color{blue}{\left(0.3333333333333333 \cdot {\ell}^{3}\right) \cdot \cos \left(0.5 \cdot K\right)}\right) + U \]

      distribute-rgt-out [=>]99.9

      \[ J \cdot \color{blue}{\left(\cos \left(0.5 \cdot K\right) \cdot \left(2 \cdot \ell + 0.3333333333333333 \cdot {\ell}^{3}\right)\right)} + U \]

      +-commutative [<=]99.9

      \[ J \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \color{blue}{\left(0.3333333333333333 \cdot {\ell}^{3} + 2 \cdot \ell\right)}\right) + U \]

      fma-def [=>]99.9

      \[ J \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \color{blue}{\mathsf{fma}\left(0.3333333333333333, {\ell}^{3}, 2 \cdot \ell\right)}\right) + U \]
    4. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\left(\left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \left(0.3333333333333333 \cdot {\ell}^{3}\right) + \left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \left(\ell \cdot 2\right)\right)} + U \]
      Step-by-step derivation

      [Start]99.9

      \[ J \cdot \left(\cos \left(0.5 \cdot K\right) \cdot \mathsf{fma}\left(0.3333333333333333, {\ell}^{3}, 2 \cdot \ell\right)\right) + U \]

      associate-*r* [=>]100.0

      \[ \color{blue}{\left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \mathsf{fma}\left(0.3333333333333333, {\ell}^{3}, 2 \cdot \ell\right)} + U \]

      fma-udef [=>]100.0

      \[ \left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \color{blue}{\left(0.3333333333333333 \cdot {\ell}^{3} + 2 \cdot \ell\right)} + U \]

      distribute-lft-in [=>]100.0

      \[ \color{blue}{\left(\left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \left(0.3333333333333333 \cdot {\ell}^{3}\right) + \left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \left(2 \cdot \ell\right)\right)} + U \]

      *-commutative [=>]100.0

      \[ \left(\left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \left(0.3333333333333333 \cdot {\ell}^{3}\right) + \left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \color{blue}{\left(\ell \cdot 2\right)}\right) + U \]
    5. Taylor expanded in K around 0 100.0%

      \[\leadsto \left(\color{blue}{0.3333333333333333 \cdot \left({\ell}^{3} \cdot J\right)} + \left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \left(\ell \cdot 2\right)\right) + U \]
    6. Simplified100.0%

      \[\leadsto \left(\color{blue}{{\ell}^{3} \cdot \left(0.3333333333333333 \cdot J\right)} + \left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \left(\ell \cdot 2\right)\right) + U \]
      Step-by-step derivation

      [Start]100.0

      \[ \left(0.3333333333333333 \cdot \left({\ell}^{3} \cdot J\right) + \left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \left(\ell \cdot 2\right)\right) + U \]

      associate-*r* [=>]100.0

      \[ \left(\color{blue}{\left(0.3333333333333333 \cdot {\ell}^{3}\right) \cdot J} + \left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \left(\ell \cdot 2\right)\right) + U \]

      *-commutative [=>]100.0

      \[ \left(\color{blue}{\left({\ell}^{3} \cdot 0.3333333333333333\right)} \cdot J + \left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \left(\ell \cdot 2\right)\right) + U \]

      associate-*l* [=>]100.0

      \[ \left(\color{blue}{{\ell}^{3} \cdot \left(0.3333333333333333 \cdot J\right)} + \left(J \cdot \cos \left(0.5 \cdot K\right)\right) \cdot \left(\ell \cdot 2\right)\right) + U \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{\ell} - e^{-\ell} \leq -\infty \lor \neg \left(e^{\ell} - e^{-\ell} \leq 0\right):\\ \;\;\;\;\cos \left(\frac{K}{2}\right) \cdot \left(\left(e^{\ell} - e^{-\ell}\right) \cdot J\right) + U\\ \mathbf{else}:\\ \;\;\;\;U + \left({\ell}^{3} \cdot \left(J \cdot 0.3333333333333333\right) + \left(J \cdot \cos \left(K \cdot 0.5\right)\right) \cdot \left(\ell \cdot 2\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy93.8%
Cost14348
\[\begin{array}{l} t_0 := \cos \left(K \cdot 0.5\right)\\ t_1 := U + \left(0.3333333333333333 \cdot t_0\right) \cdot \left(J \cdot {\ell}^{3}\right)\\ t_2 := U + \left(e^{\ell} - e^{-\ell}\right) \cdot J\\ \mathbf{if}\;\ell \leq -9.8 \cdot 10^{+86}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;\ell \leq -0.013:\\ \;\;\;\;t_2\\ \mathbf{elif}\;\ell \leq 1.1 \cdot 10^{-45}:\\ \;\;\;\;U + \left({\ell}^{3} \cdot \left(J \cdot 0.3333333333333333\right) + \left(J \cdot t_0\right) \cdot \left(\ell \cdot 2\right)\right)\\ \mathbf{elif}\;\ell \leq 1.05 \cdot 10^{+83}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Accuracy93.8%
Cost14096
\[\begin{array}{l} t_0 := \cos \left(K \cdot 0.5\right)\\ t_1 := U + \left(0.3333333333333333 \cdot t_0\right) \cdot \left(J \cdot {\ell}^{3}\right)\\ t_2 := U + \left(e^{\ell} - e^{-\ell}\right) \cdot J\\ \mathbf{if}\;\ell \leq -9.8 \cdot 10^{+86}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;\ell \leq -0.00028:\\ \;\;\;\;t_2\\ \mathbf{elif}\;\ell \leq 1.1 \cdot 10^{-45}:\\ \;\;\;\;U + J \cdot \left(t_0 \cdot \left(\ell \cdot 2\right)\right)\\ \mathbf{elif}\;\ell \leq 1.15 \cdot 10^{+84}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Accuracy78.7%
Cost13828
\[\begin{array}{l} \mathbf{if}\;\cos \left(\frac{K}{2}\right) \leq 0.05:\\ \;\;\;\;U + J \cdot \left(\cos \left(K \cdot 0.5\right) \cdot \left(\ell \cdot 2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;U + J \cdot \left({\ell}^{3} \cdot 0.3333333333333333 + \ell \cdot 2\right)\\ \end{array} \]
Alternative 4
Accuracy81.7%
Cost13648
\[\begin{array}{l} t_0 := \mathsf{log1p}\left(\mathsf{expm1}\left(\frac{-8}{U} - U\right)\right)\\ t_1 := U + J \cdot \left({\ell}^{3} \cdot 0.3333333333333333 + \ell \cdot 2\right)\\ \mathbf{if}\;\ell \leq -1.42 \cdot 10^{+54}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;\ell \leq -490:\\ \;\;\;\;t_0\\ \mathbf{elif}\;\ell \leq 1.1 \cdot 10^{+19}:\\ \;\;\;\;U + J \cdot \left(\cos \left(K \cdot 0.5\right) \cdot \left(\ell \cdot 2\right)\right)\\ \mathbf{elif}\;\ell \leq 1.15 \cdot 10^{+84}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Accuracy86.1%
Cost13577
\[\begin{array}{l} \mathbf{if}\;\ell \leq -0.015 \lor \neg \left(\ell \leq 1.1 \cdot 10^{-45}\right):\\ \;\;\;\;U + \left(e^{\ell} - e^{-\ell}\right) \cdot J\\ \mathbf{else}:\\ \;\;\;\;U + J \cdot \left(\cos \left(K \cdot 0.5\right) \cdot \left(\ell \cdot 2\right)\right)\\ \end{array} \]
Alternative 6
Accuracy65.7%
Cost7633
\[\begin{array}{l} t_0 := U + J \cdot \left(\cos \left(K \cdot 0.5\right) \cdot \left(\ell \cdot 2\right)\right)\\ \mathbf{if}\;\ell \leq -2.26 \cdot 10^{+164}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;\ell \leq -620:\\ \;\;\;\;\frac{\frac{64}{U \cdot U} - U \cdot U}{U + \frac{-8}{U}}\\ \mathbf{elif}\;\ell \leq 2.65 \cdot 10^{+17} \lor \neg \left(\ell \leq 2.45 \cdot 10^{+201}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;{U}^{-8}\\ \end{array} \]
Alternative 7
Accuracy47.0%
Cost6792
\[\begin{array}{l} \mathbf{if}\;\ell \leq -2.4 \cdot 10^{-13}:\\ \;\;\;\;\frac{\frac{64}{U \cdot U} - U \cdot U}{U + \frac{-8}{U}}\\ \mathbf{elif}\;\ell \leq 2.65 \cdot 10^{+17}:\\ \;\;\;\;U\\ \mathbf{else}:\\ \;\;\;\;{U}^{-8}\\ \end{array} \]
Alternative 8
Accuracy47.9%
Cost1225
\[\begin{array}{l} \mathbf{if}\;\ell \leq -2.4 \cdot 10^{-13} \lor \neg \left(\ell \leq 1.1 \cdot 10^{+19}\right):\\ \;\;\;\;\frac{\frac{64}{U \cdot U} - U \cdot U}{U + \frac{-8}{U}}\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]
Alternative 9
Accuracy43.9%
Cost969
\[\begin{array}{l} \mathbf{if}\;\ell \leq -1.46 \cdot 10^{-30} \lor \neg \left(\ell \leq 1.95 \cdot 10^{+19}\right):\\ \;\;\;\;U + J \cdot \left(-0.5 + \left(K \cdot K\right) \cdot 0.0625\right)\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]
Alternative 10
Accuracy39.3%
Cost324
\[\begin{array}{l} \mathbf{if}\;\ell \leq -2.4 \cdot 10^{-13}:\\ \;\;\;\;U \cdot U\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]
Alternative 11
Accuracy2.7%
Cost64
\[1 \]
Alternative 12
Accuracy37.3%
Cost64
\[U \]

Reproduce?

herbie shell --seed 2023162 
(FPCore (J l K U)
  :name "Maksimov and Kolovsky, Equation (4)"
  :precision binary64
  (+ (* (* J (- (exp l) (exp (- l)))) (cos (/ K 2.0))) U))