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

?

Percentage Accurate: 91.1% → 97.0%
Time: 6.7s
Precision: binary64
Cost: 1737

?

\[\frac{x \cdot y - z \cdot t}{a} \]
\[\begin{array}{l} [z, t] = \mathsf{sort}([z, t])\\ \\ \begin{array}{l} t_1 := x \cdot y - z \cdot t\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 10^{+269}\right):\\ \;\;\;\;y \cdot \frac{x}{a} - z \cdot \frac{t}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_1}{a}\\ \end{array} \end{array} \]
(FPCore (x y z t a) :precision binary64 (/ (- (* x y) (* z t)) a))
NOTE: z and t should be sorted in increasing order before calling this function.
(FPCore (x y z t a)
 :precision binary64
 (let* ((t_1 (- (* x y) (* z t))))
   (if (or (<= t_1 (- INFINITY)) (not (<= t_1 1e+269)))
     (- (* y (/ x a)) (* z (/ t a)))
     (/ t_1 a))))
double code(double x, double y, double z, double t, double a) {
	return ((x * y) - (z * t)) / a;
}
assert(z < t);
double code(double x, double y, double z, double t, double a) {
	double t_1 = (x * y) - (z * t);
	double tmp;
	if ((t_1 <= -((double) INFINITY)) || !(t_1 <= 1e+269)) {
		tmp = (y * (x / a)) - (z * (t / a));
	} else {
		tmp = t_1 / a;
	}
	return tmp;
}
public static double code(double x, double y, double z, double t, double a) {
	return ((x * y) - (z * t)) / a;
}
assert z < t;
public static double code(double x, double y, double z, double t, double a) {
	double t_1 = (x * y) - (z * t);
	double tmp;
	if ((t_1 <= -Double.POSITIVE_INFINITY) || !(t_1 <= 1e+269)) {
		tmp = (y * (x / a)) - (z * (t / a));
	} else {
		tmp = t_1 / a;
	}
	return tmp;
}
def code(x, y, z, t, a):
	return ((x * y) - (z * t)) / a
[z, t] = sort([z, t])
def code(x, y, z, t, a):
	t_1 = (x * y) - (z * t)
	tmp = 0
	if (t_1 <= -math.inf) or not (t_1 <= 1e+269):
		tmp = (y * (x / a)) - (z * (t / a))
	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
z, t = sort([z, t])
function code(x, y, z, t, a)
	t_1 = Float64(Float64(x * y) - Float64(z * t))
	tmp = 0.0
	if ((t_1 <= Float64(-Inf)) || !(t_1 <= 1e+269))
		tmp = Float64(Float64(y * Float64(x / a)) - Float64(z * Float64(t / a)));
	else
		tmp = Float64(t_1 / a);
	end
	return tmp
end
function tmp = code(x, y, z, t, a)
	tmp = ((x * y) - (z * t)) / a;
end
z, t = num2cell(sort([z, t])){:}
function tmp_2 = code(x, y, z, t, a)
	t_1 = (x * y) - (z * t);
	tmp = 0.0;
	if ((t_1 <= -Inf) || ~((t_1 <= 1e+269)))
		tmp = (y * (x / a)) - (z * (t / a));
	else
		tmp = t_1 / a;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_, t_, a_] := N[(N[(N[(x * y), $MachinePrecision] - N[(z * t), $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision]
NOTE: z and t should be sorted in increasing order before calling this function.
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, (-Infinity)], N[Not[LessEqual[t$95$1, 1e+269]], $MachinePrecision]], N[(N[(y * N[(x / a), $MachinePrecision]), $MachinePrecision] - N[(z * N[(t / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$1 / a), $MachinePrecision]]]
\frac{x \cdot y - z \cdot t}{a}
\begin{array}{l}
[z, t] = \mathsf{sort}([z, t])\\
\\
\begin{array}{l}
t_1 := x \cdot y - z \cdot t\\
\mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 10^{+269}\right):\\
\;\;\;\;y \cdot \frac{x}{a} - z \cdot \frac{t}{a}\\

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


\end{array}
\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 7 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

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original91.1%
Target91.6%
Herbie97.0%
\[\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)) < -inf.0 or 1e269 < (-.f64 (*.f64 x y) (*.f64 z t))

    1. Initial program 59.5%

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

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

      \[\leadsto \color{blue}{y \cdot \frac{x}{a} - \frac{t}{a} \cdot z} \]
      Step-by-step derivation

      [Start]56.4%

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

      +-commutative [=>]56.4%

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

      mul-1-neg [=>]56.4%

      \[ \frac{y \cdot x}{a} + \color{blue}{\left(-\frac{t \cdot z}{a}\right)} \]

      unsub-neg [=>]56.4%

      \[ \color{blue}{\frac{y \cdot x}{a} - \frac{t \cdot z}{a}} \]

      associate-*r/ [<=]75.2%

      \[ \color{blue}{y \cdot \frac{x}{a}} - \frac{t \cdot z}{a} \]

      associate-/l* [=>]95.3%

      \[ y \cdot \frac{x}{a} - \color{blue}{\frac{t}{\frac{a}{z}}} \]

      associate-/r/ [=>]95.3%

      \[ y \cdot \frac{x}{a} - \color{blue}{\frac{t}{a} \cdot z} \]

    if -inf.0 < (-.f64 (*.f64 x y) (*.f64 z t)) < 1e269

    1. Initial program 99.6%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot y - z \cdot t \leq -\infty \lor \neg \left(x \cdot y - z \cdot t \leq 10^{+269}\right):\\ \;\;\;\;y \cdot \frac{x}{a} - z \cdot \frac{t}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot y - z \cdot t}{a}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy97.0%
Cost1737
\[\begin{array}{l} t_1 := x \cdot y - z \cdot t\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 10^{+269}\right):\\ \;\;\;\;y \cdot \frac{x}{a} - z \cdot \frac{t}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_1}{a}\\ \end{array} \]
Alternative 2
Accuracy70.8%
Cost1944
\[\begin{array}{l} t_1 := \frac{z}{a} \cdot \left(-t\right)\\ t_2 := \frac{x \cdot y}{a}\\ \mathbf{if}\;x \cdot y \leq -5 \cdot 10^{-62}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{-163}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{-120}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{-71}:\\ \;\;\;\;\frac{t}{a} \cdot \left(-z\right)\\ \mathbf{elif}\;x \cdot y \leq 10^{-24}:\\ \;\;\;\;x \cdot \frac{y}{a}\\ \mathbf{elif}\;x \cdot y \leq 5 \cdot 10^{+59}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\frac{a}{x}}\\ \end{array} \]
Alternative 3
Accuracy94.4%
Cost1097
\[\begin{array}{l} \mathbf{if}\;z \cdot t \leq -\infty \lor \neg \left(z \cdot t \leq 10^{+190}\right):\\ \;\;\;\;\frac{t}{a} \cdot \left(-z\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot y - z \cdot t}{a}\\ \end{array} \]
Alternative 4
Accuracy68.5%
Cost912
\[\begin{array}{l} t_1 := \frac{x \cdot y}{a}\\ t_2 := \frac{t}{a} \cdot \left(-z\right)\\ \mathbf{if}\;z \leq -2.6 \cdot 10^{+67}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;z \leq -2.7 \cdot 10^{-10}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq -1.5 \cdot 10^{-175}:\\ \;\;\;\;y \cdot \frac{x}{a}\\ \mathbf{elif}\;z \leq 7 \cdot 10^{-110}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 5
Accuracy51.3%
Cost320
\[y \cdot \frac{x}{a} \]
Alternative 6
Accuracy51.6%
Cost320
\[x \cdot \frac{y}{a} \]
Alternative 7
Accuracy51.5%
Cost320
\[\frac{x}{\frac{a}{y}} \]

Reproduce?

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