\[\left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - t \cdot i\right)\right) + j \cdot \left(c \cdot a - y \cdot i\right)
\]
↓
\[\begin{array}{l}
t_1 := \left(x \cdot \left(y \cdot z - t \cdot a\right) + b \cdot \left(t \cdot i - z \cdot c\right)\right) + j \cdot \left(a \cdot c - y \cdot i\right)\\
\mathbf{if}\;t_1 \leq \infty:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x \cdot z - i \cdot j\right)\\
\end{array}
\]
(FPCore (x y z t a b c i j)
:precision binary64
(+
(- (* x (- (* y z) (* t a))) (* b (- (* c z) (* t i))))
(* j (- (* c a) (* y i)))))
↓
(FPCore (x y z t a b c i j)
:precision binary64
(let* ((t_1
(+
(+ (* x (- (* y z) (* t a))) (* b (- (* t i) (* z c))))
(* j (- (* a c) (* y i))))))
(if (<= t_1 INFINITY) t_1 (* y (- (* x z) (* i j))))))
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 24 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 t i)))) (*.f64 j (-.f64 (*.f64 c a) (*.f64 y i)))) < +inf.0
Initial program 91.0%
\[\left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - t \cdot i\right)\right) + j \cdot \left(c \cdot a - y \cdot i\right)
\]
if +inf.0 < (+.f64 (-.f64 (*.f64 x (-.f64 (*.f64 y z) (*.f64 t a))) (*.f64 b (-.f64 (*.f64 c z) (*.f64 t i)))) (*.f64 j (-.f64 (*.f64 c a) (*.f64 y i))))
Initial program 0.0%
\[\left(x \cdot \left(y \cdot z - t \cdot a\right) - b \cdot \left(c \cdot z - t \cdot i\right)\right) + j \cdot \left(c \cdot a - y \cdot i\right)
\]
Simplified18.0%
\[\leadsto \color{blue}{\mathsf{fma}\left(b, t \cdot i - z \cdot c, \mathsf{fma}\left(x, y \cdot z - t \cdot a, j \cdot \mathsf{fma}\left(y, -i, a \cdot c\right)\right)\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 - t \cdot i\right)\right) + j \cdot \left(c \cdot a - y \cdot i\right)
\]
sub-neg [=>]0.0%
\[ \color{blue}{\left(x \cdot \left(y \cdot z - t \cdot a\right) + \left(-b \cdot \left(c \cdot z - t \cdot i\right)\right)\right)} + j \cdot \left(c \cdot a - y \cdot i\right)
\]
+-commutative [=>]0.0%
\[ \color{blue}{\left(\left(-b \cdot \left(c \cdot z - t \cdot i\right)\right) + x \cdot \left(y \cdot z - t \cdot a\right)\right)} + j \cdot \left(c \cdot a - y \cdot i\right)
\]
associate-+l+ [=>]0.0%
\[ \color{blue}{\left(-b \cdot \left(c \cdot z - t \cdot i\right)\right) + \left(x \cdot \left(y \cdot z - t \cdot a\right) + j \cdot \left(c \cdot a - y \cdot i\right)\right)}
\]
distribute-rgt-neg-in [=>]0.0%
\[ \color{blue}{b \cdot \left(-\left(c \cdot z - t \cdot i\right)\right)} + \left(x \cdot \left(y \cdot z - t \cdot a\right) + j \cdot \left(c \cdot a - y \cdot i\right)\right)
\]
+-commutative [<=]0.0%
\[ b \cdot \left(-\left(c \cdot z - t \cdot i\right)\right) + \color{blue}{\left(j \cdot \left(c \cdot a - y \cdot i\right) + x \cdot \left(y \cdot z - t \cdot a\right)\right)}
\]
fma-def [=>]6.0%
\[ \color{blue}{\mathsf{fma}\left(b, -\left(c \cdot z - t \cdot i\right), j \cdot \left(c \cdot a - y \cdot i\right) + x \cdot \left(y \cdot z - t \cdot a\right)\right)}
\]
sub-neg [=>]6.0%
\[ \mathsf{fma}\left(b, -\color{blue}{\left(c \cdot z + \left(-t \cdot i\right)\right)}, j \cdot \left(c \cdot a - y \cdot i\right) + x \cdot \left(y \cdot z - t \cdot a\right)\right)
\]
+-commutative [=>]6.0%
\[ \mathsf{fma}\left(b, -\color{blue}{\left(\left(-t \cdot i\right) + c \cdot z\right)}, j \cdot \left(c \cdot a - y \cdot i\right) + x \cdot \left(y \cdot z - t \cdot a\right)\right)
\]
distribute-neg-in [=>]6.0%
\[ \mathsf{fma}\left(b, \color{blue}{\left(-\left(-t \cdot i\right)\right) + \left(-c \cdot z\right)}, j \cdot \left(c \cdot a - y \cdot i\right) + x \cdot \left(y \cdot z - t \cdot a\right)\right)
\]
unsub-neg [=>]6.0%
\[ \mathsf{fma}\left(b, \color{blue}{\left(-\left(-t \cdot i\right)\right) - c \cdot z}, j \cdot \left(c \cdot a - y \cdot i\right) + x \cdot \left(y \cdot z - t \cdot a\right)\right)
\]
remove-double-neg [=>]6.0%
\[ \mathsf{fma}\left(b, \color{blue}{t \cdot i} - c \cdot z, j \cdot \left(c \cdot a - y \cdot i\right) + x \cdot \left(y \cdot z - t \cdot a\right)\right)
\]
*-commutative [=>]6.0%
\[ \mathsf{fma}\left(b, t \cdot i - \color{blue}{z \cdot c}, j \cdot \left(c \cdot a - y \cdot i\right) + x \cdot \left(y \cdot z - t \cdot a\right)\right)
\]
\[ y \cdot \left(z \cdot x + \color{blue}{\left(-i \cdot j\right)}\right)
\]
unsub-neg [=>]52.3%
\[ y \cdot \color{blue}{\left(z \cdot x - i \cdot j\right)}
\]
Recombined 2 regimes into one program.
Final simplification83.4%
\[\leadsto \begin{array}{l}
\mathbf{if}\;\left(x \cdot \left(y \cdot z - t \cdot a\right) + b \cdot \left(t \cdot i - z \cdot c\right)\right) + j \cdot \left(a \cdot c - y \cdot i\right) \leq \infty:\\
\;\;\;\;\left(x \cdot \left(y \cdot z - t \cdot a\right) + b \cdot \left(t \cdot i - z \cdot c\right)\right) + j \cdot \left(a \cdot c - y \cdot i\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x \cdot z - i \cdot j\right)\\
\end{array}
\]
Alternatives
Alternative 1
Accuracy
82.4%
Cost
3780
\[\begin{array}{l}
t_1 := \left(x \cdot \left(y \cdot z - t \cdot a\right) + b \cdot \left(t \cdot i - z \cdot c\right)\right) + j \cdot \left(a \cdot c - y \cdot i\right)\\
\mathbf{if}\;t_1 \leq \infty:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x \cdot z - i \cdot j\right)\\
\end{array}
\]
Alternative 2
Accuracy
55.2%
Cost
1752
\[\begin{array}{l}
t_1 := b \cdot \left(t \cdot i - z \cdot c\right) - i \cdot \left(y \cdot j\right)\\
t_2 := a \cdot \left(c \cdot j - x \cdot t\right)\\
\mathbf{if}\;b \leq -1.6 \cdot 10^{+64}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;b \leq -1.45 \cdot 10^{+25}:\\
\;\;\;\;t_2\\
\mathbf{elif}\;b \leq -4.6 \cdot 10^{-36}:\\
\;\;\;\;x \cdot \left(y \cdot z - t \cdot a\right)\\
\mathbf{elif}\;b \leq -2.6 \cdot 10^{-252}:\\
\;\;\;\;j \cdot \left(a \cdot c - y \cdot i\right) + i \cdot \left(t \cdot b\right)\\
\mathbf{elif}\;b \leq 1.75 \cdot 10^{-103}:\\
\;\;\;\;y \cdot \left(x \cdot z - i \cdot j\right)\\
\mathbf{elif}\;b \leq 2.65 \cdot 10^{+41}:\\
\;\;\;\;t_2\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\]
Alternative 3
Accuracy
63.3%
Cost
1744
\[\begin{array}{l}
t_1 := a \cdot \left(c \cdot j - x \cdot t\right) - b \cdot \left(z \cdot c - t \cdot i\right)\\
t_2 := x \cdot \left(y \cdot z - t \cdot a\right)\\
\mathbf{if}\;x \leq -1.75 \cdot 10^{+180}:\\
\;\;\;\;t_2\\
\mathbf{elif}\;x \leq -9.8 \cdot 10^{-114}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;x \leq 2.35 \cdot 10^{-246}:\\
\;\;\;\;j \cdot \left(a \cdot c - y \cdot i\right) - b \cdot \left(z \cdot c\right)\\
\mathbf{elif}\;x \leq 1.55 \cdot 10^{+33}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;t_2 - i \cdot \left(y \cdot j\right)\\
\end{array}
\]
Alternative 4
Accuracy
50.8%
Cost
1620
\[\begin{array}{l}
t_1 := a \cdot \left(c \cdot j\right) - b \cdot \left(z \cdot c - t \cdot i\right)\\
\mathbf{if}\;y \leq -6 \cdot 10^{+16}:\\
\;\;\;\;x \cdot \left(y \cdot z - t \cdot a\right)\\
\mathbf{elif}\;y \leq 4.7 \cdot 10^{-189}:\\
\;\;\;\;a \cdot \left(c \cdot j - x \cdot t\right)\\
\mathbf{elif}\;y \leq 1.95 \cdot 10^{-16}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;y \leq 1.45 \cdot 10^{+44}:\\
\;\;\;\;j \cdot \left(a \cdot c - y \cdot i\right)\\
\mathbf{elif}\;y \leq 1.2 \cdot 10^{+71}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x \cdot z - i \cdot j\right)\\
\end{array}
\]
Alternative 5
Accuracy
60.2%
Cost
1620
\[\begin{array}{l}
t_1 := i \cdot \left(y \cdot j\right)\\
t_2 := b \cdot \left(t \cdot i - z \cdot c\right) - t_1\\
t_3 := x \cdot \left(y \cdot z - t \cdot a\right)\\
\mathbf{if}\;b \leq -2.2 \cdot 10^{+62}:\\
\;\;\;\;t_2\\
\mathbf{elif}\;b \leq -2 \cdot 10^{+26}:\\
\;\;\;\;a \cdot \left(c \cdot j - x \cdot t\right)\\
\mathbf{elif}\;b \leq -9.8 \cdot 10^{-37}:\\
\;\;\;\;t_3\\
\mathbf{elif}\;b \leq -1.2 \cdot 10^{-147}:\\
\;\;\;\;j \cdot \left(a \cdot c - y \cdot i\right) + i \cdot \left(t \cdot b\right)\\
\mathbf{elif}\;b \leq 4.6 \cdot 10^{+41}:\\
\;\;\;\;t_3 - t_1\\
\mathbf{else}:\\
\;\;\;\;t_2\\
\end{array}
\]
Alternative 6
Accuracy
67.9%
Cost
1612
\[\begin{array}{l}
t_1 := b \cdot \left(t \cdot i - z \cdot c\right) - i \cdot \left(y \cdot j\right)\\
\mathbf{if}\;b \leq -4.1 \cdot 10^{+64}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;b \leq 5 \cdot 10^{-74}:\\
\;\;\;\;x \cdot \left(y \cdot z - t \cdot a\right) + j \cdot \left(a \cdot c - y \cdot i\right)\\
\mathbf{elif}\;b \leq 2.1 \cdot 10^{+74}:\\
\;\;\;\;a \cdot \left(c \cdot j - x \cdot t\right) - b \cdot \left(z \cdot c - t \cdot i\right)\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\]
Alternative 7
Accuracy
52.2%
Cost
1369
\[\begin{array}{l}
t_1 := y \cdot \left(x \cdot z - i \cdot j\right)\\
\mathbf{if}\;y \leq -9 \cdot 10^{+20}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;y \leq 2.1 \cdot 10^{-187}:\\
\;\;\;\;a \cdot \left(c \cdot j - x \cdot t\right)\\
\mathbf{elif}\;y \leq 3.6 \cdot 10^{+26}:\\
\;\;\;\;c \cdot \left(a \cdot j - z \cdot b\right)\\
\mathbf{elif}\;y \leq 1.85 \cdot 10^{+102} \lor \neg \left(y \leq 2.3 \cdot 10^{+172}\right) \land y \leq 5.2 \cdot 10^{+186}:\\
\;\;\;\;i \cdot \left(t \cdot b - y \cdot j\right)\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\]
Alternative 8
Accuracy
56.4%
Cost
1356
\[\begin{array}{l}
t_1 := j \cdot \left(a \cdot c - y \cdot i\right) + i \cdot \left(t \cdot b\right)\\
\mathbf{if}\;j \leq -3.2 \cdot 10^{-91}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;j \leq 1.42 \cdot 10^{-210}:\\
\;\;\;\;x \cdot \left(y \cdot z - t \cdot a\right)\\
\mathbf{elif}\;j \leq 1.25 \cdot 10^{+53}:\\
\;\;\;\;a \cdot \left(c \cdot j\right) - b \cdot \left(z \cdot c - t \cdot i\right)\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\]
herbie shell --seed 2023263
(FPCore (x y z t a b c i j)
:name "Data.Colour.Matrix:determinant from colour-2.3.3, A"
:precision binary64
:herbie-target
(if (< x -1.469694296777705e-64) (+ (- (* x (- (* y z) (* t a))) (/ (* b (- (pow (* c z) 2.0) (pow (* t i) 2.0))) (+ (* c z) (* t i)))) (* j (- (* c a) (* y i)))) (if (< x 3.2113527362226803e-147) (- (* (- (* b i) (* x a)) t) (- (* z (* c b)) (* j (- (* c a) (* y i))))) (+ (- (* x (- (* y z) (* t a))) (/ (* b (- (pow (* c z) 2.0) (pow (* t i) 2.0))) (+ (* c z) (* t i)))) (* j (- (* c a) (* y i))))))
(+ (- (* x (- (* y z) (* t a))) (* b (- (* c z) (* t i)))) (* j (- (* c a) (* y i)))))