Maksimov and Kolovsky, Equation (3)

Percentage Accurate: 73.0% → 99.1%
Time: 18.2s
Alternatives: 8
Speedup: 1.9×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ \left(\left(-2 \cdot J\right) \cdot t_0\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot t_0}\right)}^{2}} \end{array} \end{array} \]
(FPCore (J K U)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0))))
   (* (* (* -2.0 J) t_0) (sqrt (+ 1.0 (pow (/ U (* (* 2.0 J) t_0)) 2.0))))))
double code(double J, double K, double U) {
	double t_0 = cos((K / 2.0));
	return ((-2.0 * J) * t_0) * sqrt((1.0 + pow((U / ((2.0 * J) * t_0)), 2.0)));
}
real(8) function code(j, k, u)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u
    real(8) :: t_0
    t_0 = cos((k / 2.0d0))
    code = (((-2.0d0) * j) * t_0) * sqrt((1.0d0 + ((u / ((2.0d0 * j) * t_0)) ** 2.0d0)))
end function
public static double code(double J, double K, double U) {
	double t_0 = Math.cos((K / 2.0));
	return ((-2.0 * J) * t_0) * Math.sqrt((1.0 + Math.pow((U / ((2.0 * J) * t_0)), 2.0)));
}
def code(J, K, U):
	t_0 = math.cos((K / 2.0))
	return ((-2.0 * J) * t_0) * math.sqrt((1.0 + math.pow((U / ((2.0 * J) * t_0)), 2.0)))
function code(J, K, U)
	t_0 = cos(Float64(K / 2.0))
	return Float64(Float64(Float64(-2.0 * J) * t_0) * sqrt(Float64(1.0 + (Float64(U / Float64(Float64(2.0 * J) * t_0)) ^ 2.0))))
end
function tmp = code(J, K, U)
	t_0 = cos((K / 2.0));
	tmp = ((-2.0 * J) * t_0) * sqrt((1.0 + ((U / ((2.0 * J) * t_0)) ^ 2.0)));
end
code[J_, K_, U_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(-2.0 * J), $MachinePrecision] * t$95$0), $MachinePrecision] * N[Sqrt[N[(1.0 + N[Power[N[(U / N[(N[(2.0 * J), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
\left(\left(-2 \cdot J\right) \cdot t_0\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot t_0}\right)}^{2}}
\end{array}
\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: 73.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ \left(\left(-2 \cdot J\right) \cdot t_0\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot t_0}\right)}^{2}} \end{array} \end{array} \]
(FPCore (J K U)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0))))
   (* (* (* -2.0 J) t_0) (sqrt (+ 1.0 (pow (/ U (* (* 2.0 J) t_0)) 2.0))))))
double code(double J, double K, double U) {
	double t_0 = cos((K / 2.0));
	return ((-2.0 * J) * t_0) * sqrt((1.0 + pow((U / ((2.0 * J) * t_0)), 2.0)));
}
real(8) function code(j, k, u)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u
    real(8) :: t_0
    t_0 = cos((k / 2.0d0))
    code = (((-2.0d0) * j) * t_0) * sqrt((1.0d0 + ((u / ((2.0d0 * j) * t_0)) ** 2.0d0)))
end function
public static double code(double J, double K, double U) {
	double t_0 = Math.cos((K / 2.0));
	return ((-2.0 * J) * t_0) * Math.sqrt((1.0 + Math.pow((U / ((2.0 * J) * t_0)), 2.0)));
}
def code(J, K, U):
	t_0 = math.cos((K / 2.0))
	return ((-2.0 * J) * t_0) * math.sqrt((1.0 + math.pow((U / ((2.0 * J) * t_0)), 2.0)))
function code(J, K, U)
	t_0 = cos(Float64(K / 2.0))
	return Float64(Float64(Float64(-2.0 * J) * t_0) * sqrt(Float64(1.0 + (Float64(U / Float64(Float64(2.0 * J) * t_0)) ^ 2.0))))
end
function tmp = code(J, K, U)
	t_0 = cos((K / 2.0));
	tmp = ((-2.0 * J) * t_0) * sqrt((1.0 + ((U / ((2.0 * J) * t_0)) ^ 2.0)));
end
code[J_, K_, U_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(-2.0 * J), $MachinePrecision] * t$95$0), $MachinePrecision] * N[Sqrt[N[(1.0 + N[Power[N[(U / N[(N[(2.0 * J), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
\left(\left(-2 \cdot J\right) \cdot t_0\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot t_0}\right)}^{2}}
\end{array}
\end{array}

Alternative 1: 99.1% accurate, 0.3× speedup?

\[\begin{array}{l} U = |U|\\ \\ \begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ t_1 := \left(t_0 \cdot \left(-2 \cdot J\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{t_0 \cdot \left(J \cdot 2\right)}\right)}^{2}}\\ \mathbf{if}\;t_1 \leq -\infty:\\ \;\;\;\;-U\\ \mathbf{elif}\;t_1 \leq 10^{+300}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \end{array} \]
NOTE: U should be positive before calling this function
(FPCore (J K U)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0)))
        (t_1
         (*
          (* t_0 (* -2.0 J))
          (sqrt (+ 1.0 (pow (/ U (* t_0 (* J 2.0))) 2.0))))))
   (if (<= t_1 (- INFINITY)) (- U) (if (<= t_1 1e+300) t_1 U))))
U = abs(U);
double code(double J, double K, double U) {
	double t_0 = cos((K / 2.0));
	double t_1 = (t_0 * (-2.0 * J)) * sqrt((1.0 + pow((U / (t_0 * (J * 2.0))), 2.0)));
	double tmp;
	if (t_1 <= -((double) INFINITY)) {
		tmp = -U;
	} else if (t_1 <= 1e+300) {
		tmp = t_1;
	} else {
		tmp = U;
	}
	return tmp;
}
U = Math.abs(U);
public static double code(double J, double K, double U) {
	double t_0 = Math.cos((K / 2.0));
	double t_1 = (t_0 * (-2.0 * J)) * Math.sqrt((1.0 + Math.pow((U / (t_0 * (J * 2.0))), 2.0)));
	double tmp;
	if (t_1 <= -Double.POSITIVE_INFINITY) {
		tmp = -U;
	} else if (t_1 <= 1e+300) {
		tmp = t_1;
	} else {
		tmp = U;
	}
	return tmp;
}
U = abs(U)
def code(J, K, U):
	t_0 = math.cos((K / 2.0))
	t_1 = (t_0 * (-2.0 * J)) * math.sqrt((1.0 + math.pow((U / (t_0 * (J * 2.0))), 2.0)))
	tmp = 0
	if t_1 <= -math.inf:
		tmp = -U
	elif t_1 <= 1e+300:
		tmp = t_1
	else:
		tmp = U
	return tmp
U = abs(U)
function code(J, K, U)
	t_0 = cos(Float64(K / 2.0))
	t_1 = Float64(Float64(t_0 * Float64(-2.0 * J)) * sqrt(Float64(1.0 + (Float64(U / Float64(t_0 * Float64(J * 2.0))) ^ 2.0))))
	tmp = 0.0
	if (t_1 <= Float64(-Inf))
		tmp = Float64(-U);
	elseif (t_1 <= 1e+300)
		tmp = t_1;
	else
		tmp = U;
	end
	return tmp
end
U = abs(U)
function tmp_2 = code(J, K, U)
	t_0 = cos((K / 2.0));
	t_1 = (t_0 * (-2.0 * J)) * sqrt((1.0 + ((U / (t_0 * (J * 2.0))) ^ 2.0)));
	tmp = 0.0;
	if (t_1 <= -Inf)
		tmp = -U;
	elseif (t_1 <= 1e+300)
		tmp = t_1;
	else
		tmp = U;
	end
	tmp_2 = tmp;
end
NOTE: U should be positive before calling this function
code[J_, K_, U_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[(t$95$0 * N[(-2.0 * J), $MachinePrecision]), $MachinePrecision] * N[Sqrt[N[(1.0 + N[Power[N[(U / N[(t$95$0 * N[(J * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, (-Infinity)], (-U), If[LessEqual[t$95$1, 1e+300], t$95$1, U]]]]
\begin{array}{l}
U = |U|\\
\\
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
t_1 := \left(t_0 \cdot \left(-2 \cdot J\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{t_0 \cdot \left(J \cdot 2\right)}\right)}^{2}}\\
\mathbf{if}\;t_1 \leq -\infty:\\
\;\;\;\;-U\\

\mathbf{elif}\;t_1 \leq 10^{+300}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;U\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 (*.f64 (*.f64 -2 J) (cos.f64 (/.f64 K 2))) (sqrt.f64 (+.f64 1 (pow.f64 (/.f64 U (*.f64 (*.f64 2 J) (cos.f64 (/.f64 K 2)))) 2)))) < -inf.0

    1. Initial program 5.8%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in J around 0 36.5%

      \[\leadsto \color{blue}{-1 \cdot U} \]
    4. Step-by-step derivation
      1. neg-mul-136.5%

        \[\leadsto \color{blue}{-U} \]
    5. Simplified36.5%

      \[\leadsto \color{blue}{-U} \]

    if -inf.0 < (*.f64 (*.f64 (*.f64 -2 J) (cos.f64 (/.f64 K 2))) (sqrt.f64 (+.f64 1 (pow.f64 (/.f64 U (*.f64 (*.f64 2 J) (cos.f64 (/.f64 K 2)))) 2)))) < 1.0000000000000001e300

    1. Initial program 99.9%

      \[\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}} \]

    if 1.0000000000000001e300 < (*.f64 (*.f64 (*.f64 -2 J) (cos.f64 (/.f64 K 2))) (sqrt.f64 (+.f64 1 (pow.f64 (/.f64 U (*.f64 (*.f64 2 J) (cos.f64 (/.f64 K 2)))) 2))))

    1. Initial program 7.9%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in U around -inf 51.8%

      \[\leadsto \color{blue}{U} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification83.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\cos \left(\frac{K}{2}\right) \cdot \left(-2 \cdot J\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\cos \left(\frac{K}{2}\right) \cdot \left(J \cdot 2\right)}\right)}^{2}} \leq -\infty:\\ \;\;\;\;-U\\ \mathbf{elif}\;\left(\cos \left(\frac{K}{2}\right) \cdot \left(-2 \cdot J\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\cos \left(\frac{K}{2}\right) \cdot \left(J \cdot 2\right)}\right)}^{2}} \leq 10^{+300}:\\ \;\;\;\;\left(\cos \left(\frac{K}{2}\right) \cdot \left(-2 \cdot J\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\cos \left(\frac{K}{2}\right) \cdot \left(J \cdot 2\right)}\right)}^{2}}\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]

Alternative 2: 88.0% accurate, 1.3× speedup?

\[\begin{array}{l} U = |U|\\ \\ \begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ -2 \cdot \left(t_0 \cdot \left(J \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{t_0}\right)\right)\right) \end{array} \end{array} \]
NOTE: U should be positive before calling this function
(FPCore (J K U)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0))))
   (* -2.0 (* t_0 (* J (hypot 1.0 (/ (/ U (* J 2.0)) t_0)))))))
U = abs(U);
double code(double J, double K, double U) {
	double t_0 = cos((K / 2.0));
	return -2.0 * (t_0 * (J * hypot(1.0, ((U / (J * 2.0)) / t_0))));
}
U = Math.abs(U);
public static double code(double J, double K, double U) {
	double t_0 = Math.cos((K / 2.0));
	return -2.0 * (t_0 * (J * Math.hypot(1.0, ((U / (J * 2.0)) / t_0))));
}
U = abs(U)
def code(J, K, U):
	t_0 = math.cos((K / 2.0))
	return -2.0 * (t_0 * (J * math.hypot(1.0, ((U / (J * 2.0)) / t_0))))
U = abs(U)
function code(J, K, U)
	t_0 = cos(Float64(K / 2.0))
	return Float64(-2.0 * Float64(t_0 * Float64(J * hypot(1.0, Float64(Float64(U / Float64(J * 2.0)) / t_0)))))
end
U = abs(U)
function tmp = code(J, K, U)
	t_0 = cos((K / 2.0));
	tmp = -2.0 * (t_0 * (J * hypot(1.0, ((U / (J * 2.0)) / t_0))));
end
NOTE: U should be positive before calling this function
code[J_, K_, U_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, N[(-2.0 * N[(t$95$0 * N[(J * N[Sqrt[1.0 ^ 2 + N[(N[(U / N[(J * 2.0), $MachinePrecision]), $MachinePrecision] / t$95$0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
U = |U|\\
\\
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
-2 \cdot \left(t_0 \cdot \left(J \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{t_0}\right)\right)\right)
\end{array}
\end{array}
Derivation
  1. Initial program 72.6%

    \[\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}{-2 \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \left(J \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)\right)\right)} \]
  3. Final simplification87.4%

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

Alternative 3: 77.9% accurate, 1.9× speedup?

\[\begin{array}{l} U = |U|\\ \\ \begin{array}{l} t_0 := -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\right)\\ \mathbf{if}\;J \leq -7 \cdot 10^{-99}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;J \leq -1.42 \cdot 10^{-303}:\\ \;\;\;\;U\\ \mathbf{elif}\;J \leq 1.05 \cdot 10^{-99}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \]
NOTE: U should be positive before calling this function
(FPCore (J K U)
 :precision binary64
 (let* ((t_0 (* -2.0 (* (* J (cos (/ K 2.0))) (hypot 1.0 (* 0.5 (/ U J)))))))
   (if (<= J -7e-99)
     t_0
     (if (<= J -1.42e-303) U (if (<= J 1.05e-99) (- U) t_0)))))
U = abs(U);
double code(double J, double K, double U) {
	double t_0 = -2.0 * ((J * cos((K / 2.0))) * hypot(1.0, (0.5 * (U / J))));
	double tmp;
	if (J <= -7e-99) {
		tmp = t_0;
	} else if (J <= -1.42e-303) {
		tmp = U;
	} else if (J <= 1.05e-99) {
		tmp = -U;
	} else {
		tmp = t_0;
	}
	return tmp;
}
U = Math.abs(U);
public static double code(double J, double K, double U) {
	double t_0 = -2.0 * ((J * Math.cos((K / 2.0))) * Math.hypot(1.0, (0.5 * (U / J))));
	double tmp;
	if (J <= -7e-99) {
		tmp = t_0;
	} else if (J <= -1.42e-303) {
		tmp = U;
	} else if (J <= 1.05e-99) {
		tmp = -U;
	} else {
		tmp = t_0;
	}
	return tmp;
}
U = abs(U)
def code(J, K, U):
	t_0 = -2.0 * ((J * math.cos((K / 2.0))) * math.hypot(1.0, (0.5 * (U / J))))
	tmp = 0
	if J <= -7e-99:
		tmp = t_0
	elif J <= -1.42e-303:
		tmp = U
	elif J <= 1.05e-99:
		tmp = -U
	else:
		tmp = t_0
	return tmp
U = abs(U)
function code(J, K, U)
	t_0 = Float64(-2.0 * Float64(Float64(J * cos(Float64(K / 2.0))) * hypot(1.0, Float64(0.5 * Float64(U / J)))))
	tmp = 0.0
	if (J <= -7e-99)
		tmp = t_0;
	elseif (J <= -1.42e-303)
		tmp = U;
	elseif (J <= 1.05e-99)
		tmp = Float64(-U);
	else
		tmp = t_0;
	end
	return tmp
end
U = abs(U)
function tmp_2 = code(J, K, U)
	t_0 = -2.0 * ((J * cos((K / 2.0))) * hypot(1.0, (0.5 * (U / J))));
	tmp = 0.0;
	if (J <= -7e-99)
		tmp = t_0;
	elseif (J <= -1.42e-303)
		tmp = U;
	elseif (J <= 1.05e-99)
		tmp = -U;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
NOTE: U should be positive before calling this function
code[J_, K_, U_] := Block[{t$95$0 = N[(-2.0 * N[(N[(J * N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sqrt[1.0 ^ 2 + N[(0.5 * N[(U / J), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[J, -7e-99], t$95$0, If[LessEqual[J, -1.42e-303], U, If[LessEqual[J, 1.05e-99], (-U), t$95$0]]]]
\begin{array}{l}
U = |U|\\
\\
\begin{array}{l}
t_0 := -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\right)\\
\mathbf{if}\;J \leq -7 \cdot 10^{-99}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;J \leq -1.42 \cdot 10^{-303}:\\
\;\;\;\;U\\

\mathbf{elif}\;J \leq 1.05 \cdot 10^{-99}:\\
\;\;\;\;-U\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if J < -6.9999999999999997e-99 or 1.04999999999999992e-99 < J

    1. Initial program 87.6%

      \[\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. Step-by-step derivation
      1. associate-*l*87.6%

        \[\leadsto \color{blue}{\left(-2 \cdot \left(J \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}} \]
      2. associate-*l*87.6%

        \[\leadsto \color{blue}{-2 \cdot \left(\left(J \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}}\right)} \]
      3. unpow287.6%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\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)}}}\right) \]
      4. hypot-1-def98.8%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \color{blue}{\mathsf{hypot}\left(1, \frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right) \]
      5. associate-/r*98.7%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \color{blue}{\frac{\frac{U}{2 \cdot J}}{\cos \left(\frac{K}{2}\right)}}\right)\right) \]
      6. cos-neg98.7%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{2 \cdot J}}{\color{blue}{\cos \left(-\frac{K}{2}\right)}}\right)\right) \]
      7. distribute-frac-neg98.7%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{2 \cdot J}}{\cos \color{blue}{\left(\frac{-K}{2}\right)}}\right)\right) \]
      8. associate-/r*98.8%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \color{blue}{\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{-K}{2}\right)}}\right)\right) \]
      9. associate-/r*98.7%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \color{blue}{\frac{\frac{U}{2 \cdot J}}{\cos \left(\frac{-K}{2}\right)}}\right)\right) \]
      10. distribute-frac-neg98.7%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{2 \cdot J}}{\cos \color{blue}{\left(-\frac{K}{2}\right)}}\right)\right) \]
      11. cos-neg98.7%

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

      \[\leadsto \color{blue}{-2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)\right)} \]
    4. Taylor expanded in K around 0 84.8%

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

    if -6.9999999999999997e-99 < J < -1.42e-303

    1. Initial program 45.3%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in U around -inf 44.2%

      \[\leadsto \color{blue}{U} \]

    if -1.42e-303 < J < 1.04999999999999992e-99

    1. Initial program 36.6%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in J around 0 37.2%

      \[\leadsto \color{blue}{-1 \cdot U} \]
    4. Step-by-step derivation
      1. neg-mul-137.2%

        \[\leadsto \color{blue}{-U} \]
    5. Simplified37.2%

      \[\leadsto \color{blue}{-U} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification70.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq -7 \cdot 10^{-99}:\\ \;\;\;\;-2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\right)\\ \mathbf{elif}\;J \leq -1.42 \cdot 10^{-303}:\\ \;\;\;\;U\\ \mathbf{elif}\;J \leq 1.05 \cdot 10^{-99}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\right)\\ \end{array} \]

Alternative 4: 68.8% accurate, 3.4× speedup?

\[\begin{array}{l} U = |U|\\ \\ \begin{array}{l} t_0 := -2 \cdot \left(J \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\right)\\ t_1 := \left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \mathbf{if}\;J \leq -6.8 \cdot 10^{+82}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;J \leq -6.4 \cdot 10^{-55}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;J \leq -6.5 \cdot 10^{-78}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;J \leq -1.42 \cdot 10^{-303}:\\ \;\;\;\;U\\ \mathbf{elif}\;J \leq 2.1 \cdot 10^{-59}:\\ \;\;\;\;-U\\ \mathbf{elif}\;J \leq 1.95 \cdot 10^{+90}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \end{array} \]
NOTE: U should be positive before calling this function
(FPCore (J K U)
 :precision binary64
 (let* ((t_0 (* -2.0 (* J (hypot 1.0 (* 0.5 (/ U J))))))
        (t_1 (* (* -2.0 J) (cos (* K 0.5)))))
   (if (<= J -6.8e+82)
     t_1
     (if (<= J -6.4e-55)
       t_0
       (if (<= J -6.5e-78)
         t_1
         (if (<= J -1.42e-303)
           U
           (if (<= J 2.1e-59) (- U) (if (<= J 1.95e+90) t_0 t_1))))))))
U = abs(U);
double code(double J, double K, double U) {
	double t_0 = -2.0 * (J * hypot(1.0, (0.5 * (U / J))));
	double t_1 = (-2.0 * J) * cos((K * 0.5));
	double tmp;
	if (J <= -6.8e+82) {
		tmp = t_1;
	} else if (J <= -6.4e-55) {
		tmp = t_0;
	} else if (J <= -6.5e-78) {
		tmp = t_1;
	} else if (J <= -1.42e-303) {
		tmp = U;
	} else if (J <= 2.1e-59) {
		tmp = -U;
	} else if (J <= 1.95e+90) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	return tmp;
}
U = Math.abs(U);
public static double code(double J, double K, double U) {
	double t_0 = -2.0 * (J * Math.hypot(1.0, (0.5 * (U / J))));
	double t_1 = (-2.0 * J) * Math.cos((K * 0.5));
	double tmp;
	if (J <= -6.8e+82) {
		tmp = t_1;
	} else if (J <= -6.4e-55) {
		tmp = t_0;
	} else if (J <= -6.5e-78) {
		tmp = t_1;
	} else if (J <= -1.42e-303) {
		tmp = U;
	} else if (J <= 2.1e-59) {
		tmp = -U;
	} else if (J <= 1.95e+90) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	return tmp;
}
U = abs(U)
def code(J, K, U):
	t_0 = -2.0 * (J * math.hypot(1.0, (0.5 * (U / J))))
	t_1 = (-2.0 * J) * math.cos((K * 0.5))
	tmp = 0
	if J <= -6.8e+82:
		tmp = t_1
	elif J <= -6.4e-55:
		tmp = t_0
	elif J <= -6.5e-78:
		tmp = t_1
	elif J <= -1.42e-303:
		tmp = U
	elif J <= 2.1e-59:
		tmp = -U
	elif J <= 1.95e+90:
		tmp = t_0
	else:
		tmp = t_1
	return tmp
U = abs(U)
function code(J, K, U)
	t_0 = Float64(-2.0 * Float64(J * hypot(1.0, Float64(0.5 * Float64(U / J)))))
	t_1 = Float64(Float64(-2.0 * J) * cos(Float64(K * 0.5)))
	tmp = 0.0
	if (J <= -6.8e+82)
		tmp = t_1;
	elseif (J <= -6.4e-55)
		tmp = t_0;
	elseif (J <= -6.5e-78)
		tmp = t_1;
	elseif (J <= -1.42e-303)
		tmp = U;
	elseif (J <= 2.1e-59)
		tmp = Float64(-U);
	elseif (J <= 1.95e+90)
		tmp = t_0;
	else
		tmp = t_1;
	end
	return tmp
end
U = abs(U)
function tmp_2 = code(J, K, U)
	t_0 = -2.0 * (J * hypot(1.0, (0.5 * (U / J))));
	t_1 = (-2.0 * J) * cos((K * 0.5));
	tmp = 0.0;
	if (J <= -6.8e+82)
		tmp = t_1;
	elseif (J <= -6.4e-55)
		tmp = t_0;
	elseif (J <= -6.5e-78)
		tmp = t_1;
	elseif (J <= -1.42e-303)
		tmp = U;
	elseif (J <= 2.1e-59)
		tmp = -U;
	elseif (J <= 1.95e+90)
		tmp = t_0;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
NOTE: U should be positive before calling this function
code[J_, K_, U_] := Block[{t$95$0 = N[(-2.0 * N[(J * N[Sqrt[1.0 ^ 2 + N[(0.5 * N[(U / J), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(-2.0 * J), $MachinePrecision] * N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[J, -6.8e+82], t$95$1, If[LessEqual[J, -6.4e-55], t$95$0, If[LessEqual[J, -6.5e-78], t$95$1, If[LessEqual[J, -1.42e-303], U, If[LessEqual[J, 2.1e-59], (-U), If[LessEqual[J, 1.95e+90], t$95$0, t$95$1]]]]]]]]
\begin{array}{l}
U = |U|\\
\\
\begin{array}{l}
t_0 := -2 \cdot \left(J \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\right)\\
t_1 := \left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\
\mathbf{if}\;J \leq -6.8 \cdot 10^{+82}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;J \leq -6.4 \cdot 10^{-55}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;J \leq -6.5 \cdot 10^{-78}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;J \leq -1.42 \cdot 10^{-303}:\\
\;\;\;\;U\\

\mathbf{elif}\;J \leq 2.1 \cdot 10^{-59}:\\
\;\;\;\;-U\\

\mathbf{elif}\;J \leq 1.95 \cdot 10^{+90}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if J < -6.79999999999999989e82 or -6.4000000000000003e-55 < J < -6.5000000000000003e-78 or 1.9500000000000001e90 < J

    1. Initial program 98.1%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in J around inf 83.4%

      \[\leadsto \color{blue}{-2 \cdot \left(J \cdot \cos \left(0.5 \cdot K\right)\right)} \]
    4. Step-by-step derivation
      1. associate-*r*83.4%

        \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \cos \left(0.5 \cdot K\right)} \]
      2. *-commutative83.4%

        \[\leadsto \color{blue}{\left(J \cdot -2\right)} \cdot \cos \left(0.5 \cdot K\right) \]
    5. Simplified83.4%

      \[\leadsto \color{blue}{\left(J \cdot -2\right) \cdot \cos \left(0.5 \cdot K\right)} \]

    if -6.79999999999999989e82 < J < -6.4000000000000003e-55 or 2.09999999999999997e-59 < J < 1.9500000000000001e90

    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. Step-by-step derivation
      1. associate-*l*73.0%

        \[\leadsto \color{blue}{\left(-2 \cdot \left(J \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}} \]
      2. associate-*l*73.0%

        \[\leadsto \color{blue}{-2 \cdot \left(\left(J \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}}\right)} \]
      3. unpow273.0%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\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)}}}\right) \]
      4. hypot-1-def98.4%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \color{blue}{\mathsf{hypot}\left(1, \frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right) \]
      5. associate-/r*98.2%

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

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{2 \cdot J}}{\color{blue}{\cos \left(-\frac{K}{2}\right)}}\right)\right) \]
      7. distribute-frac-neg98.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{2 \cdot J}}{\cos \color{blue}{\left(\frac{-K}{2}\right)}}\right)\right) \]
      8. associate-/r*98.4%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \color{blue}{\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{-K}{2}\right)}}\right)\right) \]
      9. associate-/r*98.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \color{blue}{\frac{\frac{U}{2 \cdot J}}{\cos \left(\frac{-K}{2}\right)}}\right)\right) \]
      10. distribute-frac-neg98.2%

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

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

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

        \[\leadsto -2 \cdot \left(\left(J \cdot \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(\frac{K}{2}\right)\right)\right)}\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)\right) \]
      2. div-inv98.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\cos \color{blue}{\left(K \cdot \frac{1}{2}\right)}\right)\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)\right) \]
      3. metadata-eval98.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(K \cdot \color{blue}{0.5}\right)\right)\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)\right) \]
    5. Applied egg-rr98.2%

      \[\leadsto -2 \cdot \left(\left(J \cdot \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(K \cdot 0.5\right)\right)\right)}\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)\right) \]
    6. Taylor expanded in K around 0 77.1%

      \[\leadsto -2 \cdot \left(\left(J \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\cos \left(K \cdot 0.5\right)\right)\right)\right) \cdot \mathsf{hypot}\left(1, \color{blue}{0.5 \cdot \frac{U}{J}}\right)\right) \]
    7. Taylor expanded in K around 0 72.8%

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

    if -6.5000000000000003e-78 < J < -1.42e-303

    1. Initial program 46.8%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in U around -inf 42.0%

      \[\leadsto \color{blue}{U} \]

    if -1.42e-303 < J < 2.09999999999999997e-59

    1. Initial program 43.5%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in J around 0 36.9%

      \[\leadsto \color{blue}{-1 \cdot U} \]
    4. Step-by-step derivation
      1. neg-mul-136.9%

        \[\leadsto \color{blue}{-U} \]
    5. Simplified36.9%

      \[\leadsto \color{blue}{-U} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification64.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq -6.8 \cdot 10^{+82}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \mathbf{elif}\;J \leq -6.4 \cdot 10^{-55}:\\ \;\;\;\;-2 \cdot \left(J \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\right)\\ \mathbf{elif}\;J \leq -6.5 \cdot 10^{-78}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \mathbf{elif}\;J \leq -1.42 \cdot 10^{-303}:\\ \;\;\;\;U\\ \mathbf{elif}\;J \leq 2.1 \cdot 10^{-59}:\\ \;\;\;\;-U\\ \mathbf{elif}\;J \leq 1.95 \cdot 10^{+90}:\\ \;\;\;\;-2 \cdot \left(J \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \end{array} \]

Alternative 5: 64.6% accurate, 3.7× speedup?

\[\begin{array}{l} U = |U|\\ \\ \begin{array}{l} t_0 := \left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \mathbf{if}\;J \leq -3.2 \cdot 10^{-78}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;J \leq -1.42 \cdot 10^{-303}:\\ \;\;\;\;U\\ \mathbf{elif}\;J \leq 1.86 \cdot 10^{+90}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \]
NOTE: U should be positive before calling this function
(FPCore (J K U)
 :precision binary64
 (let* ((t_0 (* (* -2.0 J) (cos (* K 0.5)))))
   (if (<= J -3.2e-78)
     t_0
     (if (<= J -1.42e-303) U (if (<= J 1.86e+90) (- U) t_0)))))
U = abs(U);
double code(double J, double K, double U) {
	double t_0 = (-2.0 * J) * cos((K * 0.5));
	double tmp;
	if (J <= -3.2e-78) {
		tmp = t_0;
	} else if (J <= -1.42e-303) {
		tmp = U;
	} else if (J <= 1.86e+90) {
		tmp = -U;
	} else {
		tmp = t_0;
	}
	return tmp;
}
NOTE: U should be positive before calling this function
real(8) function code(j, k, u)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u
    real(8) :: t_0
    real(8) :: tmp
    t_0 = ((-2.0d0) * j) * cos((k * 0.5d0))
    if (j <= (-3.2d-78)) then
        tmp = t_0
    else if (j <= (-1.42d-303)) then
        tmp = u
    else if (j <= 1.86d+90) then
        tmp = -u
    else
        tmp = t_0
    end if
    code = tmp
end function
U = Math.abs(U);
public static double code(double J, double K, double U) {
	double t_0 = (-2.0 * J) * Math.cos((K * 0.5));
	double tmp;
	if (J <= -3.2e-78) {
		tmp = t_0;
	} else if (J <= -1.42e-303) {
		tmp = U;
	} else if (J <= 1.86e+90) {
		tmp = -U;
	} else {
		tmp = t_0;
	}
	return tmp;
}
U = abs(U)
def code(J, K, U):
	t_0 = (-2.0 * J) * math.cos((K * 0.5))
	tmp = 0
	if J <= -3.2e-78:
		tmp = t_0
	elif J <= -1.42e-303:
		tmp = U
	elif J <= 1.86e+90:
		tmp = -U
	else:
		tmp = t_0
	return tmp
U = abs(U)
function code(J, K, U)
	t_0 = Float64(Float64(-2.0 * J) * cos(Float64(K * 0.5)))
	tmp = 0.0
	if (J <= -3.2e-78)
		tmp = t_0;
	elseif (J <= -1.42e-303)
		tmp = U;
	elseif (J <= 1.86e+90)
		tmp = Float64(-U);
	else
		tmp = t_0;
	end
	return tmp
end
U = abs(U)
function tmp_2 = code(J, K, U)
	t_0 = (-2.0 * J) * cos((K * 0.5));
	tmp = 0.0;
	if (J <= -3.2e-78)
		tmp = t_0;
	elseif (J <= -1.42e-303)
		tmp = U;
	elseif (J <= 1.86e+90)
		tmp = -U;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
NOTE: U should be positive before calling this function
code[J_, K_, U_] := Block[{t$95$0 = N[(N[(-2.0 * J), $MachinePrecision] * N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[J, -3.2e-78], t$95$0, If[LessEqual[J, -1.42e-303], U, If[LessEqual[J, 1.86e+90], (-U), t$95$0]]]]
\begin{array}{l}
U = |U|\\
\\
\begin{array}{l}
t_0 := \left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\
\mathbf{if}\;J \leq -3.2 \cdot 10^{-78}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;J \leq -1.42 \cdot 10^{-303}:\\
\;\;\;\;U\\

\mathbf{elif}\;J \leq 1.86 \cdot 10^{+90}:\\
\;\;\;\;-U\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if J < -3.2e-78 or 1.8600000000000001e90 < J

    1. Initial program 95.7%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in J around inf 76.9%

      \[\leadsto \color{blue}{-2 \cdot \left(J \cdot \cos \left(0.5 \cdot K\right)\right)} \]
    4. Step-by-step derivation
      1. associate-*r*76.9%

        \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \cos \left(0.5 \cdot K\right)} \]
      2. *-commutative76.9%

        \[\leadsto \color{blue}{\left(J \cdot -2\right)} \cdot \cos \left(0.5 \cdot K\right) \]
    5. Simplified76.9%

      \[\leadsto \color{blue}{\left(J \cdot -2\right) \cdot \cos \left(0.5 \cdot K\right)} \]

    if -3.2e-78 < J < -1.42e-303

    1. Initial program 46.8%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in U around -inf 42.0%

      \[\leadsto \color{blue}{U} \]

    if -1.42e-303 < J < 1.8600000000000001e90

    1. Initial program 50.9%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in J around 0 33.2%

      \[\leadsto \color{blue}{-1 \cdot U} \]
    4. Step-by-step derivation
      1. neg-mul-133.2%

        \[\leadsto \color{blue}{-U} \]
    5. Simplified33.2%

      \[\leadsto \color{blue}{-U} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification57.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq -3.2 \cdot 10^{-78}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \mathbf{elif}\;J \leq -1.42 \cdot 10^{-303}:\\ \;\;\;\;U\\ \mathbf{elif}\;J \leq 1.86 \cdot 10^{+90}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \end{array} \]

Alternative 6: 49.9% accurate, 45.7× speedup?

\[\begin{array}{l} U = |U|\\ \\ \begin{array}{l} \mathbf{if}\;J \leq -2.5 \cdot 10^{+43}:\\ \;\;\;\;-2 \cdot J\\ \mathbf{elif}\;J \leq -1.5 \cdot 10^{-303}:\\ \;\;\;\;U\\ \mathbf{elif}\;J \leq 1.25 \cdot 10^{+138}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot J\\ \end{array} \end{array} \]
NOTE: U should be positive before calling this function
(FPCore (J K U)
 :precision binary64
 (if (<= J -2.5e+43)
   (* -2.0 J)
   (if (<= J -1.5e-303) U (if (<= J 1.25e+138) (- U) (* -2.0 J)))))
U = abs(U);
double code(double J, double K, double U) {
	double tmp;
	if (J <= -2.5e+43) {
		tmp = -2.0 * J;
	} else if (J <= -1.5e-303) {
		tmp = U;
	} else if (J <= 1.25e+138) {
		tmp = -U;
	} else {
		tmp = -2.0 * J;
	}
	return tmp;
}
NOTE: U should be positive before calling this function
real(8) function code(j, k, u)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u
    real(8) :: tmp
    if (j <= (-2.5d+43)) then
        tmp = (-2.0d0) * j
    else if (j <= (-1.5d-303)) then
        tmp = u
    else if (j <= 1.25d+138) then
        tmp = -u
    else
        tmp = (-2.0d0) * j
    end if
    code = tmp
end function
U = Math.abs(U);
public static double code(double J, double K, double U) {
	double tmp;
	if (J <= -2.5e+43) {
		tmp = -2.0 * J;
	} else if (J <= -1.5e-303) {
		tmp = U;
	} else if (J <= 1.25e+138) {
		tmp = -U;
	} else {
		tmp = -2.0 * J;
	}
	return tmp;
}
U = abs(U)
def code(J, K, U):
	tmp = 0
	if J <= -2.5e+43:
		tmp = -2.0 * J
	elif J <= -1.5e-303:
		tmp = U
	elif J <= 1.25e+138:
		tmp = -U
	else:
		tmp = -2.0 * J
	return tmp
U = abs(U)
function code(J, K, U)
	tmp = 0.0
	if (J <= -2.5e+43)
		tmp = Float64(-2.0 * J);
	elseif (J <= -1.5e-303)
		tmp = U;
	elseif (J <= 1.25e+138)
		tmp = Float64(-U);
	else
		tmp = Float64(-2.0 * J);
	end
	return tmp
end
U = abs(U)
function tmp_2 = code(J, K, U)
	tmp = 0.0;
	if (J <= -2.5e+43)
		tmp = -2.0 * J;
	elseif (J <= -1.5e-303)
		tmp = U;
	elseif (J <= 1.25e+138)
		tmp = -U;
	else
		tmp = -2.0 * J;
	end
	tmp_2 = tmp;
end
NOTE: U should be positive before calling this function
code[J_, K_, U_] := If[LessEqual[J, -2.5e+43], N[(-2.0 * J), $MachinePrecision], If[LessEqual[J, -1.5e-303], U, If[LessEqual[J, 1.25e+138], (-U), N[(-2.0 * J), $MachinePrecision]]]]
\begin{array}{l}
U = |U|\\
\\
\begin{array}{l}
\mathbf{if}\;J \leq -2.5 \cdot 10^{+43}:\\
\;\;\;\;-2 \cdot J\\

\mathbf{elif}\;J \leq -1.5 \cdot 10^{-303}:\\
\;\;\;\;U\\

\mathbf{elif}\;J \leq 1.25 \cdot 10^{+138}:\\
\;\;\;\;-U\\

\mathbf{else}:\\
\;\;\;\;-2 \cdot J\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if J < -2.5000000000000002e43 or 1.25000000000000004e138 < J

    1. Initial program 98.1%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in J around inf 84.1%

      \[\leadsto \color{blue}{-2 \cdot \left(J \cdot \cos \left(0.5 \cdot K\right)\right)} \]
    4. Step-by-step derivation
      1. associate-*r*84.1%

        \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \cos \left(0.5 \cdot K\right)} \]
      2. *-commutative84.1%

        \[\leadsto \color{blue}{\left(J \cdot -2\right)} \cdot \cos \left(0.5 \cdot K\right) \]
    5. Simplified84.1%

      \[\leadsto \color{blue}{\left(J \cdot -2\right) \cdot \cos \left(0.5 \cdot K\right)} \]
    6. Taylor expanded in K around 0 52.7%

      \[\leadsto \color{blue}{-2 \cdot J} \]
    7. Step-by-step derivation
      1. *-commutative52.7%

        \[\leadsto \color{blue}{J \cdot -2} \]
    8. Simplified52.7%

      \[\leadsto \color{blue}{J \cdot -2} \]

    if -2.5000000000000002e43 < J < -1.50000000000000014e-303

    1. Initial program 60.4%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in U around -inf 38.2%

      \[\leadsto \color{blue}{U} \]

    if -1.50000000000000014e-303 < J < 1.25000000000000004e138

    1. Initial program 53.4%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in J around 0 31.0%

      \[\leadsto \color{blue}{-1 \cdot U} \]
    4. Step-by-step derivation
      1. neg-mul-131.0%

        \[\leadsto \color{blue}{-U} \]
    5. Simplified31.0%

      \[\leadsto \color{blue}{-U} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification41.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq -2.5 \cdot 10^{+43}:\\ \;\;\;\;-2 \cdot J\\ \mathbf{elif}\;J \leq -1.5 \cdot 10^{-303}:\\ \;\;\;\;U\\ \mathbf{elif}\;J \leq 1.25 \cdot 10^{+138}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot J\\ \end{array} \]

Alternative 7: 39.7% accurate, 103.4× speedup?

\[\begin{array}{l} U = |U|\\ \\ \begin{array}{l} \mathbf{if}\;J \leq -1.42 \cdot 10^{-303}:\\ \;\;\;\;U\\ \mathbf{else}:\\ \;\;\;\;-U\\ \end{array} \end{array} \]
NOTE: U should be positive before calling this function
(FPCore (J K U) :precision binary64 (if (<= J -1.42e-303) U (- U)))
U = abs(U);
double code(double J, double K, double U) {
	double tmp;
	if (J <= -1.42e-303) {
		tmp = U;
	} else {
		tmp = -U;
	}
	return tmp;
}
NOTE: U should be positive before calling this function
real(8) function code(j, k, u)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u
    real(8) :: tmp
    if (j <= (-1.42d-303)) then
        tmp = u
    else
        tmp = -u
    end if
    code = tmp
end function
U = Math.abs(U);
public static double code(double J, double K, double U) {
	double tmp;
	if (J <= -1.42e-303) {
		tmp = U;
	} else {
		tmp = -U;
	}
	return tmp;
}
U = abs(U)
def code(J, K, U):
	tmp = 0
	if J <= -1.42e-303:
		tmp = U
	else:
		tmp = -U
	return tmp
U = abs(U)
function code(J, K, U)
	tmp = 0.0
	if (J <= -1.42e-303)
		tmp = U;
	else
		tmp = Float64(-U);
	end
	return tmp
end
U = abs(U)
function tmp_2 = code(J, K, U)
	tmp = 0.0;
	if (J <= -1.42e-303)
		tmp = U;
	else
		tmp = -U;
	end
	tmp_2 = tmp;
end
NOTE: U should be positive before calling this function
code[J_, K_, U_] := If[LessEqual[J, -1.42e-303], U, (-U)]
\begin{array}{l}
U = |U|\\
\\
\begin{array}{l}
\mathbf{if}\;J \leq -1.42 \cdot 10^{-303}:\\
\;\;\;\;U\\

\mathbf{else}:\\
\;\;\;\;-U\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if J < -1.42e-303

    1. Initial program 76.6%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in U around -inf 27.4%

      \[\leadsto \color{blue}{U} \]

    if -1.42e-303 < J

    1. Initial program 68.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. Simplified67.9%

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
    3. Taylor expanded in J around 0 23.8%

      \[\leadsto \color{blue}{-1 \cdot U} \]
    4. Step-by-step derivation
      1. neg-mul-123.8%

        \[\leadsto \color{blue}{-U} \]
    5. Simplified23.8%

      \[\leadsto \color{blue}{-U} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification25.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq -1.42 \cdot 10^{-303}:\\ \;\;\;\;U\\ \mathbf{else}:\\ \;\;\;\;-U\\ \end{array} \]

Alternative 8: 26.5% accurate, 420.0× speedup?

\[\begin{array}{l} U = |U|\\ \\ U \end{array} \]
NOTE: U should be positive before calling this function
(FPCore (J K U) :precision binary64 U)
U = abs(U);
double code(double J, double K, double U) {
	return U;
}
NOTE: U should be positive before calling this function
real(8) function code(j, k, u)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u
    code = u
end function
U = Math.abs(U);
public static double code(double J, double K, double U) {
	return U;
}
U = abs(U)
def code(J, K, U):
	return U
U = abs(U)
function code(J, K, U)
	return U
end
U = abs(U)
function tmp = code(J, K, U)
	tmp = U;
end
NOTE: U should be positive before calling this function
code[J_, K_, U_] := U
\begin{array}{l}
U = |U|\\
\\
U
\end{array}
Derivation
  1. Initial program 72.6%

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

    \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \sqrt{1 + {\left(\frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)} \]
  3. Taylor expanded in U around -inf 30.8%

    \[\leadsto \color{blue}{U} \]
  4. Final simplification30.8%

    \[\leadsto U \]

Reproduce

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