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

?

Percentage Accurate: 98.1% → 99.0%
Time: 7.1s
Precision: binary64
Cost: 7492

?

\[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
\[\begin{array}{l} t_1 := a \cdot b + \left(x \cdot y + z \cdot t\right)\\ \mathbf{if}\;t_1 \leq \infty:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t, z, x \cdot y\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) (+ (* x y) (* z t)))))
   (if (<= t_1 INFINITY) t_1 (fma t z (* x y)))))
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) + ((x * y) + (z * t));
	double tmp;
	if (t_1 <= ((double) INFINITY)) {
		tmp = t_1;
	} else {
		tmp = fma(t, z, (x * y));
	}
	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(x * y) + Float64(z * t)))
	tmp = 0.0
	if (t_1 <= Inf)
		tmp = t_1;
	else
		tmp = fma(t, z, Float64(x * y));
	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[(x * y), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, Infinity], t$95$1, N[(t * z + N[(x * y), $MachinePrecision]), $MachinePrecision]]]
\left(x \cdot y + z \cdot t\right) + a \cdot b
\begin{array}{l}
t_1 := a \cdot b + \left(x \cdot y + z \cdot t\right)\\
\mathbf{if}\;t_1 \leq \infty:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t, z, x \cdot y\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 10 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

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 a around 0 33.3%

      \[\leadsto \color{blue}{y \cdot x + t \cdot z} \]
    3. Simplified66.7%

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

      [Start]33.3%

      \[ y \cdot x + t \cdot z \]

      +-commutative [=>]33.3%

      \[ \color{blue}{t \cdot z + y \cdot x} \]

      *-commutative [=>]33.3%

      \[ t \cdot z + \color{blue}{x \cdot y} \]

      fma-udef [<=]66.7%

      \[ \color{blue}{\mathsf{fma}\left(t, z, x \cdot y\right)} \]

      *-commutative [<=]66.7%

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

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

Alternatives

Alternative 1
Accuracy99.0%
Cost7492
\[\begin{array}{l} t_1 := a \cdot b + \left(x \cdot y + z \cdot t\right)\\ \mathbf{if}\;t_1 \leq \infty:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t, z, x \cdot y\right)\\ \end{array} \]
Alternative 2
Accuracy99.1%
Cost13248
\[\mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, a \cdot b\right)\right) \]
Alternative 3
Accuracy98.3%
Cost6976
\[a \cdot b + \mathsf{fma}\left(x, y, z \cdot t\right) \]
Alternative 4
Accuracy53.7%
Cost2272
\[\begin{array}{l} \mathbf{if}\;a \cdot b \leq -2.45 \cdot 10^{+131}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;a \cdot b \leq -3.8 \cdot 10^{-103}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;a \cdot b \leq -1.02 \cdot 10^{-141}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;a \cdot b \leq 2.5 \cdot 10^{-246}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;a \cdot b \leq 1.7 \cdot 10^{-134}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;a \cdot b \leq 2.7 \cdot 10^{-77}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;a \cdot b \leq 4.5 \cdot 10^{-27}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;a \cdot b \leq 6 \cdot 10^{+54}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;a \cdot b\\ \end{array} \]
Alternative 5
Accuracy84.4%
Cost1489
\[\begin{array}{l} \mathbf{if}\;a \cdot b \leq -3.8 \cdot 10^{+77}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{elif}\;a \cdot b \leq -1.02 \cdot 10^{-52} \lor \neg \left(a \cdot b \leq -1.05 \cdot 10^{-87}\right) \land a \cdot b \leq 1.15 \cdot 10^{-26}:\\ \;\;\;\;x \cdot y + z \cdot t\\ \mathbf{else}:\\ \;\;\;\;a \cdot b + x \cdot y\\ \end{array} \]
Alternative 6
Accuracy53.9%
Cost1233
\[\begin{array}{l} \mathbf{if}\;a \cdot b \leq -8.5 \cdot 10^{+75}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;a \cdot b \leq -1.02 \cdot 10^{-52} \lor \neg \left(a \cdot b \leq -4.8 \cdot 10^{-77}\right) \land a \cdot b \leq 0.019:\\ \;\;\;\;z \cdot t\\ \mathbf{else}:\\ \;\;\;\;a \cdot b\\ \end{array} \]
Alternative 7
Accuracy77.1%
Cost713
\[\begin{array}{l} \mathbf{if}\;t \leq -2.4 \cdot 10^{-60} \lor \neg \left(t \leq 5 \cdot 10^{+190}\right):\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{else}:\\ \;\;\;\;a \cdot b + x \cdot y\\ \end{array} \]
Alternative 8
Accuracy70.4%
Cost712
\[\begin{array}{l} \mathbf{if}\;y \leq -3.4 \cdot 10^{-13}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;y \leq 1.46 \cdot 10^{+137}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
Alternative 9
Accuracy98.1%
Cost704
\[a \cdot b + \left(x \cdot y + z \cdot t\right) \]
Alternative 10
Accuracy35.8%
Cost192
\[a \cdot b \]

Reproduce?

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