function to compute matrix inverse with caching