Calculates the inverse of a matrix.