
(FPCore (x c s) :precision binary64 (/ (cos (* 2.0 x)) (* (pow c 2.0) (* (* x (pow s 2.0)) x))))
double code(double x, double c, double s) {
return cos((2.0 * x)) / (pow(c, 2.0) * ((x * pow(s, 2.0)) * x));
}
real(8) function code(x, c, s)
real(8), intent (in) :: x
real(8), intent (in) :: c
real(8), intent (in) :: s
code = cos((2.0d0 * x)) / ((c ** 2.0d0) * ((x * (s ** 2.0d0)) * x))
end function
public static double code(double x, double c, double s) {
return Math.cos((2.0 * x)) / (Math.pow(c, 2.0) * ((x * Math.pow(s, 2.0)) * x));
}
def code(x, c, s): return math.cos((2.0 * x)) / (math.pow(c, 2.0) * ((x * math.pow(s, 2.0)) * x))
function code(x, c, s) return Float64(cos(Float64(2.0 * x)) / Float64((c ^ 2.0) * Float64(Float64(x * (s ^ 2.0)) * x))) end
function tmp = code(x, c, s) tmp = cos((2.0 * x)) / ((c ^ 2.0) * ((x * (s ^ 2.0)) * x)); end
code[x_, c_, s_] := N[(N[Cos[N[(2.0 * x), $MachinePrecision]], $MachinePrecision] / N[(N[Power[c, 2.0], $MachinePrecision] * N[(N[(x * N[Power[s, 2.0], $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\cos \left(2 \cdot x\right)}{{c}^{2} \cdot \left(\left(x \cdot {s}^{2}\right) \cdot x\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x c s) :precision binary64 (/ (cos (* 2.0 x)) (* (pow c 2.0) (* (* x (pow s 2.0)) x))))
double code(double x, double c, double s) {
return cos((2.0 * x)) / (pow(c, 2.0) * ((x * pow(s, 2.0)) * x));
}
real(8) function code(x, c, s)
real(8), intent (in) :: x
real(8), intent (in) :: c
real(8), intent (in) :: s
code = cos((2.0d0 * x)) / ((c ** 2.0d0) * ((x * (s ** 2.0d0)) * x))
end function
public static double code(double x, double c, double s) {
return Math.cos((2.0 * x)) / (Math.pow(c, 2.0) * ((x * Math.pow(s, 2.0)) * x));
}
def code(x, c, s): return math.cos((2.0 * x)) / (math.pow(c, 2.0) * ((x * math.pow(s, 2.0)) * x))
function code(x, c, s) return Float64(cos(Float64(2.0 * x)) / Float64((c ^ 2.0) * Float64(Float64(x * (s ^ 2.0)) * x))) end
function tmp = code(x, c, s) tmp = cos((2.0 * x)) / ((c ^ 2.0) * ((x * (s ^ 2.0)) * x)); end
code[x_, c_, s_] := N[(N[Cos[N[(2.0 * x), $MachinePrecision]], $MachinePrecision] / N[(N[Power[c, 2.0], $MachinePrecision] * N[(N[(x * N[Power[s, 2.0], $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\cos \left(2 \cdot x\right)}{{c}^{2} \cdot \left(\left(x \cdot {s}^{2}\right) \cdot x\right)}
\end{array}
s_m = (fabs.f64 s) NOTE: x, c, and s_m should be sorted in increasing order before calling this function. (FPCore (x c s_m) :precision binary64 (let* ((t_0 (* c (* x s_m)))) (/ (/ (cos (* x 2.0)) t_0) t_0)))
s_m = fabs(s);
assert(x < c && c < s_m);
double code(double x, double c, double s_m) {
double t_0 = c * (x * s_m);
return (cos((x * 2.0)) / t_0) / t_0;
}
s_m = abs(s)
NOTE: x, c, and s_m should be sorted in increasing order before calling this function.
real(8) function code(x, c, s_m)
real(8), intent (in) :: x
real(8), intent (in) :: c
real(8), intent (in) :: s_m
real(8) :: t_0
t_0 = c * (x * s_m)
code = (cos((x * 2.0d0)) / t_0) / t_0
end function
s_m = Math.abs(s);
assert x < c && c < s_m;
public static double code(double x, double c, double s_m) {
double t_0 = c * (x * s_m);
return (Math.cos((x * 2.0)) / t_0) / t_0;
}
s_m = math.fabs(s) [x, c, s_m] = sort([x, c, s_m]) def code(x, c, s_m): t_0 = c * (x * s_m) return (math.cos((x * 2.0)) / t_0) / t_0
s_m = abs(s) x, c, s_m = sort([x, c, s_m]) function code(x, c, s_m) t_0 = Float64(c * Float64(x * s_m)) return Float64(Float64(cos(Float64(x * 2.0)) / t_0) / t_0) end
s_m = abs(s);
x, c, s_m = num2cell(sort([x, c, s_m])){:}
function tmp = code(x, c, s_m)
t_0 = c * (x * s_m);
tmp = (cos((x * 2.0)) / t_0) / t_0;
end
s_m = N[Abs[s], $MachinePrecision]
NOTE: x, c, and s_m should be sorted in increasing order before calling this function.
code[x_, c_, s$95$m_] := Block[{t$95$0 = N[(c * N[(x * s$95$m), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[Cos[N[(x * 2.0), $MachinePrecision]], $MachinePrecision] / t$95$0), $MachinePrecision] / t$95$0), $MachinePrecision]]
\begin{array}{l}
s_m = \left|s\right|
\\
[x, c, s_m] = \mathsf{sort}([x, c, s_m])\\
\\
\begin{array}{l}
t_0 := c \cdot \left(x \cdot s\_m\right)\\
\frac{\frac{\cos \left(x \cdot 2\right)}{t\_0}}{t\_0}
\end{array}
\end{array}
Initial program 70.8%
*-un-lft-identity70.8%
add-sqr-sqrt70.7%
times-frac70.7%
sqrt-prod70.7%
sqrt-pow150.8%
metadata-eval50.8%
pow150.8%
*-commutative50.8%
associate-*r*46.6%
unpow246.6%
pow-prod-down50.8%
sqrt-pow148.0%
metadata-eval48.0%
pow148.0%
*-commutative48.0%
Applied egg-rr98.5%
associate-*l/98.5%
*-un-lft-identity98.5%
*-commutative98.5%
Applied egg-rr98.5%
Final simplification98.5%
s_m = (fabs.f64 s) NOTE: x, c, and s_m should be sorted in increasing order before calling this function. (FPCore (x c s_m) :precision binary64 (if (<= x 0.054) (pow (* c (* x s_m)) -2.0) (/ (cos (* x 2.0)) (* (* x c) (* x (* s_m (* c s_m)))))))
s_m = fabs(s);
assert(x < c && c < s_m);
double code(double x, double c, double s_m) {
double tmp;
if (x <= 0.054) {
tmp = pow((c * (x * s_m)), -2.0);
} else {
tmp = cos((x * 2.0)) / ((x * c) * (x * (s_m * (c * s_m))));
}
return tmp;
}
s_m = abs(s)
NOTE: x, c, and s_m should be sorted in increasing order before calling this function.
real(8) function code(x, c, s_m)
real(8), intent (in) :: x
real(8), intent (in) :: c
real(8), intent (in) :: s_m
real(8) :: tmp
if (x <= 0.054d0) then
tmp = (c * (x * s_m)) ** (-2.0d0)
else
tmp = cos((x * 2.0d0)) / ((x * c) * (x * (s_m * (c * s_m))))
end if
code = tmp
end function
s_m = Math.abs(s);
assert x < c && c < s_m;
public static double code(double x, double c, double s_m) {
double tmp;
if (x <= 0.054) {
tmp = Math.pow((c * (x * s_m)), -2.0);
} else {
tmp = Math.cos((x * 2.0)) / ((x * c) * (x * (s_m * (c * s_m))));
}
return tmp;
}
s_m = math.fabs(s) [x, c, s_m] = sort([x, c, s_m]) def code(x, c, s_m): tmp = 0 if x <= 0.054: tmp = math.pow((c * (x * s_m)), -2.0) else: tmp = math.cos((x * 2.0)) / ((x * c) * (x * (s_m * (c * s_m)))) return tmp
s_m = abs(s) x, c, s_m = sort([x, c, s_m]) function code(x, c, s_m) tmp = 0.0 if (x <= 0.054) tmp = Float64(c * Float64(x * s_m)) ^ -2.0; else tmp = Float64(cos(Float64(x * 2.0)) / Float64(Float64(x * c) * Float64(x * Float64(s_m * Float64(c * s_m))))); end return tmp end
s_m = abs(s);
x, c, s_m = num2cell(sort([x, c, s_m])){:}
function tmp_2 = code(x, c, s_m)
tmp = 0.0;
if (x <= 0.054)
tmp = (c * (x * s_m)) ^ -2.0;
else
tmp = cos((x * 2.0)) / ((x * c) * (x * (s_m * (c * s_m))));
end
tmp_2 = tmp;
end
s_m = N[Abs[s], $MachinePrecision] NOTE: x, c, and s_m should be sorted in increasing order before calling this function. code[x_, c_, s$95$m_] := If[LessEqual[x, 0.054], N[Power[N[(c * N[(x * s$95$m), $MachinePrecision]), $MachinePrecision], -2.0], $MachinePrecision], N[(N[Cos[N[(x * 2.0), $MachinePrecision]], $MachinePrecision] / N[(N[(x * c), $MachinePrecision] * N[(x * N[(s$95$m * N[(c * s$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
s_m = \left|s\right|
\\
[x, c, s_m] = \mathsf{sort}([x, c, s_m])\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.054:\\
\;\;\;\;{\left(c \cdot \left(x \cdot s\_m\right)\right)}^{-2}\\
\mathbf{else}:\\
\;\;\;\;\frac{\cos \left(x \cdot 2\right)}{\left(x \cdot c\right) \cdot \left(x \cdot \left(s\_m \cdot \left(c \cdot s\_m\right)\right)\right)}\\
\end{array}
\end{array}
if x < 0.0539999999999999994Initial program 73.8%
Taylor expanded in x around 0 62.1%
associate-/r*62.1%
*-commutative62.1%
unpow262.1%
unpow262.1%
swap-sqr72.0%
unpow272.0%
associate-/r*71.7%
unpow271.7%
unpow271.7%
swap-sqr85.4%
unpow285.4%
*-commutative85.4%
Simplified85.4%
pow-flip85.9%
*-commutative85.9%
metadata-eval85.9%
Applied egg-rr85.9%
if 0.0539999999999999994 < x Initial program 62.9%
clear-num62.9%
inv-pow62.9%
Applied egg-rr96.8%
unpow-196.8%
clear-num96.8%
*-un-lft-identity96.8%
unpow296.8%
frac-times96.8%
associate-*r/96.9%
*-commutative96.9%
times-frac92.9%
*-commutative92.9%
Applied egg-rr92.9%
*-commutative92.9%
associate-/r*91.6%
frac-times94.4%
associate-/r*94.4%
associate-/l/80.6%
*-commutative80.6%
associate-/l*80.5%
div-inv80.5%
associate-/r*93.1%
*-commutative93.1%
associate-/l/89.1%
associate-/l/88.6%
Applied egg-rr88.6%
associate-/l/88.5%
*-commutative88.5%
associate-*l*80.9%
*-commutative80.9%
Simplified80.9%
Final simplification84.5%
s_m = (fabs.f64 s) NOTE: x, c, and s_m should be sorted in increasing order before calling this function. (FPCore (x c s_m) :precision binary64 (/ (/ (cos (* x 2.0)) (* c (* x s_m))) (* s_m (* x c))))
s_m = fabs(s);
assert(x < c && c < s_m);
double code(double x, double c, double s_m) {
return (cos((x * 2.0)) / (c * (x * s_m))) / (s_m * (x * c));
}
s_m = abs(s)
NOTE: x, c, and s_m should be sorted in increasing order before calling this function.
real(8) function code(x, c, s_m)
real(8), intent (in) :: x
real(8), intent (in) :: c
real(8), intent (in) :: s_m
code = (cos((x * 2.0d0)) / (c * (x * s_m))) / (s_m * (x * c))
end function
s_m = Math.abs(s);
assert x < c && c < s_m;
public static double code(double x, double c, double s_m) {
return (Math.cos((x * 2.0)) / (c * (x * s_m))) / (s_m * (x * c));
}
s_m = math.fabs(s) [x, c, s_m] = sort([x, c, s_m]) def code(x, c, s_m): return (math.cos((x * 2.0)) / (c * (x * s_m))) / (s_m * (x * c))
s_m = abs(s) x, c, s_m = sort([x, c, s_m]) function code(x, c, s_m) return Float64(Float64(cos(Float64(x * 2.0)) / Float64(c * Float64(x * s_m))) / Float64(s_m * Float64(x * c))) end
s_m = abs(s);
x, c, s_m = num2cell(sort([x, c, s_m])){:}
function tmp = code(x, c, s_m)
tmp = (cos((x * 2.0)) / (c * (x * s_m))) / (s_m * (x * c));
end
s_m = N[Abs[s], $MachinePrecision] NOTE: x, c, and s_m should be sorted in increasing order before calling this function. code[x_, c_, s$95$m_] := N[(N[(N[Cos[N[(x * 2.0), $MachinePrecision]], $MachinePrecision] / N[(c * N[(x * s$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(s$95$m * N[(x * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
s_m = \left|s\right|
\\
[x, c, s_m] = \mathsf{sort}([x, c, s_m])\\
\\
\frac{\frac{\cos \left(x \cdot 2\right)}{c \cdot \left(x \cdot s\_m\right)}}{s\_m \cdot \left(x \cdot c\right)}
\end{array}
Initial program 70.8%
clear-num70.8%
inv-pow70.8%
Applied egg-rr98.1%
unpow-198.1%
clear-num98.1%
*-un-lft-identity98.1%
unpow298.1%
frac-times98.5%
associate-*r/98.5%
*-commutative98.5%
times-frac94.6%
*-commutative94.6%
Applied egg-rr94.6%
*-commutative94.6%
frac-times98.5%
div-inv98.5%
associate-*r*96.9%
associate-/r*93.9%
*-commutative93.9%
associate-*l*92.2%
*-commutative92.2%
*-commutative92.2%
Applied egg-rr92.2%
associate-/l/95.2%
*-commutative95.2%
associate-*r*96.9%
*-commutative96.9%
Simplified96.9%
Final simplification96.9%
s_m = (fabs.f64 s) NOTE: x, c, and s_m should be sorted in increasing order before calling this function. (FPCore (x c s_m) :precision binary64 (pow (* c (* x s_m)) -2.0))
s_m = fabs(s);
assert(x < c && c < s_m);
double code(double x, double c, double s_m) {
return pow((c * (x * s_m)), -2.0);
}
s_m = abs(s)
NOTE: x, c, and s_m should be sorted in increasing order before calling this function.
real(8) function code(x, c, s_m)
real(8), intent (in) :: x
real(8), intent (in) :: c
real(8), intent (in) :: s_m
code = (c * (x * s_m)) ** (-2.0d0)
end function
s_m = Math.abs(s);
assert x < c && c < s_m;
public static double code(double x, double c, double s_m) {
return Math.pow((c * (x * s_m)), -2.0);
}
s_m = math.fabs(s) [x, c, s_m] = sort([x, c, s_m]) def code(x, c, s_m): return math.pow((c * (x * s_m)), -2.0)
s_m = abs(s) x, c, s_m = sort([x, c, s_m]) function code(x, c, s_m) return Float64(c * Float64(x * s_m)) ^ -2.0 end
s_m = abs(s);
x, c, s_m = num2cell(sort([x, c, s_m])){:}
function tmp = code(x, c, s_m)
tmp = (c * (x * s_m)) ^ -2.0;
end
s_m = N[Abs[s], $MachinePrecision] NOTE: x, c, and s_m should be sorted in increasing order before calling this function. code[x_, c_, s$95$m_] := N[Power[N[(c * N[(x * s$95$m), $MachinePrecision]), $MachinePrecision], -2.0], $MachinePrecision]
\begin{array}{l}
s_m = \left|s\right|
\\
[x, c, s_m] = \mathsf{sort}([x, c, s_m])\\
\\
{\left(c \cdot \left(x \cdot s\_m\right)\right)}^{-2}
\end{array}
Initial program 70.8%
Taylor expanded in x around 0 56.1%
associate-/r*56.1%
*-commutative56.1%
unpow256.1%
unpow256.1%
swap-sqr64.7%
unpow264.7%
associate-/r*64.5%
unpow264.5%
unpow264.5%
swap-sqr75.7%
unpow275.7%
*-commutative75.7%
Simplified75.7%
pow-flip76.1%
*-commutative76.1%
metadata-eval76.1%
Applied egg-rr76.1%
Final simplification76.1%
s_m = (fabs.f64 s) NOTE: x, c, and s_m should be sorted in increasing order before calling this function. (FPCore (x c s_m) :precision binary64 (let* ((t_0 (* c (* x s_m)))) (/ 1.0 (* t_0 t_0))))
s_m = fabs(s);
assert(x < c && c < s_m);
double code(double x, double c, double s_m) {
double t_0 = c * (x * s_m);
return 1.0 / (t_0 * t_0);
}
s_m = abs(s)
NOTE: x, c, and s_m should be sorted in increasing order before calling this function.
real(8) function code(x, c, s_m)
real(8), intent (in) :: x
real(8), intent (in) :: c
real(8), intent (in) :: s_m
real(8) :: t_0
t_0 = c * (x * s_m)
code = 1.0d0 / (t_0 * t_0)
end function
s_m = Math.abs(s);
assert x < c && c < s_m;
public static double code(double x, double c, double s_m) {
double t_0 = c * (x * s_m);
return 1.0 / (t_0 * t_0);
}
s_m = math.fabs(s) [x, c, s_m] = sort([x, c, s_m]) def code(x, c, s_m): t_0 = c * (x * s_m) return 1.0 / (t_0 * t_0)
s_m = abs(s) x, c, s_m = sort([x, c, s_m]) function code(x, c, s_m) t_0 = Float64(c * Float64(x * s_m)) return Float64(1.0 / Float64(t_0 * t_0)) end
s_m = abs(s);
x, c, s_m = num2cell(sort([x, c, s_m])){:}
function tmp = code(x, c, s_m)
t_0 = c * (x * s_m);
tmp = 1.0 / (t_0 * t_0);
end
s_m = N[Abs[s], $MachinePrecision]
NOTE: x, c, and s_m should be sorted in increasing order before calling this function.
code[x_, c_, s$95$m_] := Block[{t$95$0 = N[(c * N[(x * s$95$m), $MachinePrecision]), $MachinePrecision]}, N[(1.0 / N[(t$95$0 * t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
s_m = \left|s\right|
\\
[x, c, s_m] = \mathsf{sort}([x, c, s_m])\\
\\
\begin{array}{l}
t_0 := c \cdot \left(x \cdot s\_m\right)\\
\frac{1}{t\_0 \cdot t\_0}
\end{array}
\end{array}
Initial program 70.8%
Taylor expanded in x around 0 56.1%
associate-/r*56.1%
*-commutative56.1%
unpow256.1%
unpow256.1%
swap-sqr64.7%
unpow264.7%
associate-/r*64.5%
unpow264.5%
unpow264.5%
swap-sqr75.7%
unpow275.7%
*-commutative75.7%
Simplified75.7%
*-commutative75.7%
unpow275.7%
Applied egg-rr75.7%
Final simplification75.7%
s_m = (fabs.f64 s) NOTE: x, c, and s_m should be sorted in increasing order before calling this function. (FPCore (x c s_m) :precision binary64 (let* ((t_0 (* c (* x s_m)))) (/ (/ 1.0 t_0) t_0)))
s_m = fabs(s);
assert(x < c && c < s_m);
double code(double x, double c, double s_m) {
double t_0 = c * (x * s_m);
return (1.0 / t_0) / t_0;
}
s_m = abs(s)
NOTE: x, c, and s_m should be sorted in increasing order before calling this function.
real(8) function code(x, c, s_m)
real(8), intent (in) :: x
real(8), intent (in) :: c
real(8), intent (in) :: s_m
real(8) :: t_0
t_0 = c * (x * s_m)
code = (1.0d0 / t_0) / t_0
end function
s_m = Math.abs(s);
assert x < c && c < s_m;
public static double code(double x, double c, double s_m) {
double t_0 = c * (x * s_m);
return (1.0 / t_0) / t_0;
}
s_m = math.fabs(s) [x, c, s_m] = sort([x, c, s_m]) def code(x, c, s_m): t_0 = c * (x * s_m) return (1.0 / t_0) / t_0
s_m = abs(s) x, c, s_m = sort([x, c, s_m]) function code(x, c, s_m) t_0 = Float64(c * Float64(x * s_m)) return Float64(Float64(1.0 / t_0) / t_0) end
s_m = abs(s);
x, c, s_m = num2cell(sort([x, c, s_m])){:}
function tmp = code(x, c, s_m)
t_0 = c * (x * s_m);
tmp = (1.0 / t_0) / t_0;
end
s_m = N[Abs[s], $MachinePrecision]
NOTE: x, c, and s_m should be sorted in increasing order before calling this function.
code[x_, c_, s$95$m_] := Block[{t$95$0 = N[(c * N[(x * s$95$m), $MachinePrecision]), $MachinePrecision]}, N[(N[(1.0 / t$95$0), $MachinePrecision] / t$95$0), $MachinePrecision]]
\begin{array}{l}
s_m = \left|s\right|
\\
[x, c, s_m] = \mathsf{sort}([x, c, s_m])\\
\\
\begin{array}{l}
t_0 := c \cdot \left(x \cdot s\_m\right)\\
\frac{\frac{1}{t\_0}}{t\_0}
\end{array}
\end{array}
Initial program 70.8%
*-un-lft-identity70.8%
add-sqr-sqrt70.7%
times-frac70.7%
sqrt-prod70.7%
sqrt-pow150.8%
metadata-eval50.8%
pow150.8%
*-commutative50.8%
associate-*r*46.6%
unpow246.6%
pow-prod-down50.8%
sqrt-pow148.0%
metadata-eval48.0%
pow148.0%
*-commutative48.0%
Applied egg-rr98.5%
associate-*l/98.5%
*-un-lft-identity98.5%
*-commutative98.5%
Applied egg-rr98.5%
Taylor expanded in x around 0 76.1%
Final simplification76.1%
herbie shell --seed 2024100
(FPCore (x c s)
:name "mixedcos"
:precision binary64
(/ (cos (* 2.0 x)) (* (pow c 2.0) (* (* x (pow s 2.0)) x))))