Caching functions for matrix inversion