Implementation of cached matrix inversion.