Implemented a caching version of matrix inverse