Data.Colour.Matrix:inverse from colour-2.3.3, B

?

Percentage Accurate: 91.4% → 95.5%
Time: 9.4s
Precision: binary64
Cost: 8137

?

\[ \begin{array}{c}[x, y] = \mathsf{sort}([x, y])\\ [z, t] = \mathsf{sort}([z, t])\\ \end{array} \]
\[\frac{x \cdot y - z \cdot t}{a} \]
\[\begin{array}{l} t_1 := x \cdot y - z \cdot t\\ \mathbf{if}\;t_1 \leq -2 \cdot 10^{+207} \lor \neg \left(t_1 \leq 5 \cdot 10^{+166}\right):\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{t}{\frac{a}{z}}, \frac{y}{\frac{a}{x}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{t_1}{a}\\ \end{array} \]
(FPCore (x y z t a) :precision binary64 (/ (- (* x y) (* z t)) a))
(FPCore (x y z t a)
 :precision binary64
 (let* ((t_1 (- (* x y) (* z t))))
   (if (or (<= t_1 -2e+207) (not (<= t_1 5e+166)))
     (fma -1.0 (/ t (/ a z)) (/ y (/ a x)))
     (/ t_1 a))))
double code(double x, double y, double z, double t, double a) {
	return ((x * y) - (z * t)) / a;
}
double code(double x, double y, double z, double t, double a) {
	double t_1 = (x * y) - (z * t);
	double tmp;
	if ((t_1 <= -2e+207) || !(t_1 <= 5e+166)) {
		tmp = fma(-1.0, (t / (a / z)), (y / (a / x)));
	} else {
		tmp = t_1 / a;
	}
	return tmp;
}
function code(x, y, z, t, a)
	return Float64(Float64(Float64(x * y) - Float64(z * t)) / a)
end
function code(x, y, z, t, a)
	t_1 = Float64(Float64(x * y) - Float64(z * t))
	tmp = 0.0
	if ((t_1 <= -2e+207) || !(t_1 <= 5e+166))
		tmp = fma(-1.0, Float64(t / Float64(a / z)), Float64(y / Float64(a / x)));
	else
		tmp = Float64(t_1 / a);
	end
	return tmp
end
code[x_, y_, z_, t_, a_] := N[(N[(N[(x * y), $MachinePrecision] - N[(z * t), $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision]
code[x_, y_, z_, t_, a_] := Block[{t$95$1 = N[(N[(x * y), $MachinePrecision] - N[(z * t), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$1, -2e+207], N[Not[LessEqual[t$95$1, 5e+166]], $MachinePrecision]], N[(-1.0 * N[(t / N[(a / z), $MachinePrecision]), $MachinePrecision] + N[(y / N[(a / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$1 / a), $MachinePrecision]]]
\frac{x \cdot y - z \cdot t}{a}
\begin{array}{l}
t_1 := x \cdot y - z \cdot t\\
\mathbf{if}\;t_1 \leq -2 \cdot 10^{+207} \lor \neg \left(t_1 \leq 5 \cdot 10^{+166}\right):\\
\;\;\;\;\mathsf{fma}\left(-1, \frac{t}{\frac{a}{z}}, \frac{y}{\frac{a}{x}}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{t_1}{a}\\


\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

Original91.4%
Target91.5%
Herbie95.5%
\[\begin{array}{l} \mathbf{if}\;z < -2.468684968699548 \cdot 10^{+170}:\\ \;\;\;\;\frac{y}{a} \cdot x - \frac{t}{a} \cdot z\\ \mathbf{elif}\;z < 6.309831121978371 \cdot 10^{-71}:\\ \;\;\;\;\frac{x \cdot y - z \cdot t}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{a} \cdot x - \frac{t}{a} \cdot z\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 x y) (*.f64 z t)) < -2.0000000000000001e207 or 5.0000000000000002e166 < (-.f64 (*.f64 x y) (*.f64 z t))

    1. Initial program 80.5%

      \[\frac{x \cdot y - z \cdot t}{a} \]
    2. Taylor expanded in x around 0 78.0%

      \[\leadsto \color{blue}{-1 \cdot \frac{t \cdot z}{a} + \frac{y \cdot x}{a}} \]
    3. Simplified93.1%

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

      [Start]78.0%

      \[ -1 \cdot \frac{t \cdot z}{a} + \frac{y \cdot x}{a} \]

      fma-def [=>]78.0%

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

      associate-/l* [=>]83.5%

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

      associate-/l* [=>]93.1%

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

    if -2.0000000000000001e207 < (-.f64 (*.f64 x y) (*.f64 z t)) < 5.0000000000000002e166

    1. Initial program 98.3%

      \[\frac{x \cdot y - z \cdot t}{a} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification95.9%

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

Alternatives

Alternative 1
Accuracy95.5%
Cost8137
\[\begin{array}{l} t_1 := x \cdot y - z \cdot t\\ \mathbf{if}\;t_1 \leq -2 \cdot 10^{+207} \lor \neg \left(t_1 \leq 5 \cdot 10^{+166}\right):\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{t}{\frac{a}{z}}, \frac{y}{\frac{a}{x}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{t_1}{a}\\ \end{array} \]
Alternative 2
Accuracy96.8%
Cost7945
\[\begin{array}{l} t_1 := x \cdot y - z \cdot t\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 2 \cdot 10^{+248}\right):\\ \;\;\;\;\frac{x}{\frac{a}{y}} - \frac{z}{\frac{a}{t}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(y, x, z \cdot \left(-t\right)\right)}{a}\\ \end{array} \]
Alternative 3
Accuracy96.8%
Cost1737
\[\begin{array}{l} t_1 := x \cdot y - z \cdot t\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 2 \cdot 10^{+248}\right):\\ \;\;\;\;\frac{x}{\frac{a}{y}} - \frac{z}{\frac{a}{t}}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_1}{a}\\ \end{array} \]
Alternative 4
Accuracy68.5%
Cost1040
\[\begin{array}{l} \mathbf{if}\;y \leq -2.95 \cdot 10^{-56}:\\ \;\;\;\;\frac{y}{\frac{a}{x}}\\ \mathbf{elif}\;y \leq 4.3 \cdot 10^{+16}:\\ \;\;\;\;z \cdot \frac{t}{-a}\\ \mathbf{elif}\;y \leq 3.4 \cdot 10^{+62}:\\ \;\;\;\;x \cdot \frac{y}{a}\\ \mathbf{elif}\;y \leq 10^{+88}:\\ \;\;\;\;\frac{1}{\frac{\frac{a}{-t}}{z}}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\frac{a}{y}}\\ \end{array} \]
Alternative 5
Accuracy68.5%
Cost912
\[\begin{array}{l} t_1 := z \cdot \frac{t}{-a}\\ \mathbf{if}\;y \leq -1.9 \cdot 10^{-55}:\\ \;\;\;\;\frac{y}{\frac{a}{x}}\\ \mathbf{elif}\;y \leq 4.4 \cdot 10^{+15}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 5 \cdot 10^{+61}:\\ \;\;\;\;x \cdot \frac{y}{a}\\ \mathbf{elif}\;y \leq 1.28 \cdot 10^{+91}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\frac{a}{y}}\\ \end{array} \]
Alternative 6
Accuracy68.6%
Cost912
\[\begin{array}{l} \mathbf{if}\;y \leq -1.2 \cdot 10^{-70}:\\ \;\;\;\;\frac{y}{\frac{a}{x}}\\ \mathbf{elif}\;y \leq 1.05 \cdot 10^{+16}:\\ \;\;\;\;z \cdot \frac{t}{-a}\\ \mathbf{elif}\;y \leq 5.1 \cdot 10^{+61}:\\ \;\;\;\;x \cdot \frac{y}{a}\\ \mathbf{elif}\;y \leq 10^{+88}:\\ \;\;\;\;t \cdot \left(-\frac{z}{a}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\frac{a}{y}}\\ \end{array} \]
Alternative 7
Accuracy68.5%
Cost912
\[\begin{array}{l} \mathbf{if}\;y \leq -1.8 \cdot 10^{-55}:\\ \;\;\;\;\frac{y}{\frac{a}{x}}\\ \mathbf{elif}\;y \leq 7.5 \cdot 10^{+16}:\\ \;\;\;\;z \cdot \frac{t}{-a}\\ \mathbf{elif}\;y \leq 2.6 \cdot 10^{+62}:\\ \;\;\;\;x \cdot \frac{y}{a}\\ \mathbf{elif}\;y \leq 2.8 \cdot 10^{+88}:\\ \;\;\;\;\frac{z}{\frac{-a}{t}}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\frac{a}{y}}\\ \end{array} \]
Alternative 8
Accuracy93.4%
Cost836
\[\begin{array}{l} \mathbf{if}\;x \cdot y \leq 5 \cdot 10^{+248}:\\ \;\;\;\;\frac{x \cdot y - z \cdot t}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\frac{a}{x}}\\ \end{array} \]
Alternative 9
Accuracy52.1%
Cost452
\[\begin{array}{l} \mathbf{if}\;x \leq -8.4 \cdot 10^{+218}:\\ \;\;\;\;y \cdot \frac{x}{a}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{y}{a}\\ \end{array} \]
Alternative 10
Accuracy52.3%
Cost452
\[\begin{array}{l} \mathbf{if}\;x \leq -1.35 \cdot 10^{+219}:\\ \;\;\;\;\frac{y}{\frac{a}{x}}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{y}{a}\\ \end{array} \]
Alternative 11
Accuracy51.8%
Cost320
\[y \cdot \frac{x}{a} \]

Reproduce?

herbie shell --seed 2023229 
(FPCore (x y z t a)
  :name "Data.Colour.Matrix:inverse from colour-2.3.3, B"
  :precision binary64

  :herbie-target
  (if (< z -2.468684968699548e+170) (- (* (/ y a) x) (* (/ t a) z)) (if (< z 6.309831121978371e-71) (/ (- (* x y) (* z t)) a) (- (* (/ y a) x) (* (/ t a) z))))

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