Diagrams.Solve.Polynomial:cubForm from diagrams-solve-0.1, B

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

Specification

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

\\
\left(x \cdot 3\right) \cdot y - z
\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} \\ \left(x \cdot 3\right) \cdot y - z \end{array} \]
(FPCore (x y z) :precision binary64 (- (* (* x 3.0) y) z))
double code(double x, double y, double z) {
	return ((x * 3.0) * y) - z;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = ((x * 3.0d0) * y) - z
end function
public static double code(double x, double y, double z) {
	return ((x * 3.0) * y) - z;
}
def code(x, y, z):
	return ((x * 3.0) * y) - z
function code(x, y, z)
	return Float64(Float64(Float64(x * 3.0) * y) - z)
end
function tmp = code(x, y, z)
	tmp = ((x * 3.0) * y) - z;
end
code[x_, y_, z_] := N[(N[(N[(x * 3.0), $MachinePrecision] * y), $MachinePrecision] - z), $MachinePrecision]
\begin{array}{l}

\\
\left(x \cdot 3\right) \cdot y - z
\end{array}

Alternative 1: 99.8% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(y \cdot x, 3, -z\right) \end{array} \]
(FPCore (x y z) :precision binary64 (fma (* y x) 3.0 (- z)))
double code(double x, double y, double z) {
	return fma((y * x), 3.0, -z);
}
function code(x, y, z)
	return fma(Float64(y * x), 3.0, Float64(-z))
end
code[x_, y_, z_] := N[(N[(y * x), $MachinePrecision] * 3.0 + (-z)), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(y \cdot x, 3, -z\right)
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(x \cdot 3\right) \cdot y - z \]
  2. Step-by-step derivation
    1. associate-*l*99.5%

      \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
  3. Simplified99.5%

    \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right) - z} \]
  4. Step-by-step derivation
    1. associate-*r*99.9%

      \[\leadsto \color{blue}{\left(x \cdot 3\right) \cdot y} - z \]
    2. *-commutative99.9%

      \[\leadsto \color{blue}{y \cdot \left(x \cdot 3\right)} - z \]
    3. associate-*r*99.9%

      \[\leadsto \color{blue}{\left(y \cdot x\right) \cdot 3} - z \]
    4. fma-neg99.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y \cdot x, 3, -z\right)} \]
  5. Applied egg-rr99.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(y \cdot x, 3, -z\right)} \]
  6. Final simplification99.9%

    \[\leadsto \mathsf{fma}\left(y \cdot x, 3, -z\right) \]

Alternative 2: 70.5% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1.25 \cdot 10^{+60} \lor \neg \left(x \leq 185000000\right):\\ \;\;\;\;\left(y \cdot x\right) \cdot 3\\ \mathbf{else}:\\ \;\;\;\;-z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= x -1.25e+60) (not (<= x 185000000.0))) (* (* y x) 3.0) (- z)))
double code(double x, double y, double z) {
	double tmp;
	if ((x <= -1.25e+60) || !(x <= 185000000.0)) {
		tmp = (y * x) * 3.0;
	} else {
		tmp = -z;
	}
	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.25d+60)) .or. (.not. (x <= 185000000.0d0))) then
        tmp = (y * x) * 3.0d0
    else
        tmp = -z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((x <= -1.25e+60) || !(x <= 185000000.0)) {
		tmp = (y * x) * 3.0;
	} else {
		tmp = -z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (x <= -1.25e+60) or not (x <= 185000000.0):
		tmp = (y * x) * 3.0
	else:
		tmp = -z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((x <= -1.25e+60) || !(x <= 185000000.0))
		tmp = Float64(Float64(y * x) * 3.0);
	else
		tmp = Float64(-z);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((x <= -1.25e+60) || ~((x <= 185000000.0)))
		tmp = (y * x) * 3.0;
	else
		tmp = -z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.25e+60], N[Not[LessEqual[x, 185000000.0]], $MachinePrecision]], N[(N[(y * x), $MachinePrecision] * 3.0), $MachinePrecision], (-z)]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.25 \cdot 10^{+60} \lor \neg \left(x \leq 185000000\right):\\
\;\;\;\;\left(y \cdot x\right) \cdot 3\\

\mathbf{else}:\\
\;\;\;\;-z\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1.24999999999999994e60 or 1.85e8 < x

    1. Initial program 99.8%

      \[\left(x \cdot 3\right) \cdot y - z \]
    2. Step-by-step derivation
      1. associate-*l*99.9%

        \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
    3. Simplified99.9%

      \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right) - z} \]
    4. Taylor expanded in x around 0 99.8%

      \[\leadsto \color{blue}{3 \cdot \left(x \cdot y\right)} - z \]
    5. Taylor expanded in x around inf 75.0%

      \[\leadsto \color{blue}{3 \cdot \left(x \cdot y\right)} \]

    if -1.24999999999999994e60 < x < 1.85e8

    1. Initial program 99.9%

      \[\left(x \cdot 3\right) \cdot y - z \]
    2. Step-by-step derivation
      1. associate-*l*99.3%

        \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
    3. Simplified99.3%

      \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right) - z} \]
    4. Taylor expanded in x around 0 72.1%

      \[\leadsto \color{blue}{-1 \cdot z} \]
    5. Step-by-step derivation
      1. neg-mul-172.1%

        \[\leadsto \color{blue}{-z} \]
    6. Simplified72.1%

      \[\leadsto \color{blue}{-z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification73.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.25 \cdot 10^{+60} \lor \neg \left(x \leq 185000000\right):\\ \;\;\;\;\left(y \cdot x\right) \cdot 3\\ \mathbf{else}:\\ \;\;\;\;-z\\ \end{array} \]

Alternative 3: 70.5% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -6.8 \cdot 10^{+59} \lor \neg \left(x \leq 1550000000000\right):\\ \;\;\;\;x \cdot \left(y \cdot 3\right)\\ \mathbf{else}:\\ \;\;\;\;-z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= x -6.8e+59) (not (<= x 1550000000000.0))) (* x (* y 3.0)) (- z)))
double code(double x, double y, double z) {
	double tmp;
	if ((x <= -6.8e+59) || !(x <= 1550000000000.0)) {
		tmp = x * (y * 3.0);
	} else {
		tmp = -z;
	}
	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 <= (-6.8d+59)) .or. (.not. (x <= 1550000000000.0d0))) then
        tmp = x * (y * 3.0d0)
    else
        tmp = -z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((x <= -6.8e+59) || !(x <= 1550000000000.0)) {
		tmp = x * (y * 3.0);
	} else {
		tmp = -z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (x <= -6.8e+59) or not (x <= 1550000000000.0):
		tmp = x * (y * 3.0)
	else:
		tmp = -z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((x <= -6.8e+59) || !(x <= 1550000000000.0))
		tmp = Float64(x * Float64(y * 3.0));
	else
		tmp = Float64(-z);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((x <= -6.8e+59) || ~((x <= 1550000000000.0)))
		tmp = x * (y * 3.0);
	else
		tmp = -z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[x, -6.8e+59], N[Not[LessEqual[x, 1550000000000.0]], $MachinePrecision]], N[(x * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], (-z)]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -6.8 \cdot 10^{+59} \lor \neg \left(x \leq 1550000000000\right):\\
\;\;\;\;x \cdot \left(y \cdot 3\right)\\

\mathbf{else}:\\
\;\;\;\;-z\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -6.80000000000000012e59 or 1.55e12 < x

    1. Initial program 99.8%

      \[\left(x \cdot 3\right) \cdot y - z \]
    2. Step-by-step derivation
      1. associate-*l*99.9%

        \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
    3. Simplified99.9%

      \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right) - z} \]
    4. Taylor expanded in x around 0 99.8%

      \[\leadsto \color{blue}{3 \cdot \left(x \cdot y\right)} - z \]
    5. Taylor expanded in x around inf 75.0%

      \[\leadsto \color{blue}{3 \cdot \left(x \cdot y\right)} \]
    6. Step-by-step derivation
      1. *-commutative75.0%

        \[\leadsto 3 \cdot \color{blue}{\left(y \cdot x\right)} \]
      2. associate-*r*75.1%

        \[\leadsto \color{blue}{\left(3 \cdot y\right) \cdot x} \]
    7. Simplified75.1%

      \[\leadsto \color{blue}{\left(3 \cdot y\right) \cdot x} \]

    if -6.80000000000000012e59 < x < 1.55e12

    1. Initial program 99.9%

      \[\left(x \cdot 3\right) \cdot y - z \]
    2. Step-by-step derivation
      1. associate-*l*99.3%

        \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
    3. Simplified99.3%

      \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right) - z} \]
    4. Taylor expanded in x around 0 72.1%

      \[\leadsto \color{blue}{-1 \cdot z} \]
    5. Step-by-step derivation
      1. neg-mul-172.1%

        \[\leadsto \color{blue}{-z} \]
    6. Simplified72.1%

      \[\leadsto \color{blue}{-z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification73.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -6.8 \cdot 10^{+59} \lor \neg \left(x \leq 1550000000000\right):\\ \;\;\;\;x \cdot \left(y \cdot 3\right)\\ \mathbf{else}:\\ \;\;\;\;-z\\ \end{array} \]

Alternative 4: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(y \cdot x\right) \cdot 3 - z \end{array} \]
(FPCore (x y z) :precision binary64 (- (* (* y x) 3.0) z))
double code(double x, double y, double z) {
	return ((y * x) * 3.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 = ((y * x) * 3.0d0) - z
end function
public static double code(double x, double y, double z) {
	return ((y * x) * 3.0) - z;
}
def code(x, y, z):
	return ((y * x) * 3.0) - z
function code(x, y, z)
	return Float64(Float64(Float64(y * x) * 3.0) - z)
end
function tmp = code(x, y, z)
	tmp = ((y * x) * 3.0) - z;
end
code[x_, y_, z_] := N[(N[(N[(y * x), $MachinePrecision] * 3.0), $MachinePrecision] - z), $MachinePrecision]
\begin{array}{l}

\\
\left(y \cdot x\right) \cdot 3 - z
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(x \cdot 3\right) \cdot y - z \]
  2. Step-by-step derivation
    1. associate-*l*99.5%

      \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
  3. Simplified99.5%

    \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right) - z} \]
  4. Taylor expanded in x around 0 99.9%

    \[\leadsto \color{blue}{3 \cdot \left(x \cdot y\right)} - z \]
  5. Final simplification99.9%

    \[\leadsto \left(y \cdot x\right) \cdot 3 - z \]

Alternative 5: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot \left(y \cdot 3\right) - z \end{array} \]
(FPCore (x y z) :precision binary64 (- (* x (* y 3.0)) z))
double code(double x, double y, double z) {
	return (x * (y * 3.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 = (x * (y * 3.0d0)) - z
end function
public static double code(double x, double y, double z) {
	return (x * (y * 3.0)) - z;
}
def code(x, y, z):
	return (x * (y * 3.0)) - z
function code(x, y, z)
	return Float64(Float64(x * Float64(y * 3.0)) - z)
end
function tmp = code(x, y, z)
	tmp = (x * (y * 3.0)) - z;
end
code[x_, y_, z_] := N[(N[(x * N[(y * 3.0), $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \left(y \cdot 3\right) - z
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(x \cdot 3\right) \cdot y - z \]
  2. Step-by-step derivation
    1. associate-*l*99.5%

      \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
  3. Simplified99.5%

    \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right) - z} \]
  4. Final simplification99.5%

    \[\leadsto x \cdot \left(y \cdot 3\right) - z \]

Alternative 6: 50.4% accurate, 3.5× 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 Float64(-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.9%

    \[\left(x \cdot 3\right) \cdot y - z \]
  2. Step-by-step derivation
    1. associate-*l*99.5%

      \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
  3. Simplified99.5%

    \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right) - z} \]
  4. Taylor expanded in x around 0 50.3%

    \[\leadsto \color{blue}{-1 \cdot z} \]
  5. Step-by-step derivation
    1. neg-mul-150.3%

      \[\leadsto \color{blue}{-z} \]
  6. Simplified50.3%

    \[\leadsto \color{blue}{-z} \]
  7. Final simplification50.3%

    \[\leadsto -z \]

Alternative 7: 2.3% accurate, 7.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.9%

    \[\left(x \cdot 3\right) \cdot y - z \]
  2. Step-by-step derivation
    1. associate-*l*99.5%

      \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
  3. Simplified99.5%

    \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right) - z} \]
  4. Taylor expanded in x around 0 99.9%

    \[\leadsto \color{blue}{3 \cdot \left(x \cdot y\right)} - z \]
  5. Step-by-step derivation
    1. *-commutative99.9%

      \[\leadsto \color{blue}{\left(x \cdot y\right) \cdot 3} - z \]
    2. fma-neg99.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot y, 3, -z\right)} \]
    3. add-sqr-sqrt54.9%

      \[\leadsto \mathsf{fma}\left(x \cdot y, 3, \color{blue}{\sqrt{-z} \cdot \sqrt{-z}}\right) \]
    4. sqrt-unprod57.7%

      \[\leadsto \mathsf{fma}\left(x \cdot y, 3, \color{blue}{\sqrt{\left(-z\right) \cdot \left(-z\right)}}\right) \]
    5. sqr-neg57.7%

      \[\leadsto \mathsf{fma}\left(x \cdot y, 3, \sqrt{\color{blue}{z \cdot z}}\right) \]
    6. sqrt-unprod18.8%

      \[\leadsto \mathsf{fma}\left(x \cdot y, 3, \color{blue}{\sqrt{z} \cdot \sqrt{z}}\right) \]
    7. add-sqr-sqrt49.1%

      \[\leadsto \mathsf{fma}\left(x \cdot y, 3, \color{blue}{z}\right) \]
  6. Applied egg-rr49.1%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot y, 3, z\right)} \]
  7. Taylor expanded in x around 0 2.1%

    \[\leadsto \color{blue}{z} \]
  8. Final simplification2.1%

    \[\leadsto z \]

Developer target: 99.8% accurate, 1.0× speedup?

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

\\
x \cdot \left(3 \cdot y\right) - z
\end{array}

Reproduce

?
herbie shell --seed 2023305 
(FPCore (x y z)
  :name "Diagrams.Solve.Polynomial:cubForm  from diagrams-solve-0.1, B"
  :precision binary64

  :herbie-target
  (- (* x (* 3.0 y)) z)

  (- (* (* x 3.0) y) z))