Code for matrix inverse with caching