Modify to calculate and memoize matrix inversion.