Inverse of a square matrix

The inverse of a square matrix A with a non zero determinant is
the adjoint matrix divided by the determinant, this can be written as

1

A-1 =

adj(A)

det(A)

The adjoint matrix is the transpose of the cofactor matrix. The cofactor
matrix is the matrix of determinants of the minors Aij
multiplied by -1i+j. The i,j'th minor of A is the
matrix A without the i'th column or the j'th row.

Example (3x3 matrix)

The following example illustrates each matrix type and at 3x3 the
steps can be readily calculated on paper. It can also be verified that
the original matrix A multipled by its inverse gives the identity
matrix (all zeros except along the diagonal which are ones).

A =

1

2

0

-1

1

1

1

2

3

Determinant = 9

Cofactor matrix =

+

1

1

2

3

-

-1

1

1

3

+

-1

1

1

2

-

2

0

2

3

+

1

0

1

3

-

1

2

1

2

+

2

0

1

1

-

1

0

-1

1

+

1

2

-1

1

=

1

4

-3

-6

3

-0

2

-1

3

Adjoint matrix = Transpose of cofactor matrix =

1

-6

2

4

3

-1

-3

-0

3

A-1 = Inverse of A =

1/9

-6/9

2/9

4/9

3/9

-1/9

-3/9

0

3/9

A A-1 =

1

0

0

0

1

0

0

0

1

Inverse of a 2x2 matrix

The inverse of a 2x2 matrix can be written explicitly, namely

a

b

c

d

=

1

ad - bc

d

-b

-c

a

Source code

The three functions required are the determinant, cofactor, and
transpose. Examples of these are given below.