Maksimov and Kolovsky, Equation (3)

?

Percentage Accurate: 73.2% → 88.3%
Time: 27.5s
Precision: binary64
Cost: 20352

?

\[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
\[\begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ \left(J \cdot \left(-2 \cdot t_0\right)\right) \cdot \mathsf{hypot}\left(1, \frac{U}{J \cdot \left(2 \cdot t_0\right)}\right) \end{array} \]
(FPCore (J K U)
 :precision binary64
 (*
  (* (* -2.0 J) (cos (/ K 2.0)))
  (sqrt (+ 1.0 (pow (/ U (* (* 2.0 J) (cos (/ K 2.0)))) 2.0)))))
(FPCore (J K U)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0))))
   (* (* J (* -2.0 t_0)) (hypot 1.0 (/ U (* J (* 2.0 t_0)))))))
double code(double J, double K, double U) {
	return ((-2.0 * J) * cos((K / 2.0))) * sqrt((1.0 + pow((U / ((2.0 * J) * cos((K / 2.0)))), 2.0)));
}
double code(double J, double K, double U) {
	double t_0 = cos((K / 2.0));
	return (J * (-2.0 * t_0)) * hypot(1.0, (U / (J * (2.0 * t_0))));
}
public static double code(double J, double K, double U) {
	return ((-2.0 * J) * Math.cos((K / 2.0))) * Math.sqrt((1.0 + Math.pow((U / ((2.0 * J) * Math.cos((K / 2.0)))), 2.0)));
}
public static double code(double J, double K, double U) {
	double t_0 = Math.cos((K / 2.0));
	return (J * (-2.0 * t_0)) * Math.hypot(1.0, (U / (J * (2.0 * t_0))));
}
def code(J, K, U):
	return ((-2.0 * J) * math.cos((K / 2.0))) * math.sqrt((1.0 + math.pow((U / ((2.0 * J) * math.cos((K / 2.0)))), 2.0)))
def code(J, K, U):
	t_0 = math.cos((K / 2.0))
	return (J * (-2.0 * t_0)) * math.hypot(1.0, (U / (J * (2.0 * t_0))))
function code(J, K, U)
	return Float64(Float64(Float64(-2.0 * J) * cos(Float64(K / 2.0))) * sqrt(Float64(1.0 + (Float64(U / Float64(Float64(2.0 * J) * cos(Float64(K / 2.0)))) ^ 2.0))))
end
function code(J, K, U)
	t_0 = cos(Float64(K / 2.0))
	return Float64(Float64(J * Float64(-2.0 * t_0)) * hypot(1.0, Float64(U / Float64(J * Float64(2.0 * t_0)))))
end
function tmp = code(J, K, U)
	tmp = ((-2.0 * J) * cos((K / 2.0))) * sqrt((1.0 + ((U / ((2.0 * J) * cos((K / 2.0)))) ^ 2.0)));
end
function tmp = code(J, K, U)
	t_0 = cos((K / 2.0));
	tmp = (J * (-2.0 * t_0)) * hypot(1.0, (U / (J * (2.0 * t_0))));
end
code[J_, K_, U_] := N[(N[(N[(-2.0 * J), $MachinePrecision] * N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sqrt[N[(1.0 + N[Power[N[(U / N[(N[(2.0 * J), $MachinePrecision] * N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[J_, K_, U_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, N[(N[(J * N[(-2.0 * t$95$0), $MachinePrecision]), $MachinePrecision] * N[Sqrt[1.0 ^ 2 + N[(U / N[(J * N[(2.0 * t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]
\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
\left(J \cdot \left(-2 \cdot t_0\right)\right) \cdot \mathsf{hypot}\left(1, \frac{U}{J \cdot \left(2 \cdot t_0\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 11 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. Initial program 73.0%

    \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
  2. Simplified87.4%

    \[\leadsto \color{blue}{\left(J \cdot \left(-2 \cdot \cos \left(\frac{K}{2}\right)\right)\right) \cdot \mathsf{hypot}\left(1, \frac{U}{J \cdot \left(2 \cdot \cos \left(\frac{K}{2}\right)\right)}\right)} \]
    Step-by-step derivation

    [Start]73.0

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

    *-commutative [=>]73.0

    \[ \left(\color{blue}{\left(J \cdot -2\right)} \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]

    associate-*l* [=>]73.0

    \[ \color{blue}{\left(J \cdot \left(-2 \cdot \cos \left(\frac{K}{2}\right)\right)\right)} \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]

    unpow2 [=>]73.0

    \[ \left(J \cdot \left(-2 \cdot \cos \left(\frac{K}{2}\right)\right)\right) \cdot \sqrt{1 + \color{blue}{\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}} \]

    hypot-1-def [=>]87.4

    \[ \left(J \cdot \left(-2 \cdot \cos \left(\frac{K}{2}\right)\right)\right) \cdot \color{blue}{\mathsf{hypot}\left(1, \frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)} \]

    *-commutative [=>]87.4

    \[ \left(J \cdot \left(-2 \cdot \cos \left(\frac{K}{2}\right)\right)\right) \cdot \mathsf{hypot}\left(1, \frac{U}{\color{blue}{\left(J \cdot 2\right)} \cdot \cos \left(\frac{K}{2}\right)}\right) \]

    associate-*l* [=>]87.4

    \[ \left(J \cdot \left(-2 \cdot \cos \left(\frac{K}{2}\right)\right)\right) \cdot \mathsf{hypot}\left(1, \frac{U}{\color{blue}{J \cdot \left(2 \cdot \cos \left(\frac{K}{2}\right)\right)}}\right) \]
  3. Final simplification87.4%

    \[\leadsto \left(J \cdot \left(-2 \cdot \cos \left(\frac{K}{2}\right)\right)\right) \cdot \mathsf{hypot}\left(1, \frac{U}{J \cdot \left(2 \cdot \cos \left(\frac{K}{2}\right)\right)}\right) \]

Alternatives

Alternative 1
Accuracy88.2%
Cost20352
\[\begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ J \cdot \left(t_0 \cdot \left(-2 \cdot \mathsf{hypot}\left(1, \frac{U}{J \cdot \left(2 \cdot t_0\right)}\right)\right)\right) \end{array} \]
Alternative 2
Accuracy73.3%
Cost13961
\[\begin{array}{l} \mathbf{if}\;J \leq -2 \cdot 10^{-212} \lor \neg \left(J \leq 10^{-170}\right):\\ \;\;\;\;J \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \left(-2 \cdot \mathsf{hypot}\left(1, \frac{U \cdot 0.5}{J}\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-U\\ \end{array} \]
Alternative 3
Accuracy64.0%
Cost7568
\[\begin{array}{l} t_0 := \left(J \cdot -2\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\\ t_1 := \cos \left(K \cdot 0.5\right) \cdot \left(J \cdot -2\right)\\ \mathbf{if}\;J \leq -2.7 \cdot 10^{+130}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;J \leq -4.7 \cdot 10^{-209}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;J \leq 9.6 \cdot 10^{-158}:\\ \;\;\;\;-U\\ \mathbf{elif}\;J \leq 5.8 \cdot 10^{+131}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Accuracy58.2%
Cost7112
\[\begin{array}{l} \mathbf{if}\;U \leq -1.12 \cdot 10^{+104}:\\ \;\;\;\;-U\\ \mathbf{elif}\;U \leq 2 \cdot 10^{+14}:\\ \;\;\;\;\cos \left(K \cdot 0.5\right) \cdot \left(J \cdot -2\right)\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot \left(J \cdot \frac{J}{U}\right) - U\\ \end{array} \]
Alternative 5
Accuracy38.1%
Cost1489
\[\begin{array}{l} t_0 := -2 \cdot \left(J \cdot \frac{J}{U}\right) - U\\ \mathbf{if}\;U \leq -8.5 \cdot 10^{+61}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;U \leq -1.55 \cdot 10^{+25}:\\ \;\;\;\;J \cdot \left(-2 + \left(K \cdot K\right) \cdot 0.25\right)\\ \mathbf{elif}\;U \leq -4.5 \cdot 10^{-142} \lor \neg \left(U \leq 2.6 \cdot 10^{+15}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;J \cdot -2 + J \cdot \frac{-0.25}{\frac{J}{U} \cdot \frac{J}{U}}\\ \end{array} \]
Alternative 6
Accuracy38.1%
Cost1361
\[\begin{array}{l} t_0 := -2 \cdot \left(J \cdot \frac{J}{U}\right) - U\\ \mathbf{if}\;U \leq -2.65 \cdot 10^{+65}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;U \leq -3.35 \cdot 10^{+25}:\\ \;\;\;\;J \cdot \left(-2 + \left(K \cdot K\right) \cdot 0.25\right)\\ \mathbf{elif}\;U \leq -4.5 \cdot 10^{-142} \lor \neg \left(U \leq 2.6 \cdot 10^{+17}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;J \cdot \left(-2 + \frac{-0.25}{\frac{J}{U} \cdot \frac{J}{U}}\right)\\ \end{array} \]
Alternative 7
Accuracy38.0%
Cost1105
\[\begin{array}{l} t_0 := -2 \cdot \left(J \cdot \frac{J}{U}\right) - U\\ \mathbf{if}\;U \leq -1.44 \cdot 10^{+63}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;U \leq -1.52 \cdot 10^{+25}:\\ \;\;\;\;J \cdot \left(-2 + \left(K \cdot K\right) \cdot 0.25\right)\\ \mathbf{elif}\;U \leq -4.5 \cdot 10^{-142} \lor \neg \left(U \leq 58000000000\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;J \cdot -2\\ \end{array} \]
Alternative 8
Accuracy38.0%
Cost720
\[\begin{array}{l} \mathbf{if}\;U \leq -3.95 \cdot 10^{+121}:\\ \;\;\;\;-U\\ \mathbf{elif}\;U \leq -7.4 \cdot 10^{+24}:\\ \;\;\;\;U\\ \mathbf{elif}\;U \leq -4.5 \cdot 10^{-142}:\\ \;\;\;\;-U\\ \mathbf{elif}\;U \leq 3300000000000:\\ \;\;\;\;J \cdot -2\\ \mathbf{else}:\\ \;\;\;\;-U\\ \end{array} \]
Alternative 9
Accuracy26.8%
Cost392
\[\begin{array}{l} \mathbf{if}\;K \leq -1.5 \cdot 10^{+91}:\\ \;\;\;\;-U\\ \mathbf{elif}\;K \leq -1.4 \cdot 10^{+49}:\\ \;\;\;\;U\\ \mathbf{else}:\\ \;\;\;\;-U\\ \end{array} \]
Alternative 10
Accuracy26.9%
Cost64
\[U \]

Reproduce?

herbie shell --seed 2023161 
(FPCore (J K U)
  :name "Maksimov and Kolovsky, Equation (3)"
  :precision binary64
  (* (* (* -2.0 J) (cos (/ K 2.0))) (sqrt (+ 1.0 (pow (/ U (* (* 2.0 J) (cos (/ K 2.0)))) 2.0)))))