# Matrix determinant

Calculates a square matrix determinant.

## Determinant of a matrix

The determinant of a square matrix is widely used in linear algebra, especially to determine whether a matrix is invertible and to calculate its inverse.

Matrix 2 x 2 determinant formula

det[[a,b],[c,d]] = ad - bc

Matrix 3 x 3 determinant formula

We first choose a row or a column of the matrix and apply the following technique (here we choose the 1st column)

A = [[\color{green}{a_11},a_12,a_13],[\color{red}{a_21},a_22,a_23],[\color{green}{a_31},a_32,a_33]]

Each element a_(ij) in this column corresponds to a matrix 2 x 2 obtained by "striking" the line i and column j. Thus,

A_1 = [[\color{green}{a_11},.,.],[.,a_22,a_23],[.,a_32,a_33]] -> \color{green}{+a_(11)} * det([[a_(22),a_(23)],[a_(32),a_(33)]])

A_2 = [[.,a_12,a_13],[\color{red}{a_21},.,.],[.,a_32,a_33]] -> \color{red}{-a_(21)} * det([[a_(12),a_(13)],[a_(32),a_(33)]])

A_3 = [[.,a_12,a_13],[.,a_22,a_23],[\color{green}{a_31},.,.]] -> \color{green}{+a_(31)} * det([[a_(12),a_(13)],[a_(22),a_(23)]])

Note the alternation of signs between the colored elements in column 1 (+ - +).

The determinant of A is equal to the sum of these 3 components, i.e.,

det(A) = \color{green}{+a_(11)} * det([[a_(22),a_(23)],[a_(32),a_(33)]]) \color{red}{-a_(21)} * det([[a_(12),a_(13)],[a_(32),a_(33)]]) \color{green}{+a_(31)} * det([[a_(12),a_(13)],[a_(22),a_(23)]])

So we get the following formula for a matrix 3 x 3,

det(A) = \color{green}{+a_(11)} * (a_(22)*a_(33) - a_(32) * a_(23)) \color{red}{-a_(21)} * (a_(12) *a_(33) - a_(32)* a_(13)) \color{green}{+a_(31)} * (a_(12)*a_(23)- a_(22)* a_(13) )

How to choose the starting column or row
We've chosen above the first column to do the calculations but we might choose another column or row and apply the same technique.
In order to simplify the calculations, it is always better to choose a row or a column with the maximum of zeros.
Example: Calculate the determinant of A,

A = [[1,5,\color{green}{0}],[-1,2,\color{red}{3}],[4,7,\color{green}{0}]]

We notice that column 3 has a maximum of zeros so, we choose it to do the calculations. We apply the above formula,

det(A) = \color{green}{+0} * det([[-1,2],[4,7]]) \color{red}{-3} * det([[1,5],[4,7]]) \color{green}{+0} * det([[1,5],[-1,2]])

det(A) = \color{red}{-3} * det([[1,5],[4,7]]) = -3*(1*7-4*5) = 39

If there are no zeros in the matrix we can the technique explained in the next paragraph to simplify calculations.

Techniques to quickly calculate a determinant
1- If a row or column consists of zeros then the determinant is zero.
2- If two rows or two columns are identical then the determinant is zero.
3- We can add or subtract a multiple of a column (or row) to another column (or row) without changing the value of the determinant.

Example: This example shows how to 'create' zeros in the matrix in order to simplify the calculations. We use technique 3 above.

A = [[1,5,4],[-1,2,2],[4,7,2]]

(column 3) is replaced by (column 3) - 4 * (column 1) to 'create' a zero. This gives us,

[[1,5,0],[-1,2,6],[4,7,-14]]

We create another 0 by replacing (column 2) by (column 2) - 5 * (column 1). This leads us to,

[[1,0,0],[-1,7,6],[4,-13,-14]]

Row 1 has 2 zeros, it is the ideal start base to calculate the detrminant,

det(A) = 1 * det([7,6],[-13,-14]) = -20

Note that this technique can be generalized to a 4 x 4 matrix.