Implement matrix inversion with caching