implementing caching for matrix inversion