Function to cache inverse matrix calculations.