Matrix Inversion

Matrix inversion.

Here is a simple 2×2 matrix:

m={{a,b},{c,d}}

This gives the inverse of m. In producing this formula, the Wolfram Language implicitly assumes that the determinant ad-bc is nonzero:

Inverse[m]

Multiplying the inverse by the original matrix should give the identity matrix:

%.m

You have to use (RefLink[Together, paclet:ref/Together]) to clear the denominators, and get back a standard identity matrix:

Together[%]

Here is a matrix of rational numbers:

hb=Table[1/(i+j),{i,4},{j,4}]

The Wolfram Language finds the exact inverse of the matrix:

Inverse[hb]

Multiplying by the original matrix gives the identity matrix:

%.hb

If you try to invert a singular matrix, the Wolfram Language prints a warning message, and returns the input unchanged:

Inverse[{{1,2},{1,2}}]

If you give a matrix with exact symbolic or numerical entries, the Wolfram Language gives the exact inverse. If, on the other hand, some of the entries in your matrix are approximate real numbers, then the Wolfram Language finds an approximate numerical result.

Here is a matrix containing approximate real numbers:

m={{1.2,5.7},{1.3,5.6}}

This finds the numerical inverse:

Inverse[%]

Multiplying by the original matrix gives you an identity matrix with small round-off errors:

%.m

You can get rid of small off-diagonal terms using (RefLink[Chop, paclet:ref/Chop]):

Chop[%]

When you try to invert a matrix with exact numerical entries, the Wolfram Language can always tell whether or not the matrix is singular. When you invert an approximate numerical matrix, The Wolfram Language can usually not tell for certain whether or not the matrix is singular: all it can tell is, for example, that the determinant is small compared to the entries of the matrix. When the Wolfram Language suspects that you are trying to invert a singular numerical matrix, it prints a warning.

The Wolfram Language prints a warning if you invert a numerical matrix that it suspects is singular:

Inverse[{{1.,2.},{1.,2.}}]

This matrix is singular, but the warning is different, and the result is useless:

Inverse[N[{{1,2,3},{4,5,6},{7,8,9}}]]

If you work with high-precision approximate numbers, the Wolfram Language will keep track of the precision of matrix inverses that you generate.

This generates a 6×6 numerical matrix with entries of 20-digit precision:

m=N[Table[GCD[i,j]+1,{i,6},{j,6}],20];

This takes the matrix, multiplies it by its inverse, and shows the first row of the result:

(m.Inverse[m])[[1]]

This generates a 20-digit numerical approximation to a 6×6 Hilbert matrix. Hilbert matrices are notoriously hard to invert numerically:

m=N[Table[1/(i+j-1),{i,6},{j,6}],20];

The result is still correct, but the zeros now have lower accuracy:

(m.Inverse[m])[[1]]

(RefLink[Inverse, paclet:ref/Inverse]) works only on square matrices. "Advanced Matrix Operations" discusses the function (RefLink[PseudoInverse, paclet:ref/PseudoInverse]), which can also be used with nonsquare matrices.