Implemented cached matrix inversion