Matrix caching and inversion