
(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:
Herbie found 16 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
U_m = (fabs.f64 U)
J\_m = (fabs.f64 J)
J\_s = (copysign.f64 #s(literal 1 binary64) J)
(FPCore (J_s J_m K U_m)
:precision binary64
(let* ((t_0 (cos (/ K 2.0)))
(t_1
(*
(* (* -2.0 J_m) t_0)
(sqrt (+ 1.0 (pow (/ U_m (* t_0 (* J_m 2.0))) 2.0))))))
(*
J_s
(if (<= t_1 (- INFINITY))
(- U_m)
(if (<= t_1 2e+286)
t_1
(*
(- U_m)
(fma
-2.0
(* (pow (cos (* K 0.5)) 2.0) (/ (* J_m J_m) (* U_m U_m)))
-1.0)))))))U_m = fabs(U);
J\_m = fabs(J);
J\_s = copysign(1.0, J);
double code(double J_s, double J_m, double K, double U_m) {
double t_0 = cos((K / 2.0));
double t_1 = ((-2.0 * J_m) * t_0) * sqrt((1.0 + pow((U_m / (t_0 * (J_m * 2.0))), 2.0)));
double tmp;
if (t_1 <= -((double) INFINITY)) {
tmp = -U_m;
} else if (t_1 <= 2e+286) {
tmp = t_1;
} else {
tmp = -U_m * fma(-2.0, (pow(cos((K * 0.5)), 2.0) * ((J_m * J_m) / (U_m * U_m))), -1.0);
}
return J_s * tmp;
}
U_m = abs(U) J\_m = abs(J) J\_s = copysign(1.0, J) function code(J_s, J_m, K, U_m) t_0 = cos(Float64(K / 2.0)) t_1 = Float64(Float64(Float64(-2.0 * J_m) * t_0) * sqrt(Float64(1.0 + (Float64(U_m / Float64(t_0 * Float64(J_m * 2.0))) ^ 2.0)))) tmp = 0.0 if (t_1 <= Float64(-Inf)) tmp = Float64(-U_m); elseif (t_1 <= 2e+286) tmp = t_1; else tmp = Float64(Float64(-U_m) * fma(-2.0, Float64((cos(Float64(K * 0.5)) ^ 2.0) * Float64(Float64(J_m * J_m) / Float64(U_m * U_m))), -1.0)); end return Float64(J_s * tmp) end
U_m = N[Abs[U], $MachinePrecision]
J\_m = N[Abs[J], $MachinePrecision]
J\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[J]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[J$95$s_, J$95$m_, K_, U$95$m_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(-2.0 * J$95$m), $MachinePrecision] * t$95$0), $MachinePrecision] * N[Sqrt[N[(1.0 + N[Power[N[(U$95$m / N[(t$95$0 * N[(J$95$m * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(J$95$s * If[LessEqual[t$95$1, (-Infinity)], (-U$95$m), If[LessEqual[t$95$1, 2e+286], t$95$1, N[((-U$95$m) * N[(-2.0 * N[(N[Power[N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision] * N[(N[(J$95$m * J$95$m), $MachinePrecision] / N[(U$95$m * U$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]]]
\begin{array}{l}
U_m = \left|U\right|
\\
J\_m = \left|J\right|
\\
J\_s = \mathsf{copysign}\left(1, J\right)
\\
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
t_1 := \left(\left(-2 \cdot J\_m\right) \cdot t\_0\right) \cdot \sqrt{1 + {\left(\frac{U\_m}{t\_0 \cdot \left(J\_m \cdot 2\right)}\right)}^{2}}\\
J\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_1 \leq -\infty:\\
\;\;\;\;-U\_m\\
\mathbf{elif}\;t\_1 \leq 2 \cdot 10^{+286}:\\
\;\;\;\;t\_1\\
\mathbf{else}:\\
\;\;\;\;\left(-U\_m\right) \cdot \mathsf{fma}\left(-2, {\cos \left(K \cdot 0.5\right)}^{2} \cdot \frac{J\_m \cdot J\_m}{U\_m \cdot U\_m}, -1\right)\\
\end{array}
\end{array}
\end{array}
if (*.f64 (*.f64 (*.f64 #s(literal -2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64)))) (sqrt.f64 (+.f64 #s(literal 1 binary64) (pow.f64 (/.f64 U (*.f64 (*.f64 #s(literal 2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64))))) #s(literal 2 binary64))))) < -inf.0Initial program 5.5%
Taylor expanded in J around 0
mul-1-negN/A
lower-neg.f64100.0
Applied rewrites100.0%
if -inf.0 < (*.f64 (*.f64 (*.f64 #s(literal -2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64)))) (sqrt.f64 (+.f64 #s(literal 1 binary64) (pow.f64 (/.f64 U (*.f64 (*.f64 #s(literal 2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64))))) #s(literal 2 binary64))))) < 2.00000000000000007e286Initial program 99.8%
if 2.00000000000000007e286 < (*.f64 (*.f64 (*.f64 #s(literal -2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64)))) (sqrt.f64 (+.f64 #s(literal 1 binary64) (pow.f64 (/.f64 U (*.f64 (*.f64 #s(literal 2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64))))) #s(literal 2 binary64))))) Initial program 4.9%
Taylor expanded in U around -inf
associate-*r*N/A
lower-*.f64N/A
mul-1-negN/A
lower-neg.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
lower-pow.f64N/A
lower-cos.f64N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64100.0
Applied rewrites100.0%
Final simplification99.9%
U_m = (fabs.f64 U)
J\_m = (fabs.f64 J)
J\_s = (copysign.f64 #s(literal 1 binary64) J)
(FPCore (J_s J_m K U_m)
:precision binary64
(let* ((t_0 (cos (* K 0.5)))
(t_1 (cos (/ K 2.0)))
(t_2 (* (* -2.0 J_m) t_1))
(t_3 (* t_2 (sqrt (+ 1.0 (pow (/ U_m (* t_1 (* J_m 2.0))) 2.0))))))
(*
J_s
(if (<= t_3 -2e+295)
(- U_m)
(if (<= t_3 2e-63)
(* t_2 (sqrt (+ 1.0 (pow (/ U_m (* (* J_m 2.0) 1.0)) 2.0))))
(if (<= t_3 5e+265)
(*
J_m
(*
(* -2.0 t_0)
(sqrt
(fma
U_m
(/ U_m (* (fma 0.5 (cos K) 0.5) (* (* J_m J_m) 4.0)))
1.0))))
(/ (* (- U_m) t_0) (sqrt (fma (cos K) 0.5 0.5)))))))))U_m = fabs(U);
J\_m = fabs(J);
J\_s = copysign(1.0, J);
double code(double J_s, double J_m, double K, double U_m) {
double t_0 = cos((K * 0.5));
double t_1 = cos((K / 2.0));
double t_2 = (-2.0 * J_m) * t_1;
double t_3 = t_2 * sqrt((1.0 + pow((U_m / (t_1 * (J_m * 2.0))), 2.0)));
double tmp;
if (t_3 <= -2e+295) {
tmp = -U_m;
} else if (t_3 <= 2e-63) {
tmp = t_2 * sqrt((1.0 + pow((U_m / ((J_m * 2.0) * 1.0)), 2.0)));
} else if (t_3 <= 5e+265) {
tmp = J_m * ((-2.0 * t_0) * sqrt(fma(U_m, (U_m / (fma(0.5, cos(K), 0.5) * ((J_m * J_m) * 4.0))), 1.0)));
} else {
tmp = (-U_m * t_0) / sqrt(fma(cos(K), 0.5, 0.5));
}
return J_s * tmp;
}
U_m = abs(U) J\_m = abs(J) J\_s = copysign(1.0, J) function code(J_s, J_m, K, U_m) t_0 = cos(Float64(K * 0.5)) t_1 = cos(Float64(K / 2.0)) t_2 = Float64(Float64(-2.0 * J_m) * t_1) t_3 = Float64(t_2 * sqrt(Float64(1.0 + (Float64(U_m / Float64(t_1 * Float64(J_m * 2.0))) ^ 2.0)))) tmp = 0.0 if (t_3 <= -2e+295) tmp = Float64(-U_m); elseif (t_3 <= 2e-63) tmp = Float64(t_2 * sqrt(Float64(1.0 + (Float64(U_m / Float64(Float64(J_m * 2.0) * 1.0)) ^ 2.0)))); elseif (t_3 <= 5e+265) tmp = Float64(J_m * Float64(Float64(-2.0 * t_0) * sqrt(fma(U_m, Float64(U_m / Float64(fma(0.5, cos(K), 0.5) * Float64(Float64(J_m * J_m) * 4.0))), 1.0)))); else tmp = Float64(Float64(Float64(-U_m) * t_0) / sqrt(fma(cos(K), 0.5, 0.5))); end return Float64(J_s * tmp) end
U_m = N[Abs[U], $MachinePrecision]
J\_m = N[Abs[J], $MachinePrecision]
J\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[J]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[J$95$s_, J$95$m_, K_, U$95$m_] := Block[{t$95$0 = N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(N[(-2.0 * J$95$m), $MachinePrecision] * t$95$1), $MachinePrecision]}, Block[{t$95$3 = N[(t$95$2 * N[Sqrt[N[(1.0 + N[Power[N[(U$95$m / N[(t$95$1 * N[(J$95$m * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(J$95$s * If[LessEqual[t$95$3, -2e+295], (-U$95$m), If[LessEqual[t$95$3, 2e-63], N[(t$95$2 * N[Sqrt[N[(1.0 + N[Power[N[(U$95$m / N[(N[(J$95$m * 2.0), $MachinePrecision] * 1.0), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$3, 5e+265], N[(J$95$m * N[(N[(-2.0 * t$95$0), $MachinePrecision] * N[Sqrt[N[(U$95$m * N[(U$95$m / N[(N[(0.5 * N[Cos[K], $MachinePrecision] + 0.5), $MachinePrecision] * N[(N[(J$95$m * J$95$m), $MachinePrecision] * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[((-U$95$m) * t$95$0), $MachinePrecision] / N[Sqrt[N[(N[Cos[K], $MachinePrecision] * 0.5 + 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]), $MachinePrecision]]]]]
\begin{array}{l}
U_m = \left|U\right|
\\
J\_m = \left|J\right|
\\
J\_s = \mathsf{copysign}\left(1, J\right)
\\
\begin{array}{l}
t_0 := \cos \left(K \cdot 0.5\right)\\
t_1 := \cos \left(\frac{K}{2}\right)\\
t_2 := \left(-2 \cdot J\_m\right) \cdot t\_1\\
t_3 := t\_2 \cdot \sqrt{1 + {\left(\frac{U\_m}{t\_1 \cdot \left(J\_m \cdot 2\right)}\right)}^{2}}\\
J\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_3 \leq -2 \cdot 10^{+295}:\\
\;\;\;\;-U\_m\\
\mathbf{elif}\;t\_3 \leq 2 \cdot 10^{-63}:\\
\;\;\;\;t\_2 \cdot \sqrt{1 + {\left(\frac{U\_m}{\left(J\_m \cdot 2\right) \cdot 1}\right)}^{2}}\\
\mathbf{elif}\;t\_3 \leq 5 \cdot 10^{+265}:\\
\;\;\;\;J\_m \cdot \left(\left(-2 \cdot t\_0\right) \cdot \sqrt{\mathsf{fma}\left(U\_m, \frac{U\_m}{\mathsf{fma}\left(0.5, \cos K, 0.5\right) \cdot \left(\left(J\_m \cdot J\_m\right) \cdot 4\right)}, 1\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(-U\_m\right) \cdot t\_0}{\sqrt{\mathsf{fma}\left(\cos K, 0.5, 0.5\right)}}\\
\end{array}
\end{array}
\end{array}
if (*.f64 (*.f64 (*.f64 #s(literal -2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64)))) (sqrt.f64 (+.f64 #s(literal 1 binary64) (pow.f64 (/.f64 U (*.f64 (*.f64 #s(literal 2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64))))) #s(literal 2 binary64))))) < -2e295Initial program 13.7%
Taylor expanded in J around 0
mul-1-negN/A
lower-neg.f6493.4
Applied rewrites93.4%
if -2e295 < (*.f64 (*.f64 (*.f64 #s(literal -2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64)))) (sqrt.f64 (+.f64 #s(literal 1 binary64) (pow.f64 (/.f64 U (*.f64 (*.f64 #s(literal 2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64))))) #s(literal 2 binary64))))) < 2.00000000000000013e-63Initial program 99.8%
Taylor expanded in K around 0
Applied rewrites89.4%
if 2.00000000000000013e-63 < (*.f64 (*.f64 (*.f64 #s(literal -2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64)))) (sqrt.f64 (+.f64 #s(literal 1 binary64) (pow.f64 (/.f64 U (*.f64 (*.f64 #s(literal 2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64))))) #s(literal 2 binary64))))) < 5.0000000000000002e265Initial program 99.7%
Applied rewrites94.5%
Taylor expanded in K around inf
*-commutativeN/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-cos.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6494.5
Applied rewrites94.5%
if 5.0000000000000002e265 < (*.f64 (*.f64 (*.f64 #s(literal -2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64)))) (sqrt.f64 (+.f64 #s(literal 1 binary64) (pow.f64 (/.f64 U (*.f64 (*.f64 #s(literal 2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64))))) #s(literal 2 binary64))))) Initial program 23.7%
lift-pow.f64N/A
unpow2N/A
lift-/.f64N/A
lift-*.f64N/A
associate-/r*N/A
associate-*l/N/A
lift-/.f64N/A
associate-*r/N/A
associate-/r*N/A
lower-/.f64N/A
Applied rewrites19.8%
Taylor expanded in J around 0
mul-1-negN/A
lower-neg.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-cos.f64N/A
lower-*.f64N/A
lower-cos.f64N/A
lower-*.f6485.3
Applied rewrites85.3%
Applied rewrites85.5%
Final simplification90.6%
herbie shell --seed 2024223
(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)))))