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

?

Percentage Accurate: 95.0% → 98.5%
Time: 18.9s
Precision: binary64
Cost: 13764

?

\[ \begin{array}{c}[y, z, t] = \mathsf{sort}([y, z, t])\\ \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 2.05 \cdot 10^{+58}:\\ \;\;\;\;\mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, 2, \mathsf{fma}\left(t, \left(z \cdot y\right) \cdot -9, b \cdot \left(a \cdot 27\right)\right)\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 2.05e+58)
   (fma a (* 27.0 b) (- (* x 2.0) (* 9.0 (* y (* z t)))))
   (fma x 2.0 (fma t (* (* z y) -9.0) (* 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 <= 2.05e+58) {
		tmp = fma(a, (27.0 * b), ((x * 2.0) - (9.0 * (y * (z * t)))));
	} else {
		tmp = fma(x, 2.0, fma(t, ((z * y) * -9.0), (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 <= 2.05e+58)
		tmp = fma(a, Float64(27.0 * b), Float64(Float64(x * 2.0) - Float64(9.0 * Float64(y * Float64(z * t)))));
	else
		tmp = fma(x, 2.0, fma(t, Float64(Float64(z * y) * -9.0), 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, 2.05e+58], 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[(x * 2.0 + N[(t * N[(N[(z * y), $MachinePrecision] * -9.0), $MachinePrecision] + N[(b * N[(a * 27.0), $MachinePrecision]), $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 2.05 \cdot 10^{+58}:\\
\;\;\;\;\mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\right)\\

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


\end{array}

Local Percentage Accuracy?

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 13 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Target

Original95.0%
Target94.5%
Herbie98.5%
\[\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 < 2.05e58

    1. Initial program 96.0%

      \[\left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + \left(a \cdot 27\right) \cdot b \]
    2. Simplified97.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]96.0

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

      +-commutative [=>]96.0

      \[ \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* [=>]96.0

      \[ \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 [=>]97.0

      \[ \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* [=>]96.9

      \[ \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 [=>]96.9

      \[ \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* [=>]97.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 2.05e58 < z

    1. Initial program 87.8%

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

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

      [Start]87.8

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

      associate-+l- [=>]87.8

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

      fma-neg [=>]87.8

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

      neg-sub0 [=>]87.8

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

      associate-+l- [<=]87.8

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

      neg-sub0 [<=]87.8

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

      *-commutative [=>]87.8

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

      distribute-rgt-neg-in [=>]87.8

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

      fma-def [=>]87.8

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

      *-commutative [=>]87.8

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

      associate-*r* [=>]87.8

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

      distribute-rgt-neg-in [=>]87.8

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

      *-commutative [=>]87.8

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

      metadata-eval [=>]87.8

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

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

Alternatives

Alternative 1
Accuracy98.4%
Cost7492
\[\begin{array}{l} \mathbf{if}\;z \leq 1.3 \cdot 10^{+51}:\\ \;\;\;\;\mathsf{fma}\left(a, 27 \cdot b, x \cdot 2 - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(a \cdot 27\right) + \left(x \cdot 2 - t \cdot \left(z \cdot \left(9 \cdot y\right)\right)\right)\\ \end{array} \]
Alternative 2
Accuracy45.8%
Cost1900
\[\begin{array}{l} t_1 := t \cdot \left(\left(z \cdot y\right) \cdot -9\right)\\ t_2 := 27 \cdot \left(a \cdot b\right)\\ \mathbf{if}\;b \leq -7 \cdot 10^{-63}:\\ \;\;\;\;a \cdot \left(27 \cdot b\right)\\ \mathbf{elif}\;b \leq -3.5 \cdot 10^{-199}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 9 \cdot 10^{-46}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 4.2 \cdot 10^{-14}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 1.8 \cdot 10^{+42}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 1.8 \cdot 10^{+87}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 1.25 \cdot 10^{+107}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 5.8 \cdot 10^{+115}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq 4.05 \cdot 10^{+130}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 1.6 \cdot 10^{+168}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq 4.5 \cdot 10^{+196}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(a \cdot 27\right)\\ \end{array} \]
Alternative 3
Accuracy45.6%
Cost1900
\[\begin{array}{l} t_1 := t \cdot \left(y \cdot \left(z \cdot -9\right)\right)\\ t_2 := t \cdot \left(\left(z \cdot y\right) \cdot -9\right)\\ t_3 := 27 \cdot \left(a \cdot b\right)\\ \mathbf{if}\;b \leq -1.4 \cdot 10^{-65}:\\ \;\;\;\;a \cdot \left(27 \cdot b\right)\\ \mathbf{elif}\;b \leq -4.2 \cdot 10^{-199}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 3.9 \cdot 10^{-46}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 4.2 \cdot 10^{-13}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 3.8 \cdot 10^{+46}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 1.8 \cdot 10^{+87}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 1.4 \cdot 10^{+107}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq 1.4 \cdot 10^{+115}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;b \leq 1.12 \cdot 10^{+134}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 1.25 \cdot 10^{+169}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;b \leq 4.5 \cdot 10^{+196}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(a \cdot 27\right)\\ \end{array} \]
Alternative 4
Accuracy45.8%
Cost1900
\[\begin{array}{l} t_1 := t \cdot \left(y \cdot \left(z \cdot -9\right)\right)\\ t_2 := 27 \cdot \left(a \cdot b\right)\\ \mathbf{if}\;b \leq -2.8 \cdot 10^{-66}:\\ \;\;\;\;a \cdot \left(27 \cdot b\right)\\ \mathbf{elif}\;b \leq -4.7 \cdot 10^{-200}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 3.6 \cdot 10^{-45}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 3.4 \cdot 10^{-12}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 1.15 \cdot 10^{+48}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 1.75 \cdot 10^{+87}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 1.1 \cdot 10^{+107}:\\ \;\;\;\;\left(z \cdot t\right) \cdot \left(y \cdot -9\right)\\ \mathbf{elif}\;b \leq 5.8 \cdot 10^{+115}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq 3.9 \cdot 10^{+133}:\\ \;\;\;\;x + x\\ \mathbf{elif}\;b \leq 1.25 \cdot 10^{+169}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq 4.5 \cdot 10^{+196}:\\ \;\;\;\;t \cdot \left(\left(z \cdot y\right) \cdot -9\right)\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(a \cdot 27\right)\\ \end{array} \]
Alternative 5
Accuracy76.7%
Cost1756
\[\begin{array}{l} t_1 := x \cdot 2 - \left(a \cdot b\right) \cdot -27\\ t_2 := 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\\ t_3 := 27 \cdot \left(a \cdot b\right) - t_2\\ \mathbf{if}\;z \leq -3 \cdot 10^{-31}:\\ \;\;\;\;x \cdot 2 - t_2\\ \mathbf{elif}\;z \leq -1.4 \cdot 10^{-204}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq -5.2 \cdot 10^{-253}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;z \leq 7.2 \cdot 10^{-152}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 5.4 \cdot 10^{-129}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;z \leq 1.15 \cdot 10^{-73}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 3.6 \cdot 10^{+122}:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;t \cdot \left(\left(z \cdot y\right) \cdot -9\right)\\ \end{array} \]
Alternative 6
Accuracy97.5%
Cost1476
\[\begin{array}{l} t_1 := z \cdot \left(9 \cdot y\right)\\ \mathbf{if}\;t_1 \leq 10^{+290}:\\ \;\;\;\;b \cdot \left(a \cdot 27\right) + \left(x \cdot 2 - t \cdot t_1\right)\\ \mathbf{else}:\\ \;\;\;\;27 \cdot \left(a \cdot b\right) - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\\ \end{array} \]
Alternative 7
Accuracy73.8%
Cost1232
\[\begin{array}{l} t_1 := x \cdot 2 - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\\ t_2 := x \cdot 2 - \left(a \cdot b\right) \cdot -27\\ \mathbf{if}\;b \leq -6.2 \cdot 10^{-164}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq 7.2 \cdot 10^{+44}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 1.25 \cdot 10^{+169}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq 4.5 \cdot 10^{+196}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;x \cdot 2 - b \cdot \left(a \cdot -27\right)\\ \end{array} \]
Alternative 8
Accuracy96.8%
Cost1220
\[\begin{array}{l} \mathbf{if}\;z \leq -7.5 \cdot 10^{+30}:\\ \;\;\;\;27 \cdot \left(a \cdot b\right) - 9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(a \cdot 27\right) + \left(x \cdot 2 - t \cdot \left(9 \cdot \left(z \cdot y\right)\right)\right)\\ \end{array} \]
Alternative 9
Accuracy98.0%
Cost1220
\[\begin{array}{l} \mathbf{if}\;z \leq 200:\\ \;\;\;\;x \cdot 2 + \left(a \cdot \left(27 \cdot b\right) - \left(9 \cdot y\right) \cdot \left(z \cdot t\right)\right)\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(a \cdot 27\right) + \left(x \cdot 2 - t \cdot \left(z \cdot \left(9 \cdot y\right)\right)\right)\\ \end{array} \]
Alternative 10
Accuracy75.0%
Cost840
\[\begin{array}{l} \mathbf{if}\;z \leq -1 \cdot 10^{-21}:\\ \;\;\;\;-9 \cdot \left(y \cdot \left(z \cdot t\right)\right)\\ \mathbf{elif}\;z \leq 0.1:\\ \;\;\;\;x \cdot 2 - \left(a \cdot b\right) \cdot -27\\ \mathbf{else}:\\ \;\;\;\;t \cdot \left(\left(z \cdot y\right) \cdot -9\right)\\ \end{array} \]
Alternative 11
Accuracy45.4%
Cost585
\[\begin{array}{l} \mathbf{if}\;a \leq -7.5 \cdot 10^{-10} \lor \neg \left(a \leq 1.35 \cdot 10^{-137}\right):\\ \;\;\;\;27 \cdot \left(a \cdot b\right)\\ \mathbf{else}:\\ \;\;\;\;x + x\\ \end{array} \]
Alternative 12
Accuracy30.7%
Cost192
\[x + x \]

Reproduce?

herbie shell --seed 2023160 
(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)))