Diagrams.ThreeD.Transform:aboutX from diagrams-lib-1.3.0.3, B

Percentage Accurate: 99.8% → 99.8%
Time: 5.7s
Alternatives: 7
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ x \cdot \sin y + z \cdot \cos y \end{array} \]
(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:

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 7 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: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot \sin y + z \cdot \cos y \end{array} \]
(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}

Alternative 1: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot \sin y + z \cdot \cos y \end{array} \]
(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}
Derivation
  1. Initial program 99.8%

    \[x \cdot \sin y + z \cdot \cos y \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 85.5% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := z \cdot \cos y\\ \mathbf{if}\;z \leq -1.8 \cdot 10^{+131}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq 7.2 \cdot 10^{+37}:\\ \;\;\;\;x \cdot \sin y + z\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* z (cos y))))
   (if (<= z -1.8e+131) t_0 (if (<= z 7.2e+37) (+ (* x (sin y)) z) t_0))))
double code(double x, double y, double z) {
	double t_0 = z * cos(y);
	double tmp;
	if (z <= -1.8e+131) {
		tmp = t_0;
	} else if (z <= 7.2e+37) {
		tmp = (x * sin(y)) + z;
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: tmp
    t_0 = z * cos(y)
    if (z <= (-1.8d+131)) then
        tmp = t_0
    else if (z <= 7.2d+37) then
        tmp = (x * sin(y)) + z
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = z * Math.cos(y);
	double tmp;
	if (z <= -1.8e+131) {
		tmp = t_0;
	} else if (z <= 7.2e+37) {
		tmp = (x * Math.sin(y)) + z;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = z * math.cos(y)
	tmp = 0
	if z <= -1.8e+131:
		tmp = t_0
	elif z <= 7.2e+37:
		tmp = (x * math.sin(y)) + z
	else:
		tmp = t_0
	return tmp
function code(x, y, z)
	t_0 = Float64(z * cos(y))
	tmp = 0.0
	if (z <= -1.8e+131)
		tmp = t_0;
	elseif (z <= 7.2e+37)
		tmp = Float64(Float64(x * sin(y)) + z);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = z * cos(y);
	tmp = 0.0;
	if (z <= -1.8e+131)
		tmp = t_0;
	elseif (z <= 7.2e+37)
		tmp = (x * sin(y)) + z;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(z * N[Cos[y], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -1.8e+131], t$95$0, If[LessEqual[z, 7.2e+37], N[(N[(x * N[Sin[y], $MachinePrecision]), $MachinePrecision] + z), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := z \cdot \cos y\\
\mathbf{if}\;z \leq -1.8 \cdot 10^{+131}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z \leq 7.2 \cdot 10^{+37}:\\
\;\;\;\;x \cdot \sin y + z\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -1.80000000000000016e131 or 7.19999999999999995e37 < z

    1. Initial program 99.8%

      \[x \cdot \sin y + z \cdot \cos y \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 88.3%

      \[\leadsto \color{blue}{z \cdot \cos y} \]

    if -1.80000000000000016e131 < z < 7.19999999999999995e37

    1. Initial program 99.8%

      \[x \cdot \sin y + z \cdot \cos y \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0 85.9%

      \[\leadsto x \cdot \sin y + \color{blue}{z} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 73.6% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := x \cdot \sin y\\ \mathbf{if}\;y \leq -4.3 \cdot 10^{+33}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 14:\\ \;\;\;\;z + x \cdot y\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* x (sin y))))
   (if (<= y -4.3e+33) t_0 (if (<= y 14.0) (+ z (* x y)) t_0))))
double code(double x, double y, double z) {
	double t_0 = x * sin(y);
	double tmp;
	if (y <= -4.3e+33) {
		tmp = t_0;
	} else if (y <= 14.0) {
		tmp = z + (x * y);
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: tmp
    t_0 = x * sin(y)
    if (y <= (-4.3d+33)) then
        tmp = t_0
    else if (y <= 14.0d0) then
        tmp = z + (x * y)
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = x * Math.sin(y);
	double tmp;
	if (y <= -4.3e+33) {
		tmp = t_0;
	} else if (y <= 14.0) {
		tmp = z + (x * y);
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = x * math.sin(y)
	tmp = 0
	if y <= -4.3e+33:
		tmp = t_0
	elif y <= 14.0:
		tmp = z + (x * y)
	else:
		tmp = t_0
	return tmp
function code(x, y, z)
	t_0 = Float64(x * sin(y))
	tmp = 0.0
	if (y <= -4.3e+33)
		tmp = t_0;
	elseif (y <= 14.0)
		tmp = Float64(z + Float64(x * y));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = x * sin(y);
	tmp = 0.0;
	if (y <= -4.3e+33)
		tmp = t_0;
	elseif (y <= 14.0)
		tmp = z + (x * y);
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(x * N[Sin[y], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -4.3e+33], t$95$0, If[LessEqual[y, 14.0], N[(z + N[(x * y), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := x \cdot \sin y\\
\mathbf{if}\;y \leq -4.3 \cdot 10^{+33}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y \leq 14:\\
\;\;\;\;z + x \cdot y\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -4.30000000000000028e33 or 14 < y

    1. Initial program 99.6%

      \[x \cdot \sin y + z \cdot \cos y \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 48.7%

      \[\leadsto \color{blue}{x \cdot \sin y} \]

    if -4.30000000000000028e33 < y < 14

    1. Initial program 100.0%

      \[x \cdot \sin y + z \cdot \cos y \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0 96.7%

      \[\leadsto \color{blue}{z + x \cdot y} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 73.3% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := z \cdot \cos y\\ \mathbf{if}\;z \leq -8 \cdot 10^{-129}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq 66000000000:\\ \;\;\;\;x \cdot \sin y\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* z (cos y))))
   (if (<= z -8e-129) t_0 (if (<= z 66000000000.0) (* x (sin y)) t_0))))
double code(double x, double y, double z) {
	double t_0 = z * cos(y);
	double tmp;
	if (z <= -8e-129) {
		tmp = t_0;
	} else if (z <= 66000000000.0) {
		tmp = x * sin(y);
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: tmp
    t_0 = z * cos(y)
    if (z <= (-8d-129)) then
        tmp = t_0
    else if (z <= 66000000000.0d0) then
        tmp = x * sin(y)
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = z * Math.cos(y);
	double tmp;
	if (z <= -8e-129) {
		tmp = t_0;
	} else if (z <= 66000000000.0) {
		tmp = x * Math.sin(y);
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = z * math.cos(y)
	tmp = 0
	if z <= -8e-129:
		tmp = t_0
	elif z <= 66000000000.0:
		tmp = x * math.sin(y)
	else:
		tmp = t_0
	return tmp
function code(x, y, z)
	t_0 = Float64(z * cos(y))
	tmp = 0.0
	if (z <= -8e-129)
		tmp = t_0;
	elseif (z <= 66000000000.0)
		tmp = Float64(x * sin(y));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = z * cos(y);
	tmp = 0.0;
	if (z <= -8e-129)
		tmp = t_0;
	elseif (z <= 66000000000.0)
		tmp = x * sin(y);
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(z * N[Cos[y], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -8e-129], t$95$0, If[LessEqual[z, 66000000000.0], N[(x * N[Sin[y], $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := z \cdot \cos y\\
\mathbf{if}\;z \leq -8 \cdot 10^{-129}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z \leq 66000000000:\\
\;\;\;\;x \cdot \sin y\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -7.9999999999999994e-129 or 6.6e10 < z

    1. Initial program 99.8%

      \[x \cdot \sin y + z \cdot \cos y \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 79.5%

      \[\leadsto \color{blue}{z \cdot \cos y} \]

    if -7.9999999999999994e-129 < z < 6.6e10

    1. Initial program 99.8%

      \[x \cdot \sin y + z \cdot \cos y \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 70.9%

      \[\leadsto \color{blue}{x \cdot \sin y} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 41.4% accurate, 15.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1.4 \cdot 10^{+202}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 3.3 \cdot 10^{+140}:\\ \;\;\;\;z\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= x -1.4e+202) (* x y) (if (<= x 3.3e+140) z (* x y))))
double code(double x, double y, double z) {
	double tmp;
	if (x <= -1.4e+202) {
		tmp = x * y;
	} else if (x <= 3.3e+140) {
		tmp = z;
	} else {
		tmp = x * y;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (x <= (-1.4d+202)) then
        tmp = x * y
    else if (x <= 3.3d+140) then
        tmp = z
    else
        tmp = x * y
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (x <= -1.4e+202) {
		tmp = x * y;
	} else if (x <= 3.3e+140) {
		tmp = z;
	} else {
		tmp = x * y;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if x <= -1.4e+202:
		tmp = x * y
	elif x <= 3.3e+140:
		tmp = z
	else:
		tmp = x * y
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (x <= -1.4e+202)
		tmp = Float64(x * y);
	elseif (x <= 3.3e+140)
		tmp = z;
	else
		tmp = Float64(x * y);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (x <= -1.4e+202)
		tmp = x * y;
	elseif (x <= 3.3e+140)
		tmp = z;
	else
		tmp = x * y;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[x, -1.4e+202], N[(x * y), $MachinePrecision], If[LessEqual[x, 3.3e+140], z, N[(x * y), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.4 \cdot 10^{+202}:\\
\;\;\;\;x \cdot y\\

\mathbf{elif}\;x \leq 3.3 \cdot 10^{+140}:\\
\;\;\;\;z\\

\mathbf{else}:\\
\;\;\;\;x \cdot y\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1.40000000000000008e202 or 3.3000000000000002e140 < x

    1. Initial program 99.8%

      \[x \cdot \sin y + z \cdot \cos y \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 76.1%

      \[\leadsto \color{blue}{x \cdot \sin y} \]
    4. Taylor expanded in y around 0 41.5%

      \[\leadsto \color{blue}{x \cdot y} \]

    if -1.40000000000000008e202 < x < 3.3000000000000002e140

    1. Initial program 99.8%

      \[x \cdot \sin y + z \cdot \cos y \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0 42.0%

      \[\leadsto \color{blue}{z} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 52.1% accurate, 41.4× speedup?

\[\begin{array}{l} \\ z + x \cdot y \end{array} \]
(FPCore (x y z) :precision binary64 (+ z (* x y)))
double code(double x, double y, double z) {
	return z + (x * y);
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = z + (x * y)
end function
public static double code(double x, double y, double z) {
	return z + (x * y);
}
def code(x, y, z):
	return z + (x * y)
function code(x, y, z)
	return Float64(z + Float64(x * y))
end
function tmp = code(x, y, z)
	tmp = z + (x * y);
end
code[x_, y_, z_] := N[(z + N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
z + x \cdot y
\end{array}
Derivation
  1. Initial program 99.8%

    \[x \cdot \sin y + z \cdot \cos y \]
  2. Add Preprocessing
  3. Taylor expanded in y around 0 49.7%

    \[\leadsto \color{blue}{z + x \cdot y} \]
  4. Add Preprocessing

Alternative 7: 38.9% accurate, 207.0× speedup?

\[\begin{array}{l} \\ z \end{array} \]
(FPCore (x y z) :precision binary64 z)
double code(double x, double y, double z) {
	return z;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = z
end function
public static double code(double x, double y, double z) {
	return z;
}
def code(x, y, z):
	return z
function code(x, y, z)
	return z
end
function tmp = code(x, y, z)
	tmp = z;
end
code[x_, y_, z_] := z
\begin{array}{l}

\\
z
\end{array}
Derivation
  1. Initial program 99.8%

    \[x \cdot \sin y + z \cdot \cos y \]
  2. Add Preprocessing
  3. Taylor expanded in y around 0 35.6%

    \[\leadsto \color{blue}{z} \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2024077 -o generate:simplify
(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))))