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

?

Percentage Accurate: 90.9% → 97.2%
Time: 9.0s
Precision: binary64
Cost: 8649

?

\[\frac{x \cdot y - \left(z \cdot 9\right) \cdot t}{a \cdot 2} \]
\[\begin{array}{l} [z, t] = \mathsf{sort}([z, t])\\ \\ \begin{array}{l} t_1 := x \cdot y - \left(z \cdot 9\right) \cdot t\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 5 \cdot 10^{+273}\right):\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{a}, \frac{y}{2}, 0.5 \cdot \frac{t}{\frac{a}{z \cdot -9}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, z \cdot \left(t \cdot -9\right)\right)}{a \cdot 2}\\ \end{array} \end{array} \]
(FPCore (x y z t a)
 :precision binary64
 (/ (- (* x y) (* (* z 9.0) t)) (* a 2.0)))
NOTE: z and t should be sorted in increasing order before calling this function.
(FPCore (x y z t a)
 :precision binary64
 (let* ((t_1 (- (* x y) (* (* z 9.0) t))))
   (if (or (<= t_1 (- INFINITY)) (not (<= t_1 5e+273)))
     (fma (/ x a) (/ y 2.0) (* 0.5 (/ t (/ a (* z -9.0)))))
     (/ (fma x y (* z (* t -9.0))) (* a 2.0)))))
double code(double x, double y, double z, double t, double a) {
	return ((x * y) - ((z * 9.0) * t)) / (a * 2.0);
}
assert(z < t);
double code(double x, double y, double z, double t, double a) {
	double t_1 = (x * y) - ((z * 9.0) * t);
	double tmp;
	if ((t_1 <= -((double) INFINITY)) || !(t_1 <= 5e+273)) {
		tmp = fma((x / a), (y / 2.0), (0.5 * (t / (a / (z * -9.0)))));
	} else {
		tmp = fma(x, y, (z * (t * -9.0))) / (a * 2.0);
	}
	return tmp;
}
function code(x, y, z, t, a)
	return Float64(Float64(Float64(x * y) - Float64(Float64(z * 9.0) * t)) / Float64(a * 2.0))
end
z, t = sort([z, t])
function code(x, y, z, t, a)
	t_1 = Float64(Float64(x * y) - Float64(Float64(z * 9.0) * t))
	tmp = 0.0
	if ((t_1 <= Float64(-Inf)) || !(t_1 <= 5e+273))
		tmp = fma(Float64(x / a), Float64(y / 2.0), Float64(0.5 * Float64(t / Float64(a / Float64(z * -9.0)))));
	else
		tmp = Float64(fma(x, y, Float64(z * Float64(t * -9.0))) / Float64(a * 2.0));
	end
	return tmp
end
code[x_, y_, z_, t_, a_] := N[(N[(N[(x * y), $MachinePrecision] - N[(N[(z * 9.0), $MachinePrecision] * t), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
NOTE: z and t should be sorted in increasing order before calling this function.
code[x_, y_, z_, t_, a_] := Block[{t$95$1 = N[(N[(x * y), $MachinePrecision] - N[(N[(z * 9.0), $MachinePrecision] * t), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$1, (-Infinity)], N[Not[LessEqual[t$95$1, 5e+273]], $MachinePrecision]], N[(N[(x / a), $MachinePrecision] * N[(y / 2.0), $MachinePrecision] + N[(0.5 * N[(t / N[(a / N[(z * -9.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * y + N[(z * N[(t * -9.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]]]
\frac{x \cdot y - \left(z \cdot 9\right) \cdot t}{a \cdot 2}
\begin{array}{l}
[z, t] = \mathsf{sort}([z, t])\\
\\
\begin{array}{l}
t_1 := x \cdot y - \left(z \cdot 9\right) \cdot t\\
\mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 5 \cdot 10^{+273}\right):\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{a}, \frac{y}{2}, 0.5 \cdot \frac{t}{\frac{a}{z \cdot -9}}\right)\\

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


\end{array}
\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

Original90.9%
Target93.3%
Herbie97.2%
\[\begin{array}{l} \mathbf{if}\;a < -2.090464557976709 \cdot 10^{+86}:\\ \;\;\;\;0.5 \cdot \frac{y \cdot x}{a} - 4.5 \cdot \frac{t}{\frac{a}{z}}\\ \mathbf{elif}\;a < 2.144030707833976 \cdot 10^{+99}:\\ \;\;\;\;\frac{x \cdot y - z \cdot \left(9 \cdot t\right)}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{a} \cdot \left(x \cdot 0.5\right) - \frac{t}{a} \cdot \left(z \cdot 4.5\right)\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 x y) (*.f64 (*.f64 z 9) t)) < -inf.0 or 4.99999999999999961e273 < (-.f64 (*.f64 x y) (*.f64 (*.f64 z 9) t))

    1. Initial program 58.9%

      \[\frac{x \cdot y - \left(z \cdot 9\right) \cdot t}{a \cdot 2} \]
    2. Simplified60.5%

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

      [Start]58.9%

      \[ \frac{x \cdot y - \left(z \cdot 9\right) \cdot t}{a \cdot 2} \]

      sub-neg [=>]58.9%

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

      +-commutative [=>]58.9%

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

      neg-sub0 [=>]58.9%

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

      associate-+l- [=>]58.9%

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

      sub0-neg [=>]58.9%

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

      neg-mul-1 [=>]58.9%

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

      associate-/l* [=>]58.9%

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

      associate-/r/ [=>]58.9%

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

      *-commutative [=>]58.9%

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

      sub-neg [=>]58.9%

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

      +-commutative [=>]58.9%

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

      neg-sub0 [=>]58.9%

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

      associate-+l- [=>]58.9%

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

      sub0-neg [=>]58.9%

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

      distribute-lft-neg-out [=>]58.9%

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

      distribute-rgt-neg-in [=>]58.9%

      \[ \color{blue}{\left(x \cdot y - \left(z \cdot 9\right) \cdot t\right) \cdot \left(-\frac{-1}{a \cdot 2}\right)} \]
    3. Applied egg-rr74.8%

      \[\leadsto \color{blue}{\frac{x}{a} \cdot \frac{y}{2} + 0.5 \cdot \frac{t \cdot \left(z \cdot -9\right)}{a}} \]
      Step-by-step derivation

      [Start]60.5%

      \[ \mathsf{fma}\left(x, y, z \cdot \left(t \cdot -9\right)\right) \cdot \frac{0.5}{a} \]

      *-commutative [=>]60.5%

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

      fma-udef [=>]58.9%

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

      *-commutative [=>]58.9%

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

      metadata-eval [<=]58.9%

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

      distribute-lft-neg-in [<=]58.9%

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

      distribute-rgt-neg-in [<=]58.9%

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

      distribute-rgt-in [=>]55.7%

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

      clear-num [=>]55.7%

      \[ \left(x \cdot y\right) \cdot \color{blue}{\frac{1}{\frac{a}{0.5}}} + \left(-z \cdot \left(9 \cdot t\right)\right) \cdot \frac{0.5}{a} \]

      div-inv [=>]55.7%

      \[ \left(x \cdot y\right) \cdot \frac{1}{\color{blue}{a \cdot \frac{1}{0.5}}} + \left(-z \cdot \left(9 \cdot t\right)\right) \cdot \frac{0.5}{a} \]

      metadata-eval [=>]55.7%

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

      div-inv [<=]55.7%

      \[ \color{blue}{\frac{x \cdot y}{a \cdot 2}} + \left(-z \cdot \left(9 \cdot t\right)\right) \cdot \frac{0.5}{a} \]
    4. Simplified96.7%

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

      [Start]74.8%

      \[ \frac{x}{a} \cdot \frac{y}{2} + 0.5 \cdot \frac{t \cdot \left(z \cdot -9\right)}{a} \]

      fma-def [=>]76.4%

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

      associate-/l* [=>]96.7%

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

    if -inf.0 < (-.f64 (*.f64 x y) (*.f64 (*.f64 z 9) t)) < 4.99999999999999961e273

    1. Initial program 99.6%

      \[\frac{x \cdot y - \left(z \cdot 9\right) \cdot t}{a \cdot 2} \]
    2. Simplified99.6%

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

      [Start]99.6%

      \[ \frac{x \cdot y - \left(z \cdot 9\right) \cdot t}{a \cdot 2} \]

      div-sub [=>]98.5%

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

      +-rgt-identity [<=]98.5%

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

      div-sub [<=]99.6%

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

      +-rgt-identity [=>]99.6%

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

      fma-neg [=>]99.6%

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

      associate-*l* [=>]99.6%

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

      distribute-rgt-neg-in [=>]99.6%

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

      *-commutative [=>]99.6%

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

      distribute-rgt-neg-in [=>]99.6%

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

      metadata-eval [=>]99.6%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot y - \left(z \cdot 9\right) \cdot t \leq -\infty \lor \neg \left(x \cdot y - \left(z \cdot 9\right) \cdot t \leq 5 \cdot 10^{+273}\right):\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{a}, \frac{y}{2}, 0.5 \cdot \frac{t}{\frac{a}{z \cdot -9}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, z \cdot \left(t \cdot -9\right)\right)}{a \cdot 2}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy97.2%
Cost8649
\[\begin{array}{l} t_1 := x \cdot y - \left(z \cdot 9\right) \cdot t\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 5 \cdot 10^{+273}\right):\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{a}, \frac{y}{2}, 0.5 \cdot \frac{t}{\frac{a}{z \cdot -9}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, z \cdot \left(t \cdot -9\right)\right)}{a \cdot 2}\\ \end{array} \]
Alternative 2
Accuracy91.9%
Cost7364
\[\begin{array}{l} \mathbf{if}\;x \cdot y \leq 4 \cdot 10^{+177}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, z \cdot \left(t \cdot -9\right)\right)}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{y}{\frac{a}{x}}\\ \end{array} \]
Alternative 3
Accuracy71.0%
Cost2008
\[\begin{array}{l} t_1 := -4.5 \cdot \frac{t}{\frac{a}{z}}\\ t_2 := \left(x \cdot y\right) \cdot \frac{0.5}{a}\\ \mathbf{if}\;x \cdot y \leq -5 \cdot 10^{-62}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{-163}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{-120}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{-71}:\\ \;\;\;\;-4.5 \cdot \left(z \cdot \frac{t}{a}\right)\\ \mathbf{elif}\;x \cdot y \leq 10^{-24}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \cdot y \leq 5 \cdot 10^{+59}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{y}{\frac{a}{x}}\\ \end{array} \]
Alternative 4
Accuracy71.0%
Cost2008
\[\begin{array}{l} t_1 := -4.5 \cdot \frac{t}{\frac{a}{z}}\\ t_2 := \left(x \cdot y\right) \cdot \frac{0.5}{a}\\ \mathbf{if}\;x \cdot y \leq -5 \cdot 10^{-62}:\\ \;\;\;\;\frac{x \cdot y}{a \cdot 2}\\ \mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{-163}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{-120}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{-71}:\\ \;\;\;\;-4.5 \cdot \left(z \cdot \frac{t}{a}\right)\\ \mathbf{elif}\;x \cdot y \leq 10^{-24}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \cdot y \leq 5 \cdot 10^{+59}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{y}{\frac{a}{x}}\\ \end{array} \]
Alternative 5
Accuracy91.8%
Cost1732
\[\begin{array}{l} \mathbf{if}\;x \cdot y - \left(z \cdot 9\right) \cdot t \leq 5 \cdot 10^{+273}:\\ \;\;\;\;\frac{x \cdot y - z \cdot \left(9 \cdot t\right)}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{a} \cdot \frac{y}{2} + 0.5 \cdot \frac{t \cdot \left(z \cdot -9\right)}{a}\\ \end{array} \]
Alternative 6
Accuracy91.6%
Cost1092
\[\begin{array}{l} \mathbf{if}\;x \cdot y \leq 4 \cdot 10^{+177}:\\ \;\;\;\;\left(x \cdot y + t \cdot \left(z \cdot -9\right)\right) \cdot \frac{0.5}{a}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{y}{\frac{a}{x}}\\ \end{array} \]
Alternative 7
Accuracy91.7%
Cost1092
\[\begin{array}{l} \mathbf{if}\;x \cdot y \leq 4 \cdot 10^{+177}:\\ \;\;\;\;\frac{x \cdot y - z \cdot \left(9 \cdot t\right)}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{y}{\frac{a}{x}}\\ \end{array} \]
Alternative 8
Accuracy67.9%
Cost712
\[\begin{array}{l} \mathbf{if}\;z \leq -1.35 \cdot 10^{+67}:\\ \;\;\;\;-4.5 \cdot \left(z \cdot \frac{t}{a}\right)\\ \mathbf{elif}\;z \leq 2.1 \cdot 10^{-110}:\\ \;\;\;\;0.5 \cdot \frac{y}{\frac{a}{x}}\\ \mathbf{else}:\\ \;\;\;\;-4.5 \cdot \frac{t}{\frac{a}{z}}\\ \end{array} \]
Alternative 9
Accuracy68.3%
Cost712
\[\begin{array}{l} \mathbf{if}\;z \leq -4 \cdot 10^{+67}:\\ \;\;\;\;-4.5 \cdot \left(z \cdot \frac{t}{a}\right)\\ \mathbf{elif}\;z \leq 7 \cdot 10^{-110}:\\ \;\;\;\;x \cdot \left(y \cdot \frac{0.5}{a}\right)\\ \mathbf{else}:\\ \;\;\;\;-4.5 \cdot \frac{t}{\frac{a}{z}}\\ \end{array} \]
Alternative 10
Accuracy51.5%
Cost448
\[-4.5 \cdot \left(t \cdot \frac{z}{a}\right) \]
Alternative 11
Accuracy51.4%
Cost448
\[-4.5 \cdot \frac{t}{\frac{a}{z}} \]

Reproduce?

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

  :herbie-target
  (if (< a -2.090464557976709e+86) (- (* 0.5 (/ (* y x) a)) (* 4.5 (/ t (/ a z)))) (if (< a 2.144030707833976e+99) (/ (- (* x y) (* z (* 9.0 t))) (* a 2.0)) (- (* (/ y a) (* x 0.5)) (* (/ t a) (* z 4.5)))))

  (/ (- (* x y) (* (* z 9.0) t)) (* a 2.0)))