
(FPCore (J l K U) :precision binary64 (+ (* (* J (- (exp l) (exp (- l)))) (cos (/ K 2.0))) U))
double code(double J, double l, double K, double U) {
return ((J * (exp(l) - exp(-l))) * cos((K / 2.0))) + U;
}
real(8) function code(j, l, k, u)
real(8), intent (in) :: j
real(8), intent (in) :: l
real(8), intent (in) :: k
real(8), intent (in) :: u
code = ((j * (exp(l) - exp(-l))) * cos((k / 2.0d0))) + u
end function
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;
}
def code(J, l, K, U): return ((J * (math.exp(l) - math.exp(-l))) * math.cos((K / 2.0))) + U
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 tmp = code(J, l, K, U) tmp = ((J * (exp(l) - exp(-l))) * cos((K / 2.0))) + U; 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]
\begin{array}{l}
\\
\left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 20 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (J l K U) :precision binary64 (+ (* (* J (- (exp l) (exp (- l)))) (cos (/ K 2.0))) U))
double code(double J, double l, double K, double U) {
return ((J * (exp(l) - exp(-l))) * cos((K / 2.0))) + U;
}
real(8) function code(j, l, k, u)
real(8), intent (in) :: j
real(8), intent (in) :: l
real(8), intent (in) :: k
real(8), intent (in) :: u
code = ((j * (exp(l) - exp(-l))) * cos((k / 2.0d0))) + u
end function
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;
}
def code(J, l, K, U): return ((J * (math.exp(l) - math.exp(-l))) * math.cos((K / 2.0))) + U
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 tmp = code(J, l, K, U) tmp = ((J * (exp(l) - exp(-l))) * cos((K / 2.0))) + U; 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]
\begin{array}{l}
\\
\left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U
\end{array}
(FPCore (J l K U) :precision binary64 (fma (* 2.0 (* J (sinh l))) (cos (* -0.5 K)) U))
double code(double J, double l, double K, double U) {
return fma((2.0 * (J * sinh(l))), cos((-0.5 * K)), U);
}
function code(J, l, K, U) return fma(Float64(2.0 * Float64(J * sinh(l))), cos(Float64(-0.5 * K)), U) end
code[J_, l_, K_, U_] := N[(N[(2.0 * N[(J * N[Sinh[l], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[N[(-0.5 * K), $MachinePrecision]], $MachinePrecision] + U), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(2 \cdot \left(J \cdot \sinh \ell\right), \cos \left(-0.5 \cdot K\right), U\right)
\end{array}
Initial program 87.2%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6487.2
Applied rewrites99.9%
Final simplification99.9%
(FPCore (J l K U)
:precision binary64
(let* ((t_0 (cos (/ K 2.0)))
(t_1 (fma (* 2.0 (* J (sinh l))) (fma (* K K) -0.125 1.0) U)))
(if (<= t_0 -0.81)
t_1
(if (<= t_0 0.14)
(fma (* (* 2.0 l) J) (cos (* 0.5 K)) U)
(if (<= t_0 0.9999)
(fma
(*
(fma
(*
(fma (* (* (* l l) l) 0.0003968253968253968) l 0.3333333333333333)
l)
l
2.0)
l)
J
U)
t_1)))))
double code(double J, double l, double K, double U) {
double t_0 = cos((K / 2.0));
double t_1 = fma((2.0 * (J * sinh(l))), fma((K * K), -0.125, 1.0), U);
double tmp;
if (t_0 <= -0.81) {
tmp = t_1;
} else if (t_0 <= 0.14) {
tmp = fma(((2.0 * l) * J), cos((0.5 * K)), U);
} else if (t_0 <= 0.9999) {
tmp = fma((fma((fma((((l * l) * l) * 0.0003968253968253968), l, 0.3333333333333333) * l), l, 2.0) * l), J, U);
} else {
tmp = t_1;
}
return tmp;
}
function code(J, l, K, U) t_0 = cos(Float64(K / 2.0)) t_1 = fma(Float64(2.0 * Float64(J * sinh(l))), fma(Float64(K * K), -0.125, 1.0), U) tmp = 0.0 if (t_0 <= -0.81) tmp = t_1; elseif (t_0 <= 0.14) tmp = fma(Float64(Float64(2.0 * l) * J), cos(Float64(0.5 * K)), U); elseif (t_0 <= 0.9999) tmp = fma(Float64(fma(Float64(fma(Float64(Float64(Float64(l * l) * l) * 0.0003968253968253968), l, 0.3333333333333333) * l), l, 2.0) * l), J, U); else tmp = t_1; end return tmp end
code[J_, l_, K_, U_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[(2.0 * N[(J * N[Sinh[l], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(N[(K * K), $MachinePrecision] * -0.125 + 1.0), $MachinePrecision] + U), $MachinePrecision]}, If[LessEqual[t$95$0, -0.81], t$95$1, If[LessEqual[t$95$0, 0.14], N[(N[(N[(2.0 * l), $MachinePrecision] * J), $MachinePrecision] * N[Cos[N[(0.5 * K), $MachinePrecision]], $MachinePrecision] + U), $MachinePrecision], If[LessEqual[t$95$0, 0.9999], N[(N[(N[(N[(N[(N[(N[(N[(l * l), $MachinePrecision] * l), $MachinePrecision] * 0.0003968253968253968), $MachinePrecision] * l + 0.3333333333333333), $MachinePrecision] * l), $MachinePrecision] * l + 2.0), $MachinePrecision] * l), $MachinePrecision] * J + U), $MachinePrecision], t$95$1]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
t_1 := \mathsf{fma}\left(2 \cdot \left(J \cdot \sinh \ell\right), \mathsf{fma}\left(K \cdot K, -0.125, 1\right), U\right)\\
\mathbf{if}\;t\_0 \leq -0.81:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 0.14:\\
\;\;\;\;\mathsf{fma}\left(\left(2 \cdot \ell\right) \cdot J, \cos \left(0.5 \cdot K\right), U\right)\\
\mathbf{elif}\;t\_0 \leq 0.9999:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\left(\left(\ell \cdot \ell\right) \cdot \ell\right) \cdot 0.0003968253968253968, \ell, 0.3333333333333333\right) \cdot \ell, \ell, 2\right) \cdot \ell, J, U\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (cos.f64 (/.f64 K #s(literal 2 binary64))) < -0.81000000000000005 or 0.99990000000000001 < (cos.f64 (/.f64 K #s(literal 2 binary64))) Initial program 86.1%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6486.1
Applied rewrites100.0%
Taylor expanded in K around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6493.4
Applied rewrites93.4%
if -0.81000000000000005 < (cos.f64 (/.f64 K #s(literal 2 binary64))) < 0.14000000000000001Initial program 87.0%
Taylor expanded in l around 0
+-commutativeN/A
associate-*r*N/A
associate-*r*N/A
lower-fma.f64N/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-*.f64N/A
lower-cos.f64N/A
*-commutativeN/A
lower-*.f6464.4
Applied rewrites64.4%
if 0.14000000000000001 < (cos.f64 (/.f64 K #s(literal 2 binary64))) < 0.99990000000000001Initial program 85.6%
Taylor expanded in K around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower--.f64N/A
lower-exp.f64N/A
lower-exp.f64N/A
lower-neg.f6484.8
Applied rewrites84.8%
Taylor expanded in l around 0
Applied rewrites82.8%
Taylor expanded in l around inf
Applied rewrites82.8%
Final simplification86.0%
herbie shell --seed 2024229
(FPCore (J l K U)
:name "Maksimov and Kolovsky, Equation (4)"
:precision binary64
(+ (* (* J (- (exp l) (exp (- l)))) (cos (/ K 2.0))) U))