Implemented caching of matrix inverse