
(FPCore (x y z) :precision binary64 (+ (* x (sin y)) (* z (cos y))))
double code(double x, double y, double z) {
return (x * sin(y)) + (z * cos(y));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * sin(y)) + (z * cos(y))
end function
public static double code(double x, double y, double z) {
return (x * Math.sin(y)) + (z * Math.cos(y));
}
def code(x, y, z): return (x * math.sin(y)) + (z * math.cos(y))
function code(x, y, z) return Float64(Float64(x * sin(y)) + Float64(z * cos(y))) end
function tmp = code(x, y, z) tmp = (x * sin(y)) + (z * cos(y)); end
code[x_, y_, z_] := N[(N[(x * N[Sin[y], $MachinePrecision]), $MachinePrecision] + N[(z * N[Cos[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \sin y + z \cdot \cos y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* x (sin y)) (* z (cos y))))
double code(double x, double y, double z) {
return (x * sin(y)) + (z * cos(y));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * sin(y)) + (z * cos(y))
end function
public static double code(double x, double y, double z) {
return (x * Math.sin(y)) + (z * Math.cos(y));
}
def code(x, y, z): return (x * math.sin(y)) + (z * math.cos(y))
function code(x, y, z) return Float64(Float64(x * sin(y)) + Float64(z * cos(y))) end
function tmp = code(x, y, z) tmp = (x * sin(y)) + (z * cos(y)); end
code[x_, y_, z_] := N[(N[(x * N[Sin[y], $MachinePrecision]), $MachinePrecision] + N[(z * N[Cos[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \sin y + z \cdot \cos y
\end{array}
(FPCore (x y z) :precision binary64 (fma (cos y) z (* (sin y) x)))
double code(double x, double y, double z) {
return fma(cos(y), z, (sin(y) * x));
}
function code(x, y, z) return fma(cos(y), z, Float64(sin(y) * x)) end
code[x_, y_, z_] := N[(N[Cos[y], $MachinePrecision] * z + N[(N[Sin[y], $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos y, z, \sin y \cdot x\right)
\end{array}
Initial program 99.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
(FPCore (x y z) :precision binary64 (if (or (<= x -7.5e-91) (not (<= x 2e-94))) (fma 1.0 z (* (sin y) x)) (* (cos y) z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -7.5e-91) || !(x <= 2e-94)) {
tmp = fma(1.0, z, (sin(y) * x));
} else {
tmp = cos(y) * z;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -7.5e-91) || !(x <= 2e-94)) tmp = fma(1.0, z, Float64(sin(y) * x)); else tmp = Float64(cos(y) * z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -7.5e-91], N[Not[LessEqual[x, 2e-94]], $MachinePrecision]], N[(1.0 * z + N[(N[Sin[y], $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision], N[(N[Cos[y], $MachinePrecision] * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.5 \cdot 10^{-91} \lor \neg \left(x \leq 2 \cdot 10^{-94}\right):\\
\;\;\;\;\mathsf{fma}\left(1, z, \sin y \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\cos y \cdot z\\
\end{array}
\end{array}
if x < -7.50000000000000051e-91 or 1.9999999999999999e-94 < x Initial program 99.7%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.7
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.7
Applied rewrites99.7%
Taylor expanded in y around 0
Applied rewrites89.0%
if -7.50000000000000051e-91 < x < 1.9999999999999999e-94Initial program 99.8%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6491.6
Applied rewrites91.6%
Final simplification89.9%
(FPCore (x y z)
:precision binary64
(if (or (<= y -1.7) (not (<= y 0.5)))
(* (cos y) z)
(+
(*
(fma
(* x (fma 0.008333333333333333 (* y y) -0.16666666666666666))
(* y y)
x)
y)
(*
z
(fma
(fma
(fma -0.001388888888888889 (* y y) 0.041666666666666664)
(* y y)
-0.5)
(* y y)
1.0)))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -1.7) || !(y <= 0.5)) {
tmp = cos(y) * z;
} else {
tmp = (fma((x * fma(0.008333333333333333, (y * y), -0.16666666666666666)), (y * y), x) * y) + (z * fma(fma(fma(-0.001388888888888889, (y * y), 0.041666666666666664), (y * y), -0.5), (y * y), 1.0));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -1.7) || !(y <= 0.5)) tmp = Float64(cos(y) * z); else tmp = Float64(Float64(fma(Float64(x * fma(0.008333333333333333, Float64(y * y), -0.16666666666666666)), Float64(y * y), x) * y) + Float64(z * fma(fma(fma(-0.001388888888888889, Float64(y * y), 0.041666666666666664), Float64(y * y), -0.5), Float64(y * y), 1.0))); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -1.7], N[Not[LessEqual[y, 0.5]], $MachinePrecision]], N[(N[Cos[y], $MachinePrecision] * z), $MachinePrecision], N[(N[(N[(N[(x * N[(0.008333333333333333 * N[(y * y), $MachinePrecision] + -0.16666666666666666), $MachinePrecision]), $MachinePrecision] * N[(y * y), $MachinePrecision] + x), $MachinePrecision] * y), $MachinePrecision] + N[(z * N[(N[(N[(-0.001388888888888889 * N[(y * y), $MachinePrecision] + 0.041666666666666664), $MachinePrecision] * N[(y * y), $MachinePrecision] + -0.5), $MachinePrecision] * N[(y * y), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.7 \lor \neg \left(y \leq 0.5\right):\\
\;\;\;\;\cos y \cdot z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x \cdot \mathsf{fma}\left(0.008333333333333333, y \cdot y, -0.16666666666666666\right), y \cdot y, x\right) \cdot y + z \cdot \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.001388888888888889, y \cdot y, 0.041666666666666664\right), y \cdot y, -0.5\right), y \cdot y, 1\right)\\
\end{array}
\end{array}
if y < -1.69999999999999996 or 0.5 < y Initial program 99.6%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6447.7
Applied rewrites47.7%
if -1.69999999999999996 < y < 0.5Initial program 100.0%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6499.5
Applied rewrites99.5%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.5
Applied rewrites99.5%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites100.0%
Final simplification73.4%
(FPCore (x y z) :precision binary64 (fma y x z))
double code(double x, double y, double z) {
return fma(y, x, z);
}
function code(x, y, z) return fma(y, x, z) end
code[x_, y_, z_] := N[(y * x + z), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x, z\right)
\end{array}
Initial program 99.8%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6451.2
Applied rewrites51.2%
(FPCore (x y z) :precision binary64 (* 1.0 z))
double code(double x, double y, double z) {
return 1.0 * z;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 1.0d0 * z
end function
public static double code(double x, double y, double z) {
return 1.0 * z;
}
def code(x, y, z): return 1.0 * z
function code(x, y, z) return Float64(1.0 * z) end
function tmp = code(x, y, z) tmp = 1.0 * z; end
code[x_, y_, z_] := N[(1.0 * z), $MachinePrecision]
\begin{array}{l}
\\
1 \cdot z
\end{array}
Initial program 99.8%
lift-+.f64N/A
flip-+N/A
div-subN/A
sub-negN/A
lift-*.f64N/A
associate-*l*N/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
Applied rewrites69.1%
Taylor expanded in z around inf
*-commutativeN/A
sub-negN/A
mul-1-negN/A
remove-double-negN/A
+-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-sin.f64N/A
lower-cos.f6490.5
Applied rewrites90.5%
Taylor expanded in x around inf
Applied rewrites32.4%
Taylor expanded in y around 0
Applied rewrites39.3%
herbie shell --seed 2024323
(FPCore (x y z)
:name "Diagrams.ThreeD.Transform:aboutX from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ (* x (sin y)) (* z (cos y))))