Added caching function for computing the inverse matrix.