function to calculate the inverse of a square matrix