\[\left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right) + j \cdot \left(c \cdot t - i \cdot y\right)
\]
↓
\[\begin{array}{l}
t_1 := b \cdot \left(a \cdot i - z \cdot c\right)\\
t_2 := t \cdot c - y \cdot i\\
\mathbf{if}\;j \cdot t_2 - \left(b \cdot \left(z \cdot c - a \cdot i\right) + x \cdot \left(t \cdot a - y \cdot z\right)\right) \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(j, t_2, t_1 + x \cdot \left(y \cdot z - t \cdot a\right)\right)\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\]
(FPCore (x y z t a b c i j)
:precision binary64
(+
(- (* x (- (* y z) (* t a))) (* b (- (* c z) (* i a))))
(* j (- (* c t) (* i y)))))
↓
(FPCore (x y z t a b c i j)
:precision binary64
(let* ((t_1 (* b (- (* a i) (* z c)))) (t_2 (- (* t c) (* y i))))
(if (<=
(- (* j t_2) (+ (* b (- (* z c) (* a i))) (* x (- (* t a) (* y z)))))
INFINITY)
(fma j t_2 (+ t_1 (* x (- (* y z) (* t a)))))
t_1)))
\left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right) + j \cdot \left(c \cdot t - i \cdot y\right)
↓
\begin{array}{l}
t_1 := b \cdot \left(a \cdot i - z \cdot c\right)\\
t_2 := t \cdot c - y \cdot i\\
\mathbf{if}\;j \cdot t_2 - \left(b \cdot \left(z \cdot c - a \cdot i\right) + x \cdot \left(t \cdot a - y \cdot z\right)\right) \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(j, t_2, t_1 + x \cdot \left(y \cdot z - t \cdot a\right)\right)\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
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 31 alternatives:
Alternative
Accuracy
Speedup
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.
if (+.f64 (-.f64 (*.f64 x (-.f64 (*.f64 y z) (*.f64 t a))) (*.f64 b (-.f64 (*.f64 c z) (*.f64 i a)))) (*.f64 j (-.f64 (*.f64 c t) (*.f64 i y)))) < +inf.0
Initial program 93.1%
\[\left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right) + j \cdot \left(c \cdot t - i \cdot y\right)
\]
Simplified93.1%
\[\leadsto \color{blue}{\mathsf{fma}\left(j, t \cdot c - y \cdot i, x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(z \cdot c - a \cdot i\right)\right)}
\]
Step-by-step derivation
[Start]93.1%
\[ \left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right) + j \cdot \left(c \cdot t - i \cdot y\right)
\]
+-commutative [=>]93.1%
\[ \color{blue}{j \cdot \left(c \cdot t - i \cdot y\right) + \left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right)}
\]
fma-def [=>]93.1%
\[ \color{blue}{\mathsf{fma}\left(j, c \cdot t - i \cdot y, x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right)}
\]
*-commutative [=>]93.1%
\[ \mathsf{fma}\left(j, c \cdot t - \color{blue}{y \cdot i}, x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right)
\]
*-commutative [=>]93.1%
\[ \mathsf{fma}\left(j, \color{blue}{t \cdot c} - y \cdot i, x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right)
\]
*-commutative [=>]93.1%
\[ \mathsf{fma}\left(j, t \cdot c - y \cdot i, x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - \color{blue}{a \cdot i}\right)\right)
\]
*-commutative [=>]93.1%
\[ \mathsf{fma}\left(j, t \cdot c - y \cdot i, x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(\color{blue}{z \cdot c} - a \cdot i\right)\right)
\]
if +inf.0 < (+.f64 (-.f64 (*.f64 x (-.f64 (*.f64 y z) (*.f64 t a))) (*.f64 b (-.f64 (*.f64 c z) (*.f64 i a)))) (*.f64 j (-.f64 (*.f64 c t) (*.f64 i y))))
Initial program 0.0%
\[\left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right) + j \cdot \left(c \cdot t - i \cdot y\right)
\]
Simplified0.0%
\[\leadsto \color{blue}{\left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(z \cdot c - a \cdot i\right)\right) + j \cdot \left(t \cdot c - y \cdot i\right)}
\]
Step-by-step derivation
[Start]0.0%
\[ \left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right) + j \cdot \left(c \cdot t - i \cdot y\right)
\]
cancel-sign-sub [<=]0.0%
\[ \color{blue}{\left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right) - \left(-j\right) \cdot \left(c \cdot t - i \cdot y\right)}
\]
cancel-sign-sub-inv [=>]0.0%
\[ \color{blue}{\left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - i \cdot a\right)\right) + \left(-\left(-j\right)\right) \cdot \left(c \cdot t - i \cdot y\right)}
\]
*-commutative [=>]0.0%
\[ \left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - \color{blue}{a \cdot i}\right)\right) + \left(-\left(-j\right)\right) \cdot \left(c \cdot t - i \cdot y\right)
\]
*-commutative [=>]0.0%
\[ \left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(\color{blue}{z \cdot c} - a \cdot i\right)\right) + \left(-\left(-j\right)\right) \cdot \left(c \cdot t - i \cdot y\right)
\]
remove-double-neg [=>]0.0%
\[ \left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(z \cdot c - a \cdot i\right)\right) + \color{blue}{j} \cdot \left(c \cdot t - i \cdot y\right)
\]
*-commutative [=>]0.0%
\[ \left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(z \cdot c - a \cdot i\right)\right) + j \cdot \left(\color{blue}{t \cdot c} - i \cdot y\right)
\]
*-commutative [=>]0.0%
\[ \left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(z \cdot c - a \cdot i\right)\right) + j \cdot \left(t \cdot c - \color{blue}{y \cdot i}\right)
\]
Taylor expanded in b around inf 48.8%
\[\leadsto \color{blue}{\left(a \cdot i - c \cdot z\right) \cdot b}
\]
Recombined 2 regimes into one program.
Final simplification84.1%
\[\leadsto \begin{array}{l}
\mathbf{if}\;j \cdot \left(t \cdot c - y \cdot i\right) - \left(b \cdot \left(z \cdot c - a \cdot i\right) + x \cdot \left(t \cdot a - y \cdot z\right)\right) \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(j, t \cdot c - y \cdot i, b \cdot \left(a \cdot i - z \cdot c\right) + x \cdot \left(y \cdot z - t \cdot a\right)\right)\\
\mathbf{else}:\\
\;\;\;\;b \cdot \left(a \cdot i - z \cdot c\right)\\
\end{array}
\]
Alternatives
Alternative 1
Accuracy
80.2%
Cost
10052
\[\begin{array}{l}
t_1 := b \cdot \left(a \cdot i - z \cdot c\right)\\
t_2 := t \cdot c - y \cdot i\\
\mathbf{if}\;j \cdot t_2 - \left(b \cdot \left(z \cdot c - a \cdot i\right) + x \cdot \left(t \cdot a - y \cdot z\right)\right) \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(j, t_2, t_1 + x \cdot \left(y \cdot z - t \cdot a\right)\right)\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\]
Alternative 2
Accuracy
80.2%
Cost
3780
\[\begin{array}{l}
t_1 := j \cdot \left(t \cdot c - y \cdot i\right) - \left(b \cdot \left(z \cdot c - a \cdot i\right) + x \cdot \left(t \cdot a - y \cdot z\right)\right)\\
\mathbf{if}\;t_1 \leq \infty:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;b \cdot \left(a \cdot i - z \cdot c\right)\\
\end{array}
\]
Alternative 3
Accuracy
65.9%
Cost
1745
\[\begin{array}{l}
t_1 := b \cdot \left(a \cdot i - z \cdot c\right)\\
\mathbf{if}\;b \leq -7 \cdot 10^{+145}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;b \leq -1.4 \cdot 10^{+72}:\\
\;\;\;\;a \cdot \left(b \cdot i - x \cdot t\right) - y \cdot \left(i \cdot j\right)\\
\mathbf{elif}\;b \leq -1.4 \cdot 10^{+32} \lor \neg \left(b \leq 2.85 \cdot 10^{+144}\right):\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(y \cdot z - t \cdot a\right) + j \cdot \left(t \cdot c - y \cdot i\right)\\
\end{array}
\]
Alternative 4
Accuracy
66.0%
Cost
1744
\[\begin{array}{l}
t_1 := y \cdot \left(x \cdot z - i \cdot j\right)\\
t_2 := b \cdot \left(a \cdot i - z \cdot c\right)\\
\mathbf{if}\;x \leq -7.2 \cdot 10^{-72}:\\
\;\;\;\;j \cdot \left(t \cdot c\right) - x \cdot \left(t \cdot a - y \cdot z\right)\\
\mathbf{elif}\;x \leq 8.6 \cdot 10^{-88}:\\
\;\;\;\;t_2 + j \cdot \left(t \cdot c - y \cdot i\right)\\
\mathbf{elif}\;x \leq 1.15 \cdot 10^{+160}:\\
\;\;\;\;t_2 + x \cdot \left(y \cdot z - t \cdot a\right)\\
\mathbf{elif}\;x \leq 8.2 \cdot 10^{+253}:\\
\;\;\;\;a \cdot \left(b \cdot i - x \cdot t\right) + t_1\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\]
Alternative 5
Accuracy
67.3%
Cost
1744
\[\begin{array}{l}
t_1 := b \cdot \left(a \cdot i - z \cdot c\right)\\
t_2 := y \cdot \left(x \cdot z - i \cdot j\right)\\
t_3 := x \cdot \left(y \cdot z - t \cdot a\right)\\
\mathbf{if}\;x \leq -2.1 \cdot 10^{-73}:\\
\;\;\;\;\left(c \cdot \left(t \cdot j\right) - y \cdot \left(i \cdot j\right)\right) + t_3\\
\mathbf{elif}\;x \leq 8.6 \cdot 10^{-89}:\\
\;\;\;\;t_1 + j \cdot \left(t \cdot c - y \cdot i\right)\\
\mathbf{elif}\;x \leq 2.05 \cdot 10^{+157}:\\
\;\;\;\;t_1 + t_3\\
\mathbf{elif}\;x \leq 3.8 \cdot 10^{+253}:\\
\;\;\;\;a \cdot \left(b \cdot i - x \cdot t\right) + t_2\\
\mathbf{else}:\\
\;\;\;\;t_2\\
\end{array}
\]
herbie shell --seed 2023263
(FPCore (x y z t a b c i j)
:name "Linear.Matrix:det33 from linear-1.19.1.3"
:precision binary64
:herbie-target
(if (< t -8.120978919195912e-33) (- (* x (- (* z y) (* a t))) (- (* b (- (* z c) (* a i))) (* (- (* c t) (* y i)) j))) (if (< t -4.712553818218485e-169) (+ (- (* x (- (* y z) (* t a))) (* b (- (* c z) (* i a)))) (/ (* j (- (pow (* c t) 2.0) (pow (* i y) 2.0))) (+ (* c t) (* i y)))) (if (< t -7.633533346031584e-308) (- (* x (- (* z y) (* a t))) (- (* b (- (* z c) (* a i))) (* (- (* c t) (* y i)) j))) (if (< t 1.0535888557455487e-139) (+ (- (* x (- (* y z) (* t a))) (* b (- (* c z) (* i a)))) (/ (* j (- (pow (* c t) 2.0) (pow (* i y) 2.0))) (+ (* c t) (* i y)))) (- (* x (- (* z y) (* a t))) (- (* b (- (* z c) (* a i))) (* (- (* c t) (* y i)) j)))))))
(+ (- (* x (- (* y z) (* t a))) (* b (- (* c z) (* i a)))) (* j (- (* c t) (* i y)))))