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

?

Percentage Accurate: 95.4% → 98.7%
Time: 12.5s
Precision: binary64
Cost: 7492

?

\[ \begin{array}{c}[y, z, t] = \mathsf{sort}([y, z, t])\\ [a, b] = \mathsf{sort}([a, b])\\ \end{array} \]
\[\left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + \left(a \cdot 27\right) \cdot b \]
\[\begin{array}{l} \mathbf{if}\;z \leq 6 \cdot 10^{+32}:\\ \;\;\;\;\mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot 2 - t \cdot \left(9 \cdot \left(z \cdot y\right)\right)\right) + b \cdot \left(a \cdot 27\right)\\ \end{array} \]
(FPCore (x y z t a b)
 :precision binary64
 (+ (- (* x 2.0) (* (* (* y 9.0) z) t)) (* (* a 27.0) b)))
(FPCore (x y z t a b)
 :precision binary64
 (if (<= z 6e+32)
   (fma a (* 27.0 b) (- (* x 2.0) (* 9.0 (* y (* z t)))))
   (+ (- (* x 2.0) (* t (* 9.0 (* z y)))) (* b (* a 27.0)))))
double code(double x, double y, double z, double t, double a, double b) {
	return ((x * 2.0) - (((y * 9.0) * z) * t)) + ((a * 27.0) * b);
}
double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if (z <= 6e+32) {
		tmp = fma(a, (27.0 * b), ((x * 2.0) - (9.0 * (y * (z * t)))));
	} else {
		tmp = ((x * 2.0) - (t * (9.0 * (z * y)))) + (b * (a * 27.0));
	}
	return tmp;
}
function code(x, y, z, t, a, b)
	return Float64(Float64(Float64(x * 2.0) - Float64(Float64(Float64(y * 9.0) * z) * t)) + Float64(Float64(a * 27.0) * b))
end
function code(x, y, z, t, a, b)
	tmp = 0.0
	if (z <= 6e+32)
		tmp = fma(a, Float64(27.0 * b), Float64(Float64(x * 2.0) - Float64(9.0 * Float64(y * Float64(z * t)))));
	else
		tmp = Float64(Float64(Float64(x * 2.0) - Float64(t * Float64(9.0 * Float64(z * y)))) + Float64(b * Float64(a * 27.0)));
	end
	return tmp
end
code[x_, y_, z_, t_, a_, b_] := N[(N[(N[(x * 2.0), $MachinePrecision] - N[(N[(N[(y * 9.0), $MachinePrecision] * z), $MachinePrecision] * t), $MachinePrecision]), $MachinePrecision] + N[(N[(a * 27.0), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[z, 6e+32], N[(a * N[(27.0 * b), $MachinePrecision] + N[(N[(x * 2.0), $MachinePrecision] - N[(9.0 * N[(y * N[(z * t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(x * 2.0), $MachinePrecision] - N[(t * N[(9.0 * N[(z * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(b * N[(a * 27.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + \left(a \cdot 27\right) \cdot b
\begin{array}{l}
\mathbf{if}\;z \leq 6 \cdot 10^{+32}:\\
\;\;\;\;\mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\left(x \cdot 2 - t \cdot \left(9 \cdot \left(z \cdot y\right)\right)\right) + b \cdot \left(a \cdot 27\right)\\


\end{array}

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.

Herbie found 11 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Target

Original95.4%
Target95.1%
Herbie98.7%
\[\begin{array}{l} \mathbf{if}\;y < 7.590524218811189 \cdot 10^{-161}:\\ \;\;\;\;\left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + a \cdot \left(27 \cdot b\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot 2 - 9 \cdot \left(y \cdot \left(t \cdot z\right)\right)\right) + \left(a \cdot 27\right) \cdot b\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if z < 6e32

    1. Initial program 98.3%

      \[\left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + \left(a \cdot 27\right) \cdot b \]
    2. Simplified98.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\right)} \]
      Step-by-step derivation

      [Start]98.3%

      \[ \left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + \left(a \cdot 27\right) \cdot b \]

      +-commutative [=>]98.3%

      \[ \color{blue}{\left(a \cdot 27\right) \cdot b + \left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right)} \]

      associate-*l* [=>]98.3%

      \[ \color{blue}{a \cdot \left(27 \cdot b\right)} + \left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) \]

      fma-def [=>]99.4%

      \[ \color{blue}{\mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right)} \]

      associate-*l* [=>]98.0%

      \[ \mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - \color{blue}{\left(y \cdot 9\right) \cdot \left(z \cdot t\right)}\right) \]

      *-commutative [=>]98.0%

      \[ \mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - \color{blue}{\left(9 \cdot y\right)} \cdot \left(z \cdot t\right)\right) \]

      associate-*l* [=>]98.0%

      \[ \mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - \color{blue}{9 \cdot \left(y \cdot \left(z \cdot t\right)\right)}\right) \]

    if 6e32 < z

    1. Initial program 89.9%

      \[\left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + \left(a \cdot 27\right) \cdot b \]
    2. Taylor expanded in y around 0 89.9%

      \[\leadsto \left(x \cdot 2 - \color{blue}{\left(9 \cdot \left(y \cdot z\right)\right)} \cdot t\right) + \left(a \cdot 27\right) \cdot b \]
  3. Recombined 2 regimes into one program.
  4. Final simplification96.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 6 \cdot 10^{+32}:\\ \;\;\;\;\mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot 2 - t \cdot \left(9 \cdot \left(z \cdot y\right)\right)\right) + b \cdot \left(a \cdot 27\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy98.7%
Cost7492
\[\begin{array}{l} \mathbf{if}\;z \leq 6 \cdot 10^{+32}:\\ \;\;\;\;\mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot 2 - t \cdot \left(9 \cdot \left(z \cdot y\right)\right)\right) + b \cdot \left(a \cdot 27\right)\\ \end{array} \]
Alternative 2
Accuracy97.7%
Cost1476
\[\begin{array}{l} \mathbf{if}\;z \cdot \left(9 \cdot y\right) \leq 2 \cdot 10^{+306}:\\ \;\;\;\;\left(x \cdot 2 - t \cdot \left(9 \cdot \left(z \cdot y\right)\right)\right) + b \cdot \left(a \cdot 27\right)\\ \mathbf{else}:\\ \;\;\;\;27 \cdot \left(a \cdot b\right) - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\\ \end{array} \]
Alternative 3
Accuracy77.9%
Cost1361
\[\begin{array}{l} t_1 := 27 \cdot \left(a \cdot b\right) - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\\ \mathbf{if}\;b \leq -1.04 \cdot 10^{-38}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 5.6 \cdot 10^{+57}:\\ \;\;\;\;x \cdot 2 - 9 \cdot \left(z \cdot \left(y \cdot t\right)\right)\\ \mathbf{elif}\;b \leq 3.5 \cdot 10^{+153} \lor \neg \left(b \leq 4.4 \cdot 10^{+216}\right):\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;x \cdot 2 - b \cdot \left(a \cdot -27\right)\\ \end{array} \]
Alternative 4
Accuracy98.3%
Cost1220
\[\begin{array}{l} \mathbf{if}\;z \leq 5 \cdot 10^{-51}:\\ \;\;\;\;x \cdot 2 + \left(a \cdot \left(27 \cdot b\right) - y \cdot \left(9 \cdot \left(z \cdot t\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot 2 - t \cdot \left(9 \cdot \left(z \cdot y\right)\right)\right) + b \cdot \left(a \cdot 27\right)\\ \end{array} \]
Alternative 5
Accuracy48.9%
Cost976
\[\begin{array}{l} t_1 := \left(y \cdot \left(z \cdot t\right)\right) \cdot -9\\ t_2 := 27 \cdot \left(a \cdot b\right)\\ \mathbf{if}\;b \leq -1.12 \cdot 10^{-36}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq -5.8 \cdot 10^{-126}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 2 \cdot 10^{-175}:\\ \;\;\;\;x \cdot 2\\ \mathbf{elif}\;b \leq 4.5 \cdot 10^{+125}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 6
Accuracy48.4%
Cost976
\[\begin{array}{l} t_1 := 27 \cdot \left(a \cdot b\right)\\ \mathbf{if}\;b \leq -2.8 \cdot 10^{-37}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq -1.95 \cdot 10^{-127}:\\ \;\;\;\;\left(y \cdot \left(z \cdot t\right)\right) \cdot -9\\ \mathbf{elif}\;b \leq 2.4 \cdot 10^{-174}:\\ \;\;\;\;x \cdot 2\\ \mathbf{elif}\;b \leq 5.6 \cdot 10^{+125}:\\ \;\;\;\;\left(z \cdot \left(y \cdot t\right)\right) \cdot -9\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 7
Accuracy78.0%
Cost968
\[\begin{array}{l} \mathbf{if}\;b \leq -1.12 \cdot 10^{-36}:\\ \;\;\;\;x \cdot 2 - a \cdot \left(b \cdot -27\right)\\ \mathbf{elif}\;b \leq 2.25 \cdot 10^{+124}:\\ \;\;\;\;x \cdot 2 - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot 2 - \left(a \cdot b\right) \cdot -27\\ \end{array} \]
Alternative 8
Accuracy76.5%
Cost968
\[\begin{array}{l} \mathbf{if}\;b \leq -1.95 \cdot 10^{-37}:\\ \;\;\;\;x \cdot 2 - a \cdot \left(b \cdot -27\right)\\ \mathbf{elif}\;b \leq 2.08 \cdot 10^{+124}:\\ \;\;\;\;x \cdot 2 - 9 \cdot \left(z \cdot \left(y \cdot t\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot 2 - \left(a \cdot b\right) \cdot -27\\ \end{array} \]
Alternative 9
Accuracy76.0%
Cost840
\[\begin{array}{l} \mathbf{if}\;z \leq -1.9 \cdot 10^{+39}:\\ \;\;\;\;y \cdot \left(\left(z \cdot t\right) \cdot -9\right)\\ \mathbf{elif}\;z \leq 1.2 \cdot 10^{-6}:\\ \;\;\;\;x \cdot 2 - a \cdot \left(b \cdot -27\right)\\ \mathbf{else}:\\ \;\;\;\;\left(z \cdot \left(y \cdot t\right)\right) \cdot -9\\ \end{array} \]
Alternative 10
Accuracy47.4%
Cost585
\[\begin{array}{l} \mathbf{if}\;b \leq -9.5 \cdot 10^{-48} \lor \neg \left(b \leq 3.6 \cdot 10^{+65}\right):\\ \;\;\;\;27 \cdot \left(a \cdot b\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot 2\\ \end{array} \]
Alternative 11
Accuracy30.9%
Cost192
\[x \cdot 2 \]

Reproduce?

herbie shell --seed 2023171 
(FPCore (x y z t a b)
  :name "Diagrams.Solve.Polynomial:cubForm  from diagrams-solve-0.1, A"
  :precision binary64

  :herbie-target
  (if (< y 7.590524218811189e-161) (+ (- (* x 2.0) (* (* (* y 9.0) z) t)) (* a (* 27.0 b))) (+ (- (* x 2.0) (* 9.0 (* y (* t z)))) (* (* a 27.0) b)))

  (+ (- (* x 2.0) (* (* (* y 9.0) z) t)) (* (* a 27.0) b)))