matrix inversion function with cached value