# Numerical methods – Determinant 2×2 numerically robust?

How can the determinant of a 2×2 matrix?
$$begin {vmatrix} a & b \ CD end {vmatrix} = a d – b c$$
Will it be computed in floating point without suffering unnecessary catastrophic cancellations?

For a challenging case, consider the determinant:

$$begin {vmatrix} 10 ^ 9 and 10 ^ 9-1 \ 10 ^ 9-1 and 10 ^ 9-2 end {vmatrix} = begin {vmatrix} 1 000 000 000 and 999 999 999 \ 999 999 999 and 999 999 998 end {vmatrix} = -1$$

All the previous numbers are exactly representable in double IEEE precision
arithmetic, including the determinant that is exactly $$-1$$.

But naively calculate the determinant as $$a b – c d$$ in double precision produces $$0$$:
$$begin {eqnarray} a b – c d & = y 10 ^ 9 (10 ^ 9-2) – (10 ^ 9-1) ^ 2 \ & = & (10 ^ {18} – 2 * 10 ^ 9) – (10 ^ {18} – 2 * 10 ^ 9 + 1) \ & = & (10 ^ {18} – 2 * 10 ^ 9) – (10 ^ {18} – 2 * 10 ^ 9) textrm {(the 1 is lost in rounding)} \ & = & 0 end {eqnarray}$$

An apparently reasonable alternative approach could be to reduce
the matrix to triangular form using row operations that preserve the determinant;
that is, Gaussian elimination
(possibly with some form of row and / or pivoting column),
and then take the product of the diagonal entries in the reduced triangular matrix.

Trying that in the previous example, we see that
the upper left entrance $$a$$, being the entry of maximum magnitude,
It is already the optimal pivot, so initially the permutation of rows or columns is not performed.
So the Gaussian elimination proceeds to zero the lower left entry.
subtracting $$c / a = (10 ^ 9-1) / 10 ^ 9$$ times the first row of the second row, resulting in:
$$begin {eqnarray} & & begin {vmatrix} 10 ^ 9 and 10 ^ 9-1 \ 0 y (10 ^ 9-2) – (10 ^ 9-1) * (10 ^ 9-1) / 10 ^ 9 end {vmatrix} \ & = & begin {vmatrix} 10 ^ 9 and 10 ^ 9-1 \ 0 y (10 ^ 9-2) – (10 ^ 9 – 2 + 10 ^ {- 9}) end {vmatrix} \ & = & begin {vmatrix} 10 ^ 9 and 10 ^ 9-1 \ 0 y (10 ^ 9-2) – (10 ^ 9-2) end {vmatrix} textrm {(the} 10 ^ {- 9} textrm {is lost in rounding}} \ & = & begin {vmatrix} 10 ^ 9 and 10 ^ 9-1 \ 0 and 0 end {vmatrix} \ & = & 0 end {eqnarray}$$

So the Gaussian elimination, too, has failed in this example.

This example can be resolved in double precision,
through the following ad-hoc sequence of row and column operations that preserve the determinant.
$$begin {pmatrix} 10 ^ 9 and 10 ^ 9-1 \ 10 ^ 9-1 and 10 ^ 9-2 end {pmatrix}$$
Subtract the first row from the second:
$$begin {pmatrix} 10 ^ 9 and 10 ^ 9-1 \ -1 and -1 end {pmatrix}$$
Subtract the second column from the first:
$$begin {pmatrix} 1 and 10 ^ 9-1 \ 0 and -1 end {pmatrix}$$
Then the determinant can be read as $$(1 * -1) – (10 ^ 9-1) * 0 = -1$$,