Linear.V3:$cdot from linear-1.19.1.3, B

?

Percentage Accurate: 97.7% → 98.9%
Time: 4.4s
Precision: binary64
Cost: 7492

?

\[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
\[\begin{array}{l} t_1 := a \cdot b + \left(z \cdot t + x \cdot y\right)\\ \mathbf{if}\;t_1 \leq \infty:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, a \cdot b\right)\\ \end{array} \]
(FPCore (x y z t a b) :precision binary64 (+ (+ (* x y) (* z t)) (* a b)))
(FPCore (x y z t a b)
 :precision binary64
 (let* ((t_1 (+ (* a b) (+ (* z t) (* x y)))))
   (if (<= t_1 INFINITY) t_1 (fma y x (* a b)))))
double code(double x, double y, double z, double t, double a, double b) {
	return ((x * y) + (z * t)) + (a * b);
}
double code(double x, double y, double z, double t, double a, double b) {
	double t_1 = (a * b) + ((z * t) + (x * y));
	double tmp;
	if (t_1 <= ((double) INFINITY)) {
		tmp = t_1;
	} else {
		tmp = fma(y, x, (a * b));
	}
	return tmp;
}
function code(x, y, z, t, a, b)
	return Float64(Float64(Float64(x * y) + Float64(z * t)) + Float64(a * b))
end
function code(x, y, z, t, a, b)
	t_1 = Float64(Float64(a * b) + Float64(Float64(z * t) + Float64(x * y)))
	tmp = 0.0
	if (t_1 <= Inf)
		tmp = t_1;
	else
		tmp = fma(y, x, Float64(a * b));
	end
	return tmp
end
code[x_, y_, z_, t_, a_, b_] := N[(N[(N[(x * y), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision] + N[(a * b), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_, t_, a_, b_] := Block[{t$95$1 = N[(N[(a * b), $MachinePrecision] + N[(N[(z * t), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, Infinity], t$95$1, N[(y * x + N[(a * b), $MachinePrecision]), $MachinePrecision]]]
\left(x \cdot y + z \cdot t\right) + a \cdot b
\begin{array}{l}
t_1 := a \cdot b + \left(z \cdot t + x \cdot y\right)\\
\mathbf{if}\;t_1 \leq \infty:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, a \cdot b\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.

Derivation?

  1. Split input into 2 regimes
  2. if (+.f64 (+.f64 (*.f64 x y) (*.f64 z t)) (*.f64 a b)) < +inf.0

    1. Initial program 100.0%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]

    if +inf.0 < (+.f64 (+.f64 (*.f64 x y) (*.f64 z t)) (*.f64 a b))

    1. Initial program 0.0%

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

      \[\leadsto \color{blue}{a \cdot b + y \cdot x} \]
    3. Simplified60.0%

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

      [Start]30.0

      \[ a \cdot b + y \cdot x \]

      +-commutative [=>]30.0

      \[ \color{blue}{y \cdot x + a \cdot b} \]

      fma-def [=>]60.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;a \cdot b + \left(z \cdot t + x \cdot y\right) \leq \infty:\\ \;\;\;\;a \cdot b + \left(z \cdot t + x \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, a \cdot b\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy98.8%
Cost13248
\[\mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, a \cdot b\right)\right) \]
Alternative 2
Accuracy98.5%
Cost1476
\[\begin{array}{l} t_1 := a \cdot b + \left(z \cdot t + x \cdot y\right)\\ \mathbf{if}\;t_1 \leq \infty:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;a \cdot b\\ \end{array} \]
Alternative 3
Accuracy53.7%
Cost1232
\[\begin{array}{l} \mathbf{if}\;a \cdot b \leq -2.5 \cdot 10^{+46}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;a \cdot b \leq -2.7 \cdot 10^{-149}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;a \cdot b \leq 2.2 \cdot 10^{-156}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;a \cdot b \leq 3.3 \cdot 10^{+85}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;a \cdot b\\ \end{array} \]
Alternative 4
Accuracy78.2%
Cost978
\[\begin{array}{l} \mathbf{if}\;y \leq -4.3 \cdot 10^{-67} \lor \neg \left(y \leq 2.4 \cdot 10^{+50} \lor \neg \left(y \leq 1.05 \cdot 10^{+184}\right) \land y \leq 5 \cdot 10^{+207}\right):\\ \;\;\;\;a \cdot b + x \cdot y\\ \mathbf{else}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \end{array} \]
Alternative 5
Accuracy85.2%
Cost969
\[\begin{array}{l} \mathbf{if}\;a \cdot b \leq -7.8 \cdot 10^{+47} \lor \neg \left(a \cdot b \leq 4.5 \cdot 10^{+83}\right):\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{else}:\\ \;\;\;\;z \cdot t + x \cdot y\\ \end{array} \]
Alternative 6
Accuracy67.9%
Cost713
\[\begin{array}{l} \mathbf{if}\;y \leq -9.8 \cdot 10^{-64} \lor \neg \left(y \leq 9 \cdot 10^{+207}\right):\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \end{array} \]
Alternative 7
Accuracy54.1%
Cost712
\[\begin{array}{l} \mathbf{if}\;a \cdot b \leq -8.5 \cdot 10^{-35}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;a \cdot b \leq 7.8 \cdot 10^{+89}:\\ \;\;\;\;z \cdot t\\ \mathbf{else}:\\ \;\;\;\;a \cdot b\\ \end{array} \]
Alternative 8
Accuracy34.5%
Cost192
\[a \cdot b \]

Error

Reproduce?

herbie shell --seed 2023160 
(FPCore (x y z t a b)
  :name "Linear.V3:$cdot from linear-1.19.1.3, B"
  :precision binary64
  (+ (+ (* x y) (* z t)) (* a b)))