Calculates inverse of matrix