compute matrix inverse with caching